Trading sessions
Shade session hours with a background plot and box the developing session range with entities.
This example works with clock time instead of bar indices: it declares two UTC sessions, tints the background while New York is open, and draws a live box around the developing London range.

script "Trading Sessions"
data chart = subscribe(data.ohlcv)
input (
londonColor = input.color(color.blue, title: "London")
nyColor = input.color(color.orange, title: "New York")
)
state (
london = time.session("08:00", "16:00", name: "London")
newYork = time.session("13:30", "20:00", name: "New York")
boxes = entities.boxPool(max: 8)
inLondon: bool = false
slot: int = 0
sessionStart: time? = null
sessionHigh: float = 0.0
sessionLow: float = 0.0
)
plot nyBg = plot.bg(title: "New York Hours")
on chart.update {
let now = (chart.time ?? time.fromUnixSeconds(0))
let barEnd = now + time.bars(1)
// Tint the background while New York is open. Not calling plot leaves
// the bar untinted.
if newYork.contains(now) {
nyBg.plot(color: color.withAlpha(nyColor, 22))
}
// Track the developing London range and draw it as a box.
if london.contains(now) {
if !inLondon {
inLondon = true
slot = slot >= 7 ? 0 : slot + 1
sessionStart = (london.windowStart(now) ?? now)
sessionHigh = (chart.high ?? 0.0)
sessionLow = (chart.low ?? 0.0)
} else {
sessionHigh = math.max(sessionHigh, (chart.high ?? 0.0))
sessionLow = math.min(sessionLow, (chart.low ?? 0.0))
}
boxes.get("london", slot).set(
x1: (sessionStart ?? now),
y1: sessionHigh,
x2: barEnd,
y2: sessionLow,
color: color.withAlpha(londonColor, 28),
borderColor: londonColor,
borderWidth: 1.0,
text: "London",
textColor: color.white,
textSize: 11.0,
)
} else {
inLondon = false
}
}Sessions are time windows, not bar counts
time.session defines a repeating UTC window that works on any chart timeframe. contains answers whether the current bar falls inside it, and windowStart recovers the session's opening timestamp so the box can anchor to the true session start even if the first in-session bar arrives late.
One box per session instance
Each London session uses a numbered slot in the box pool. The pool holds eight boxes. When the ninth session starts, it reuses the oldest box.
Update the same box
On each bar during the session, set updates the same box with the latest high, low, and right edge. Supplying all its properties keeps the update simple.