There are multiple ways of doing it, one of them is to create a session from time x to time y and get the start and the end of it.
I added comments for each section to see how it's working.
//@version=4
study("My Script", overlay = true)
//Sessions
session = input(title="Session", type=input.session, defval="0930-1555")
//Check if it's new bars
is_newbar2(sess) =>
t = time("D", sess)
na(t[1]) and not na(t) or t[1] < t
//Check if it's in session
is_session(sess) =>
not na(time(timeframe.period, sess))
//Call the function
Session = is_session(session)
//Plot the background color to see the session
bgcolor(Session ? color.new(color.aqua, 95) : na)
//Start and end of the session
start = Session and not Session[1]
end = (not Session) and Session[1]
//Plot the start and the end of the session
plotshape(start, style=shape.labeldown, color = color.aqua, text = "Start", textcolor = color.black, size = size.small)
plotshape(end, style=shape.labeldown, color = color.purple, text = "End", textcolor = color.black, size = size.small)
//Alerts
if start
alert("text alert", alert.freq_once_per_bar)
if end
alert("text alert", alert.freq_once_per_bar_close)
It's very important to write the starting of the session (if it start at 7am, then write exactly 07:00) and the time from the last candle of the session (if you are in 5m chart and the session expire at 2pm, write 13:55, if you are in 1H chart write 13:00...and so on), that's because after the end of the session, Tradingview will block any functions...since the market is closed, so we need to get the last bar of the session.
For the alerts, you just put your text and it's ready to go.
