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

遵循最佳实践后Pine Script仍存在重绘问题求助

求助:Pine Script策略实时运行与回测/刷新后结果不一致问题

此前我曾提出过类似问题,原以为已解决,但因无法提供可视化展示,故再次发帖求助,当前问题仍未解决。

我编写的简单策略(代码附后)使用request.security()方法获取更高时间框架的数据,虽尝试多种方案避免重绘,但问题依旧,无法定位原因。

将该策略应用于SPY的5分钟K线图(未开启盘后交易时段),实时运行一段时间后得到如下交易记录:
实时交易记录

但刷新网页后,结果更新为:
刷新后交易记录
(注意当日后续交易记录的变化)

我期望该策略在K线回放、回测工具及实时交易中表现一致(无需定期刷新页面)。当前K线回放与回测工具表现一致,仅在实时交易运行数日后才会出现问题,测试难度较大。

我的猜测是:实时交易时request.security()未按预期工作,它会基于当前K线时间聚合更高时间框架(此处为2小时)的数据。例如当前时间为14:10时,更高时间框架的前一根完整K线收盘时间为12:10,这可能导致每5分钟信号切换,因更高时间框架的K线持续更新;但回测引擎/刷新页面时,会基于回测的历史时间(如12:00、14:00等)计算K线。不过我对该猜测并不完全确定,需要明确的解释。

我已尝试多种方案:

  • 在request.security()调用中使用close[1]
  • 确保策略参数中calculate_on_every_tick设为false
  • 尝试不同的gaps和lookahead组合
  • 尝试获取“前一根已完全闭合的更高时间框架K线”数据

但问题仍存在于实时交易中。

我能想到的唯一修复方案是:编写方法对时间取模,当时间为更高时间框架的整数倍时,才获取前一根更高时间框架K线的数据,以此避免实时状态下更高时间框架K线的重绘。但我原以为使用barstate.isconfirmed或类似选项就能实现该逻辑,不过尝试多种配置后均无效。

以下是策略代码:

//@version=5
global_strategy_name = "Mobius_Useful_Candles2_Strat_Matts"
//=========================== IMPORTS AND STRATEGY ===========================//
strategy(global_strategy_name, shorttitle=global_strategy_name, 
     overlay=true, 
     initial_capital=1000, 
     pyramiding=1, 
     precision=2,
     calc_on_order_fills=false, 
     calc_on_every_tick=false, 
     //backtest_fill_limits_assumption=30, 
     default_qty_type=strategy.fixed,//strategy.percent_of_equity, 
     default_qty_value=1,//100, 
     currency=currency.NONE, 
     slippage=30, 
     commission_type=strategy.commission.cash_per_contract, 
     commission_value=0.01)

//=========================== INPUTS ===========================//
//========= STRATEGY
input_agg_period = input.timeframe(
     title = "Higher TimeFrame Aggregation Period", 
     defval = "120", //"240", // 120
     group = 'Strategy',
     tooltip = 'The higher timeframe to use with strategy')

input_trend_periods = input.int(
     title = "Trend Periods", 
     minval = 1, 
     defval = 358,
     group = 'Strategy',
     tooltip = 'The number of bars/periods to use for calculating trend')


//=========================== STRATEGY ===========================//
// Get the upper timeframe's previous, fully closed/formed bar's high, low and close
h = request.security(syminfo.ticker, input_agg_period, high, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)[2]
l = request.security(syminfo.ticker, input_agg_period, low, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)[2]
c = request.security(syminfo.ticker, input_agg_period, close, gaps = barmerge.gaps_off, lookahead = barmerge.lookahead_on)[2]

hh = ta.highest(h, input_trend_periods)
ll = ta.lowest(l, input_trend_periods)

// If "trend" > 0.5, then true/uptrend, else false/downtrend
trendSignal = (c - ll) / (hh - ll) > 0.5 ? 1 : 0

// Update the bar colours for visual inspection
barcolor(trendSignal == 1 ? color.rgb(0, 252, 0) : color.red)

// Define entry conditions
longCondition = trendSignal == 1
shortCondition = trendSignal == 0

// Check for currently held positions
bool longIsActive = strategy.position_size > 0
bool shortIsActive = strategy.position_size < 0

// Define exit conditions
bool validExitLongPosition = shortCondition and longIsActive
bool validExitShortPosition = longCondition and shortIsActive

// Enter/Exit trades
if longCondition
    strategy.entry("Enter Long", strategy.long, alert_message = "MOBIUS - Enter Long")
else if shortCondition
    strategy.entry("Enter Short", strategy.short, alert_message = "MOBIUS - Enter Short")

if validExitLongPosition or validExitShortPosition
    strategy.close_all("x", alert_message = global_strategy_name + " - Exit Position")

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 17:03:14