Skip to content

trade ​

Trades are intents: the script asks, the engine fills at a later block against the pool reserves, or rejects.

NameDescription
trade.buy()Asks to buy alpha with TAO.
trade.sell()Asks to sell alpha from the position.
trade.closePosition()Sells the whole position.
trade.cancel()Cancels an intent that has not filled yet.
trade.cancelAll()Cancels every intent that has not filled yet; returns how many.
trade.stop()Engine-managed: sell when price <= level.
trade.takeProfit()Engine-managed: sell when price >= level.
trade.trailingStop()Engine-managed: sell when price falls pct percent below the running high since creation.
trade.cancelConditional()Cancels an active stop, take-profit or trailing stop.

trade.buy() ​

ts
trade.buy(options: BuyOptions): IntentId

Asks to buy alpha with TAO. Fills at a later block against the pool, or is rejected; see event.onFill and event.onReject. Returns null before the tester's trading start.

Types: BuyOptions, IntentId

trade.sell() ​

ts
trade.sell(options: SellOptions): IntentId

Asks to sell alpha from the position. Long only: without a position the intent is rejected with "no-position". Returns null before the tester's trading start.

Types: SellOptions, IntentId

trade.closePosition() ​

ts
trade.closePosition(options?: { reason?: string }): IntentId | null

Sells the whole position. Returns null when there is no position.

Types: IntentId

trade.cancel() ​

ts
trade.cancel(id: IntentId): boolean

Cancels an intent that has not filled yet. Returns false when it already filled, expired or was rejected.

Types: IntentId

trade.cancelAll() ​

ts
trade.cancelAll(): number

Cancels every intent that has not filled yet; returns how many.

trade.stop() ​

ts
trade.stop(options: ConditionalOptions & { price: Price }): string

Engine-managed: sell when price <= level. Evaluated every block (precise/live) or at bar open/low/high/close (quick). Fills after the normal latency, never at the trigger price. Place it once the position exists (e.g. in event.onPositionOpened): an order checked while flat is cancelled.

Types: ConditionalOptions, Price

trade.takeProfit() ​

ts
trade.takeProfit(options: ConditionalOptions & { price: Price }): string

Engine-managed: sell when price >= level.

Types: ConditionalOptions, Price

trade.trailingStop() ​

ts
trade.trailingStop(options: ConditionalOptions & { pct: number }): string

Engine-managed: sell when price falls pct percent below the running high since creation.

Types: ConditionalOptions

trade.cancelConditional() ​

ts
trade.cancelConditional(id: string): boolean

Cancels an active stop, take-profit or trailing stop. Returns false when it already triggered or was cancelled.

Types ​

BuyOptions ​

Options of trade.buy(). Give the size as tao (TAO to spend, fee included) or as alpha.

FieldTypeDescription
tao?numberTAO to spend, fee included
alpha?numberalpha to buy, converted to TAO at the pool price when the intent fills
maxPrice?Pricereject (or partially fill) above this effective price
ttlBlocks?numberblocks the intent may wait for a fill before it expires (default 3)
partialOk?booleanwhen the whole size would break maxPrice or exceed the cash, fill what fits instead of rejecting (default true)
reason?stringa label shown in the journal and the tester's trade list

SellOptions ​

Options of trade.sell(). Give the size as alpha or as tao (converted at the pool price).

FieldTypeDescription
alpha?numberalpha to sell; more than the position sells the whole position
tao?numberTAO worth of alpha to sell, converted at the pool price when the intent fills
minPrice?Pricereject (or partially fill) below this price
ttlBlocks?numberblocks the intent may wait for a fill before it expires (default 3)
partialOk?booleanwhen the whole size would break minPrice, fill what fits instead of rejecting (default true)
reason?stringa label shown in the journal and the tester's trade list

ConditionalOptions ​

Options shared by the conditional orders.

FieldTypeDescription
alpha?number | "all"alpha to sell when triggered (default "all": the whole position)
ttlBlocks?numberblocks the triggered sell may wait for a fill (default 3)
reason?stringa label for the triggered sell (default: the order's kind)

Rejection ​

Why an intent was not filled, as passed to event.onReject.

FieldTypeDescription
intentIntent
reasonstring"price-limit" | "no-position" | "insufficient-cash" | "empty" | "no-price" | "risk:<rule>" (e.g. "risk:maxOrderTao")

AlphaScript language v0.1 · runtime 0.2.0