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

Pine Script版本升级及重绘(Repaint)问题修复请求

解决Pine Script策略重绘问题并升级至v5版本

问题根源分析

原v2版本脚本的重绘问题主要来自以下几点:

  • 旧版security()函数默认启用前瞻(lookahead)模式,会引用未来未闭合K线的数据,导致信号事后变动
  • 时间周期参数设为字符串类型,存在类型不匹配风险
  • 条件判断中重复调用security(),加剧重绘概率

升级后的无重绘v5代码

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

// 将时间周期改为整数输入,避免类型错误
tim = input.int(160, title="Higher Timeframe Period")

// 使用request.security并禁用前瞻,确保无重绘
// barmerge.lookahead_off 彻底关闭未来数据引用
[htf_open, htf_close] = request.security(syminfo.tickerid, tim, [open, close], barmerge.lookahead_off)

// 绘制更高时间周期的开收盘价
plot(htf_open, color=color.red, title="HTF Open")
plot(htf_close, color=color.green, title="HTF Close")

// 基于提前获取的高周期数据判断交叉条件
long_condition = ta.crossover(htf_close, htf_open)
short_condition = ta.crossunder(htf_close, htf_open)

// 多单入场:仅在无持仓时开多
if (long_condition)
    strategy.entry("Long", strategy.long, when=strategy.position_size == 0)

// 空单入场:仅在无持仓时开空
if (short_condition)
    strategy.entry("Short", strategy.short, when=strategy.position_size == 0)

关键修改说明

  • 版本升级:切换至Pine Script v5,使用更稳定的request.security()函数
  • 禁用前瞻数据:添加barmerge.lookahead_off参数,确保所有信号仅基于已闭合K线的数据,彻底解决重绘
  • 优化参数类型:将时间周期输入改为整数,避免字符串转译的潜在问题
  • 减少重复调用:一次性获取高周期的开收盘价,避免多次调用request.security()
  • 持仓逻辑优化:添加when=strategy.position_size == 0,避免同时持有多空仓位

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 07:27:16