Search code examples
pine-scriptpine-script-v5

Get high and low of 200 sma drawn on the next day


updated...

I want static H/L lines from the previous days 200 ma on the next day. Right now the interactive date doesn't work as expected and the lines are from the last day only. I'd like to check other days of the month. Needs to work in extended hours.

This is my code

//@version=5
indicator('200ma hi/low', '', true)

in_date = input.time(timestamp("01 Mar 2024 GMT-6"), "Date", confirm = true)
sma200 = ta.sma(close, 200)
srcHi = ta.highest(sma200,1)
srcLo = ta.lowest(sma200,1)
timeAllowed = input.session('0300-1959', 'Allowed hours', confirm=true)


timeIsAllowed = time(timeframe.period, timeAllowed + ':23456')

var hi = 10e-10
var lo = 10e10

if timeIsAllowed and in_date
    
    if not timeIsAllowed[1]
        hi := srcHi
        lo := srcLo
        
    else
        
        hi := math.max(srcHi, hi)
        lo := math.min(srcLo, lo)
        

line.new(bar_index, hi, bar_index + 1, hi, extend=extend.right, color=color.rgb(218, 15, 15), width=1)
line.new(bar_index, lo, bar_index + 1, lo, extend=extend.right, color=color.rgb(218, 15, 15), width=1)

Solution

  • Solved it by using

    timeIsAllowed = not na(time(timeframe.period, dRange)) newSession = timeIsAllowed and not timeIsAllowed[1]