该策略实现了一个可配置的百分比追踪止损机制,用于控制交易风险。它允许设定长仓和短仓的止损幅度百分比,从入场价格开始不断向有利方向追踪最高价或最低价,从而实现动态止损。
该策略的主要逻辑是:
策略允许自定义止损百分比,例如设置为10%。长仓时,它会实时计算最低价的10%上方作为止损线;短仓时,计算最高价的10%下方作为止损线。
这样,止损线会不断向有利方向移动,实现动态追踪止损,最大程度保护了利润,同时也控制了风险。
应对方法:
该策略可优化的点:
该策略提供了一个行之有效的百分比追踪止损方法,可以动态调整止损线。它可以最大限度保护利润,也可以有效控制风险。通过参数优化、指标集成等方式,可以使止损策略更加智能化和优化。
/*backtest
start: 2023-08-19 00:00:00
end: 2023-09-18 00:00:00
period: 2h
basePeriod: 15m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
// © theCrypster
//@version=4
strategy("Percent Trailing Stop %", overlay=true)
//ENTER SOME SETUP TRADES FOR TSL EXAMPLE
longCondition = crossover(sma(close, 10), sma(close, 20))
if (longCondition)
strategy.entry("My Long Entry Id", strategy.long)
shortCondition = crossunder(sma(close, 10), sma(close, 20))
if (shortCondition)
strategy.entry("My Short Entry Id", strategy.short)
//TRAILING STOP CODE
trailStop = input(title="Long Trailing Stop (%)", type=input.float, minval=0.0, step=0.1, defval=10) * 0.01
longStopPrice = 0.0
shortStopPrice = 0.0
longStopPrice := if strategy.position_size > 0
stopValue = close * (1 - trailStop)
max(stopValue, longStopPrice[1])
else
0
shortStopPrice := if strategy.position_size < 0
stopValue = close * (1 + trailStop)
min(stopValue, shortStopPrice[1])
else
999999
//PLOT TSL LINES
plot(series=strategy.position_size > 0 ? longStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Long Trail Stop", offset=1, title="Long Trail Stop")
plot(series=strategy.position_size < 0 ? shortStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=1, title="Short Trail Stop", offset=1, title="Short Trail Stop")
//EXIT TRADE @ TSL
if strategy.position_size > 0
strategy.exit(id="Close Long", stop=longStopPrice)
if strategy.position_size < 0
strategy.exit(id="Close Short", stop=shortStopPrice)