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

Pine Script技术求助:工作日特定GMT时间绘制虚线竖线失败

问题修复:工作日特定GMT时间绘制虚线竖线

原代码存在两个核心问题:

  • 触发画线的条件错误,仅在t1到t2区间内的K线重复创建线条,导致线条重叠看起来像实线,且无法确保只画一次
  • 未判断是否是当天首次到达目标时间,导致重复生成大量线条

修复后的代码:

//@version=5
indicator('Timegap', overlay=true, max_lines_count=500, max_bars_back=4999)

// 判断是否为工作日
is_weekday = dayofweek != dayofweek.saturday and dayofweek != dayofweek.sunday

// 定义目标GMT时间戳
t1 = timestamp("GMT", year, month, dayofmonth, 11, 30, 0)
t2 = timestamp("GMT", year, month, dayofmonth, 15, 30, 0)

// 仅在当天首次到达目标时间、且为工作日时画线
if is_weekday
    // 检测当前K线是否包含t1时间点(避免跨K线遗漏)
    if time[1] < t1 and time >= t1
        line.new(t1, ta.lowest(low, 100), t1, ta.highest(high, 100), 
                 xloc.bar_time, extend.both, color.rgb(255,255,0), 
                 line.style_dashed, 1)
    // 检测当前K线是否包含t2时间点
    if time[1] < t2 and time >= t2
        line.new(t2, ta.lowest(low, 100), t2, ta.highest(high, 100), 
                 xloc.bar_time, extend.both, color.rgb(255,255,0), 
                 line.style_dashed, 1)

关键修改说明:

  • 调整触发逻辑:通过time[1] < t1 and time >= t1判断当前K线是否是首次覆盖目标时间点,确保每个目标时间只画一次线条
  • 替换高低点参数:用ta.lowest(low,100)和ta.highest(high,100)替代单根K线的low/high,让线条覆盖更多区间,避免因单根K线范围过小导致显示不明显
  • 移除错误的timeIsOk条件:原条件会在t1到t2之间的所有K线重复画线,导致线条重叠成实线,现在改为精准触发单次画线

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 07:50:27