
この戦略はATR (Average True Range) 指数とSMA (Simple Moving Average) 指数を組み合わせて,動的ストップロスを追跡する取引システムを実現する.価格がSMAの上で多項を開くと同時にATRに基づく動的ストップを設定すると,ストップ価格は価格の上昇に伴い上昇する.価格が動的ストップロスを破るときは平仓する.この戦略の主要な考え方は,動的ストップを活用してトレンドの状況で利益をロックし,撤回を低減することである.
この戦略はATRとSMA指標に基づくダイナミックなストップ損失追跡取引システムを実現し,トレンドの状況で自動的にストップ損失位置を調整して利益を保護し,リスクを制御する役割を果たします.戦略の論理は明確で,優位性は明らかですが,いくつかの制限とリスク点もあります.空白ロジック,ポジション管理の最適化,最大損失のストップ設定などの合理的な最適化と改善により,戦略の安定性と収益性をさらに向上させることができます.実際のアプリケーションでは,異なる取引品種と周期に応じて戦略のパラメータを柔軟に調整し,リスクを厳格に制御する必要があります.
/*backtest
start: 2023-03-05 00:00:00
end: 2024-03-10 00:00:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=4
strategy("Trailingstop", overlay=true)
if close > sma(close, 50)
strategy.entry("long", strategy.long)
// Trailing stop loss for long positions
Trailperc = 0.20
price_stop_long = 0.0
if (strategy.position_size > 0)
stopValue = close * (1 - Trailperc)
price_stop_long := max(stopValue, price_stop_long[1])
else
price_stop_long := 0
if (strategy.position_size > 0)
strategy.exit(id="stoploss_long", stop=price_stop_long)
// Trailing stop loss for short positions
Trailperc_short = 0.20
price_stop_short = 0.0
if (strategy.position_size < 0)
stopValue_short = close * (1 + Trailperc_short)
price_stop_short := min(stopValue_short, price_stop_short[1])
else
price_stop_short := 0
if (strategy.position_size < 0)
strategy.exit(id="stoploss_short", stop=price_stop_short)
// ATR Trailing Stop for visualization
keyvalue = input(3, title="Key Value. 'This changes the sensitivity'", step=0.5)
atrperiod = input(10, title="ATR Period")
xATR = atr(atrperiod)
nLoss = keyvalue * xATR
xATRTrailingStop = 0.0
xATRTrailingStop := iff(close > nz(xATRTrailingStop[1], 0) and close[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), close - nLoss),
iff(close < nz(xATRTrailingStop[1], 0) and close[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), close + nLoss),
iff(close > nz(xATRTrailingStop[1], 0), close - nLoss, close + nLoss)))
pos = 0
pos := iff(close[1] < nz(xATRTrailingStop[1], 0) and close > nz(xATRTrailingStop[1], 0), 1,
iff(close[1] > nz(xATRTrailingStop[1], 0) and close < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))
xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue
plot(xATRTrailingStop, color = xcolor, title = "Trailing Stop")