How to plot only during part of the session
You cannot move the plot call inside a branch: every chart-output function
must be called at the top level. Vary the value instead. An na in a plotted
series leaves a gap on that bar rather than drawing a zero, so plot na
outside the window.
indicator("Close during the morning", "am", true)
bool in_window = hour >= 9 and hour < 12
plot(in_window ? close : na, title = "Morning close", color = color.blue)hour and minute are series int values read off the bar under evaluation.
Build the test from those.
If the window needs the instrument’s timezone
The bare hour variable reads the current bar; this edition does not state
which timezone it is expressed in. Where that matters, call the function of the
same name, which takes a time and a timezone and requires both.
hour(series int time, series string timezone) → series inthour(time, syminfo.timezone)The instrument’s own timezone is syminfo.timezone, a simple string.
If you would rather mark the window than hide the plot
Keep the plot continuous and put the condition into a marker. plotshape takes
the condition as its first argument, so the marker appears on the bars in the
window and nowhere else.
indicator("Morning bars", "ammark", true)
plot(close, title = "Close", color = color.gray)
plotshape(hour >= 9 and hour < 12, title = "Morning", shape = shape.circle, location = location.belowbar, color = color.blue, size = size.tiny)The failure to watch for
Writing the window test as a branch around the plot call is rejected with an
error saying the call must be made in the global scope. The condition belongs
in an argument. See Plotting.