Pine講座63 タートルズ流投資の魔術|ドンチャン・トレンドシステム

已更新
「タートルズ流投資の魔術
 カーティス・フェイス著」

で紹介されている手法の再現。
5つ目です。

これまでの手法に比べると
すこしスパンの短い手法。

取引回数が増えます。

この手法をこれまでの手法と比較すると
「取引回数の重要さ」が良く分かると思います。

(長期のトレンドフォロー
 なので分散投資が必須です)

※ コピペする場合は以下の変更を行ってください
[](全角の角括弧)→(半角の角括弧)
(全角スペース)→(半角スペース)

=====
//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 : countTradingDays[1] + 1
countNonTradingDays := strategy.position_size!=0 ? 0 : countNonTradingDays[1] + 1
entry1  = close
entry2  = close
entry3  = close
entry4  = close
entry1 := strategy.position_size==0 ? na : entry1[1]
entry2 := strategy.position_size==0 ? na : entry2[1]
entry3 := strategy.position_size==0 ? na : entry3[1]
entry4 := strategy.position_size==0 ? na : entry4[1]
lo2   = close
lo3   = close
lo4   = close
lo2   := strategy.position_size==0 ? na : lo2[1]
lo3   := strategy.position_size==0 ? na : lo3[1]
lo4   := strategy.position_size==0 ? na : lo4[1]



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 ? losscut[1]
     : 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)
=====
註釋
次の講座
Pine講座64 タートルズ流投資の魔術|時限式ドンチアン・トレンド
Beyond Technical Analysispinescript

小次郎講師公式インジケーターのお申込
bit.ly/2vdSV4Q

小次郎講師のLINE@
bit.ly/2VZQFu3

小次郎講師のチャート情報局
bit.ly/2GvLAEp
更多:

相關出版品

免責聲明