You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

策略中Trailing Stop Loss失效问题求助(附Pine Script代码)

问题分析与解决方案

核心错误

你的追踪止损逻辑计算完全错误,导致止损价异常偏离预期:

  • 多头仓位中,stopValue = close * (1 - targetPrice):targetPrice是入场价上浮后的止盈价(大于1),1 - targetPrice为负数,计算出的stopValue会是极低值甚至负数,触发后直接在远低于入场价的位置平仓。
  • 空头仓位中,stopValue = close * (1 + targetPrice):targetPrice是入场价下浮后的止盈价(小于1),1 + targetPrice大于1,计算出的stopValue远高于入场价,导致空头过早平仓。

修正方案

根据你“从止盈价启动追踪止损”的需求,我们需要先判断价格是否触及止盈,之后再启用ATR追踪止损,否则保持初始固定止损。以下是修正后的完整代码:

//@version=4
strategy("Sanky Strategy", overlay=true)

a = input(1, title="Key Value. 'This changes the sensitivity'")
c = input(10, title="ATR Period")
h = input(false, title="Signals from Heikin Ashi Candles")
profit_target_percent = input(0.5, title="Profit Target (%)")
stop_loss = input(0.8, title="Stop Loss (%)")
trail_atr_multiplier = input(1.0, title="Trail ATR Multiplier") // 新增追踪止损ATR系数

xATR = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead=false) : close

xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
   iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
   iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))

pos = 0
pos := iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
   iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 0)))

xcolor = pos == -1 ? color.red : pos == 1 ? color.green : color.blue

ema = ema(src, 1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)

buy = src > xATRTrailingStop and above
sell = src < xATRTrailingStop and below

barbuy = src > xATRTrailingStop
barsell = src < xATRTrailingStop

strategy.entry("long", true, when=buy)
strategy.entry("short", false, when=sell)

// 仓位管理与目标价、初始止损
var float entryPrice = na
var float targetPrice = na
var float initialStop = na
var bool isTargetHit = false // 标记是否触及止盈

if (buy)
    entryPrice := src
    targetPrice := entryPrice * (1 + profit_target_percent / 100)
    initialStop := entryPrice * (1 - stop_loss/100)
    isTargetHit := false // 重置标记
if (sell)
    entryPrice := src
    targetPrice := entryPrice * (1 - profit_target_percent / 100)
    initialStop := entryPrice * (1 + stop_loss/100)
    isTargetHit := false // 重置标记

// 检查是否触及止盈
if (strategy.position_size > 0 and close >= targetPrice)
    isTargetHit := true
if (strategy.position_size < 0 and close <= targetPrice)
    isTargetHit := true

// 追踪止损逻辑
var float trailStop = na

if (strategy.position_size > 0)
    if (isTargetHit)
        // 多头触及止盈后,用ATR向上追踪止损
        trailStop := max(nz(trailStop[1], initialStop), close - trail_atr_multiplier * xATR)
    else
        trailStop := initialStop // 未触及止盈前保持初始止损
    strategy.exit(id="stoplong", stop=trailStop)

if (strategy.position_size < 0)
    if (isTargetHit)
        // 空头触及止盈后,用ATR向下追踪止损
        trailStop := min(nz(trailStop[1], initialStop), close + trail_atr_multiplier * xATR)
    else
        trailStop := initialStop // 未触及止盈前保持初始止损
    strategy.exit(id="stopshort", stop=trailStop)

plotshape(buy, title="Buy", text='Buy', style=shape.labelup, location=location.belowbar, color=color.green, textcolor=color.white, transp=0, size=size.tiny)
plotshape(sell, title="Sell", text='Sell', style=shape.labeldown, location=location.abovebar, color=color.red, textcolor=color.white, transp=0, size=size.tiny)

barcolor(barbuy ? color.green : na)
barcolor(barsell ? color.red : na)

// 可选:绘制止损线
plot(trailStop, color=color.orange, title="Trailing Stop", style=style.line, linewidth=2)

关键修正点

  1. 新增isTargetHit变量,标记价格是否达到止盈价,只有触发后才启动追踪止损
  2. 修正追踪止损计算逻辑:
    • 多头触及止盈后,止损价随价格上涨而抬高(取当前止损价和close - ATR系数*ATR的最大值)
    • 空头触及止盈后,止损价随价格下跌而降低(取当前止损价和close + ATR系数*ATR的最小值)
  3. 未触及止盈前,保持初始固定止损,避免过早触发
  4. 新增trail_atr_multiplier参数,方便调整追踪止损的敏感度

内容的提问来源于stack exchange,提问作者Alpha-Q

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 23:50:58