Skip to content

Writing a λ program

A λ program has a predicate, an underlying order, and a lifetime. When the predicate is met, the λ program fires, i.e. places its order by sending it to the on-chip matching engine, up to its lifetime. See the REST API reference for every field.

A market data event triggers the λ program only if every condition in predicate is met:

Condition Field Satisfied when
Market market_id The event happens in this market.
Side side The event is on this side of the book (ask or bid).
Event event rested: the event added liquidity.
removed: the event removed liquidity for any reason.
canceled: the event removed liquidity with a cancel of resting liquidity.
crossed: the event removed liquidity with a trade, but did not rest the incoming order’s remainder.
Price px any: any price.
event_px: the event’s price is in [min, max] ticks.
tob_depth: the event’s price is in [min, max] ticks within the top-of-book on side as it was before the event. An event at or inside the best price, or on an empty side, is within 0 ticks.
Quantity qty any: any quantity.
event_qty: the quantity the event added or removed is in [min, max].
final_level_qty: the quantity resting at the event’s price after the event is in [min, max].

A range may leave out min or max to leave that end open, but not both (use any instead). λ programs do not trigger on events caused by orders they placed.

The order field contains an order template that’s filled and fired when its predicate is met. Its kind is the type of order it places:

Kind Fields Places
limit market_id, side, tif, px, qty A limit order on side of the market.
tif is gtc, ioc, or alo; alo is canceled if it would cross, while ioc crosses and cancels any unfilled quantity.
cancel market_id, side, px, qty A cancel of up to qty of the account’s own resting liquidity on side at px.

The order’s px and qty templates specify how the order’s price and quantity are computed:

Template Field Fired with
Price px fixed: the literal price given in ticks.
Quantity qty fixed: the literal quantity given in qty, in the base asset’s native units.

Today, fixed is the only supported template for both.

The lifetime field describes when the λ program stops firing:

Kind Stops firing
once After it fires one time.

Today, once is the only supported lifetime.

A refused request answers with what went wrong and, when one part of the request is at fault, where:

{ "error": { "code": "invalid_request", "message": "Quantity must be a multiple of the market's step size.", "field": "program.order.qty.qty" } }

Suppose a trader has a resting bid for 0.01 BTC (1,000,000 satoshis, BTC’s native unit) at 99,990 ticks on BTC-USDC and views a large sell just above that bid as a potential signal the price will keep falling. They place a λ program that cancels the bid if a single taker order removes 0.5 BTC (50,000,000 satoshis) or more from the bids and its deepest level reached is between 99,995 and 100,000 ticks.

// POST /v1/lambda/submit
{
"program": {
"kind": "local",
"predicate": {
"market_id": 1, // BTC-USDC
"side": "bid",
"event": "crossed", // removed by a trade
"px": { "kind": "event_px", "min": 99995, "max": 100000 },
"qty": { "kind": "event_qty", "min": 50000000 } // at least 0.5 BTC removed
},
"order": {
"kind": "cancel", "market_id": 1, "side": "bid",
"px": { "kind": "fixed", "ticks": 99990 },
"qty": { "kind": "fixed", "qty": 1000000 } // cancel 0.01 BTC
},
"lifetime": { "kind": "once" }
},
"client_lambda_id": "protect-bid-99990"
}

The response returns the exchange-assigned id for the λ program:

{ "lambda_id": "0192f3c1-7a2e-7c41-9b1d-5f0e8a3c2d10" }

To cancel the λ program, name it by that id, or by its own client_lambda_id while it is live:

// POST /v1/lambda/cancel, by the exchange-assigned id
{ "key": { "kind": "exchange", "id": "0192f3c1-7a2e-7c41-9b1d-5f0e8a3c2d10" } }
// or by the client-assigned id
{ "key": { "kind": "client", "id": "protect-bid-99990" } }

llms.txt

Regulated services provided by 1Money USA, Inc., a licensed money transmitter, NMLS ID 2628653 · Licenses