Skip to content

event ​

Handlers run when something happens: a new block, a closed bar, a fill. Register them at the top level.

NameDescription
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): void

Runs once, before the first bar.

event.onStop() ​

ts
event.onStop(fn: () => void): void

Runs once, after the last bar of the run.

event.onBlock() ​

ts
event.onBlock(fn: (block: Block) => void): void

Runs 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): void

Runs 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): void

Runs 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): void

Runs 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): void

Runs when one of the script's intents fills, fully or partly.

Types: Fill

event.onReject() ​

ts
event.onReject(fn: (rejection: Rejection) => void): void

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.

Types: Rejection

event.onExpire() ​

ts
event.onExpire(fn: (intent: Intent) => void): void

Runs when an intent waited longer than its ttlBlocks without a fill.

Types: Intent

event.onPositionOpened() ​

ts
event.onPositionOpened(fn: (position: Position) => void): void

Runs when a buy opens a position from flat.

Types: Position

event.onPositionClosed() ​

ts
event.onPositionClosed(fn: (closed: { position: Position; realizedTao: number }) => void): void

Runs 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): void

Runs 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.

FieldTypeDescription
timeMsbar open time
openPrice
highPrice
lowPrice
closePrice
volumenumberalpha traded through the pool in this bar
turnovernumberTAO traded through the pool in this bar
tradesnumbernumber of pool trades in this bar
firstBlockBlockNumberfirst and last block in this bar
lastBlockBlockNumber
confirmedbooleanconfirmed: the bar has closed. final: its last block is finalized and can no longer be reorganized.
finalboolean

Block ​

One block of the subnet's pool, as passed to event.onBlock.

FieldTypeDescription
numberBlockNumber
timeMs
pricePricepool price after the block, TAO per alpha
taoInnumberpool reserves after the block: TAO in the pool, alpha in the pool, alpha outstanding (staked)
alphaInnumber
alphaOutnumber
movingPricePricethe protocol's moving (smoothed) price
tradesnumberpool trades in this block (bookkeeping rows excluded)
volumenumberalpha / TAO traded through the pool in this block
turnovernumber
finalbooleanthe 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.

FieldTypeDescription
idstring
timeMs
blockBlockNumber
side"Buy" | "Sell"Buy: TAO in, alpha out. Sell: alpha in, TAO out.
pricePrice
alphanumber
taonumber
feenumberfee paid, in TAO

AlphaScript language v0.1 · runtime 0.2.0