yuya_takahashi_

Pine講座55 バックテスト|Strategy Code Example - Risk Management の解説

教育
yuya_takahashi_ アップデート済   
FX:USDJPY   米ドル/円
JayRogersさんの
Strategy Code Example - Risk Management
の解説です。

前回、前々回に解説して
利益確定やロスカットを含む
リスク管理のサンプルですね。

トレーリングストップや
トレーリングストップのオフセットを
設定することもできます。

明日は、これに取引量を算出する
ロジックも付け足してみようと思います。

※ 解説はコードの中で

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

=====
//@version=2

strategy(title = "Strategy Code Example", shorttitle = "Strategy Code Example", overlay = true, pyramiding = 0, default_qty_type = strategy.percent_of_equity, default_qty_value = 10, currency = currency.GBP)

// Revision: 1
// Author: @JayRogers
//
// *** THIS IS JUST AN EXAMPLE OF STRATEGY RISK MANAGEMENT CODE IMPLEMENTATION ***

// 設定項目と初期値
////

// 移動平均
maFastSource = input(defval = open, title = "Fast MA Source")
maFastLength = input(defval = 14, title = "Fast MA Period", minval = 1)
maSlowSource = input(defval = open, title = "Slow MA Source")
maSlowLength = input(defval = 21, title = "Slow MA Period", minval = 1)

// リスク管理
tradeInvert = input(defval = false, title = "Invert Trade Direction?")
inpTakeProfit = input(defval = 1000, title = "Take Profit", minval = 0)
inpStopLoss = input(defval = 200, title = "Stop Loss", minval = 0)
inpTrailStop = input(defval = 200, title = "Trailing Stop Loss", minval = 0)
inpTrailOffset = input(defval = 0, title = "Trailing Stop Loss Offset", minval = 0)

// リスク管理の値を整える(0→ na )
// 整えないと挙動がおかしくなる
//(0を渡すと「価格0で決済」みたいなことになる)
useTakeProfit = inpTakeProfit >= 1 ? inpTakeProfit : na
useStopLoss = inpStopLoss >= 1 ? inpStopLoss : na
useTrailStop = inpTrailStop >= 1 ? inpTrailStop : na
useTrailOffset = inpTrailOffset >= 1 ? inpTrailOffset : na

// 移動平均の算出と描画
maFast = ema(maFastSource, maFastLength)
maSlow = ema(maSlowSource, maSlowLength)
fast = plot(maFast, title = "Fast MA", color = green, linewidth = 2, style = line, transp = 50)
slow = plot(maSlow, title = "Slow MA", color = red, linewidth = 2, style = line, transp = 50)

// エントリーのロジック
// tradeInertがtrueのときはロジックを反転
aboveBelow = maFast >= maSlow ? true : false
tradeDirection = tradeInvert ? aboveBelow ? false : true : aboveBelow ? true : false

// 売買
// hogehoge()=> で関数化しているけど、挙動は変数とかわらない
enterLong() => not tradeDirection[1] and tradeDirection
exitLong() => tradeDirection[1] and not tradeDirection
strategy.entry(id = "Long", long = true, when = enterLong())
strategy.close(id = "Long", when = exitLong())
enterShort() => tradeDirection[1] and not tradeDirection
exitShort() => not tradeDirection[1] and tradeDirection
strategy.entry(id = "Short", long = false, when = enterShort())
strategy.close(id = "Short", when = exitShort())

// ここで
// 利益確定、ロスカット、トレーリングストップ
// を設定している
strategy.exit("Exit Long", from_entry = "Long", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)
strategy.exit("Exit Short", from_entry = "Short", profit = useTakeProfit, loss = useStopLoss, trail_points = useTrailStop, trail_offset = useTrailOffset)

=====
コメント:
次の講座

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

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

小次郎講師のチャート情報局
bit.ly/2GvLAEp
免責事項

これらの情報および投稿は、TradingViewが提供または保証する金融、投資、取引、またはその他の種類のアドバイスや推奨を意図したものではなく、またそのようなものでもありません。詳しくは利用規約をご覧ください。