Event Types
This reference documents all WebSocket event types in the Trading API, including market data events, order/position updates, and client messages.
Event Message Structure
All events are delivered via WebSocket with a consistent structure:
{
"type": "event_type",
"timestamp": "2025-01-15T10:30:00Z",
...event-specific fields
}
The type field identifies the event category. Each event type has specific additional fields documented below.
Format Conventions
A handful of encoding rules apply uniformly across every event payload.
Prices and monetary values
Prices, quantities, balances, and P&L are serialized as JSON strings (e.g., "150.25", "1.0846"). The Gateway represents these as arbitrary-precision Decimal values internally; sending them as strings preserves every digit across the wire. Parse them into a decimal type (Python decimal.Decimal, Java BigDecimal, Rust rust_decimal::Decimal, Go shopspring/decimal.Decimal) rather than float or number — JSON float parsers will silently round values like 0.1 + 0.2, and accumulated rounding across fills and P&L updates can drift by pennies over a trading session.
Timestamps
Most timestamps are ISO-8601 UTC strings with optional fractional seconds:
"2025-01-15T10:30:00Z" "2025-01-15T10:30:00.125Z"
The one exception is event_ack.timestamp, which is a Unix millisecond integer (e.g., 1700000000000). The Gateway uses this format for backtest-engine time advancement, where an integer is cheaper to compare than a parsed date. Every other timestamp — on events, on quotes, on orders — is the ISO-8601 form.
Market Data Events
Candle Event
Delivers OHLCV bar data for subscribed instruments.
Type: candle
| Field | Type | Description |
|---|---|---|
type | string | Always "candle" |
bar | Bar | The bar data |
timestamp | datetime | Event timestamp (when event was emitted) |
Bar Schema
| Field | Type | Description |
|---|---|---|
symbol | string | Symbol in canonical format (e.g., "EUR/USD", "AAPL") |
provider | string | Data source provider (e.g., "alpaca", "binance") |
timeframe | Timeframe | Bar timeframe |
timestamp | datetime | Bar start timestamp |
open | string | Opening price |
high | string | Highest price during the period |
low | string | Lowest price during the period |
close | string | Closing price |
volume | string | Trading volume during the period |
Timeframe Enum
| Value | Description |
|---|---|
1m | 1 minute |
2m | 2 minutes |
5m | 5 minutes |
10m | 10 minutes |
15m | 15 minutes |
30m | 30 minutes |
1h | 1 hour |
2h | 2 hours |
4h | 4 hours |
12h | 12 hours |
1d | 1 day |
1w | 1 week |
Example
{
"type": "candle",
"bar": {
"symbol": "AAPL",
"provider": "alpaca",
"timeframe": "1h",
"timestamp": "2025-01-15T10:00:00Z",
"open": "150.00",
"high": "151.50",
"low": "149.75",
"close": "151.25",
"volume": "1500000"
},
"timestamp": "2025-01-15T11:00:00Z"
}
Quote Event
Delivers real-time bid/ask quotes.
Type: quote
| Field | Type | Description |
|---|---|---|
type | string | Always "quote" |
quote | Quote | The quote data |
timestamp | datetime | Event timestamp |
Quote Schema
| Field | Type | Description |
|---|---|---|
symbol | string | Symbol in canonical format |
provider | string | Data source provider |
bid | string | Best bid price (highest buy offer) |
bid_size | string? | Quantity available at bid price |
ask | string | Best ask price (lowest sell offer) |
ask_size | string? | Quantity available at ask price |
last | string | Last traded price |
volume | string? | Trading volume (if available) |
timestamp | datetime | Quote timestamp |
Example
{
"type": "quote",
"quote": {
"symbol": "EUR/USD",
"provider": "alpaca",
"bid": "1.0845",
"bid_size": "100000",
"ask": "1.0847",
"ask_size": "100000",
"last": "1.0846",
"timestamp": "2025-01-15T10:30:00.123Z"
},
"timestamp": "2025-01-15T10:30:00.125Z"
}
Order Events
Delivers order state changes.
Type: order
| Field | Type | Description |
|---|---|---|
type | string | Always "order" |
event | OrderEventType | Type of order event |
order | Order | Current state of the order |
parent_order_id | string? | Parent order ID for bracket/OCO child orders |
timestamp | datetime | Event timestamp |
OrderEventType Enum
| Value | Description |
|---|---|
ORDER_CREATED | Order has been created and submitted |
ORDER_MODIFIED | Order has been modified (price, quantity, etc.) |
ORDER_CANCELLED | Order has been cancelled |
ORDER_REJECTED | Order was rejected by the provider |
ORDER_FILLED | Order has been completely filled |
ORDER_PARTIALLY_FILLED | Order has been partially filled |
ORDER_EXPIRED | Order has expired based on time_in_force |
BRACKET_ORDER_CREATED | Bracket order (with SL/TP) has been created |
BRACKET_ORDER_MODIFIED | Bracket order has been modified |
Example
{
"type": "order",
"event": "ORDER_FILLED",
"order": {
"id": "ord_abc123",
"symbol": "AAPL",
"side": "BUY",
"order_type": "LIMIT",
"quantity": "100",
"filled_quantity": "100",
"remaining_quantity": "0",
"average_fill_price": "150.25",
"status": "FILLED",
"time_in_force": "GTC",
"limit_price": "150.50",
"created_at": "2025-01-15T10:30:00Z",
"updated_at": "2025-01-15T10:30:05Z"
},
"timestamp": "2025-01-15T10:30:05Z"
}
Position Events
Delivers position state changes.
Type: position
| Field | Type | Description |
|---|---|---|
type | string | Always "position" |
event | PositionEventType | Type of position event |
position | Position | Current state of the position |
timestamp | datetime | Event timestamp |
PositionEventType Enum
| Value | Description |
|---|---|
POSITION_OPENED | New position has been opened |
POSITION_MODIFIED | Position has been modified (partial close, averaging) |
POSITION_CLOSED | Position has been fully closed |
Example
{
"type": "position",
"event": "POSITION_OPENED",
"position": {
"id": "pos_xyz789",
"symbol": "BTC/USD",
"side": "LONG",
"quantity": "0.5",
"average_entry_price": "42000.00",
"current_price": "42100.00",
"unrealized_pnl": "50.00",
"realized_pnl": "0.00",
"margin_mode": "ISOLATED",
"leverage": "10",
"liquidation_price": "38000.00",
"opened_at": "2025-01-15T10:30:00Z",
"updated_at": "2025-01-15T10:30:00Z"
},
"timestamp": "2025-01-15T10:30:00Z"
}
margin_mode, leverage, and liquidation_price are optional. They are populated for leveraged positions (typically crypto and futures) and omitted for cash equities. See Position Types for the full schema and the MarginMode enum.
Account Events
Delivers account state changes.
Type: account
| Field | Type | Description |
|---|---|---|
type | string | Always "account" |
event | AccountEventType | Type of account event |
account | Account | Current account state |
timestamp | datetime | Event timestamp |
AccountEventType Enum
| Value | Description |
|---|---|
BALANCE_UPDATED | Account balance has changed |
MARGIN_WARNING | Margin utilization approaching limit |
MARGIN_CALL | Margin call triggered - reduce positions or add funds |
Example
{
"type": "account",
"event": "BALANCE_UPDATED",
"account": {
"balance": "10000.00",
"equity": "10250.00",
"margin_used": "1500.00",
"margin_available": "8500.00",
"unrealized_pnl": "250.00",
"currency": "USD"
},
"timestamp": "2025-01-15T10:30:05Z"
}
Trade Events
Delivers individual trade/fill records.
Type: trade
| Field | Type | Description |
|---|---|---|
type | string | Always "trade" |
event | TradeEventType | Type of trade event |
trade | Trade | The executed trade |
timestamp | datetime | Event timestamp |
TradeEventType Enum
| Value | Description |
|---|---|
TRADE_FILLED | A trade has been executed |
Trade Schema
| Field | Type | Description |
|---|---|---|
id | string | Unique trade identifier |
order_id | string | Order ID this trade belongs to |
symbol | string | Symbol traded |
side | Side | Trade side (BUY or SELL) |
quantity | string | Fill quantity in base units |
price | string | Fill price |
commission | string | Commission/fee amount |
commission_currency | string | Currency of the commission |
is_maker | boolean? | Whether this was a maker trade |
timestamp | datetime | Trade execution timestamp |
Example
{
"type": "trade",
"event": "TRADE_FILLED",
"trade": {
"id": "trd_123456",
"order_id": "ord_abc123",
"symbol": "AAPL",
"side": "BUY",
"quantity": "60",
"price": "150.00",
"commission": "0.50",
"commission_currency": "USD",
"is_maker": false,
"timestamp": "2025-01-15T10:30:05Z"
},
"timestamp": "2025-01-15T10:30:05Z"
}
Connection Events
Delivers state changes for the strategy's link to the Gateway and for the Gateway's link to its upstream broker.
Type: connection
| Field | Type | Description |
|---|---|---|
type | string | Always "connection" |
event | ConnectionEventType | Type of connection event |
broker | string? | Upstream broker/provider identifier (e.g. "alpaca-paper"). Present on BROKER_* variants. |
error | string? | Error message when the event indicates a failure or unexpected disconnect |
gap_duration_ms | integer? | Outage duration in milliseconds. Present on BROKER_RECONNECTED. |
timestamp | datetime | Event timestamp |
ConnectionEventType Enum
| Value | Description |
|---|---|
CONNECTED | Strategy successfully connected to the Gateway |
DISCONNECTING | Gateway connection is being closed |
RECONNECTING | Attempting to reconnect after connection loss |
BROKER_DISCONNECTED | Upstream broker link dropped |
BROKER_RECONNECTED | Upstream broker link restored after an outage; gap_duration_ms reports the gap |
BROKER_CONNECTION_FAILED | Initial broker connect failed, or a reconnect attempt failed terminally |
Example
{
"type": "connection",
"event": "CONNECTED",
"timestamp": "2025-01-15T10:30:00Z"
}
{
"type": "connection",
"event": "BROKER_RECONNECTED",
"broker": "alpaca-paper",
"gap_duration_ms": 4200,
"timestamp": "2025-01-15T10:31:04Z"
}
Rate Limit Events
Notifies about rate limiting status.
Type: rate_limit
| Field | Type | Description |
|---|---|---|
type | string | Always "rate_limit" |
event | RateLimitEventType | Type of rate limit event |
requests_remaining | integer | Number of requests remaining in current window |
reset_at | datetime | When the rate limit window resets |
timestamp | datetime | Event timestamp |
RateLimitEventType Enum
| Value | Description |
|---|---|
RATE_LIMIT_WARNING | Approaching rate limit threshold |
RATE_LIMIT_HIT | Rate limit exceeded, requests will be delayed |
Example
{
"type": "rate_limit",
"event": "RATE_LIMIT_WARNING",
"requests_remaining": 10,
"reset_at": "2025-01-15T10:31:00Z",
"timestamp": "2025-01-15T10:30:50Z"
}
Data Staleness Events
Emitted when the Gateway detects that upstream market-data freshness for one or more symbols has crossed a threshold. Useful for pausing or resuming trading logic that depends on live quotes.
Type: data_staleness
| Field | Type | Description |
|---|---|---|
type | string | Always "data_staleness" |
event | DataStalenessEventType | STALE when data goes stale, FRESH when it recovers |
symbols | string[] | Affected symbols |
stale_since | datetime? | Timestamp when data first went stale. Present on STALE. |
broker | string? | Upstream broker/provider identifier, when the staleness is scoped to a specific provider |
timestamp | datetime | Event timestamp |
DataStalenessEventType Enum
| Value | Description |
|---|---|
STALE | Upstream data freshness fell below the configured threshold |
FRESH | Upstream data freshness has recovered |
Example
{
"type": "data_staleness",
"event": "STALE",
"symbols": ["AAPL", "MSFT"],
"stale_since": "2025-01-15T10:30:00Z",
"broker": "alpaca-paper",
"timestamp": "2025-01-15T10:30:05Z"
}
{
"type": "data_staleness",
"event": "FRESH",
"symbols": ["AAPL", "MSFT"],
"broker": "alpaca-paper",
"timestamp": "2025-01-15T10:31:12Z"
}
Error Events
Delivers error notifications.
Type: error
| Field | Type | Description |
|---|---|---|
type | string | Always "error" |
code | WsErrorCode | Error code |
message | string | Human-readable error message |
details | object? | Additional error context |
timestamp | datetime | Event timestamp |
WsErrorCode Enum
| Value | Description |
|---|---|
INVALID_MESSAGE | Received message could not be parsed |
INTERNAL_ERROR | Server encountered an internal error |
POSITION_UNPROTECTED | A position is open without its configured bracket protection (stop-loss or take-profit) attached |
LIQUIDATION | Position was forcibly liquidated by the broker or risk engine |
OCO_DOUBLE_EXIT | Both legs of an OCO group fired before the cancel for the second leg reached the broker |
Example
{
"type": "error",
"code": "INVALID_MESSAGE",
"message": "Failed to parse order request: missing required field 'symbol'",
"details": {
"field": "symbol"
},
"timestamp": "2025-01-15T10:30:00Z"
}
Client Messages
Messages sent from your strategy to the server.
Ping/Pong (Heartbeat)
The server sends ping messages to check connection health. Your client must respond with pong.
Server sends:
{
"type": "ping",
"timestamp": "2025-01-15T10:30:00Z"
}
Client responds:
{
"type": "pong"
}
Event Acknowledgment
Strategies acknowledge received events by sending an event_ack frame.
Type: event_ack
| Field | Type | Description |
|---|---|---|
type | string | Always "event_ack" |
correlation_id | string | Caller-chosen ID (typically a UUID) useful for correlating ACKs in logs |
events_processed | string[] | Informational — strings the strategy chose to record. The Gateway auto-correlates ACKs with outstanding events; it does not match entries in this array to any specific event. Common choices: the id values from orders/trades/positions the strategy handled, a strategy-generated correlation ID, or an empty array. |
timestamp | integer | Unix timestamp (milliseconds) when the strategy finished processing |
Mode-dependent behavior:
- Simulation mode (backtest engine) — acknowledgment is required. After emitting an event, the engine stops advancing simulated time until it sees any
event_ackframe from the strategy. The frame's arrival is what matters; the Gateway drains its full set of outstanding events on each ACK (auto-correlation). A strategy that never sends anevent_ackstalls the backtest indefinitely. - Live or paper trading — acknowledgment is informational-only. The Gateway does not gate on ACKs. Strategies may send
event_ackfor telemetry or skip it entirely.
Example
{
"type": "event_ack",
"correlation_id": "550e8400-e29b-41d4-a716-446655440000",
"events_processed": ["evt_123", "evt_124"],
"timestamp": 1700000000000
}
Related Types
- Order Types - Order schemas and enums
- Position Types - Position schemas and P&L