TradingView Pine Script多空止盈止损回测问题修复
问题根因
- Pine Script v4中,对同一持仓多次调用
strategy.exit()提交不同ID的退出指令时,后执行的指令会直接覆盖先前的指令。原代码将多头止盈、多头止损拆分为两个独立ID(Long TP/LONG StopLoss)的退出调用,空头止盈、止损也做了相同拆分,导致每次仅最后提交的止盈或止损规则生效,止盈止损无法同时触发。 - 原代码所有退出指令未通过
from_entry参数绑定对应方向的开仓单,多空退出逻辑容易出现串扰,无法精准匹配对应持仓。 - 原代码缺失SuperTrend指标计算、开仓逻辑、
buySignal/sellSignal/barcoloring变量的定义,直接运行会报未定义错误。
修复规则
- 同一方向持仓的止盈、止损参数合并到同一个
strategy.exit()调用中,每个方向仅使用一个固定退出ID,避免指令互相覆盖。 - 开仓时指定唯一的开仓ID,退出指令通过
from_entry参数绑定对应方向的开仓单,确保退出规则仅作用于对应方向的持仓。 - 补全缺失的SuperTrend计算、开仓逻辑、变量定义,保证代码可直接运行回测。
修正后可运行代码
//@version=4 strategy("SuperTrend STRATEGY", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000, initial_capital=100000, commission_type=strategy.commission.percent, commission_value=0.1) // SuperTrend指标参数 stLength = input(title="SuperTrend 周期", type=input.integer, minval=1, defval=10) stMultiplier = input(title="SuperTrend 乘数", type=input.float, minval=0.1, step=0.1, defval=3.0) barcoloring = input(title="显示K线多空着色", type=input.bool, defval=true) // 计算SuperTrend与多空信号 [stLine, stDirection] = supertrend(stMultiplier, stLength) buySignal = stDirection < 0 and stDirection[1] > 0 sellSignal = stDirection > 0 and stDirection[1] < 0 alertcondition(buySignal, title='Buy Signal', message='Buy Signal Alert') alertcondition(sellSignal, title='Sell Signal', message='Sell Signal Alert') // K线着色逻辑 buyBarsSince = barssince(buySignal) sellBarsSince = barssince(sellSignal) barColor = buyBarsSince[1] < sellBarsSince[1] ? color.green : buyBarsSince[1] > sellBarsSince[1] ? color.red : na barcolor(barcoloring ? barColor : na) // 止盈止损输入参数 longProfitPerc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=2.3) * 0.01 shortProfitPerc = input(title="Short Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=5.2) * 0.01 longLossPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01 shortLossPerc = input(title="Short Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=1) * 0.01 // 计算止盈止损价格 longTpPrice = strategy.position_avg_price * (1 + longProfitPerc) shortTpPrice = strategy.position_avg_price * (1 - shortProfitPerc) longSlPrice = strategy.position_avg_price * (1 - longLossPerc) shortSlPrice = strategy.position_avg_price * (1 + shortLossPerc) // 开仓逻辑:信号触发时平反向仓、开对应方向仓位 if buySignal strategy.close("Short Entry", comment="平空") strategy.entry("Long Entry", strategy.long) if sellSignal strategy.close("Long Entry", comment="平多") strategy.entry("Short Entry", strategy.short) // 同方向止盈止损合并为单个exit调用,绑定对应开仓ID if strategy.position_size > 0 strategy.exit(id="Long Exit", from_entry="Long Entry", limit=longTpPrice, stop=longSlPrice) if strategy.position_size < 0 strategy.exit(id="Short Exit", from_entry="Short Entry", limit=shortTpPrice, stop=shortSlPrice) // 绘图 plot(stLine, title="SuperTrend", color=stDirection < 0 ? color.green : color.red, linewidth=2) plot(series=(strategy.position_size > 0) ? longTpPrice : na, color=color.green, style=plot.style_circles, linewidth=3, title="Long Take Profit") plot(series=(strategy.position_size < 0) ? shortTpPrice : na, color=color.red, style=plot.style_circles, linewidth=3, title="Short Take Profit") plot(series=(strategy.position_size > 0) ? longSlPrice : na, color=color.red, style=plot.style_cross, linewidth=2, title="Long Stop Loss") plot(series=(strategy.position_size < 0) ? shortSlPrice : na, color=color.red, style=plot.style_cross, linewidth=2, title="Short Stop Loss")
内容的提问来源于stack exchange,提问作者Marco Baglioni
相关产品推荐
相关产品推荐

