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

Pine Script自定义指标仅绘制当日数据 不显示历史两日绘制对象问题求助

问题根因

你的脚本无法显示历史标识主要有两个核心原因:

  1. Pine Script默认对box、line这类自定义绘图对象的存储上限很低,历史生成的对象会被后续新增的对象挤出显示队列,所以仅最新的当日标识可以正常展示
  2. 跨品种取数未关闭未来数据引用,且周期匹配逻辑用字符串比对存在兼容问题,可能导致历史信号计算错位、绘制坐标失效

修复后完整代码

//@version=5
indicator(title="Divergence", overlay=true, max_boxes_count=500, max_lines_count=1000)

tf_short = input.timeframe(title="Timeframe", defval='15')
// 校验当前图表周期和设置的分析周期一致
if tf_short != timeframe.period
    runtime.error("请将当前图表周期调整为和Timeframe参数设置的周期一致")

sym1_short = input(title="Symbol1", defval="eurusd")
sym2_short = input(title="Symbol2", defval="gbpusd")

// 跨品种取数关闭未来引用,保证历史信号稳定
open_price1 = request.security(sym1_short, tf_short, open, barmerge.gaps_on, barmerge.lookahead_off) 
close_price1 = request.security(sym1_short, tf_short, close, barmerge.gaps_on, barmerge.lookahead_off) 
high_price1 = request.security(sym1_short, tf_short, high, barmerge.gaps_on, barmerge.lookahead_off)
low_price1 = request.security(sym1_short, tf_short, low, barmerge.gaps_on, barmerge.lookahead_off)
open_price2 = request.security(sym2_short, tf_short, open, barmerge.gaps_on, barmerge.lookahead_off)
close_price2 = request.security(sym2_short, tf_short, close, barmerge.gaps_on, barmerge.lookahead_off)

down_bar2 = close_price2 < open_price2
up_bar2 = close_price2 > open_price2
up_bar1 = close_price1 > open_price1
down_bar1 = close_price1 < open_price1

up_bar = up_bar1 and down_bar2
down_bar = down_bar1 and up_bar2

color_up = color.new(color.green, transp = 80)
color_down = color.new(color.red, transp = 80)

// 用周期乘数判断,兼容不同周期格式的识别
tf_m = timeframe.multiplier
box_offset_x = tf_m == 5 ? -2 : tf_m == 15 ? -1 : na  
box_offset_y = tf_m == 5 ? -1 : tf_m == 15 ? 1 : na 
line_offset_x = tf_m == 5 ? -2 : tf_m == 15 ? -2 : na  
line_offset_y = tf_m == 5 ? +3 : tf_m == 15 ? +6 : na 

if up_bar
    box.new(bar_index+box_offset_x, high_price1,bar_index+box_offset_y, low_price1, border_color=na , bgcolor=color_up )
    line.new(bar_index+line_offset_x, high_price1, bar_index+line_offset_y,  high_price1 , color=color_up)
    line.new(bar_index+line_offset_x, low_price1, bar_index+line_offset_y,  low_price1 , color=color_up)

if down_bar
    box.new(bar_index+box_offset_x, high_price1,bar_index+box_offset_y, low_price1, border_color=na , bgcolor=color_down )
    line.new(bar_index+line_offset_x, high_price1, bar_index+line_offset_y,  high_price1 , color=color_down)
    line.new(bar_index+line_offset_x, low_price1, bar_index+line_offset_y,  low_price1 , color=color_down)

补充说明

如果需要显示更久的历史标识,可以按需调高max_boxes_count和max_lines_count的参数值,两者最大支持设置为10000。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 23:24:04