Static_getContext-free read of the current state value for a signal. Routes to StateBacktest or StateLive based on dto.backtest.
Flag indicating if the context is backtest or live
State name
Default value when no persisted state exists
Signal identifier
Logical timestamp at which the read is happening (look-ahead guard)
Current state value
Static_setContext-free update of the state value for a signal. Routes to StateBacktest or StateLive based on dto.backtest.
New value or updater function receiving current value
Flag indicating if the context is backtest or live
State name
Default value when no persisted state exists
Signal identifier
Logical timestamp this value belongs to
Updated state value
StaticdisableDisables state storage by unsubscribing from signal lifecycle events. Safe to call multiple times.
StaticenableEnables state storage by subscribing to signal lifecycle events. Clears memoized instances in StateBacktest and StateLive when a signal is cancelled or closed, preventing stale instances from accumulating. Uses singleshot to ensure one-time subscription.
Read the current state value for the active pending or scheduled signal. Resolves the signal, mode and timestamp from execution context — no context arguments required.
Update the state value for the active pending or scheduled signal. Resolves the signal, mode and timestamp from execution context — no context arguments required.
Per-signal mutable state scoped by state name.
Works like a value bound to the CURRENT pending or scheduled signal:
new State({ name: "trade", initialData: { peakPercent: 0 } }).setState(...)inside any strategy lifecycle callback. No context is passed through arguments — every instance method resolves the signal, mode and timestamp itself frombacktest.methodContextService/backtest.executionContextService, so the class is unavailable outside async_hooks lifecycle callbacks by design.initialDataprovides the default value when no state exists yet — a plain object or a sync/async factory returning one. The factory receives an InitialDispatchContract payload with the resolved signal context (signal row, active/schedule type, currentPrice, mode, logical time), so the initial state can be derived from the actual entry; it yields a fresh object per access, so the default is never shared by reference.Look-ahead bias protection: a read at a
whenearlier than the storedwhenyieldsinitialData, and a write with a smallerwhenoverwrites (a restarted backtest resets live-written state).Requires an explicit
State.enable()call before use — the subscription it creates disposes per-signal instances when the signal is cancelled or closed, preventing stale instances from accumulating.Example