Appearance
event
Handlers run when something happens: a new block, a closed bar, a fill. Register them at the top level.
| Name | Description |
|---|---|
event.onStart() | Runs once, before the first bar. |
event.onStop() | Runs once, after the last bar of the run. |
event.onBlock() | Runs on every block in precise, 1B and live runs. |
event.onTrade() | Runs for every pool trade by anyone on the chain, after the block's onBlock, when trade data is loaded. |
event.onBarUpdate() | Runs on every update of the open bar. |
event.onCandle() | Runs once per bar when it closes (or, with actOn: "final", when it is finalized). |
event.onFill() | Runs when one of the script's intents fills, fully or partly. |
event.onReject() | Runs when an intent is rejected: by a risk rule when it is created, or by its price limit, the cash or the pool when it would fill. |
event.onExpire() | Runs when an intent waited longer than its ttlBlocks without a fill. |
event.onPositionOpened() | Runs when a buy opens a position from flat. |
event.onPositionClosed() | Runs when a sell takes the position back to flat, with the round trip's realized profit in TAO. |
event.onReorg() | Runs when the chain replaced blocks the script had already seen; the runtime has rewound its state to toBlock. |
event.onStart()
ts
event.onStart(fn: () => void): voidRuns once, before the first bar.
event.onStop()
ts
event.onStop(fn: () => void): voidRuns once, after the last bar of the run.
event.onBlock()
ts
event.onBlock(fn: (block: Block) => void): voidRuns on every block in precise, 1B and live runs. Quick backtests process whole bars and never call it.
Types: Block
event.onTrade()
ts
event.onTrade(fn: (trade: PoolTrade) => void): voidRuns for every pool trade by anyone on the chain, after the block's onBlock, when trade data is loaded.
Types: PoolTrade
event.onBarUpdate()
ts
event.onBarUpdate(fn: (bar: Bar) => void): voidRuns on every update of the open bar. Only called when the strategy sets calcOnEveryBlock: true.
Types: Bar
event.onCandle()
ts
event.onCandle(fn: (bar: Bar) => void): voidRuns once per bar when it closes (or, with actOn: "final", when it is finalized). The usual place to trade.
Types: Bar
event.onFill()
ts
event.onFill(fn: (fill: Fill) => void): voidRuns when one of the script's intents fills, fully or partly.
Types: Fill
event.onReject()
ts
event.onReject(fn: (rejection: Rejection) => void): voidRuns when an intent is rejected: by a risk rule when it is created, or by its price limit, the cash or the pool when it would fill.
Types: Rejection
event.onExpire()
ts
event.onExpire(fn: (intent: Intent) => void): voidRuns when an intent waited longer than its ttlBlocks without a fill.
Types: Intent
event.onPositionOpened()
ts
event.onPositionOpened(fn: (position: Position) => void): voidRuns when a buy opens a position from flat.
Types: Position
event.onPositionClosed()
ts
event.onPositionClosed(fn: (closed: { position: Position; realizedTao: number }) => void): voidRuns when a sell takes the position back to flat, with the round trip's realized profit in TAO.
Types: Position
event.onReorg()
ts
event.onReorg(fn: (reorg: { fromBlock: BlockNumber; toBlock: BlockNumber }) => void): voidRuns when the chain replaced blocks the script had already seen; the runtime has rewound its state to toBlock.
Types: BlockNumber
Types
Bar
One bar (candle), as passed to event.onCandle and event.onBarUpdate.
| Field | Type | Description |
|---|---|---|
time | Ms | bar open time |
open | Price | |
high | Price | |
low | Price | |
close | Price | |
volume | number | alpha traded through the pool in this bar |
turnover | number | TAO traded through the pool in this bar |
trades | number | number of pool trades in this bar |
firstBlock | BlockNumber | first and last block in this bar |
lastBlock | BlockNumber | |
confirmed | boolean | confirmed: the bar has closed. final: its last block is finalized and can no longer be reorganized. |
final | boolean |
Block
One block of the subnet's pool, as passed to event.onBlock.
| Field | Type | Description |
|---|---|---|
number | BlockNumber | |
time | Ms | |
price | Price | pool price after the block, TAO per alpha |
taoIn | number | pool reserves after the block: TAO in the pool, alpha in the pool, alpha outstanding (staked) |
alphaIn | number | |
alphaOut | number | |
movingPrice | Price | the protocol's moving (smoothed) price |
trades | number | pool trades in this block (bookkeeping rows excluded) |
volume | number | alpha / TAO traded through the pool in this block |
turnover | number | |
final | boolean | the block is finalized and can no longer be reorganized |
PoolTrade
One trade through the pool by anyone on the chain, as passed to event.onTrade.
| Field | Type | Description |
|---|---|---|
id | string | |
time | Ms | |
block | BlockNumber | |
side | "Buy" | "Sell" | Buy: TAO in, alpha out. Sell: alpha in, TAO out. |
price | Price | |
alpha | number | |
tao | number | |
fee | number | fee paid, in TAO |
