
この戦略は,スライドストップとスライドストップを組み合わせた段階的なストップを利用した退出戦略である. これは,最初のストップに達した後にストップを損益均衡点に移動し,第二のストップに達した後にストップを損益均衡点に移動し,段階的なストップ・スライドストップの仕組みを実現する. これは,利益の一部をロックしながら,大きな利益の余地を維持する.
この戦略は,以下の部分によって,分段の滑り止め点を実現します.
具体的には,まず100点のストップ距離と100/200/300点の3つのストップ距離を設定します. そして,現在の価格と開設価格に基づいて,利得数を計算する関数を定義します.curProfitInPtsポイントの距離からストップ・プローストの関数calcStopLossPrice。
重要な論理はgetCurrentStage関数,現在のポジションがあるかどうかを判断し,得益数が特定のストップポイントを超えたかどうかを判断し,超えた場合は次の段階へ進みます.例えば,100ポイントストップに達すると第2段階へ進み,200ポイントストップに達すると第3段階へ進みます.
最後に,段階によって異なるストップを修正して,滑点ストップを実現する.第一段階のストップは,元の設定を維持し,第二段階は,利回り均衡に移動し,第三段階は,最初のストップポイントに移動する.
この段階的止滑点策には以下の利点があります.
この戦略にはいくつかのリスクがあります.
この戦略は以下の方向から最適化できます.
/*backtest
start: 2023-11-20 00:00:00
end: 2023-11-27 00:00:00
period: 3m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov
// @description
// when tp1 is reached, sl is moved to break-even
// when tp2 is reached, sl is moved to tp1
// when tp3 is reached - exit
//@version=4
strategy("Stepped trailing strategy example", overlay=true)
// random entry condition
longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
// sl & tp in points
sl = input(100)
tp1 = input(100)
tp2 = input(200)
tp3 = input(300)
curProfitInPts() =>
if strategy.position_size > 0
(high - strategy.position_avg_price) / syminfo.mintick
else if strategy.position_size < 0
(strategy.position_avg_price - low) / syminfo.mintick
else
0
calcStopLossPrice(OffsetPts) =>
if strategy.position_size > 0
strategy.position_avg_price - OffsetPts * syminfo.mintick
else if strategy.position_size < 0
strategy.position_avg_price + OffsetPts * syminfo.mintick
else
0
calcProfitTrgtPrice(OffsetPts) =>
calcStopLossPrice(-OffsetPts)
getCurrentStage() =>
var stage = 0
if strategy.position_size == 0
stage := 0
if stage == 0 and strategy.position_size != 0
stage := 1
else if stage == 1 and curProfitInPts() >= tp1
stage := 2
else if stage == 2 and curProfitInPts() >= tp2
stage := 3
stage
stopLevel = -1.
profitLevel = calcProfitTrgtPrice(tp3)
// based on current stage set up exit
// note: we use same exit ids ("x") consciously, for MODIFY the exit's parameters
curStage = getCurrentStage()
if curStage == 1
stopLevel := calcStopLossPrice(sl)
strategy.exit("x", loss = sl, profit = tp3, comment = "sl or tp3")
else if curStage == 2
stopLevel := calcStopLossPrice(0)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "breakeven or tp3")
else if curStage == 3
stopLevel := calcStopLossPrice(-tp1)
strategy.exit("x", stop = stopLevel, profit = tp3, comment = "tp1 or tp3")
else
strategy.cancel("x")
// this is debug plots for visulalize TP & SL levels
plot(stopLevel > 0 ? stopLevel : na, style = plot.style_linebr)
plot(profitLevel > 0 ? profitLevel : na, style = plot.style_linebr)