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

TradingView双策略用close作参数时结果不同步问题咨询

TradingView双策略同步异常问题排查

问题现象

两个TradingView交易策略出现同步异常:

  • 当两者均使用strategy.position_avg_price(持仓平均价)作为止损、止盈、追踪止损的计算基准时,回测与实盘结果完全一致
  • 当计算基准替换为close(收盘价)时,两者结果出现偏差,部分开仓、平仓点位不一致

我此前编写了带资金管理绘图功能、以收盘价作为平仓基准的常规策略,以下是两份待匹配逻辑的策略代码:

策略代码

BOT1代码

strategy("ETHLONG1h", overlay=false, initial_capital = 100, currency = "USD", pyramiding = 1, calc_on_every_tick=true,
 default_qty_type = strategy.fixed, default_qty_value = 0.02, commission_value = 0.08)
//==========================================================================                     
// ALERT TEXT //SSP
//==========================================================================
alert_tip = "Place this code in the message box while creating/setting alerts"
i_alert_box_code     = input("{{strategy.order.comment}}", "Alert Box code", group="alerts", tooltip=alert_tip)

//=========================================================================
// Initial inputs //
Act_RSI_VWAP        = input(true, "RSI VOLUME WEIGHTED AVERAGE PRICE")
RSI_VWAP_length     = input(1,   "RSI-VWAP LENGTH")
RSI_VWAP_overSold   = input(1,   "RSI-VWAP OVERSOLD", type=input.float)
RSI_VWAP_overBought = input(1,   "RSI-VWAP OVERBOUGHT", type=input.float)

// RSI with VWAP as source //
RSI_VWAP = rsi(vwap(close), RSI_VWAP_length)

// Plotting, overlay=false //
r  = plot(RSI_VWAP, color = RSI_VWAP > RSI_VWAP_overBought ? color.red : RSI_VWAP < RSI_VWAP_overSold ? color.lime : color.blue, title="rsi", linewidth=2, style=plot.style_line)

h1 = plot(RSI_VWAP_overBought, color = color.gray, style=plot.style_stepline)
h2 = plot(RSI_VWAP_overSold,   color = color.gray, style=plot.style_stepline)

fill(r,h1, color = RSI_VWAP > RSI_VWAP_overBought ? color.red  : na, transp = 60)
fill(r,h2, color = RSI_VWAP < RSI_VWAP_overSold   ? color.lime : na, transp = 60)
  
// ENTRY SIGNALS //

strategy.entry("long", strategy.long, when = (crossover(RSI_VWAP, RSI_VWAP_overSold)), comment="00b033c2_ENTER-LONG_BYBIT_ETH/USDT_00b033c2-ETH1HLONG_1H")
strategy.exit("long SL/TP/TSL", "long", loss = close * 0.018 / syminfo.mintick, trail_points = close *0.008 / syminfo.mintick, trail_offset = close * 0.0001 / syminfo.mintick, comment="00b033c2_EXIT-LONG_BYBIT_ETH/USDT_00b033c2-ETH1HLONG_1H")

BOT2代码

strategy("1hlongETH TRACING", overlay=true, initial_capital = 100, currency = "USD", pyramiding = 1, calc_on_every_tick=true,
 default_qty_type = strategy.fixed, default_qty_value = 0.02, commission_value = 0.08)
                    
// ALERT TEXT and Time stamps//
bt_start      = input(timestamp("01 jan 2015  00:00 +0000"), "Start Date", input.time,  group="backtesting period")
bt_end        = input(timestamp("31 dec 9999  00:00 +0000"), "End Date", input.time,    group="backtesting period")

bt_window = time >= bt_start and time <= bt_end // backtesting window

alert_tip = "Place this code in the message box while creating/setting alerts"
i_alert_box_code     = input("{{strategy.order.comment}}", "Alert Box code", group="alerts", tooltip=alert_tip)

//Trailing,Stoploss,Takeprofit inputs//

i_profit_perc = input(0, "Take Profit (in %)",       group="risk reward", inline="profit") / 100
i_profit_clr  = input(color.green, "",                 group="risk reward", inline="profit")
i_loss_perc   = input(1.8, "StopLoss (in %)",         group="risk reward", inline="loss") / 100
i_loss_clr    = input(color.red, "",                   group="risk reward", inline="loss")
i_trail_act   = input(0.8, "Activate Trail At (in %)", group="risk reward", inline="trailat") / 100
i_trailat_clr   = input(color.yellow, "",                group="risk reward", inline="trailat")
i_trail_off   = input(0.01, "Trail Offset (in %)",   group="risk reward", inline="trailof") / 100
i_trailof_clr   = input(color.fuchsia, "",                 group="risk reward", inline="trailof")

// ADDITIONAL FUNCTIONS //
var float buy_en = na
var float buy_sl = na
var float buy_tp = na
var float buy_tl = na
var float trail_act_price = na
var bool trail_activated = false
buy_pos  = strategy.position_size >  0
// ======================================================================
// Initial inputs //
Act_RSI_VWAP        = input(true, "RSI VOLUME WEIGHTED AVERAGE PRICE")
RSI_VWAP_length     = input(1,   "RSI-VWAP LENGTH")
RSI_VWAP_overSold   = input(1,   "RSI-VWAP OVERSOLD", type=input.float)
RSI_VWAP_overBought = input(1,   "RSI-VWAP OVERBOUGHT", type=input.float)

// RSI with VWAP as source //
RSI_VWAP = rsi(vwap(close), RSI_VWAP_length)

// Plotting, overlay=false
// r  = plot(RSI_VWAP, color = RSI_VWAP > RSI_VWAP_overBought ? color.red : RSI_VWAP < RSI_VWAP_overSold ? color.lime : color.blue, title="rsi", linewidth=2, style=plot.style_line)

// h1 = plot(RSI_VWAP_overBought, color = color.gray, style=plot.style_stepline)
// h2 = plot(RSI_VWAP_overSold,   color = color.gray, style=plot.style_stepline)

// fill(r,h1, color = RSI_VWAP > RSI_VWAP_overBought ? color.red  : na, transp = 60)
// fill(r,h2, color = RSI_VWAP < RSI_VWAP_overSold   ? color.lime : na, transp = 60)


// ENTRY SIGNALS SL/TP/TSL //
if bt_window
    strategy.entry("long", strategy.long, when = (crossover(RSI_VWAP, RSI_VWAP_overSold)), comment="00b033c2_ENTER-LONG_BYBIT_ETH/USDT_00b033c2-LONG5MIN_5M")

if buy_pos
    buy_en := close
    buy_sl := buy_en * (1 - i_loss_perc)
    buy_tp := i_profit_perc > 0.0 ? buy_en * (1 + i_profit_perc) : na
    trail_act_price := buy_en * (1 + i_trail_act)
    trail_off = (buy_en * i_trail_off) / syminfo.mintick
    // if (high - (trail_off * syminfo.mintick)) > buy_tl
    //     buy_tl := high - (trail_off * syminfo.mintick)
    strategy.exit("long SL/TP/TSL", "long", stop=buy_sl, limit=buy_tp, trail_price=trail_act_price, trail_offset=trail_off, comment="00b033c2_EXIT-LONG_BYBIT_ETH/USDT_00b033c2-LONG5MIN_5M")

    if high >= trail_act_price and not trail_activated
        buy_tl := (high - (trail_off * syminfo.mintick))
        trail_activated := true
    if (high - (trail_off * syminfo.mintick)) > buy_tl[1] and trail_activated
        buy_tl := (high - (trail_off * syminfo.mintick))


if strategy.position_size == 0
    buy_tl := na
    trail_activated := false
//==========================================================================
//                 PLOT
//==========================================================================
ben = plot(buy_pos ? buy_en : na, "Buy entry", color.white, 1, plot.style_linebr)
bsl = plot(buy_pos ? buy_sl : na, "Buy Sl", i_loss_clr, 1, plot.style_linebr)
btp = plot(buy_pos ? buy_tp : na, "Buy TP", i_profit_clr, 1, plot.style_linebr)
btl = plot(buy_pos ? buy_tl : na, "Buy Trail", i_trailof_clr, 1, plot.style_linebr)
plot(buy_pos ? trail_act_price : na, "Trail Activate Price", i_trailat_clr, 1, plot.style_linebr)
fill(ben, bsl, color.red, 90)
fill(ben, btp, color.green, 90)

基准替换验证说明

将两份代码中计算止损止盈的基准替换为strategy.position_avg_price即可实现完全同步,示例代码如下:

//BOT1//
strategy.exit("long SL/TP/TSL", "long", loss = strategy.position_avg_price * 0.018 / syminfo.mintick, trail_points = strategy.position_avg_price *0.008 / syminfo.mintick, trail_offset = strategy.position_avg_price * 0.0001 / syminfo.mintick, comment="00b033c2_EXIT-LONG_BYBIT_ETH/USDT_00b033c2-ETH1HLONG_1H") 

//BOT2//
if buy_pos
    buy_en := strategy.position_avg_price
    buy_sl := buy_en * (1 - i_loss_perc)
    buy_tp := i_profit_perc > 0.0 ? buy_en * (1 + i_profit_perc) : na
    trail_act_price := buy_en * (1 + i_trail_act)
    trail_off = (buy_en * i_trail_off) / syminfo.mintick
    // if (high - (trail_off * syminfo.mintick)) > buy_tl
    //     buy_tl := high - (trail_off * syminfo.mintick)
    strategy.exit("long SL/TP/TSL", "long", stop=buy_sl, limit=buy_tp, trail_price=trail_act_price, trail_offset=trail_off, comment="00b033c2_EXIT-LONG_BYBIT_ETH/USDT_00b033c2-LONG5MIN_5M")

    if high >= trail_act_price and not trail_activated
        buy_tl := (high - (trail_off * syminfo.mintick))
        trail_activated := true
    if (high - (trail_off * syminfo.mintick)) > buy_tl[1] and trail_activated
        buy_tl := (high - (trail_off * syminfo.mintick))

需求说明

希望继续使用close作为计算基准,该参数逻辑经过多次实盘测试收益表现优异,无需调整已经验证有效的运行逻辑,需排查遗漏的配置或逻辑差异点。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 23:36:04