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

PineScript绘制指定日期垂直线 未来日期不显示问题咨询

PineScript未来日期垂直线不显示问题解决

原代码

//@version=4
study("Plot vertical line with date", overlay=true)
plot((time == timestamp("GMT+0", 2021,11,11,0,0)) ? 100000 : na, color = color.rgb(255, 0, 0, 22), linewidth = 1, title = "Happy 11:11", style =  plot.style_histogram)
plot((time == timestamp("GMT+0", 2022,1,1,0,0)) ? 100000 : na, color = color.rgb(255, 0, 0, 22), linewidth = 1, title = "Happy New Year 2022", style =  plot.style_histogram)

问题原因

你使用的plot加直方图样式的逻辑,必须依赖对应时间点存在已生成的K线才能触发条件判断输出有效值,未来日期还没有对应K线数据,所以条件永远返回na,线自然不会渲染。

解决方法

方案1:改用line.new函数绘制(推荐)

该方法不需要依赖K线存在,可直接在指定时间坐标位置渲染垂直线,修改后代码如下:

//@version=4
study("Plot vertical line with date", overlay=true)
draw_vline(target_ts, line_color, line_title) =>
    var line v = na
    // 仅在最后一根K线计算时执行,避免重复生成线
    if barstate.islast
        // 线的高度覆盖当前图表最近100根K线的高低价范围,可自行调整
        v := line.new(
             x1=target_ts, y1=lowest(low, 100), 
             x2=target_ts, y2=highest(high, 100), 
             color=line_color, width=1, title=line_title
            )
        // 删除上一根生成的线,避免冗余
        line.delete(v[1])

// 调用函数绘制对应日期的线
draw_vline(timestamp("GMT+0", 2021,11,11,0,0), color.rgb(255,0,0,22), "Happy 11:11")
draw_vline(timestamp("GMT+0", 2022,1,1,0,0), color.rgb(255,0,0,22), "Happy New Year 2022")

方案2:保留plot语法的调整方法

如果不想修改绘制逻辑,可同步做两个调整:

  • 打开TradingView图表设置,开启「扩展右边界」,给未来时间预留显示区域
  • 将plot的条件修改为time >= target_ts and time[1] < target_ts,兼容未来时间点位的触发判断

额外注意

要确保timestamp参数里的时区和你当前图表设置的时区一致,否则会出现时间偏移,找不到对应点位的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 02:27:01