I am trying to build out an indicator that uses previous sessions to give levels. But some tickers start the new day at "0000" and some start at "2100" or "2200" and I need to figure out the best way to identify this so my script can calculate when to display the new sessions levels at the correct time. Currently I'm using if statements to check if it's forex, futures or indexes, but this does not work on all pairs and may cause issues with some tickers of that type that don't start at the different hours. For example, xauusd does not fall into these types, but the session starts at 2200 UTC instead of 0000. My code will use the change in session to plot the new levels, so what is the best way to identify this change in session when creating lines on various different timeframes all within the same script?
I also need to make sure it is using the correct timezone of GMT+0.
The code below works, but doesn't capture every ticker that has a different session start time.
fourHourTime = time("240", "0000-0000", "GMT+0")
if syminfo.type == "futures" or syminfo.type == "forex" or syminfo.type == "index"
fourHourTime := time("240", "2100-2100", "GMT+0")
dailyTime = time("1D", "0000-0000", "GMT+0")
if syminfo.type == "futures" or syminfo.type == "forex" or syminfo.type == "index"
dailyTime := time("1D", "2100-2100", "GMT+0")
if dailyTime != dailyTime[1]
line.new()
`
I would use the ta.change
function. In the code below it will highlight every first bar of a new day on an intraday chart. You can switch from stocks, forex, crypto and note the bar time, it will adjust automatically
//@version=5
indicator("New day", overlay = true)
newDay = ta.change(time('D'))
bgcolor(newDay ? color.yellow : na)