ドンチアン・トレンド・システム
タートルズ流 投資の魔術 からの引用
ここで紹介するドンチアン・トレンド・システムは、5章で説明しているが、わたしたちがタートル時代用いていたものを単純にしたバージョンだ。これは20日ブレイクアウトを仕掛けに、10日ブレイクアウトを手じまいに使用し、350日/25日移動平均をトレンドフィルターとして用いる。
取引は、速いほうの移動平均が示す方向にのみ行われる。25日移動平均が350日移動平均を上回っていれば、買いもちのみ、25日移動平均が350日移動平均を下回っていれば、売り持ちのみということになる。システムは当初のタートル・システムと同じように2ーATRストップを用いる。
ストラテジーを作成
エントリーとイグジット
https://gyazo.com/ab23aae04fccaab2d1421ccfbf39fc39
https://gyazo.com/27e661588f11525f34bfcf99aa74dc65
バックテスト
期間
2005年1月1日 ~ 2017年12月31日
12年間
銘柄
為替(5):
USDJPY、EURJPY、GBPJPY、CHFJPY、CADJPY
株価指数(5):
NKY 日経225、DJI NYダウ、DAX ドイツ、UKX イギリス、HSI 香港ハンセン
日本株(5):
6098リクルート、4452花王、5711三菱マテリアル、7201日産、9984ソフトバンクグループ
米株(5):
AAPLアップル、AXPアメリカン・エクスプレス、BAボーイング、JNJジョンソン・エンド・ジョンソン、MCDマクドナルド
海外商品(5):
金、白金、原油、コーン、大豆
その他
資金管理:単利
各主要な市場から5銘柄ずつピックアップしてテスト。
テスト1
table:テスト1設定
DC_en DC_ex EMA_m EMA_l Losscut Pyramiding
20 10 25 350 2-ATR None
本に掲載されていたものとまったく同じテスト(と思われる)
table:テスト1結果
損益 最大DD 取引数 勝数 勝率 RR比 勝ち保有 負け保有
USDJPY 11.97 15.49 89 25 28.09 2.971 32 10
EURJPY 12.39 34.69 89 28 31.46 2.391 29 10
GBPJPY 88.09 20.92 82 40 48.78 2.125 26 11
CHFJPY 3.29 12.29 89 31 34.83 1.948 27 9
CADJPY 3.59 35.85 85 29 34.12 2.012 29 10
NKY 11113.52 3743.34 76 35 46.05 2.135 30 10
DJI 8294.76 2905.75 88 32 36.36 3.035 34 9
DAX 2261.61 3540.36 89 31 34.83 2.322 30 10
UKX -2364.90 3704.30 94 33 35.11 1.266 27 9
HSI 13234.81 5877.74 84 39 46.43 1.783 26 8
6098 659.00 248.00 11 4 59.09 4.433 43 11
4452 1753.00 1085.00 84 32 38.10 2.213 25 9
5711 6075.00 1250.00 69 27 39.13 3.056 31 11
7201 -439.29 739.10 82 26 31.71 1.688 27 9
9984 4192.00 1920.00 73 30 41.10 2.168 29 10
AAPL 85.64 23.38 81 37 45.68 2.399 31 8
AXP -6.74 30.85 93 29 31.18 2.059 30 10
BA 189.03 50.53 85 33 38.82 3.817 29 9
JNJ 7.41 24.87 84 31 36.90 1.870 28 9
MCD 12.99 26.03 87 33 37.93 1.863 30 8
GOLD 32750.00 21400.00 82 40 48.78 1.342 25 9
PLATINUM 26605.00 30440.00 85 39 35.29 2.275 28 9
WTI 42200.00 69770.00 87 33 37.93 2.089 27 8
CORN -4625.00 20625.00 89 24 26.97 2.440 30 9
SOY BEANS 3237.05 30600.00 86 32 37.21 1.750 27 9
TOTAL 2043 773 37.84 2.200
破産の確率 0.01%
期待値/リスク 0.210
利益見込み 429.88
※ 破産の確率は損失の許容=2%で算出
テスト1考察
期待値の計算式
$ WIN \times RR- | 1-WIN|
利益見込みの計算式
期待値 ✕ 取引回数
何か周期のようなものが表れている気がする
どの銘柄も勝ちトレードが30営業日前後
どの銘柄も負けトレードが10営業日前後
短期間のトレードになった分、期待値が大幅に定価
そのかわり、取引回数が大幅に増加
今でも十分に通用しそう
ただし、分散投資が必須
単一銘柄でやる手法ではない
破産の確率が顕在化した(ゼロじゃなくなった)が、数値としてはほぼ問題ない水準
テスト2
table:テスト2設定
DC_en DC_ex EMA_m EMA_l Losscut Pyramiding
20 10 25 350 None None
ロスカットをなしに
table:テスト2結果
損益 最大DD 取引数 勝数 勝率 RR比 勝ち保有 負け保有
USDJPY 15.02 19.04 89 26 30.23 2.784 32 11
EURJPY 10.17 41.29 87 29 33.33 2.147 30 11
GBPJPY 98.17 13.53 80 40 50.00 2.279 26 12
CHFJPY -1.68 13.85 90 31 34.44 1.866 27 10
CADJPY 5.07 40.77 81 29 35.80 1.901 31 11
NKY 9679.24 4072.17 76 35 46.05 1.930 30 10
DJI 9088.30 2292.62 84 32 38.10 3.000 35 10
DAX 2014.50 4051.45 86 31 36.05 2.142 31 11
UKX -3025.70 4818.10 90 33 36.67 1.100 27 11
HSI 10194.40 8126.61 84 39 46.43 1.546 26 9
6098 596.00 260.00 11 4 36.36 3.932 43 11
4452 1292.00 1217.00 81 32 39.51 1.863 25 11
5711 6210.00 1590.00 67 28 41.79 2.723 31 12
7201 -504.10 834.10 80 27 33.75 1.492 27 10
9984 3725.00 2076.00 73 31 51.03 1.922 29 11
AAPL 107.75 18.85 75 38 50.67 2.506 30 11
AXP -1.42 30.69 90 29 32.22 2.073 30 11
BA 199.95 42.04 79 34 43.04 3.492 30 11
JNJ 3.20 21.45 83 31 37.35 1.742 28 10
MCD 15.65 25.65 85 33 38.83 1.845 31 9
GOLD 38340.00 20840.00 80 40 50.00 1.342 25 11
PLATINUM 33240.00 34585.00 83 30 36.14 2.307 29 10
WTI 18470.00 79120.00 86 33 38.37 1.775 28 9
CORN -5650.00 19937.50 87 24 27.59 2.320 30 11
SOY BEAN -4012.50 34475.00 86 32 37.21 1.616 27 9
TOTAL 1993 771 38.69 2.07
破産の確率 0.01%
期待値/リスク 0.187
利益見込み 372.6
※ 破産の確率は損失の許容=2%で算出
テスト2考察
悪化
テスト3
table:テスト3設定
DC_en DC_ex EMA_m EMA_l Losscut Pyramiding
20 10 25 350 2-ATR 1-ATR 上限4
table:テスト3結果
損益 最大DD 取引数 勝数 勝率 RR比 勝ち保有 負け保有
USDJPY 51.90 42.89 204 70 34.31 2.492 31 9
EURJPY -19.47 75.98 215 69 32.09 1.961 25 9
GBPJPY 77.88 82.57 237 87 36.71 2.114 24 8
CHFJPY -12.34 67.28 211 65 30.81 2.109 27 8
CADJPY 0.40 86.21 204 69 33.82 1.946 28 9
NKY 24196.85 7703.55 212 82 38.68 2.608 28 9
DJI 29991.63 5830.94 217 102 27.00 2.564 29 8
DAX 9162.11 5504.51 218 90 41.28 2.060 28 8
UKX -7690.80 8475.60 234 73 31.20 1.222 23 8
HSI 3538.84 32801.44 235 95 40.43 1.543 24 8
6098 2752.00 454.00 30 10 33.33 7.354 55 9
4452 1888.00 2858.00 213 75 35.21 2.090 22 9
5711 15660.00 2870.00 191 80 41.88 2.590 27 9
7201 -2447.50 2660.40 217 60 27.65 1.483 23 8
9984 7991.00 5140.00 200 79 39.50 2.178 25 10
AAPL 247.16 39.34 235 111 47.23 2.455 27 9
AXP -26.53 100.92 227 76 33.48 1.789 28 9
BA 518.54 77.84 223 90 40.36 3.857 27 9
JNJ 49.57 47.19 192 71 36.98 2.246 29 9
MCD 75.70 69.33 218 80 36.70 2.332 29 8
GOLD 25220.00 83980.00 221 95 42.99 1.437 23 9
PLATINUM 42340.00 99125.00 222 79 35.59 2.115 25 8
WTI 138170.00 170150.00 211 74 35.07 2.544 28 9
CORN 20900.00 34500.00 202 61 30.20 2.754 29 8
SOY BEAN 50212.50 50712.50 198 73 36.87 2.170 27 9
TOTAL 5187 1916 36.94 2.23
破産の確率 0.02
期待値/リスク 0.19
利益見込み 1009.02
※ 破産の確率は損失の許容=2%で算出
テスト3考察
ピラミッティングはやれば良いというものではない。
勝てない銘柄でやると傷口を広げる
「利益を伸ばしつつ、余計なエントリーはしない」スキームが必要
Pineスクリプト
code:Pineスクリプト(js)
//@version=3
strategy("Strategy Turtle Donchian Trend System"
,default_qty_type=strategy.fixed
,default_qty_value=1
,pyramiding=4
,overlay=true)
src = close
len_dc_entry = input(20 ,minval=1 ,title="length of dc entry")
len_dc_exit = input(10 ,minval=1 ,title="length of dc exit")
len_ema_m = input(25 ,minval=1 ,title="length of middle ema")
len_ema_l = input(350 ,minval=1 ,title="length of long ema")
SO_bool = input(false,type=bool ,title="loss cut")
SO_len = input(20 ,type=integer ,minval=1 ,title="loss cut ATR length")
SO_N = input(2 ,type=float ,minval=0.5 ,title="loss cut ATR*N")
MAX_N = input(1 ,type=integer ,minval=1 ,maxval=4 ,title="maximun num of unit")
LO_len = input(20 ,type=integer ,minval=1 ,title="pyramiding ATR length")
LO_N = input(1 ,type=float ,minval=0.5 ,title="pyramiding ATR*N")
Tm_bool = input(false,type=bool ,title="timed exit")
Tm_len = input(80 ,type=integer ,minval=1 ,title="timed exit length")
fromYear = input(2005 ,type=integer ,minval=1900 ,title="test start")
endYear = input(2017 ,type=integer ,minval=1900 ,title="test end")
isWork = timestamp(fromYear ,1 ,1 ,00 ,00) <= time and time < timestamp(endYear+1 ,1 ,1 ,00 ,00)
upper_en = highest(high ,len_dc_entry)1 upper_ex = highest(high ,len_dc_exit)1 lower_en = lowest(low ,len_dc_entry)1 lower_ex = lowest(low ,len_dc_exit)1 ema_m = ema(src ,len_ema_m)
ema_l = ema(src ,len_ema_l)
atr_SO_ = ema(tr ,SO_len)
atr_LO_ = ema(tr ,LO_len)
atr_SO = atr_SO_*SO_N
atr_LO = atr_LO_*LO_N
countTradingDays = na
countNonTradingDays = na
countTradingDays := strategy.position_size==0 ? 0 : countTradingDays1 + 1 countNonTradingDays := strategy.position_size!=0 ? 0 : countNonTradingDays1 + 1 entry1 = close
entry2 = close
entry3 = close
entry4 = close
entry1 := strategy.position_size==0 ? na : entry11 entry2 := strategy.position_size==0 ? na : entry21 entry3 := strategy.position_size==0 ? na : entry31 entry4 := strategy.position_size==0 ? na : entry41 lo2 = close
lo3 = close
lo4 = close
lo2 := strategy.position_size==0 ? na : lo21 lo3 := strategy.position_size==0 ? na : lo31 lo4 := strategy.position_size==0 ? na : lo41 L_EntrySig = strategy.position_size==0 and high >= upper_en and ema_m >= ema_l
S_EntrySig = strategy.position_size==0 and low <= lower_en and ema_m <= ema_l
lo_sig2 = strategy.position_size>0 ? lo2 < high : strategy.position_size<0 ? lo2 > low : na
lo_sig3 = strategy.position_size>0 ? lo3 < high : strategy.position_size<0 ? lo3 > low : na
lo_sig4 = strategy.position_size>0 ? lo4 < high : strategy.position_size<0 ? lo4 > low : na
losscut = close
losscut := SO_bool==false ? na
: L_EntrySig ? close - atr_SO
: S_EntrySig ? close + atr_SO
: strategy.position_size>0 and (lo_sig2 or lo_sig3 or lo_sig4) ? close - atr_SO
: strategy.position_size<0 and (lo_sig2 or lo_sig3 or lo_sig4) ? close + atr_SO
: strategy.position_size!=0 ? losscut1 : na
ExitPrice = close
ExitPrice := L_EntrySig or strategy.position_size>0 ? SO_bool ? max(losscut ,lower_ex) : lower_ex
: S_EntrySig or strategy.position_size<0 ? SO_bool ? min(losscut ,upper_ex) : upper_ex
: na
if(strategy.position_size != 0)
L_ExitSig = (low <= lower_ex or S_EntrySig) and strategy.position_size > 0
S_ExitSig = (high >= upper_ex or L_EntrySig) and strategy.position_size < 0
TimedSig = countTradingDays > Tm_len and Tm_bool
strategy.close_all(when = L_ExitSig or S_ExitSig or TimedSig)
if(L_ExitSig or S_ExitSig)
entry1 := na
entry2 := na
entry3 := na
entry4 := na
lo2 := na
lo3 := na
lo4 := na
losscut := na
if(strategy.position_size > 0)
strategy.exit("L-Entry1" ,stop=ExitPrice)
if(entry2!=na)
strategy.exit("L-Entry2" ,stop=ExitPrice)
if(entry3!=na)
strategy.exit("L-Entry3" ,stop=ExitPrice)
if(entry4!=na)
strategy.exit("L-Entry4" ,stop=ExitPrice)
if(lo_sig2 and MAX_N >= 2)
lo2 := na
if(SO_bool)
strategy.entry("L-Entry2" ,strategy.long ,stop=ExitPrice ,comment="L-Entry2")
strategy.exit("L-Entry1" ,stop=ExitPrice)
else
strategy.entry("L-Entry2" ,strategy.long ,comment="L-Entry2")
if(lo_sig3 and MAX_N >= 3)
lo3 := na
if(SO_bool)
strategy.entry("L-Entry3" ,strategy.long ,stop=ExitPrice ,comment="L-Entry3")
strategy.exit("L-Entry2" ,stop=ExitPrice)
strategy.exit("L-Entry1" ,stop=ExitPrice)
else
strategy.entry("L-Entry3" ,strategy.long ,comment="L-Entry3")
if(lo_sig4 and MAX_N >= 4)
lo4 := na
if(SO_bool)
strategy.entry("L-Entry4" ,strategy.long ,stop=ExitPrice ,comment="L-Entry4")
strategy.exit("L-Entry3" ,stop=ExitPrice)
strategy.exit("L-Entry2" ,stop=ExitPrice)
strategy.exit("L-Entry1" ,stop=ExitPrice)
else
strategy.entry("L-Entry4" ,strategy.long ,comment="L-Entry4")
if(strategy.position_size < 0)
strategy.exit("S-Entry1" ,stop=ExitPrice)
if(entry2!=na)
strategy.exit("S-Entry2" ,stop=ExitPrice)
if(entry3!=na)
strategy.exit("S-Entry3" ,stop=ExitPrice)
if(entry4!=na)
strategy.exit("S-Entry4" ,stop=ExitPrice)
if(lo_sig2 and MAX_N >= 2)
lo2 := na
if(SO_bool)
strategy.entry("S-Entry2" ,strategy.short ,stop=ExitPrice ,comment="S-Entry2")
strategy.exit("S-Entry1" ,stop=ExitPrice)
else
strategy.entry("S-Entry2" ,strategy.short ,comment="S-Entry2")
if(lo_sig3 and MAX_N >= 3)
lo3 := na
if(SO_bool)
strategy.entry("S-Entry3" ,strategy.short ,stop=ExitPrice ,comment="S-Entry3")
strategy.exit("S-Entry2" ,stop=ExitPrice)
strategy.exit("S-Entry1" ,stop=ExitPrice)
else
strategy.entry("S-Entry3" ,strategy.short ,comment="S-Entry3")
if(lo_sig4 and MAX_N >= 4)
lo4 := na
if(SO_bool)
strategy.entry("S-Entry4" ,strategy.short ,stop=ExitPrice ,comment="S-Entry4")
strategy.exit("S-Entry3" ,stop=ExitPrice)
strategy.exit("S-Entry2" ,stop=ExitPrice)
strategy.exit("S-Entry1" ,stop=ExitPrice)
else
strategy.entry("S-Entry4" ,strategy.short ,comment="S-Entry4")
if((L_EntrySig or S_EntrySig) and isWork)
countTradingDays := 0
entry1 := close
if(L_EntrySig)
if(SO_bool)
strategy.entry("L-Entry1" ,strategy.long ,stop=ExitPrice ,comment="L-Entry1")
else
strategy.entry("L-Entry1" ,strategy.long ,comment="L-Entry1")
lo2 := MAX_N >= 2 ? close + atr_LO : na
lo3 := MAX_N >= 3 ? close + atr_LO * 2 : na
lo4 := MAX_N >= 4 ? close + atr_LO * 3 : na
if(S_EntrySig)
if(SO_bool)
strategy.entry("S-Entry1" ,strategy.short ,stop=ExitPrice ,comment="S-Entry1")
else
strategy.entry("S-Entry1" ,strategy.short ,comment="S-Entry1")
lo2 := MAX_N >= 2 ? close - atr_LO : na
lo3 := MAX_N >= 3 ? close - atr_LO * 2 : na
lo4 := MAX_N >= 4 ? close - atr_LO * 3 : na
plot(strategy.position_size ,transp=0 ,title="保有ポジションの数")
plot(strategy.openprofit ,transp=0 ,title="未決済の損益")
plot(strategy.netprofit ,transp=0 ,title="決済済みの損益")
plot(strategy.closedtrades ,transp=0 ,title="決済済み取引数")
plot(countTradingDays ,transp=0 ,title="取引日数")
plot(countNonTradingDays ,transp=0 ,title="ノンポジ日数")
plot(entry1 ,title="entry1" ,color=blue ,transp=0 ,style=linebr)
plot(lo2 ,title="lo2" ,color=red ,transp=0 ,style=linebr)
plot(lo3 ,title="lo3" ,color=red ,transp=0 ,style=linebr)
plot(lo4 ,title="lo4" ,color=red ,transp=0 ,style=linebr)
plot(ExitPrice ,title="ExitPrice" ,color=red ,transp=0 ,style=linebr)
plot(atr_SO ,transp=0 ,title="ATR_SO")
plot(atr_LO ,transp=0 ,title="ATR_LO")
// plot(strategy.max_drawdown ,transp=50 ,title="最大DD")
// plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)
p1 = plot(ema_m ,color=#303F9F ,title="ema_m" ,style=line ,linewidth=1, transp=0)
p2 = plot(ema_l ,color=#4CAF50 ,title="ema_l" ,style=line ,linewidth=1, transp=0)
fill(p1 ,p2 ,color=#2196F3 ,title="fill" ,transp=80)
p3 = plot(lower_en ,color=gray ,title="lower_entry" ,style=linebr ,linewidth=1 ,transp=40)
p4 = plot(upper_en ,color=gray ,title="upper_entry" ,style=linebr ,linewidth=1 ,transp=40)
fill(p3 ,p4 ,color=gray ,title="fill" ,transp=90)
plot(strategy.position_size>0 ? lower_ex : na ,color=red ,title="lower_exit" ,style=linebr ,linewidth=1 ,transp=30)
plot(strategy.position_size<0 ? upper_ex : na ,color=red ,title="upper_exit" ,style=linebr ,linewidth=1 ,transp=30)