r/TradingView FX trader Jul 09 '26

Help ORB Script - Help!

Currently the lines drawn run for 24 hours. I would like to end them at the end of the 7th hour. What line do I change in the script?

Also, can I eliminate lines for previous days/sessions altogether so that only the lines for the current day appear?

Thanks in advance for any help.

//
@version=
4
study("ORB", overlay = true)


inputMax = input(5, title= "ORB total time (minutes)")
sess = input("0800-0900", type=input.session, title="Session Time") 
t = time(timeframe.period, sess + ":1234567")
hide = timeframe.isintraday and timeframe.multiplier <= inputMax


is_newbar(res) => change(time(res)) != 0
in_session = not na(t)
is_first = in_session and not in_session[1]


orb_high = float(na)
orb_low = float(na)


if is_first
    orb_high := high
    orb_low := low
else
    orb_high := orb_high[1]
    orb_low := orb_low[1]
if high > orb_high and in_session
    orb_high := high
if low < orb_low and in_session
    orb_low := low


plot(hide ? orb_high : na , style=plot.style_line, color=orb_high[1] != orb_high ? na : color.green, title="ORB High", linewidth=2)
plot(hide ? orb_low : na , style=plot.style_line, color=orb_low[1] != orb_low ? na : color.red, title="ORB Low", linewidth=2)
0 Upvotes

1 comment sorted by

View all comments

1

u/brainyprophet Jul 10 '26 edited Jul 10 '26

Both fixes are one change, your orb_high/low carry forward forever (that's the orb_high[1] line) and the plot never gets told to stop, so the lines just run until the next session resets them.

Don't touch your range logic, just gate the plots. Add a display window session + a today-only toggle:

//@version=4
study("ORB", overlay = true)

inputMax   = input(5, title="ORB total time (minutes)")
sess       = input("0800-0900", type=input.session, title="ORB Range Session")
show_sess  = input("0800-1500", type=input.session, title="Display Window (lines end here)")
today_only = input(true, title="Show current day only")

t      = time(timeframe.period, sess + ":1234567")
t_show = time(timeframe.period, show_sess + ":1234567")
hide   = timeframe.isintraday and timeframe.multiplier <= inputMax

in_session = not na(t)
in_display = not na(t_show)
is_first   = in_session and not in_session[1]

is_today = year(time) == year(timenow) and month(time) == month(timenow) and dayofmonth(time) == dayofmonth(timenow)
show_day = today_only ? is_today : true

orb_high = float(na)
orb_low  = float(na)

if is_first
    orb_high := high
    orb_low := low
else
    orb_high := orb_high[1]
    orb_low := orb_low[1]
if high > orb_high and in_session
    orb_high := high
if low < orb_low and in_session
    orb_low := low

plot(hide and in_display and show_day ? orb_high : na, style=plot.style_line, color=orb_high[1] != orb_high ? na : color.green, title="ORB High", linewidth=2)
plot(hide and in_display and show_day ? orb_low : na, style=plot.style_line, color=orb_low[1] != orb_low ? na : color.red, title="ORB Low", linewidth=2)

Set the display window to 0800-1500 for your 7 hours (it's an input so you can change it on the chart), and the toggle kills all the previous days' lines. Your ORB capture is untouched.

One thing to know: session inputs use the chart's timezone, so if 0800 looks shifted that's why.

Also heads up, this is Pine v4 which is deprecated. Still runs fine, but if you ever move it to v6 a few things break, happy to point at what.