0

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!

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

offset is indeed the solution. Thank you very much! I was able to dynamically calculate the needed offset depending on the displayed resolution by offset = 1170 / timeframe.multiplier. The 1170 are the number of minutes in my securities (extended) trading session (starts at 2:30, ends at 22:00). The 1170 offset doesn't work on securities that are traded 24 hours though... Any idea on how to also dynamically calculate the number of minutes in a trading session of the currently viewed security?
1

It's not possible as of PineScript v5 to plot/draw in future bars but you can use offset argument as bbnm already answered.

However I just wanted to add that you can calculate exactly how many bars you need to offset dynamically for any timeframe, making your life much easier when switching between timeframes.

You can use timeframe.in_seconds to calculate how many seconds are in a day and how many seconds are in the current timeframe, and then divide them to get exactly how many bars there are in a single day on the current timeframe.

tfInSeconds = timeframe.in_seconds(timeframe.period) dayInSeconds = timeframe.in_seconds("1D") offset = tfInSeconds < dayInSeconds ? dayInSeconds / tfInSeconds : 0 bgcolor(time("", "0200-0800", "GMT+2") ? color.new(color.orange , 92) : na, offset=offset) 

Only issue I noticed is that the calculation isn't precise if the asset has some missing bars either be it because of the data source not providing all price data for the trading-day for some reason, or because the asset doesn't trade 24h (like SPX500 EIGHTCAP for instance, that's missing price data between 5 & 6pm NY time). But for all other more usual cases it works fine.

I worked on a simple sessions indicator script that does pretty much the same thing and handles the issue I mentioned with an additional input variable when those cases are detected. In case it's useful for anyone reading: https://www.tradingview.com/script/7xNm43Sm-Simple-Sessions/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.