Search code examples
pine-scriptpine-script-v4

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


I'm really new to pine-script and I'm trying to make a script to draw a horizontal ray line that starts at the first previous day candle to show Previous Day High, Low and Close. I found a similar script for can't get it to work.

//@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])

With this script the horizontal ray-line line starts 1 prior candle. I think I have to use xloc.bar_time instead of bar_index but cannot figuere it out.

As always any help would be greatly appreciated.

EDIT

The answer provided by @vitruvius works well. I'm now trying to add to more lines for PreMarket High/Low.

My new code looks like:

//@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])

This works but I would like to move the PreMarket Horizontal Ray to start at the First PreMarket candle. Currently is at last bar of the pre-market


Solution

  • You should use the History reference operator to get the data from the previous day together with the security() function.

    Then figure out the bar index of the first bar of the previous day.

    //@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])
    

    enter image description here