> ## Documentation Index
> Fetch the complete documentation index at: https://docs.0xarchive.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Algo script reference

> algo(), the order types, triggers and the account: scripts that trade, armed in the tab or on 0xArchive's server.

Scripts that trade an account through the fixed order types, armed in the tab or on 0xArchive's server.

## algo

An algo: a script that trades an account through the fixed order types (`order.*`). Must be the first
statement, in place of `indicator()`. It runs as orders when every `order.*` call sits in `algo.onArm`:
arming runs that function once and places the orders it makes, fixed numbers that the tab or 0xArchive's
server runs. An order call anywhere else is live logic (later).

**onArm()** `(fn: () =\> void): void`

Runs once when the trader arms the algo, after setup and history: the orders it makes are what arming places.

**describe()** `(fn: () =\> string): void`

The sentence the algo's settings lead with ("Chase buy \$1,000, never past 61,290").

**arm()** `(o?: \{ shots?: number; expires?: Duration; sides?: 'long' | 'short' | 'both'; cooldown?: Duration \}): void`

A strategy's arm (`lifetime: 'standing'`): how many trades it may take, until when, on which sides, and the wait
after each. From a button's `onPress`, or at the top level to arm it from the start (full auto, `shots: Infinity`).

**bias** `((o?: \{ owner?: 'trader' | 'code'; initial?: 'long' | 'short' | 'both' | 'off' \}) =\> void) & \{ /** The script's bias, when it owns it. The trader's Off still wins. */ set(dir: 'long' | 'short' | 'both' | 'off'): void; \}`

Who sets the direction allowed: the trader (the strip's Long, Short, Both, Off) or the script, with `algo.bias.set`. Top level.

**governor()** `(o: \{ dailyLossUsd: number; dailyProfitUsd?: number; maxTrades?: number \}): void`

The day's limits, from the account's real fills (manual trades count): a breach halts entries. Top level. Every strategy has one.

**zones()** `(list: readonly \{ lo: number; hi: number; side?: 'long' | 'short'; label?: string \}[]): void`

Where the strategy may engage: a trigger outside every zone for its side is journaled, not traded.

**trigger()** `(o: \{ side: 'long' | 'short'; price: number; why: string; strength?: number \}): boolean`

The moment the strategy would trade. The tab trades it when armed, the bias allows its side, its price is in a
zone (when there are zones), and the governor has room; otherwise it is journaled as "would have traded". Live
events only: whether it was sent.

**plan()** `(fn: (trigger: \{ side: 'long' | 'short'; price: number; why: string; strength: number | null \}) =\> StrategyPlan): void`

The trade a trigger makes, in trader terms. The stop goes in before the entry, the targets once it fills. Top level.

## AlgoOptions

An algo's display options are an indicator's; it runs on a market, never in a panel.

**role?** `'entry' | 'protect' | 'both'`

What its orders do to risk: `'protect'` may only reduce a position, `'entry'` may add. Default `'entry'`.

**lifetime?** `'once' | 'standing'`

`'once'` (the default) ends when its orders have; `'standing'` stays armed and runs as a script (later).

**trades?** `\{ markets?: 'chart' | readonly string[]; maxPositionUsd?: number \}`

What it may trade, shown when it is armed: the chart's market or a list, and a cap on the position.

## Side

```ts theme={"theme":"github-dark"}
type Side = 'buy' | 'sell';
```

## OrderSize

An order's size. `pctOfPosition` sizes an exit; `riskUsd` is the size that loses that much if `stop` is hit.

```ts theme={"theme":"github-dark"}
type OrderSize = { usd: number } | { coins: number } | { pctOfPosition: number } | { pctOfEquity: number } | { riskUsd: number; stop: number };
```

## Duration

Seconds, or text such as `'30s'`, `'5m'`, `'4h'`, `'2d'`.

```ts theme={"theme":"github-dark"}
type Duration = number | string;
```

## OrderRef

An order an algo made. In a plan it reads `'planned'`. Armed, a call on a live event reads `'sent'`: a request the
tab admits or refuses, whose answer and fills arrive on `account().onOrder` and `onFill`, by `tag`. In history, or
before the algo is armed, it reads `'none'`: nothing was sent.

**id** `string`

**tag** `string | null`

**state** `'none' | 'planned' | 'sent'`

**filled** `number`

**avgPx** `number`

## Leg

A resting order: an OCO's leg, or a limit.

**side** `Side`

**size** `OrderSize`

**px** `number | null`

The limit price. `null` (an empty price input) is refused at arm.

**postOnly?** `boolean`

Post only: refused rather than taking.

**restFor?** `Duration`

How long a condition's limit rests.

**tag?** `string`

## Trigger

A test of one of the execution service's own prices, for `order.when`. Made by `trigger.*`.

**input** `'mark' | 'book' | 'trade' | 'funding' | 'oiUsd'`

**test** `TriggerTest`

**value** `number | null`

## TriggerTest

```ts theme={"theme":"github-dark"}
type TriggerTest = 'above' | 'below' | 'crossesAbove' | 'crossesBelow';
```

## TriggerOptions

**confirm?** `Duration`

How long the test must hold before it fires.

**market?** `string`

Another market's price; the algo's own by default.

## control

A strategy's controls: how the trader drives it now. Declared at the top level, at most 16. A change is an event,
never a re-run. In history a control reads its `history` value, and a button never fires.

**number()** `(label: string, value: number, o?: \{ min?: number; max?: number; step?: number; unit?: 'usd' | 'bps'; history?: number \}): \{ readonly value: number \}`

**choice()** `\<T extends string\>(label: string, value: T, choices: readonly T[], o?: \{ history?: T \}): \{ readonly value: T \}`

**toggle()** `(label: string, value: boolean, o?: \{ history?: boolean \}): \{ readonly value: boolean \}`

**price()** `(label: string, o?: \{ value?: number \}): \{ readonly value: number | null \}`

A price the trader types or drags. Empty in history: a level set today gets no credit for last month.

**button()** `(label: string): \{ onPress(fn: () =\> void): void \}`

## StrategyPlan

A trade in trader terms. `r` targets count from the entry to the stop.

**entry?** `\{ how?: 'cross' | 'chase'; worstBps?: number \}`

**size** `\{ riskUsd: number \} | \{ notionalUsd: number \} | \{ equityPct: number \}`

**stop** `\{ bps: number \} | \{ price: number \} | \{ beyondZone: true; padBps?: number \}`

**targets?** `\{ r: number; pct: number \}[]`

**timeStop?** `Duration`

## order

The order types an algo places. Each call is one order of that type, checked against its ranges at arm.

**chase()** `(o: \{ side: Side; size: OrderSize; limitPx?: number | null; peg?: 'touch' | 'inside'; maxReprices?: number; maxTime?: Duration; onEnd?: 'cancel' | 'cross'; reduceOnly?: boolean; tag?: string \}): OrderRef`

Rest at the best price and follow it until filled, never past `limitPx`. `maxTime` gives up; `onEnd: 'cross'` takes the rest.

**fastStop()** `(o: \{ px: number | null; source?: 'mark' | 'book' | 'trade'; size?: OrderSize; confirm?: Duration; bandPct?: number; attempts?: number; tag?: string \}): OrderRef`

Close the position held at arm (or `size` of it) when the price reaches `px`: the mark, the best bid or ask
(`'book'`), or the last trade. Watched by the runner, not resting on the venue. Exits in reduce-only rounds
within `bandPct` of the mark, at most `attempts` times.

**timeStop()** `(o: \{ after?: Duration; at?: number; size?: OrderSize; bandPct?: number; attempts?: number; tag?: string \}): OrderRef`

Close the position held at arm (or `size` of it) after a hold, or at a time.

**oco()** `(a: Leg, b: Leg, o?: \{ tag?: string \}): OrderRef`

Two resting orders: the first to fill cancels the other.

**when()** `(trigger: Trigger, then: () =\> OrderRef, o?: \{ tag?: string \}): OrderRef`

When the trigger's test holds, make the one order `then` makes: `order.reduce`, `order.close`, `order.limit` or `order.market`.

**reduce()** `(size: OrderSize, o?: \{ bandPct?: number; tag?: string \}): OrderRef`

A reduce-only order at market within the band, as a condition's action.

**close()** `(o?: \{ bandPct?: number; tag?: string \}): OrderRef`

Close the whole position at market within the band, as a condition's action.

**limit()** `(o: Leg & \{ reduceOnly?: boolean \}): OrderRef`

A resting limit, as a condition's action.

**market()** `(o: \{ side: Side; size: OrderSize; bandPct?: number; reduceOnly?: boolean; tag?: string \}): OrderRef`

An order at market within the band, as a condition's action.

**cancel()** `(target: OrderRef | string): void`

Live logic: cancel the working orders made with this tag (or this ref's tag). Not in `algo.onArm`.

**quote()** `(o: \{ bidPx: number | null; askPx: number | null; size: \{ usd: number \} | \{ coins: number \}; tag?: string \}): OrderRef`

Live logic: quote both sides. A post-only bid at `bidPx` and ask at `askPx`, each of `size`, rest in the account;
each call moves them (a moved quote goes to the back of its new level), and `null` pulls that side. The host keeps
the position inside `algo({ trades: { maxPositionUsd } })`: the side that would pass it is pulled. Quoting runs on the Paper account.

**bracket()** `(o: \{ entry: Leg; stop: number; takeProfit?: number; tag?: string \}): OrderRef`

Not available: use the ticket's TP/SL, or `order.fastStop` with `order.when`.

**trailing()** `(o: \{ distance: \{ pct: number \} | \{ price: number \}; size?: OrderSize; tag?: string \}): OrderRef`

Not available.

## trigger

The prices `order.when` can test, as the execution service reads them. A `null` value (an empty price input) is refused at arm.

**mark()** `(test: TriggerTest, value: number | null, options?: TriggerOptions): Trigger`

**book()** `(test: TriggerTest, value: number | null, options?: TriggerOptions): Trigger`

The book's mid.

**trade()** `(test: TriggerTest, value: number | null, options?: TriggerOptions): Trigger`

The last trade.

**funding()** `(test: TriggerTest, value: number | null, options?: TriggerOptions): Trigger`

The funding rate per hour, as a fraction.

**oiUsd()** `(test: TriggerTest, value: number | null, options?: TriggerOptions): Trigger`

Open interest in USD.

## account()

The account the algo trades. Declared at the top level, once.

```ts theme={"theme":"github-dark"}
function account(): AlgoAccount;
```

## OrderEvent

What became of one of an algo's orders: the tab's answer to it, or a change in what it placed.

**tag** `string | null`

**state** `'working' | 'filled' | 'ended' | 'refused'`

`'refused'` with `code` (`needs_stop`, `shots_used`, `entry_rate_limited`, ...) and `words` when the tab did not place it.

**code** `string | null`

**words** `string | null`

**filled** `number`

Filled so far, in coins, and its average price (`NaN` with nothing filled).

**avgPx** `number`

## AlgoAccount

The account the algo trades. In `algo.onArm`, as it stands at the arm. Armed, as the tab last read it, for live
logic. Otherwise it reads flat.

**position** `\{ readonly side: 'long' | 'short' | 'flat'; readonly size: number; readonly entryPrice: number; readonly notionalUsd: number \}`

The position in the algo's market: `size` in coins, `entryPrice` the average entry.

**equity** `number`

The account's equity in USD.

**mark** `number`

The market's mark price.

**onOrder()** `(fn: (order: OrderEvent) =\> void): void`

Each answer and change to the algo's orders, on live events only, never in history. Registered at the top level.

**onFill()** `(fn: (order: OrderEvent) =\> void): void`

When one of the algo's orders fills more. Registered at the top level.

## Next step

Continue with [Inputs](https://docs.0xarchive.io/terminal/reference/inputs.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.