pine-script

Pine Script - bgcolor() one day into the future


I want to color the background of certain timeframes (pre-trading, pre-market, after-hours) during the day. The code below works, except I also want the background to be colored one day into the future, so I can visually grasp in advance when the price graph is approaching the end of the trading session and the beginning of after-hours.

//@version=4
study(title="Trading Hours", overlay=true)

bgcolor(timeframe.isintraday and time("", "0200-0800", "GMT+2") ? color.new(color.orange , 92) : na)
bgcolor(timeframe.isintraday and time("", "0800-0900", "GMT+2") ? color.new(color.blue, 95) : na)
bgcolor(timeframe.isintraday and time("", "1730-2200", "GMT+2") ? color.new(color.blue, 95) : na)

New to Pine Script, any help is appreciated. Thanks in advance!


Solution

  • I don't think it's possible to color future bars. You can however use the offset parameter, but you'll have to calculate the equivalent of 24 hours for each timeframe and manually input it in the parameter since it only accepts constant values.

    //@version=4
    study(title="Trading Hours", overlay=true)
    
    bgcolor(timeframe.isintraday and time("", "0200-0800", "GMT+2") ? color.new(color.orange , 92) : na, offset=48)
    bgcolor(timeframe.isintraday and time("", "0800-0900", "GMT+2") ? color.new(color.blue, 95) : na, offset=48)
    bgcolor(timeframe.isintraday and time("", "1730-2200", "GMT+2") ? color.new(color.blue, 95) : na, offset=48)
    

    The script uses 48 as the offset, it adjusts colors by 48 hours into the future since I'm currently on the 1 hour chart. If you're on the 5 min chart, offsetting it by 48 will adjust the colors by 240 mins since 48*5mins = 240 mins.

    enter image description here

    enter image description here