Lifecycle
How a script starts, replays history, goes live and restarts.
Every start runs the steps below: handlers see all history first, then live data.
Start
- Inputs. Saved values are checked against their inputs. An invalid value falls back to its default and shows as an error.
subscribeandstaterun.state()returns a fresh state object.- Symbols (
ctx.market.symbol()) resolve first. No handler and nodrawruns before, soctx.streams.<name>.infois always set. - History. All other streams load their history in parallel. The runtime waits for all of them, then replays them merged into one timeline through the handlers.
ctx.phaseis"History". - Live. Events that arrived during the replay follow in arrival order, then live data as it comes.
ctx.phaseis"Live"from here.
History and live data go through the same handler, so the same code builds state from both. Check ctx.phase for things that should only happen live, e.g. ctx.alert.
Replay order
Events replay by the time they became known:
- a bar at its close time (open time plus its timeframe),
- a trade, book snapshot or signal at its own time.
A 4h bar replays after the 1m bars inside it, so a handler never sees the future. Equal times replay in subscription order.
Draw
draw runs inside the chart's frame whenever the chart redraws: after handlers changed state, on pan, zoom and resize. See State and draw.
Restart
These restart the script: streams stop, state is dropped, and it starts again from step 1.
- an input change,
- a symbol or timeframe change of the chart,
- saving the script (hot reload),
- showing a hidden script.
Nothing survives a restart. Hiding a script stops its streams and keeps its pane.
Remove
Streams stop, drawings, widgets and the script's pane disappear.
Errors
The runtime catches everything a script throws. A throw in a handler, draw, an event handler, widgets or legend skips that one call and the script keeps running. A throw in subscribe or state stops the script until the next restart. Errors show in the indicator list and in the editor's Problems panel with their line.