Trading with the API
The order routes need a key of the trade scope (Authentication) and an Idempotency-Key header
on every request (Idempotency). Their bodies and answers are the terminal’s own: the same checks, the
same rules of your program, the same refusals. Bodies are JSON with integers only (a decimal number is a 400), at
most 16,384 bytes.
| Route | Does |
|---|---|
POST /accounts/{id}/orders |
place an order (201) |
PATCH /accounts/{id}/orders/{order_id} |
change a working order |
DELETE /accounts/{id}/orders/{order_id} |
cancel a working order |
POST /accounts/{id}/positions/{symbol}/close |
exit the whole position at market (201) |
PUT /accounts/{id}/positions/{symbol}/brackets |
set the position’s stop loss and take profit |
POST /accounts/{id}/positions/{symbol}/reverse |
exit and open the same size the other way, at market, both or neither (201) |
PUT /accounts/{id}/leverage/{symbol} |
choose the leverage of one market |
Units: qty in market units, prices in micro-USD per market unit (Markets and quotes). Before trading, read
GET /accounts/{id}/trading-terms: the order types your account offers (order_types), whether a trailing stop and a
leverage choice are offered, the most open orders per market, and per market the leverage ceiling (max_x) and the
leverage in force (chosen_x).
Place an order
Section titled “Place an order”POST /accounts/{id}/orders
| Field | |
|---|---|
symbol |
required: a symbol of GET /markets |
side |
required: buy or sell |
qty |
required: market units, at least 1 |
order_type |
market (default), limit, stop or stop_limit |
limit_price |
for limit and stop_limit only (refused otherwise) |
trigger_price |
for stop and stop_limit only (refused otherwise) |
reduce_only |
true: the order may only reduce a position |
stop_loss, take_profit |
armed on the position the order opens (see below) |
stop_loss_trail |
with a stop_loss: the stop loss follows the price at this distance |
take_profits |
instead of take_profit: 1 to 3 targets {price, share}, TP1 first, whole-percent shares summing to 100 |
client_order_id |
optional: the Idempotency-Key itself (the order’s id) |
The answer: {order, decisions, as_of_seq, replayed}. The order row has its order_id, status (working,
filled, canceled, rejected), qty, filled_qty, rev, the prices and legs it carries, and legs_not_armed: a
stop loss or take profit it carried that was not placed, with why. What protects a position is what
GET /accounts/{id}/positions shows as armed.
A stop loss and a take profit ride on an order only when the order opens a position or turns it to its side, and only
when your account’s order_types (GET /accounts/{id}/trading-terms) include stop or stop_limit. An order that adds to a position
already open on its side is refused with 422 LEGS_ON_OPEN_POSITION, a reduce-only order with 422 LEGS_ON_REDUCTION,
and an account without stop orders with 422 LEGS_NOT_OFFERED: place the order without them, then set them on the
position with PUT …/positions/{symbol}/brackets (below).
Change or cancel a working order
Section titled “Change or cancel a working order”PATCH /accounts/{id}/orders/{order_id} with expected_rev (the order’s rev as you read it) and at least one of
qty, limit_price, trigger_price, stop_loss, take_profit (null removes that leg). The order keeps its
order_id; its rev goes up by one. If the order changed since you read it: 409 STALE_STATE. A market order, a
partly filled order, an order with several targets and a stop that protects a position are not modified (a 409 or
422 names the case): cancel and place again.
DELETE /accounts/{id}/orders/{order_id} cancels it. An order already filled, canceled or rejected:
409 ORDER_NOT_OPEN.
Positions
Section titled “Positions”- Exit:
POST …/positions/{symbol}/closewith an optionalexpected_position_qty(the signedqtyyou read): if the position is no longer that,409 POSITION_CHANGEDand nothing is done. It places a reduce-only market order. - Stop loss and take profit:
PUT …/positions/{symbol}/bracketswithstop_loss,take_profitandexpected_position_qty, all three required (nullremoves a leg: send the current value to keep it). Long: the stop loss below the mark, the take profit above; short: the reverse (422 SL_WRONG_SIDE/TP_WRONG_SIDEotherwise). Optional:stop_loss_trail,take_profits(a list without shares moves the targets held), andexpected_stop_loss, required when the armed stop loss trails the price (its current level, or422 STOP_LOSS_MOVED). - Reverse:
POST …/positions/{symbol}/reversewithexpected_position_qtyand optionalstop_loss,take_profit,stop_loss_trailfor the new position.
Leverage
Section titled “Leverage”PUT /accounts/{id}/leverage/{symbol} with {"leverage_x": n}: a whole number from 1 to the market’s max_x. The
answer gives leverage_x and max_x. Refused: LEVERAGE_INVALID (above the ceiling), LEVERAGE_NOT_OFFERED (rules
without a leverage choice), LEVERAGE_BELOW_POSITION (lower than the open position needs at its mark).
What the rules refuse
Section titled “What the rules refuse”An order is checked exactly like one from the terminal. A refusal is an error answer with a code and nothing done:
the rules of your program (422, source engine), the market (422, source exec: a frozen market, no fresh
quote…), or an operator’s pause of new orders (503 ORDERS_BLOCKED: a reduce-only market or limit order still goes through;
a stop does not). See Errors and rate limits.
Every order sent through the API is recorded as sent through the API, with the prefix of the key that sent it.