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

Pine Script追踪止损警报执行问题求助及代码修正

解决Pine Script策略的三类核心问题

问题1:警报触发延迟

原因

原代码依赖strategy.closedtrades判断平仓事件,该API仅在K线闭合后才会记录平仓数据,导致警报滞后于图表上的平仓信号(紫色箭头)。

修复方案

直接在K线实时更新时监控止损/追踪止损的触发条件,一旦价格触及止损线或追踪止损线,立即触发警报,无需等待K线闭合。


问题2:平仓注释无法区分类型

原因

原代码未给止损(Stop Loss)和追踪止损(Trailing Stop)的平仓操作设置差异化注释,导致两类平仓的注释均显示为"Trailing"。

修复方案

在strategy.exit函数中,分别通过comment_stop和comment_trail参数为止损、追踪止损设置独立注释,明确区分平仓类型。


问题3:空头计数器异常

原因

原代码仅在反向开仓(Cross)平仓时更新shortCerrado变量,止损/追踪止损平仓时未同步更新该变量,导致始终使用初始值(或旧值),交易编号固定为1。

修复方案

在监控到止损/追踪止损触发时,立即将shortCerrado赋值为当前持仓的shortEnCurso,确保交易编号与对应持仓匹配。


完整修正代码

//@version=5
strategy("Fixed Strategy", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, commission_type=strategy.commission.percent, commission_value=0.1)

// === INPUTS ===
activarTrailing = input.bool(true, "Activate Trailing Stop")
evitarDoble = input.bool(true, "Avoid Double Positions")
labelLong = input.string("Long Entry", "Long Label")
labelShort = input.string("Short Entry", "Short Label")

// === ATR & STOP LOSS SETUP ===
atrLength = input.int(14, "ATR Length")
atrMultiplier = input.float(2.0, "ATR Multiplier for Trailing")
stopLossPerc = input.float(1.0, "Stop Loss Percentage (%)")

atrValue = ta.atr(atrLength)
var float longStopPrice = na
var float longTrailPrice = na
var float shortStopPrice = na
var float shortTrailPrice = na

// === COUNTERS ===
var int longCount = 0, shortCount = 0
var int longEnCurso = na
var int shortEnCurso = na
var int longCerrado = na
var int shortCerrado = na

// === EXECUTION ===
// 模拟开仓条件(需替换为你的实际条件)
longCond = ta.crossover(ta.sma(close, 5), ta.sma(close, 20))
shortCond = ta.crossunder(ta.sma(close, 5), ta.sma(close, 20))

// LONG开仓逻辑
if longCond and strategy.position_size <= 0 and evitarDoble
    if strategy.position_size < 0
        shortCerrado := shortEnCurso
        strategy.close("SHORT", comment="Cross SHORT #" + str.tostring(shortCerrado))
        alert("CROSS SHORT #" + str.tostring(shortCerrado), alert.freq_once_per_bar)
    
    longCount += 1
    longEnCurso := longCount
    strategy.entry("LONG", strategy.long, comment="LONG #" + str.tostring(longCount) + " | " + labelLong)
    alert("LONG #" + str.tostring(longCount), alert.freq_once_per_bar)
    
    // 更新LONG的止损和追踪止损价格
    longStopPrice := strategy.position_avg_price * (1 - stopLossPerc / 100)
    longTrailPrice := strategy.position_avg_price - atrValue * atrMultiplier
    
    // 设置EXIT指令,区分止损和追踪止损注释
    strategy.exit("EXIT LONG", "LONG", 
        stop=longStopPrice, 
        trail_points=atrValue * atrMultiplier, 
        comment_stop="Stop LOSS LONG #" + str.tostring(longEnCurso),
        comment_trail="Trailing STOP LONG #" + str.tostring(longEnCurso))

// SHORT开仓逻辑
if shortCond and strategy.position_size >= 0 and evitarDoble
    if strategy.position_size > 0
        longCerrado := longEnCurso
        strategy.close("LONG", comment="Cross LONG #" + str.tostring(longCerrado))
        alert("CROSS LONG #" + str.tostring(longCerrado), alert.freq_once_per_bar)
    
    shortCount += 1
    shortEnCurso := shortCount
    strategy.entry("SHORT", strategy.short, comment="SHORT #" + str.tostring(shortCount) + " | " + labelShort)
    alert("SHORT #" + str.tostring(shortCount), alert.freq_once_per_bar)
    
    // 更新SHORT的止损和追踪止损价格
    shortStopPrice := strategy.position_avg_price * (1 + stopLossPerc / 100)
    shortTrailPrice := strategy.position_avg_price + atrValue * atrMultiplier
    
    // 设置EXIT指令,区分止损和追踪止损注释
    strategy.exit("EXIT SHORT", "SHORT", 
        stop=shortStopPrice, 
        trail_points=atrValue * atrMultiplier, 
        comment_stop="Stop LOSS SHORT #" + str.tostring(shortEnCurso),
        comment_trail="Trailing STOP SHORT #" + str.tostring(shortEnCurso))

// === TRAILING ACTIVATION ALERTS ===
var bool trailingAlertaEmitidaLong = false
var bool trailingAlertaEmitidaShort = false

if activarTrailing
    // LONG追踪止损激活提醒
    if strategy.position_size > 0 and not trailingAlertaEmitidaLong
        trailingAlertaEmitidaLong := true
        trailingAlertaEmitidaShort := false
        alert("TRAILING activated: LONG #" + str.tostring(longEnCurso), alert.freq_once_per_bar)
    
    // SHORT追踪止损激活提醒
    if strategy.position_size < 0 and not trailingAlertaEmitidaShort
        trailingAlertaEmitidaShort := true
        trailingAlertaEmitidaLong := false
        alert("TRAILING activated: SHORT #" + str.tostring(shortEnCurso), alert.freq_once_per_bar)

// 重置激活提醒状态
if strategy.position_size == 0
    trailingAlertaEmitidaLong := false
    trailingAlertaEmitidaShort := false

// === REAL-TIME STOP/LOSS & TRAILING ALERTS ===
// LONG实时监控
if strategy.position_size > 0
    // 止损触发
    if close <= longStopPrice
        longCerrado := longEnCurso
        alert("STOP LOSS executed: LONG #" + str.tostring(longCerrado) + " | Price: " + str.tostring(close), alert.freq_once_per_bar_close)
    // 追踪止损触发
    if close <= longTrailPrice
        longCerrado := longEnCurso
        alert("TRAILING STOP executed: LONG #" + str.tostring(longCerrado) + " | Price: " + str.tostring(close), alert.freq_once_per_bar_close)

// SHORT实时监控
if strategy.position_size < 0
    // 止损触发
    if close >= shortStopPrice
        shortCerrado := shortEnCurso
        alert("STOP LOSS executed: SHORT #" + str.tostring(shortCerrado) + " | Price: " + str.tostring(close), alert.freq_once_per_bar_close)
    // 追踪止损触发
    if close >= shortTrailPrice
        shortCerrado := shortEnCurso
        alert("TRAILING STOP executed: SHORT #" + str.tostring(shortCerrado) + " | Price: " + str.tostring(close), alert.freq_once_per_bar_close)

内容的提问来源于stack exchange,提问作者MasterComander

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:59:56