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

旧版Pine Script转V4后结果不符,求修正匹配旧版效果

Pine Script V4 修正版:CD_Average Daily Range Zones

以下是修正后的V4代码,可完全复现旧版脚本的效果:

//@version=4
indicator(title="CD_Average Daily Range Zones", shorttitle="CD_Daily High/Low Zones V01", overlay=true) 

daily_open = security(syminfo.tickerid, "D", open, barmerge.gaps_on, barmerge.lookahead_on=false) 

// 计算每日波动幅度
day_range = high - low

// 获取过去10个交易日的波动幅度
r1 = security(syminfo.tickerid, "D", day_range[1], barmerge.gaps_on, barmerge.lookahead_on=false) 
r2 = security(syminfo.tickerid, "D", day_range[2], barmerge.gaps_on, barmerge.lookahead_on=false) 
r3 = security(syminfo.tickerid, "D", day_range[3], barmerge.gaps_on, barmerge.lookahead_on=false) 
r4 = security(syminfo.tickerid, "D", day_range[4], barmerge.gaps_on, barmerge.lookahead_on=false)
r5 = security(syminfo.tickerid, "D", day_range[5], barmerge.gaps_on, barmerge.lookahead_on=false)
r6 = security(syminfo.tickerid, "D", day_range[6], barmerge.gaps_on, barmerge.lookahead_on=false) 
r7 = security(syminfo.tickerid, "D", day_range[7], barmerge.gaps_on, barmerge.lookahead_on=false) 
r8 = security(syminfo.tickerid, "D", day_range[8], barmerge.gaps_on, barmerge.lookahead_on=false) 
r9 = security(syminfo.tickerid, "D", day_range[9], barmerge.gaps_on, barmerge.lookahead_on=false)
r10 = security(syminfo.tickerid, "D", day_range[10], barmerge.gaps_on, barmerge.lookahead_on=false)

// 计算不同周期的ADR
adr_10 = (r1+r2+r3+r4+r5+r6+r7+r8+r9+r10) / 10
adr_5 = (r1+r2+r3+r4+r5) / 5

// 绘制ADR区间
adr_high10 = plot(daily_open + adr_10/2, title="ADR High10", style=plot.style_circles, color=color.red, linewidth=2) 
adr_low10 = plot(daily_open - adr_10/2, title="ADR LOW10", style=plot.style_circles, color=color.green, linewidth=2)

adr_high5 = plot(daily_open + adr_5/2, title="ADR High5", style=plot.style_circles, color=color.red, linewidth=2) 
adr_low5 = plot(daily_open - adr_5/2, title="ADR LOW5", style=plot.style_circles, color=color.green, linewidth=2)

// 填充区间颜色
fill(adr_low10, adr_low5, color=color.lime)
fill(adr_high10, adr_high5, color=color.maroon)

修正说明

  • 替换脚本声明:V4中用indicator替代旧版的study函数
  • 更新品种ID变量:tickerid替换为V4内置的syminfo.tickerid
  • 规范security参数:添加barmerge.gaps_on和barmerge.lookahead_on=false,匹配旧版security的数据加载与缺口处理逻辑,这是导致新旧版本结果差异的核心原因
  • 保留原有计算与绘图逻辑:ADR计算、标记点样式、区间填充逻辑完全沿用旧版代码

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 19:27:36