@ … バックテストを行う通貨に一つだけ半角で「1」と入力して下さい。
A … 短期MAの日数を入力して下さい。
B … 長期MAの日数を入力して下さい。
C … スプレッドを入力して下さい。
D … 1万通貨に対する往復の手数料を×100円として入力して下さい。例えば手数料が300円なら「3」と入力して下さい。
取引数量にかかわらず「1万通貨に対する手数料」です。
E … 一回の取引に対する取引数量を入力して下さい。1万ドルの取引なら「10000」と入力して下さい。
F … バックテストが終わるとここに結果が表示されます。
G … エラーメッセージなどが表示されます。
H … 設定を入力してこのボタンをクリックするとバックテストを開始します。
I … バックテストが終わるとここに取引履歴が表示されます。
※ スプレッドと手数料を計算に入れない場合は何も入力しないで下さい。
------- 以下コード -------------------------------------------------------------------------------------
Sub test()
'-----------------------------------------------------------------------
'変数の宣言
'-----------------------------------------------------------------------
Dim hiz(5000) As Date '日付データ
Dim ne1(5000) As Long '始値データ
Dim ne2(5000) As Long '高値データ
Dim ne3(5000) As Long '安値データ
Dim ne4(5000) As Long '終値データ
Dim ma1(5000) As Long '短期平均線データ
Dim ma2(5000) As Long '長期平均線データ
Dim posrec(500) As Integer '取引履歴 ポジション(1:買 -1:売 0:ノンポジ)
Dim hi1rec(500) As Date '取引履歴 注文時日付
Dim ra1rec(500) As Long '取引履歴 注文時レート
Dim hi2rec(500) As Date '取引履歴 決済時日付
Dim ra2rec(500) As Long '取引履歴 決済時レート
Dim so1rec(500) As Long '取引履歴 損益
Dim so2rec(500) As Long '取引履歴 損益計
Dim winrec As Integer 'バックテスト結果 勝ちトレード数
Dim losrec As Integer 'バックテスト結果 負けトレード数
Dim drarec As Long 'バックテスト結果 最低時損益計
Dim ma1inp As Integer '条件設定値 短期MA日数
Dim ma2inp As Integer '条件設定値 長期MA日数
Dim sprinp As Integer '条件設定値 スプレッド
Dim tesinp As Long '条件設定値 手数料
Dim suuinp As Long '条件設定値 取引数量
Dim x As Long '雑計算用
Dim y As Long '雑計算用
Dim z As Long '雑計算用
'-----------------------------------------------------------------------
'前回のバックテスト結果を消す
'-----------------------------------------------------------------------
Worksheets("HOME").Select
z = 4
Do Until Cells(z + 1, 11).Value = ""
z = z + 1
Loop
For x = 4 To z
For y = 11 To 17
Cells(x, y).Value = ""
Cells(x, y).Interior.ColorIndex = 0
Next y
Next x
Cells(13, 2).Value = ""
Cells(13, 3).Value = ""
Cells(13, 4).Value = ""
Cells(13, 5).Value = ""
Cells(13, 6).Value = ""
Cells(16, 2).Value = ""
Cells(16, 2).Interior.ColorIndex = 2
'-----------------------------------------------------------------------
'エラー判定
'-----------------------------------------------------------------------
z = WorksheetFunction.Sum(Range(Cells(4, 2), Cells(4, 9)))
If z <> 1 Then
Cells(16, 2).Value = "通貨を正しく選択して下さい"
Cells(16, 2).Interior.ColorIndex = 39
Exit Sub
End If
If Cells(8, 2).Value = "" Or Cells(8, 3).Value = "" Then
Cells(16, 2).Value = "MA日数を入力して下さい"
Cells(16, 2).Interior.ColorIndex = 39
Exit Sub
End If
If Cells(8, 6).Value = "" Then
Cells(16, 2).Value = "取引数量を入力して下さい"
Cells(16, 2).Interior.ColorIndex = 39
Exit Sub
End If
'-----------------------------------------------------------------------
'条件設定を変数に読み込む
'-----------------------------------------------------------------------
ma1inp = Cells(8, 2).Value '短期MA日数を読み込む
ma2inp = Cells(8, 3).Value '長期MA日数を読み込む
sprinp = Cells(8, 4).Value 'スプレッドを読み込む
tesinp = Cells(8, 5).Value '手数料を読み込む
suuinp = Cells(8, 6).Value '取引数量を読み込む
'-----------------------------------------------------------------------
'為替データを変数に読み込む
'-----------------------------------------------------------------------
If Cells(4, 2).Value = 1 Then
Worksheets("USD").Select
End If
If Cells(4, 3).Value = 1 Then
Worksheets("EUR").Select
End If
If Cells(4, 4).Value = 1 Then
Worksheets("EU-US").Select
End If
If Cells(4, 5).Value = 1 Then
Worksheets("AUD").Select
End If
If Cells(4, 6).Value = 1 Then
Worksheets("GBP").Select
End If
If Cells(4, 7).Value = 1 Then
Worksheets("NZD").Select
End If
If Cells(4, 8).Value = 1 Then
Worksheets("CAD").Select
End If
If Cells(4, 9).Value = 1 Then
Worksheets("CHF").Select
End If
For x = 2 To 5000
hiz(x) = Cells(x, 1).Value '日付データを読み込む
ne1(x) = Cells(x, 2).Value '始値データを読み込む
ne2(x) = Cells(x, 3).Value '高値データを読み込む
ne3(x) = Cells(x, 4).Value '安値データを読み込む
ne4(x) = Cells(x, 5).Value '終値データを読み込む
Next x
'-----------------------------------------------------------------------
'移動平均線の計算
'-----------------------------------------------------------------------
For x = 263 To 1042
ma1(x) = WorksheetFunction.Average(Range(Cells(x - (ma1inp - 1), 5), Cells(x, 5)))
ma2(x) = WorksheetFunction.Average(Range(Cells(x - (ma2inp - 1), 5), Cells(x, 5)))
Next x
'-----------------------------------------------------------------------
'売買計算
'-----------------------------------------------------------------------
z = 0
For x = 263 To 1041 'USDシートの263行目から1041行目までを計算
If posrec(z) <= 0 And ma1(x) > ma2(x) Then '買いサイン発生の場合
If posrec(z) = -1 Then '現在のポジションが売りの場合
hi2rec(z) = hiz(x + 1) '決済日付に翌日の日付を読み込む
ra2rec(z) = ne1(x + 1) '決済レートに翌日の始値を読み込む
so1rec(z) = ra1rec(z) - ra2rec(z) - sprinp - tesinp '損益計算
so2rec(z) = so2rec(z - 1) + so1rec(z) '損益計計算
End If
z = z + 1 '取引履歴を一行移動
posrec(z) = 1 '現在のポジションを売りにする
hi1rec(z) = hiz(x + 1) '注文日付に翌日の日付を読み込む
ra1rec(z) = ne1(x + 1) '注文レートに翌日の始値を読み込む
End If
If posrec(z) >= 0 And ma1(x) < ma2(x) Then '売りサイン発生の場合
If posrec(z) = 1 Then '現在のポジションが買いの場合
hi2rec(z) = hiz(x + 1) '決済日付に翌日の日付を読み込む
ra2rec(z) = ne1(x + 1) '決済レートに翌日の始値を読み込む
so1rec(z) = ra2rec(z) - ra1rec(z) - sprinp - tesinp '損益計算
so2rec(z) = so2rec(z - 1) + so1rec(z) '損益計計算
End If
z = z + 1 '取引履歴を一行移動
posrec(z) = -1 '現在のポジションを買いにする
hi1rec(z) = hiz(x + 1) '注文日付に翌日の日付を読み込む
ra1rec(z) = ne1(x + 1) '注文レートに翌日の始値を読み込む
End If
Next x
'--------最終日計算---------------------
If posrec(z) = 1 Then '現在のポジションが買いの場合
hi2rec(z) = hiz(1042) '最終日の日付を表示
ra2rec(z) = ne4(1042) '最終日のレートを表示
so1rec(z) = ra2rec(z) - ra1rec(z) - sprinp - tesinp '損益計算
so2rec(z) = so2rec(z - 1) + so1rec(z) '損益計計算
End If
If posrec(z) = -1 Then '現在のポジションが売りの場合
hi2rec(z) = hiz(1042) '最終日の日付を表示
ra2rec(z) = ne4(1042) '最終日のレートを表示
so1rec(z) = ra2rec(z) - ra1rec(z) - sprinp - tesinp '損益計算
so2rec(z) = so2rec(z - 1) + so1rec(z) '損益計計算
End If
'-----------------------------------------------------------------------
'取引履歴を表示
'-----------------------------------------------------------------------
Worksheets("HOME").Select
For x = 1 To z
If posrec(x) = 1 Then 'ポジションが1の場合
Cells(x + 3, 11).Value = "買" 'セルに「買」と表示
End If
If posrec(x) = -1 Then 'ポジションが-1の場合
Cells(x + 3, 11).Value = "売" 'セルに「売」と表示
End If
If so1rec(x) > 0 Then '利益が出た場合
For y = 11 To 17
Cells(x + 3, y).Interior.ColorIndex = 37 'セルを青色に塗る
Next y
End If
If so1rec(x) < 0 Then '損失が出た場合
For y = 11 To 17
Cells(x + 3, y).Interior.ColorIndex = 38 'セルを赤色に塗る
Next y
End If
Cells(x + 3, 12).Value = hi1rec(x) '注文日付を表示
Cells(x + 3, 13).Value = ra1rec(x) '注文レートを表示
Cells(x + 3, 14).Value = hi2rec(x) '決済日付を表示
Cells(x + 3, 15).Value = ra2rec(x) '決済レートを表示
Cells(x + 3, 16).Value = so1rec(x) * (suuinp / 100) '損益を表示
Cells(x + 3, 17).Value = so2rec(x) * (suuinp / 100) '損益計を表示
Next x
'-----------------------------------------------------------------------
'バックテスト結果を表示
'-----------------------------------------------------------------------
drarec = so2rec(1)
For x = 1 To z
'最低時損益計を出す
If drarec > so2rec(x) Then '
drarec = so2rec(x)
End If
'勝ちトレード数をカウント
If so1rec(x) > 0 Then
winrec = winrec + 1
End If
'負けトレード数をカウント
If so1rec(x) < 0 Then
losrec = losrec + 1
End If
Next x
Cells(13, 2).Value = z '総トレード数を表示
Cells(13, 3).Value = winrec '勝ちトレード数を表示
Cells(13, 4).Value = losrec '負けトレード数を表示
Cells(13, 5).Value = drarec * (suuinp / 100) '最低時損益計を表示
Cells(13, 6).Value = so2rec(z) * (suuinp / 100) '最終損益計を表示
Cells(16, 2).Value = "バックテスト終了"
End Sub |