TradingView
yuya_takahashi_
2019年9月11日午前2時3分

Pine講座56 バックテストで資金管理 教育

U.S. Dollar/Japanese YenFXCM

詳細

昨日のコードに
資金管理を追加してみます。

具体的には、

まず以下を設定し
・初期資金
・lot
・1エントリーの資金に対する割合
・複利と単利の別

以下から取引量を算出
・単利の場合、初期資金
・複利の場合、残高
・atr

そして、
エントリー時に取引量を反映

ということを行っています。

※ 解説はコードの中で

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

=====
//@version=4

strategy(
title="Strategy Code Example"
,shorttitle="Strategy Code Example"
,overlay=true
,pyramiding=0
,initial_capital=1000000
,default_qty_type=strategy.fixed
,default_qty_value=1)

// 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)

// 資金管理
lot = input( defval=1000 )
unit_val_pct = input( defval=0.01 )
interest_type = input( defval="compound" ,options=["compound" ,"simple"] )

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

// 資金管理
atr = ema( tr ,20 )
unit_val_src = interest_type=="compound" ? strategy.initial_capital + strategy.netprofit : strategy.initial_capital
unit_val = unit_val_src * unit_val_pct
amount = round( unit_val / atr / lot ) * lot

// 移動平均の算出と描画
maFast = ema(maFastSource, maFastLength)
maSlow = ema(maSlowSource, maSlowLength)
fast = plot(maFast, title="Fast MA", color=color.green, linewidth=2, style=plot.style_line, transp=50)
slow = plot(maSlow, title="Slow MA", color=color.red, linewidth=2, style=plot.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", qty=amount, long=true, when=enterLong())
strategy.close(id="Long", when=exitLong())
strategy.entry(id="Short", qty=amount, long=false, when=exitLong())
strategy.close(id="Short", when=enterLong())

// ここで
// 利益確定、ロスカット、トレーリングストップ
// を設定している
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)
=====

コメント

次の講座
詳細