Skip to main content
You cannot put plot inside an if: output calls must be at the top level. Vary the value instead. A plotted na leaves a gap on that bar, so plot na outside the window. time(interval, session) returns na for bars outside the session string, which makes it the test for a trading window. The session string is "HHMM-HHMM"; ranges may be joined with commas and followed by days (see Sessions and intervals).
To read the session in a particular zone, add the zone as the third argument: time(interval.interval, "0930-1600", "America/New_York").

Let the chart user choose the window

input.session returns the session string the reader typed, ready for time(). bgcolor with na outside the window marks it:

Test the hour yourself

hour(time, timezone) returns the hour of a time in a zone you name, so the same script can ask the question in two zones. syminfo.timezone is the instrument’s own zone. The bare variable hour is the hour of the bar’s own timestamp and takes no zone, so use the function when the zone matters.
To mark the window instead of hiding the rest, plot close unconditionally and let plotshape mark the bars: it takes the condition as its first argument.

The mistake to avoid

Making the call itself conditional. The compiler says: plot() must be called in the global scope.
The editor’s check reports it. See Sessions and intervals and Plotting.