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

Pine Script v5盘前开盘价固定显示问题技术求助

问题解决:固定显示盘前开盘价而非跟随最高价变动

问题根源

你的代码中openPMO := math.max(open, openPMO)这一行会持续将openPMO更新为当前开盘价与历史值的最大值,导致最终显示的是盘前时段的最高价,而非固定的盘前第一根K线开盘价。

解决方案

仅在盘前时段的第一根K线记录开盘价,后续不再修改该值。修改后的完整代码如下:

//@version=5
indicator("testCYD PreMarket High/Low Mid", shorttitle="CYD PM H/L Mid", overlay=true)
t = time("1440", "0000-0930") 

is_first = na(t[1]) and not na(t) or t[1] < t

ending_hour = input(defval=9, title="Ending Hour")
ending_minute = input(defval=29, title="Ending Minute")

day_high = float(na)
day_low = float(na)

if is_first and barstate.isnew and (hour < ending_hour or hour >= 16 or hour == ending_hour and minute < ending_minute)
    day_high := high
    day_low := low
    day_low
else
    day_high := day_high[1]
    day_low := day_low[1]
    day_low

if high > day_high and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
    day_high := high
    day_high

if low < day_low and ((hour < ending_hour or hour >= 16) and hour < 16 or hour == ending_hour and minute < ending_minute)
    day_low := low
    day_low


plot(day_high, style=plot.style_line, color=#009688, linewidth=2, title="PM High")
plot((day_low + day_high) / 2, color=#fff9c4, linewidth=2, title="PM Mid")
plot(day_low, style=plot.style_line, color=#009688, linewidth=2, title="PM Low")


/////////////////
//Pre-Market Open

PMOTime = input.session('0400-0929:1234567', "Session", group="PreMarket Open")
PMOStyle = input.string ("Dashed", "Line Style", options=["Solid", "Dotted", "Dashed"], group="PreMarket Open")
PMOColor = input.color (#09b1d3, group="PreMarket Open")


tPMO = time          ("1", PMOTime)

_PMOStyle   = PMOStyle ==    "Solid" ? line.style_solid : PMOStyle == "Dotted" ? line.style_dotted : line.style_dashed

var line lne = na
var float openPMO = na  // 初始化为na,确保仅在第一根K线赋值

// 仅在盘前时段启动的第一根K线记录开盘价
if tPMO and not tPMO[1]
    openPMO := open

// 仅在openPMO有有效值且K线确认时绘制/更新线条
if not na(openPMO) and barstate.isconfirmed
    // 更新旧线条的终点
    if not na(lne)
        line.set_x2(lne, time)
    // 创建新线条并延伸至当日收盘后
    lne := line.new(time, openPMO, last_bar_time + 14400000/2, openPMO, xloc.bar_time, extend.none, PMOColor, _PMOStyle, 2)

关键修改说明

  1. 将openPMO初始化为na,避免初始默认值干扰逻辑
  2. 移除持续更新openPMO的代码,仅在盘前时段启动的第一根K线(tPMO and not tPMO[1])时赋值为当前开盘价
  3. 优化线条绘制逻辑,确保仅在openPMO有有效值时执行操作,避免无效绘制

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 15:01:12