The code I have written plots a circle with offset to attempt to place the circle at the start of the next session as soon as the previous session has expired. This is not always accurate, eg. if the previous session is shortened - for example on Monday (20 Feb '23).
Attached is the code I generated which mostly gives the right result but using faulty logic as per the screenshot. Is there a more elegant solution to look forward and paint the dot (or preferably a line segment as per attached dark screenshot) on (from) the first bar of the next session (ie. looking forward in time)?
//Shows prev session high as green dot at session start
//Modify to paint dot at isFirstBar
//@version=5
indicator(title="Prev session high", overlay=true)
// SessionHigh() returns the highest price during the specified
// session, optionally corrected for the given time zone.
// Returns 'na' when the session hasn't started or isn't on the chart.
SessionHigh(sessionTime, sessionTimeZone=syminfo.timezone) =>
var bool insideSession = na
insideSession := not na(time(timeframe.period, sessionTime, sessionTimeZone))
var float sessionHighPrice = na
if insideSession and not insideSession[1]
sessionHighPrice := high
else if insideSession
sessionHighPrice := math.max(sessionHighPrice, high)
sessionHighPrice
// InSession() returns 'true' when the current bar happens inside
// the specified session, corrected for the given time zone (optional).
// Returns 'false' when the bar doesn't happen in that time period,
// or when the chart's time frame is 1 day or higher.
InSession(sessionTimes, sessionTimeZone=syminfo.timezone) =>
not na(time(timeframe.period, sessionTimes, sessionTimeZone))
// Configure session with inputs
session = input.session("0930-1600", title="Trading Session")
timeZone = input.string("America/New_York", title="Time Zone")
IsLastBarSession(sessionTime, sessionTimeZone=syminfo.timezone) =>
var int lastBarHour = na
var int lastBarMinute = na
var int lastBarSecond = na
inSess = not na(time(timeframe.period, sessionTime, sessionTimeZone))
not inSess and inSess[1]
// Get the close value of the last bar of the session
lastBarClose = IsLastBarSession("0930-1600", "America/New_York") ? close[1] : na
// Determine if the current bar is the first bar after a IsLastBarSession
barssinceLastSession = ta.barssince(IsLastBarSession("0930-1600", "America/New_York"))
firstBarAfterLastSession = IsLastBarSession("0930-1600", "America/New_York") ? 0 : barssinceLastSession == 1
// Get the session high
sessHigh = SessionHigh("0930-1600", "America/New_York")
// Plot the session high only for the IsLastBarSession bar
plot(firstBarAfterLastSession ? sessHigh : na, color=color.green, title="Session High", linewidth = 2, style = plot.style_circles, offset=65)
//plot(lastBarClose, "Last Session Bar Close", color = color.red, linewidth = 2, style = plot.style_circles, offset=66)