Skip to Content
API ReferenceStop & Take Orders API

Stop & Take Orders

Two ways to attach a trigger. Both are signed by api key 0. You send one signed request. On a spot market, the engine fires it when the last trade price reaches triggerPrice. On a perp, it fires when the mark reaches triggerPrice.

  • A field on POST /api/orders: tp and sl on the parent. Each leg is signed alone.
  • A standalone conditional: POST /api/orders/conditional.

Cancels take a login token or the web app’s Privy JWT. Placement ignores Authorization.

Attached to a parent

On POST /api/orders, tp and sl are optional. Each leg:

{ "triggerPrice": 180, "price": 180, "l2OrderIndex": 101, "l2ExpiryMs": 1765000000000, "l2SigS": "…80 hex…", "l2SigE": "…80 hex…" }

tp fires as TAKE_MARKET. sl fires as STOP_MARKET. The leg’s quantity is the parent’s quantity. The side is the opposite of the parent. The rest of the signed order is the parent’s, except the leg’s own price, index, expiry, and signature. On a perp the leg is reduce-only on the parent’s pool.

Rules:

  • Each leg needs its own reserved l2OrderIndex.
  • On a spot market, legs are allowed only on a BUY. They close the position that buy opens.
  • On a perp, legs are allowed on BUY or SELL, and not on a reduce-only parent.
  • On a spot BUY or a perp long, the take-profit trigger is above the stop-loss trigger. On a perp short, the take-profit is below the stop-loss.
  • On a LIMIT parent, the stop-loss sits on the losing side of price and the take-profit on the winning side. A MARKET parent’s price is a worst case, so its legs are checked against the market at placement.
  • A perp leg is refused TRIGGER_THROUGH when the mark has already reached its trigger: any leg on a MARKET parent, and the stop-loss on a LIMIT parent. A spot leg is not refused TRIGGER_THROUGH.
  • A perp stop or take that can open a position is refused INSUFFICIENT_BALANCE at placement when its pool cannot back it.
  • Batch fields are refused on a body that carries a leg: l2 batch: a conditional order or an attached leg is signed alone.
  • quoteOrderQty cannot be combined with legs.

The place response lists attachedLegIds in request order, take-profit first. Those ids are conditional-order ids. Cancel one with POST /api/orders/conditional/cancel and { "orderId": <attachedLegId> }.

When one attached leg fires, the engine cancels the other.

Standalone conditional

POST /api/orders/conditional

{ "l2ChainId": 1, "l2AccountIndex": 4, "l2MarketIndex": 2048, "l2OrderIndex": 110, "l2Epoch": 3, "l2FeeRateCap": 7000, "l2ExpiryMs": 1765000000000, "l2BaseAssetId": 7, "l2QuoteAssetId": 3, "l2SigS": "…80 hex…", "l2SigE": "…80 hex…", "side": "SELL", "type": "STOP_MARKET", "triggerPrice": 150, "price": 150, "quantity": 2.5, "timeInForce": "GTC" }

type is STOP_LIMIT, STOP_MARKET, TAKE_LIMIT, or TAKE_MARKET. timeInForce is GTC only. A perp sends l2Pool and l2ReduceOnly instead of l2BaseAssetId, same as a parent order.

On a spot market, triggerPrice is compared with the last trade price. On a perp, it is compared with the mark. A trigger that price has already reached, or a trigger equal to it, is TRIGGER_THROUGH.

The response is { "success": true, "orderId": <conditional id> }. orderId is the conditional’s id. After it fires, history shows the booked order as placedOrderId.

Cancel with POST /api/orders/conditional/cancel and { "orderId": <conditional id> }.

A conditional body carries no orderTag, sendByMs, replacesOrderId, quoteOrderQty, or batch fields.

History and the socket

History and GET /api/portfolio/component openOrders return a conditional with these fields.

FieldMeaning
typeSTOP_LIMIT, STOP_MARKET, TAKE_LIMIT, or TAKE_MARKET
statusPENDING, ARMED, PLACED, FAILED, or CANCELLED
triggerPriceThe trigger
parentOrderIdThe parent, when this leg was attached
placedOrderIdThe booked order, after the trigger fires
triggeredAtWhen it fired

The portfolio socket sends order_update with the conditional when the status changes, and order_status when a booked order moves. Seed from History or the component, then listen on portfolio.

Errors

The same code values as orders. The ones that show up on triggers:

codeStatusWhen
TRIGGER_THROUGH400The last trade price (on a perp, the mark) is already through the trigger
INSUFFICIENT_BALANCE400The pool cannot back an opening conditional
OPEN_ORDER_LIMIT400Too many open orders. A PENDING or ARMED stop or take counts, as a resting order does
INVALID_REQUEST400A leg on a spot SELL, on a reduce-only parent, or a take-profit on the wrong side of the stop. A leg on the wrong side of a LIMIT parent’s price, or of the market on a spot MARKET parent
ORDER_INDEX_USED400The conditional’s index was already used, including by another stop or take
Last updated on