跟踪止损会在每根新柱子上触发的原因通常是在计算止损时使用了“以下最低点/最高点”的函数。这些函数将检测到价格在下一根柱子上超过了止损水平,因此会在那根柱子上触发止损,即使价格最终在同一柱子内反弹回止损水平。
解决此问题的方法是使用“以下/以上最低/最高点的依赖项”函数来计算跟踪止损,这将防止在每根柱子上都触发止损。以下是一个包含此函数的示例跟踪止损策略代码:
//@version=4
strategy("Trailing Stop Loss Example")
// user inputs
stop_loss_percent = input(title="Stop Loss %", type=input.float, defval=1.0, minval=0.1, maxval=5.0, step=0.1)
trail_amount_percent = input(title="Trail Amount %", type=input.float, defval=0.5, minval=0.1, maxval=5.0, step=0.1)
// compute trailing stop loss level
lowest_low = lowest(low, 14)
stop_loss_level = lowest_low * (1 - stop_loss_percent/100)
trail_amount = lowest_low * (trail_amount_percent/100)
trailing_stop = math.max(stop_loss_level, close - trail_amount)
// plot trailing stop loss level
plot(trailing_stop)
// enter long position if conditions are met
if (close > open)
strategy.entry("Long", strategy.long)
// exit long position if trailing stop loss is hit
if (strategy.position_size > 0 && close < trailing_stop)
strategy.exit("Exit Long", from_entry="Long")