yuya_takahashi_

Pine講座㊽ バックテスト|[STRATEGY][RS]MicuRobert EMA cross V2 の解説

Education
FX:USDJPY   U.S. Dollar / Japanese Yen
・LazyBearさんの反応の早いMAのクロスによるエントリー
・トレイリングストップ

上記を組み合わせたストラテジーですね。

トレンドが多くてノイズが少ない銘柄&足種で有効だと思います。

※ 解説はコードの中で

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

=====
//@version=2
strategy(title='MicuRobert EMA cross V2 の解説', overlay=true, pyramiding=0, initial_capital=100000)

//
// 設定
////

// セッションタイムを区切るかどうか
USE_TRADESESSION = input(title='Use Trading Session?', type=bool, defval=true)
// トレーリングストップを使うか
USE_TRAILINGSTOP = input(title='Use Trailing Stop?', type=bool, defval=true)
// セッションの時間を指定
// 設定を元にセッションの判断と背景色の指定
trade_session = input(title='Trade Session:', type=string, defval='0400-1500', confirm=false)
istradingsession = not USE_TRADESESSION ? false : not na(time('1', trade_session))
bgcolor(istradingsession?gray:na)
// 取引量の指定
trade_size = input(title='Trade Size:', type=float, defval=10000.00)
// 利確幅と損切り幅
tp = input(title='Take profit in pips:', type=float, defval=55.0) * (syminfo.mintick*10)
sl = input(title='Stop loss in pips:', type=float, defval=22.0) * (syminfo.mintick*10)
// 平均線の計算値
ma_length00 = input(title='EMA length:', type=integer, defval=5)
ma_length01 = input(title='DEMA length:', type=integer, defval=34)
// 計算の元とする価格
price = input(title='Price source:', type=source, defval=open)

//
// ストラテジーに必要なデータの算出と記録、描画まで
////

// LazyBearさんによる反応が早い移動平均線の計算式
// ||--- NO LAG EMA, Credit LazyBear: ---||
f_LB_zlema(_src, _length)=>
_ema1=ema(_src, _length)
_ema2=ema(_ema1, _length)
_d=_ema1-_ema2
_zlema=_ema1+_d
// ||-------------------------------------||

// 平均線の算出と描画
ma00 = f_LB_zlema(price, ma_length00)
ma01 = f_LB_zlema(price, ma_length01)
plot(title='M0', series=ma00, color=black)
plot(title='M1', series=ma01, color=black)

// エントリーしたかどうかの判別
isnewbuy = change(strategy.position_size)>0 and change(strategy.opentrades)>0
isnewsel = change(strategy.position_size)<0 and change(strategy.opentrades)>0

// 新たなエントリー → 価格を記録 → ポジションがあるときだけチャートに描画
buy_entry_price = isnewbuy ? price : buy_entry_price[1]
sel_entry_price = isnewsel ? price : sel_entry_price[1]
plot(title='BE', series=buy_entry_price, style=circles, color=strategy.position_size <= 0 ? na : aqua)
plot(title='SE', series=sel_entry_price, style=circles, color=strategy.position_size >= 0 ? na : aqua)

// 新たなエントリー → 高値・安値を記録 → ポジションがあるときだけ描画
// トレーリングストップに使う
buy_appex = na(buy_appex[1]) ? price : isnewbuy ? high : high >= buy_appex[1] ? high : buy_appex[1]
sel_appex = na(sel_appex[1]) ? price : isnewsel ? low : low <= sel_appex[1] ? low : sel_appex[1]
plot(title='BA', series=buy_appex, style=circles, color=strategy.position_size <= 0 ? na : teal)
plot(title='SA', series=sel_appex, style=circles, color=strategy.position_size >= 0 ? na : teal)

// トレーリングストップの価格を算出 → ポジションがあるときだけ描画
buy_ts = buy_appex - sl
sel_ts = sel_appex + sl
plot(title='Bts', series=buy_ts, style=circles, color=strategy.position_size <= 0 ? na : red)
plot(title='Sts', series=sel_ts, style=circles, color=strategy.position_size >= 0 ? na : red)

//
// トレーディング
////

// エントリーの条件1:移動平均線のGC
buy_cond1 = crossover(ma00, ma01) and (USE_TRADESESSION ? istradingsession : true)
// エントリーの条件2:価格と移動平均線のGC
buy_cond0 = crossover(price, ma00) and ma00 > ma01 and (USE_TRADESESSION ? istradingsession : true)
// ①か②を満たしたところでエントリーとする
buy_entry = buy_cond1 or buy_cond0
// トレーリングストップがONの場合:安値がストップを下回ったところ、もしくは利確幅を超えたら決済
// OFFの場合:固定のストップを下回るか、利確幅を超えたら決済
buy_close = (not USE_TRAILINGSTOP ? low <= buy_entry_price - sl: low <= buy_ts) or high>=buy_entry_price+tp//high>=last_traded_price + tp or low<=last_traded_price - sl //high >= hh or
// 売りは上記の逆
sel_cond1 = crossunder(ma00, ma01) and (USE_TRADESESSION ? istradingsession : true)
sel_cond0 = crossunder(price, ma00) and ma00 < ma01 and (USE_TRADESESSION ? istradingsession : true)
sel_entry = sel_cond1 or sel_cond0
sel_close = (not USE_TRAILINGSTOP ? high >= sel_entry_price + sl : high >= sel_ts) or low<=sel_entry_price-tp//low<=last_traded_price - tp or high>=last_traded_price + sl //low <= ll or

// すべて成行注文
strategy.entry('buy', long=strategy.long, qty=trade_size, comment='buy', when=buy_entry)
strategy.close('buy', when=buy_close)
strategy.entry('sell', long=strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.close('sell', when=sel_close)
=====
Comment:
次の講座

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

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

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

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.