Pine Script 从前一天的第一根蜡烛开始绘制水平射线(盘中)

Pine Script draw horizontal ray starting from first previous day candle (intraday)

我对 pine-script 真的很陌生,我正在尝试制作一个脚本来绘制一条水平射线,该射线从前一天的第一根蜡烛开始,以显示前一天的高点、低点和收盘价。我找到了一个类似的脚本,但无法使其正常工作。

//@version=4
study("PRIOR DAY LEVELS", overlay=true)

PDH = security(syminfo.tickerid,"D",high)
PDC = security(syminfo.tickerid,"D",close)
PDL = security(syminfo.tickerid,"D",low)

var line l_pdh = na, var line l_pdc = na, var line l_pdl = na

// if barstate.islast
l_pdh := line.new(bar_index-1, PDH, bar_index, PDH, extend=extend.right, color=color.green)
l_pdc := line.new(bar_index-1, PDC, bar_index, PDC, extend=extend.right, color=color.purple)
l_pdl := line.new(bar_index-1, PDL, bar_index, PDL, extend=extend.right, color=color.red)

line.delete(l_pdh[1])
line.delete(l_pdc[1])
line.delete(l_pdl[1])

使用此脚本,水平射线线从 1 个先前的蜡烛开始。我想我必须使用 xloc.bar_time 而不是 bar_index 但无法解决它。

一如既往,我们将不胜感激。

编辑

@vitruvius 提供的答案很好。我现在正在尝试为 PreMarket High/Low.

添加更多行

我的新代码如下:

//@version=4
study("PRIOR DAY LEVELS", overlay=true)

PDH = security(syminfo.tickerid,"D",high[1])
PDC = security(syminfo.tickerid,"D",close[1])
PDL = security(syminfo.tickerid,"D",low[1])

//Premarket high and low
t = time("1440","0400-0930") // 1440 is the number of minutes in a whole day.
bar_idx_pm = bar_index - valuewhen(t, bar_index, 1)

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 >= 1600) or (hour == ending_hour and minute < ending_minute))
    pm_high := high
    
if low < pm_low and ((hour < ending_hour or hour >= 1600) or (hour == ending_hour and minute < ending_minute))
    pm_low := low

// PREMARKET END


is_new_day = change(time("1D"))
bar_idx_prev_day = bar_index - valuewhen(is_new_day, bar_index, 1)
bgcolor(is_new_day ? color.blue : na)

var line l_pdh = na, var line l_pdc = na, var line l_pdl = na, var line l_pmh = na, var line l_pml = na

// if barstate.islast
l_pdh := line.new(bar_index-bar_idx_prev_day, PDH, bar_index, PDH, extend=extend.right, color=color.green)
l_pdc := line.new(bar_index-bar_idx_prev_day, PDC, bar_index, PDC, extend=extend.right, color=color.purple)
l_pdl := line.new(bar_index-bar_idx_prev_day, PDL, bar_index, PDL, extend=extend.right, color=color.red)

l_pmh := line.new(bar_index-bar_idx_pm, pm_high, bar_index, pm_high, extend=extend.right, color=color.blue)
l_pml := line.new(bar_index-bar_idx_pm, pm_low, bar_index, pm_low, extend=extend.right, color=color.orange)

line.delete(l_pdh[1])
line.delete(l_pdc[1])
line.delete(l_pdl[1])
line.delete(l_pmh[1])
line.delete(l_pml[1])

这可行,但我想移动 PreMarket 水平射线以从第一个 PreMarket 蜡烛开始。目前是盘前的最后一根柱线

您应该使用 History reference operatorsecurity() 函数一起获取前一天的数据。

然后算出前一天第一根柱的柱指数。

//@version=4
study("PRIOR DAY LEVELS", overlay=true)

PDH = security(syminfo.tickerid,"D",high[1])
PDC = security(syminfo.tickerid,"D",close[1])
PDL = security(syminfo.tickerid,"D",low[1])

is_new_day = change(time("1D"))
bar_idx_prev_day = bar_index - valuewhen(is_new_day, bar_index, 1)
bgcolor(is_new_day ? color.blue : na)

var line l_pdh = na, var line l_pdc = na, var line l_pdl = na

// if barstate.islast
l_pdh := line.new(bar_index-bar_idx_prev_day, PDH, bar_index, PDH, extend=extend.right, color=color.green)
l_pdc := line.new(bar_index-bar_idx_prev_day, PDC, bar_index, PDC, extend=extend.right, color=color.purple)
l_pdl := line.new(bar_index-bar_idx_prev_day, PDL, bar_index, PDL, extend=extend.right, color=color.red)

line.delete(l_pdh[1])
line.delete(l_pdc[1])
line.delete(l_pdl[1])