Skip to content

Events and execution order ​

Handlers are functions the runtime calls when something happens. Register each one once, at the top level.

HandlerRunsTypical use
onStart()once, before the first barset up state
onCandle(bar)once per bar, when it closes (or is finalized with actOn: "final")decide and trade
onBarUpdate(bar)on every update of the open bar, only with calcOnEveryBlock: truereact inside the bar
onBlock(block)on every block, when the run has block datastops on the block price, pool watching
onTrade(trade)for every pool trade by anyone, after the block's onBlockfollow large trades
onFill(fill)when one of your intents fills, fully or partlyplace stops, log
onReject(rejection)when an intent is rejectedlog, retry smaller
onExpire(intent)when an intent waited past its ttlBlocksretry
onPositionOpened(position)when a buy opens a position from flatplace a stop
onPositionClosed({ position, realizedTao })when a sell takes the position back to flatrecord the result
onReorg({ fromBlock, toBlock })when the chain replaced blocks you had seenreset your own state
onStop()once, after the last barfinal log

Which handlers run where ​

What a run can deliver depends on its data:

Quick backtestPrecise backtest1B backtestLive stream
onCandleevery barevery barevery block (each block is a bar)every bar
onBlockneverevery blockevery blockevery block
onTradeneverevery pool tradeevery pool tradeevery pool trade
onBarUpdateonce per barevery blockevery blockevery block

A quick backtest processes whole bars and has no blocks, so onBlock and onTrade never run there. Logic that must run on every block (a stop on the block price, say) needs a precise or 1B backtest to be tested, or an engine-managed conditional order, which works in every mode.

Trade data is loaded only for scripts that register onTrade, so leave it out if you do not need it.

Order within one block ​

When a block arrives the runtime does, in this order:

  1. If the block is the first of a new bar, the previous bar is confirmed and onCandle(bar) runs for it.
  2. Advance the block series and apply the block's pool state; fold the block into the open bar and recompute the bar series. Handlers now see candle.* up to this block.
  3. onBlock(block).
  4. onTrade(trade) for each pool trade in the block, in chain order.
  5. onBarUpdate(bar), if calcOnEveryBlock is on.
  6. Settlement: intents that are due fill, are rejected or expire; onFill, onReject, onExpire, onPositionOpened and onPositionClosed run.

An intent created in any handler is queued. It fills no earlier than fill.latencyBlocks blocks later (default 2), never in the block that created it. See Trading on the pool.

Errors stop the run ​

If a handler throws, the run stops there: the journal records run.error with the handler's name and the message, and no further handler runs. Guard against missing values (NaN, an empty position) instead of letting the handler throw.

Reorgs ​

Blocks that are not final yet can be replaced by the chain. When that happens on the live stream the runtime rewinds everything it owns (series, the open bar, the account, open intents) to the last common block, marks the journal events after it as superseded, calls onReorg({ fromBlock, toBlock }) and then processes the replacement blocks.

A script whose state lives only in series needs to do nothing. A script that keeps its own variables should forget what it learned above toBlock. Backtests run on final blocks and never see a reorg.

AlphaScript language v0.1 · runtime 0.2.0