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:tpandslon 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
BUYorSELL, and not on a reduce-only parent. - On a spot
BUYor 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
LIMITparent, the stop-loss sits on the losing side ofpriceand the take-profit on the winning side. AMARKETparent’spriceis a worst case, so its legs are checked against the market at placement. - A perp leg is refused
TRIGGER_THROUGHwhen the mark has already reached its trigger: any leg on aMARKETparent, and the stop-loss on aLIMITparent. A spot leg is not refusedTRIGGER_THROUGH. - A perp stop or take that can open a position is refused
INSUFFICIENT_BALANCEat 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. quoteOrderQtycannot 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.
| Field | Meaning |
|---|---|
type | STOP_LIMIT, STOP_MARKET, TAKE_LIMIT, or TAKE_MARKET |
status | PENDING, ARMED, PLACED, FAILED, or CANCELLED |
triggerPrice | The trigger |
parentOrderId | The parent, when this leg was attached |
placedOrderId | The booked order, after the trigger fires |
triggeredAt | When 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:
code | Status | When |
|---|---|---|
TRIGGER_THROUGH | 400 | The last trade price (on a perp, the mark) is already through the trigger |
INSUFFICIENT_BALANCE | 400 | The pool cannot back an opening conditional |
OPEN_ORDER_LIMIT | 400 | Too many open orders. A PENDING or ARMED stop or take counts, as a resting order does |
INVALID_REQUEST | 400 | A 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_USED | 400 | The conditional’s index was already used, including by another stop or take |