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

已有盘前高低线代码,如何获取常规交易时段首根K线OHLC?

获取常规交易时段第一根K线OHLC数据的实现方案

需求说明

我正在开发一款突破指标,当前使用以下基于盘前交易时段(pre-market session)的代码。现需获取常规交易时段(regular session)第一根K线的OHLC数据(而非当日第一根K线),用于线条计算,请问该如何实现?

原代码

//@version=5

indicator(title='script', shorttitle='script', overlay=true)

//Premarket high and low
t = time("1440","0400-0930")
is_first = na(t[1]) and not na(t) or t[1] < t
ending_hour = 9
ending_minute = 30

pm_high = float(na)
pm_low = float(na)


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

if high > pm_high and (hour < ending_hour or hour >= 1600 or hour == ending_hour and minute < ending_minute)
    pm_high := high
    pm_high

if low < pm_low and (hour < ending_hour or hour >= 1600 or hour == ending_hour and minute < ending_minute)
    pm_low := low
    pm_low

//variable
td = time - time[5]
pm_calc = pm_low + ((pm_high - pm_low) /2)
pm_new = pm_low + ((pm_high - pm_low) /2)



//Premarket high and low lines
line.new(bar_index, pm_high, bar_index +1, pm_high, extend=extend.none, color=#d50000, width =1)

//plot(pm_high, style=plot.style_line, title='Premarket high', trackprice=true, color=color.new(#000000, 0), linewidth=1)
pmh = label.new(x=time + td, y=pm_high, text='PM High', xloc=xloc.bar_time, style=label.style_none, textcolor=#000000, size=size.normal, textalign=text.align_center)
label.delete(pmh[1])

line.new(bar_index, pm_low, bar_index +1, pm_low, extend=extend.none, color=#d50000, width =1)
//plot(pm_low, style=plot.style_line, title='Premarket low', trackprice=true, color=color.new(#000000, 0), linewidth=1)
pml = label.new(x=time + td, y=pm_low, text='PM Low', xloc=xloc.bar_time, style=label.style_none, textcolor=#000000, size=size.normal, textalign=text.align_center)
label.delete(pml[1])

实现方案

要捕获常规交易时段的第一根K线OHLC,核心思路是标记常规时段的起始K线,并将该K线的价格数据持久化保存。以下是修改后的完整代码,已添加相关逻辑:

//@version=5

indicator(title='script', shorttitle='script', overlay=true)

// ---------------------- 盘前时段逻辑(保留原代码) ----------------------
t = time("1440","0400-0930")
is_first = na(t[1]) and not na(t) or t[1] < t
ending_hour = 9
ending_minute = 30

pm_high = float(na)
pm_low = float(na)

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

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

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

// ---------------------- 新增:常规交易时段首K线OHLC捕获逻辑 ----------------------
// 定义常规交易时段(示例为美股09:30-16:00,可根据市场调整)
regular_session_start = 930
regular_session_end = 1600
regular_session_time = time("1440", str.tostring(regular_session_start) + "-" + str.tostring(regular_session_end))

// 判断是否是常规时段的第一根K线
is_regular_first = na(regular_session_time[1]) and not na(regular_session_time) or regular_session_time[1] < regular_session_time

// 初始化常规时段首K的OHLC变量(用var确保值持久化)
var float reg_open = na
var float reg_high = na
var float reg_low = na
var float reg_close = na

// 捕获首K数据,仅在常规时段起始时更新一次
if is_regular_first and barstate.isnew
    reg_open := open
    reg_high := high
    reg_low := low
    reg_close := close

// ---------------------- 原变量及绘图逻辑(保留) ----------------------
td = time - time[5]
pm_calc = pm_low + ((pm_high - pm_low) /2)
pm_new = pm_low + ((pm_high - pm_low) /2)

// 盘前高低线绘制
line.new(bar_index, pm_high, bar_index +1, pm_high, extend=extend.none, color=#d50000, width =1)
pmh = label.new(x=time + td, y=pm_high, text='PM High', xloc=xloc.bar_time, style=label.style_none, textcolor=#000000, size=size.normal, textalign=text.align_center)
label.delete(pmh[1])

line.new(bar_index, pm_low, bar_index +1, pm_low, extend=extend.none, color=#d50000, width =1)
pml = label.new(x=time + td, y=pm_low, text='PM Low', xloc=xloc.bar_time, style=label.style_none, textcolor=#000000, size=size.normal, textalign=text.align_center)
label.delete(pml[1])

// 可选:绘制常规首K的价格线用于验证
plot(reg_open, color=color.blue, title="Regular Session Open", linewidth=1)
plot(reg_high, color=color.green, title="Regular Session High", linewidth=1)
plot(reg_low, color=color.red, title="Regular Session Low", linewidth=1)
plot(reg_close, color=color.orange, title="Regular Session Close", linewidth=1)

关键逻辑说明

  1. 常规时段标记:用time("1440", "0930-1600")识别当日常规交易时段内的K线,1440代表每日周期,后面的字符串是时段范围。
  2. 首K判断:is_regular_first通过对比当前和前一根K线的时段时间,确定是否是常规时段的第一根K线。
  3. 数据持久化:用var关键字定义OHLC变量,确保变量值在整个交易日内仅在首K出现时更新一次,后续保持不变。
  4. 避免重复赋值:结合barstate.isnew确保只在K线生成的第一时刻捕获数据,防止重复赋值。

你可以根据目标市场的常规交易时段调整regular_session_start和regular_session_end的数值(比如A股是0930-1130、1300-1500,需要拆分处理,核心逻辑一致)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 15:01:01