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

Pinescript v2转v5策略结果不一致,寻求解决办法

PineScript v2转v5策略结果不一致问题

我有一个基于PineScript v2的简单策略,希望转换为PineScript v5,但转换后的策略运行结果与v2版本完全不同。

原PineScript v2代码

//@version=2
strategy(title='Strategy 1', shorttitle='Strategy 1', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input(1)
tf = input('60')
r = heikenashi(tickerid)
ro = security(r, tf, open)
rc = security(r, tf, close)
sel_entry = crossunder(rc, ro)
buy_entry = crossover(rc, ro)
strategy.entry('sell', long=strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', strategy.long, qty=trade_size, comment='buy', when=buy_entry)

已转换的PineScript v5代码(存在问题)

//@version=5
strategy(title='v5', shorttitle='v5', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input.int(1)
tf = input.timeframe('60')
ha_ticker = ticker.heikinashi(syminfo.tickerid)
ha_open = request.security(ha_ticker, tf, open)
ha_close = request.security(ha_ticker, tf, close)
sel_entry = ta.crossunder(ha_close, ha_open)
buy_entry = ta.crossover(ha_close, ha_open)
strategy.entry('sell', strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', strategy.long, qty=trade_size, comment='buy', when=buy_entry)

问题原因与修复方案

核心差异

PineScript v2的security函数与v5的request.security默认行为不同:

  • v2默认启用barmerge.lookahead_on和barmerge.gaps_on,会在当前K线完全收盘后,获取完整的更高时间框架Heikin Ashi K线数据
  • v5默认使用barmerge.lookahead_off和barmerge.gaps_off,会在更高时间框架K线形成过程中就返回数据,导致信号触发时机与v2错位

修复后的PineScript v5代码

//@version=5
strategy(title='Strategy 1', shorttitle='Strategy 1', overlay=true, pyramiding=0, initial_capital=10, currency=currency.USD)
trade_size = input.int(1, title="Trade Size")
tf = input.timeframe('60', title="Timeframe")
ha_ticker = ticker.heikinashi(syminfo.tickerid)
// 对齐v2 security函数的barmerge行为
[ha_open, ha_close] = request.security(ha_ticker, tf, [open, close], barmerge.gaps_on, barmerge.lookahead_on)
sel_entry = ta.crossunder(ha_close, ha_open)
buy_entry = ta.crossover(ha_close, ha_open)
strategy.entry('sell', strategy.short, qty=trade_size, comment='sell', when=sel_entry)
strategy.entry('buy', strategy.long, qty=trade_size, comment='buy', when=buy_entry)

额外说明

  • 修复后的代码保留了原v2策略的标题,避免混淆
  • 将两次request.security调用合并为一次,同时获取open和close数据,提升效率
  • 显式指定barmerge参数,确保与v2的信号触发时机完全一致

内容的提问来源于stack exchange,提问作者Psyll.com

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:37:46