> ## Documentation Index
> Fetch the complete documentation index at: https://gocharting.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to plot only during part of the session

> Leaving a gap outside a time window by plotting na, testing the window with time() and a session string or with hour() in a named zone.

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](https://gocharting.com/docs/scripting/market-data/sessions-and-intervals.md)).

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Close during the morning", overlay=true)
inWindow = not na(time(interval.interval, "0900-1200"))
plot(inWindow ? close : na, title="Morning close")
```

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:

```lipi theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Chosen session", overlay=true)
sess = input.session("0930-1600", "Session")
inSession = not na(time(interval.interval, sess))
bgcolor(inSession ? color.new(color.blue, 0.9) : na, title="Session")
```

## 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.

```lipi wrap theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
//@lipi=2
indicator("Morning hours in the exchange zone", overlay=true)
h = hour(time, syminfo.timezone)
plot(h >= 9 and h < 12 ? close : na, title="Morning close")
plotshape(h >= 9 and h < 12, title="Morning", shape=shape.circle, location=location.belowbar, color=color.blue, size=size.tiny)
```

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

> [!WARNING]
>
> Making the call itself conditional. The compiler says: `plot()` must be called in the global scope.
>
> ```lipi-invalid theme={"languages":{"custom":["/languages/lipi.json","/languages/lipi-invalid.json"]}}
> //@lipi=2
> indicator("Plot in a branch", overlay=true)
> if hour >= 9 and hour < 12 {
>     plot(close)
> }
> ```

The editor's check reports it. See [Sessions and intervals](https://gocharting.com/docs/scripting/market-data/sessions-and-intervals.md) and [Plotting](https://gocharting.com/docs/scripting/chart-output/plotting.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.