REST Endpoints

The Trading API provides REST endpoints for order management, position tracking, trade history, account information, and market data. All endpoints return JSON responses.

Base URL

The REST API is available at the same host as the WebSocket connection:

  • Backtest mode: http://localhost:8080
  • Live trading: http://localhost:8080 (proxy)

Authentication

Include your API credentials in the request headers as configured during connection setup. See the Connection Guide for details.

Response Format

All successful responses return JSON. Error responses use a standard format:

{
  "code": "ORDER_NOT_FOUND",
  "message": "Order not found: abc123",
  "details": { "resource": "order", "id": "abc123" }
}

Common Status Codes

StatusDescription
200Success
201Created (for POST /orders)
400Invalid request parameters
404Resource not found
409Conflict (e.g., order not modifiable)
422Order rejected
503Provider unavailable

Orders

POST /orders

Submit a new order.

Request Body (OrderRequest)

FieldTypeRequiredDescription
symbolstringYesTrading symbol (e.g., "AAPL", "EUR/USD")
sidestringYesBUY or SELL
quantitystringYesOrder quantity (decimal string)
order_typestringYesMARKET, LIMIT, STOP, STOP_LIMIT, TRAILING_STOP
limit_pricestringNoRequired for LIMIT and STOP_LIMIT orders
stop_pricestringNoRequired for STOP and STOP_LIMIT orders
time_in_forcestringNoGTC (default), DAY, IOC, FOK
client_order_idstringNoClient-provided ID for idempotency
stop_lossstringNoStop loss price for bracket order
take_profitstringNoTake profit price for bracket order
reduce_onlybooleanNoIf true, can only reduce position
post_onlybooleanNoIf true, reject if would immediately fill
oco_group_idstringNoLink orders for one-cancels-other
hiddenbooleanNoIf true, order not visible in order book
display_quantitystringNoVisible quantity for iceberg orders
leveragestringNoLeverage multiplier
margin_modestringNoCROSS or ISOLATED margin mode
position_idstringNoTarget position ID (for hedging accounts)
trailing_distancestringNoDistance for TRAILING_STOP orders
trailing_typestringNoABSOLUTE or PERCENT trailing type

Example Request

{
  "symbol": "AAPL",
  "side": "BUY",
  "quantity": "100",
  "order_type": "LIMIT",
  "limit_price": "150.00",
  "time_in_force": "GTC"
}

Response (201 Created)

Returns an OrderHandle with the assigned order ID:

{
  "id": "ord_abc123",
  "status": "PENDING",
  "client_order_id": null
}

Error Responses

  • 400 - Invalid request (missing required fields, invalid values)
  • 422 - Order rejected (insufficient margin, invalid price, etc.)
  • 503 - Provider unavailable

GET /orders

List open orders.

Query Parameters

ParameterTypeDescription
symbolstringFilter by symbol
statusstring[]Filter by status (multiple allowed)
sidestringFilter by side (BUY or SELL)
client_order_idstringFilter by client order ID
oco_group_idstringFilter by OCO group ID
sincedatetimeFilter orders created after this time
untildatetimeFilter orders created before this time
limitintegerMaximum number of orders to return

Response (200 OK)

[
  {
    "id": "ord_abc123",
    "symbol": "AAPL",
    "side": "BUY",
    "order_type": "LIMIT",
    "quantity": "100",
    "filled_quantity": "0",
    "remaining_quantity": "100",
    "limit_price": "150.00",
    "status": "OPEN",
    "time_in_force": "GTC",
    "created_at": "2025-01-15T10:30:00Z",
    "updated_at": "2025-01-15T10:30:00Z"
  }
]

GET /orders/{order_id}

Get a specific order by ID.

Path Parameters

ParameterTypeDescription
order_idstringThe order ID

Response (200 OK)

Returns the full Order object (same schema as list response items).

Error Responses

  • 404 - Order not found
  • 503 - Provider unavailable

PATCH /orders/{order_id}

Modify an existing order. Only non-null fields will be updated.

Path Parameters

ParameterTypeDescription
order_idstringThe order ID

Request Body (ModifyOrderRequest)

FieldTypeDescription
quantitystringNew total quantity (must be >= filled_quantity)
limit_pricestringNew limit price
stop_pricestringNew stop price
stop_lossstringNew stop loss price
take_profitstringNew take profit price
trailing_distancestringNew trailing distance

Example Request

{
  "limit_price": "151.00"
}

Response (200 OK)

Returns the updated Order object.

Error Responses

  • 400 - Invalid request
  • 404 - Order not found
  • 409 - Order not modifiable (already filled, cancelled, etc.)
  • 503 - Provider unavailable

DELETE /orders/{order_id}

Cancel a specific order.

Path Parameters

ParameterTypeDescription
order_idstringThe order ID

Response (200 OK)

{
  "success": true,
  "order": {
    "id": "ord_abc123",
    "status": "CANCELLED",
    ...
  }
}

Error Responses

  • 404 - Order not found
  • 503 - Provider unavailable

DELETE /orders

Cancel all open orders.

Query Parameters

ParameterTypeDescription
symbolstringFilter by symbol (optional, omit to cancel all)

Response (200 OK)

{
  "cancelled_count": 5,
  "failed_count": 0,
  "failed_order_ids": null
}

GET /orders/history

Get historical orders (filled, cancelled, rejected, expired).

Query Parameters

Same as GET /orders.

Response (200 OK)

Returns an array of Order objects.


Positions

GET /positions

Get all open positions.

Query Parameters

ParameterTypeDescription
symbolstringFilter by symbol (optional)

Response (200 OK)

[
  {
    "id": "pos_xyz789",
    "symbol": "AAPL",
    "side": "LONG",
    "quantity": "100",
    "average_entry_price": "150.25",
    "current_price": "151.50",
    "unrealized_pnl": "125.00",
    "realized_pnl": "0",
    "opened_at": "2025-01-15T10:30:01Z",
    "updated_at": "2025-01-15T11:00:00Z"
  }
]

GET /positions/{position_id}

Get a specific position by ID.

Path Parameters

ParameterTypeDescription
position_idstringThe position ID

Response (200 OK)

Returns the full Position object.

Error Responses

  • 404 - Position not found
  • 503 - Provider unavailable

DELETE /positions/{position_id}

Close a specific position.

Path Parameters

ParameterTypeDescription
position_idstringThe position ID

Request Body (optional)

FieldTypeDescription
quantitystringQuantity to close (omit for full close)
order_typestringMARKET (default) or LIMIT
limit_pricestringRequired if order_type is LIMIT
cancel_associated_ordersbooleanCancel SL/TP orders (default: true)

Response (200 OK)

Returns an OrderHandle for the close order.

Error Responses

  • 404 - Position not found
  • 503 - Provider unavailable

DELETE /positions

Close all open positions.

Query Parameters

ParameterTypeDescription
symbolstringFilter by symbol (optional, omit to close all)

Response (200 OK)

Returns an array of OrderHandle objects for each close order created.


Trades

GET /trades

Get trade/fill history.

Query Parameters

ParameterTypeDescription
symbolstringFilter by symbol
order_idstringFilter by order ID
sincedatetimeReturn trades after this time (inclusive)
untildatetimeReturn trades before this time (exclusive)
limitintegerMaximum number of trades to return

Response (200 OK)

[
  {
    "id": "trd_def456",
    "order_id": "ord_abc123",
    "symbol": "AAPL",
    "side": "BUY",
    "quantity": "100",
    "price": "150.25",
    "commission": "0.50",
    "commission_currency": "USD",
    "is_maker": false,
    "timestamp": "2025-01-15T10:30:01Z"
  }
]

Account

GET /account

Get account information including balances and margin.

Response (200 OK)

{
  "balance": "10000.00",
  "equity": "10500.00",
  "margin_used": "2000.00",
  "margin_available": "8500.00",
  "unrealized_pnl": "500.00",
  "currency": "USD"
}

Error Responses

  • 400 - Invalid request
  • 503 - Provider unavailable

Market Data

GET /quotes/{symbol}

Get the current quote for a symbol.

Path Parameters

ParameterTypeDescription
symbolstringSymbol (e.g., "AAPL", "BTC/USD")

Response (200 OK)

{
  "symbol": "AAPL",
  "provider": "alpaca",
  "bid": "150.20",
  "bid_size": "100",
  "ask": "150.25",
  "ask_size": "200",
  "last": "150.22",
  "timestamp": "2025-01-15T10:30:00.123Z"
}

Error Responses

  • 404 - Symbol not found
  • 503 - Provider unavailable

GET /bars/{symbol}

Get historical OHLCV bars for a symbol.

Path Parameters

ParameterTypeDescription
symbolstringSymbol (e.g., "S:AAPL", "C:BTCUSD")

Query Parameters

ParameterTypeRequiredDescription
timeframestringYesBar interval: 1m, 5m, 15m, 30m, 1h, 4h, 1d, 1w
startdatetimeNoStart of date range (inclusive)
enddatetimeNoEnd of date range (inclusive)
limitintegerNoMaximum number of bars to return

Response (200 OK)

[
  {
    "symbol": "AAPL",
    "provider": "polygon",
    "timeframe": "1h",
    "timestamp": "2025-01-15T10:00:00Z",
    "open": "150.00",
    "high": "151.50",
    "low": "149.75",
    "close": "151.25",
    "volume": "1234567"
  }
]

Error Responses

  • 400 - Invalid request (bad timeframe or symbol)
  • 404 - Symbol not found
  • 503 - Provider unavailable

System

GET /capabilities

Get provider capabilities describing supported features.

Response (200 OK)

{
  "supported_asset_classes": ["STOCK", "CRYPTO"],
  "supported_order_types": ["MARKET", "LIMIT", "STOP", "STOP_LIMIT"],
  "position_mode": "NETTING",
  "features": ["trailing_stop", "oco", "bracket_orders"],
  "max_leverage": "20"
}

GET /status

Get connection status to the trading provider.

Response (200 OK)

{
  "connected": true,
  "latency_ms": 15,
  "last_heartbeat": "2025-01-15T10:30:00Z"
}

GET /health

Health check endpoint for liveness probes. Returns 200 OK if the service is running.

Response (200 OK)

{
  "status": "ok"
}

Use this endpoint for Kubernetes liveness probes. It does not check provider connections.


GET /ready

Readiness check endpoint for readiness probes. Returns 200 OK if providers are configured.

Response (200 OK)

{
  "ready": true,
  "providers": ["alpaca-paper"],
  "provider_count": 1
}

Response (503 Service Unavailable)

Returned when no providers are configured:

{
  "ready": false,
  "providers": [],
  "provider_count": 0,
  "message": "No providers configured"
}

Use this endpoint for Kubernetes readiness probes.


Related Documentation