STXWebSocket
Describes stx-python 0.10.1, the current release.
Async Phoenix-channels client for the documented STX topics.
Build it from an AsyncSTX client, which supplies the host, the key
and your user id:
async with AsyncSTX() as client: async with client.websocket() as ws: book = await ws.orderbook(["<market-id>"], on_message=print) orders = await ws.orders() print(await orders.wait_snapshot()) await ws.run_forever()or standalone with the same settings arguments as AsyncSTX.
Constructor
Section titled “Constructor”| Parameter | Description |
|---|---|
rest |
an AsyncSTX to take host, key and user id from. |
user_id |
your user id, if you already have it (skips GET /me). |
heartbeat_interval |
seconds between socket heartbeats. The server closes a socket silent for 60 s. |
channel_ping_interval |
seconds between channel ping frames on every joined topic; None disables them. orders with cancel-on-disconnect pings faster, from the granted timeout. |
reconnect |
reconnect after a drop (default True). |
reconnect_policy |
backoff for reconnects. |
on_reconnect |
called (sync or async) after every successful reconnect and rejoin, and after rejoining a channel the server closed or errored while the socket stayed up: the moment to call orders() and anything else you show again. |
on_auth_error |
called with the exception when the socket gives up because its credentials are dead (an OAuth grant was revoked, stx.oauth.STXGrantRevokedException). The socket is closed by then and ws.error holds the same exception. |
access_token |
standalone only, an OAuth member access token instead of an API key. |
join_timeout |
seconds to wait for a join or push reply. |
queue_size |
messages buffered per channel for async for; the oldest is dropped when full. |
Attributes
Section titled “Attributes”| Attribute | Type | Description |
|---|---|---|
url |
str |
|
error |
Optional[Exception] |
|
verify_tls |
bool |
|
channels |
Dict[str, Channel] |
|
connected |
bool |
Methods
Section titled “Methods”account()
Section titled “account()”account(*, market_ids: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelaccount:{user_id}: everything the five channels above carry, on
one join. Do not also join a per-type channel (you would get every
message twice), and use orders if you need cancel-on-disconnect.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
account_view()
Section titled “account_view()”account_view(*, market_ids: Optional[Sequence[str]] = None, account_id: Optional[str] = None, on_change: Optional[Callable[[AccountChange], Any]] = None, snapshot_timeout: Optional[float] = 15.0, keep_flat_positions: bool = False, max_fills: int = 1000) -> AccountViewA live copy of your account: balance, open orders, fills and positions.
Joins balances, orders, fills and positions, waits for
their join snapshots, then keeps the view current from what the
exchange pushes; after a reconnect each snapshot replaces its part.
on_change gets a stx.AccountChange per applied message.
See stx.AccountView.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
account_id |
Optional[str] |
|
on_change |
Optional[Callable[[AccountChange], Any]] |
|
snapshot_timeout |
Optional[float] |
|
keep_flat_positions |
bool |
|
max_fills |
int |
balances()
Section titled “balances()”balances(*, account_id: Optional[str] = None, on_message: Optional[MessageHandler] = None) -> Channelbalances:{user_id}: balances on join, then update and
payment_update. account_id picks one of your accounts.
| Parameter | Type | Description |
|---|---|---|
account_id |
Optional[str] |
|
on_message |
Optional[MessageHandler] |
combo_quote()
Section titled “combo_quote()”combo_quote(*, on_message: Optional[MessageHandler] = None) -> ComboQuoteChannelcombo_quote:{user_id}: your maker channel for combos.
Returns an stx.combos.ComboQuoteChannel, which receives every
request opened after the join (rfq_opened) and sends quote
and withdraw_quote. Needs a read_write key.
Raises stx.STXCombosUnavailableException on an environment
without combos, and stx.STXComboException with the reason
for any other refusal.
| Parameter | Type | Description |
|---|---|---|
on_message |
Optional[MessageHandler] |
connect()
Section titled “connect()”connect() -> NoneOpen the socket. Idempotent.
fills()
Section titled “fills()”fills(*, market_ids: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelfills:{user_id}: all_trades on join, then one trade per
execution or status change.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
join()
Section titled “join()”join(topic: str, payload: Optional[Dict[str, Any]] = None, *, on_message: Optional[MessageHandler] = None, ping_interval: Any = _UNSET) -> ChannelJoin topic with payload and wait for the reply.
Raises STXChannelException with the server’s reason (for example
market_ids_required or unauthorized) if the join is refused.
| Parameter | Type | Description |
|---|---|---|
topic |
str |
|
payload |
Optional[Dict[str, Any]] |
|
on_message |
Optional[MessageHandler] |
|
ping_interval |
Any |
market()
Section titled “market()”market(market_id: str, *, on_message: Optional[MessageHandler] = None) -> Channelmarket:<id>: one market in full.
The join reply (channel.reply) is the market’s current state:
every market field plus ob, its aggregated book ({"b": [...], "o": [...]}, each level {p, q, l, tc, tl}). Then
market_update carries the fields that changed and
order_book_update a fresh {"ob": ...} about every 200 ms.
The event’s live status rides along (event_brief,
detailed_event_brief, event_status). market_id is the
market id or symbol. Prices are converted from cents to dollar
strings and book levels are written as strings. A resulted or voided
market sends one last market_update and the server closes the
topic.
| Parameter | Type | Description |
|---|---|---|
market_id |
str |
|
on_message |
Optional[MessageHandler] |
market_stats()
Section titled “market_stats()”market_stats(market_ids: Sequence[str], *, range: Optional[str] = None, on_message: Optional[MessageHandler] = None) -> Channelmarket_stats: a price series per market. The history is in the
join reply (channel.reply["markets"]); market_stats pushes
changed buckets (upsert by timestamp_us) and
market_stats_snapshot replaces a series.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Sequence[str] |
|
range |
Optional[str] |
|
on_message |
Optional[MessageHandler] |
market_updates()
Section titled “market_updates()”market_updates(watch: Optional[Sequence[str]] = None, *, on_message: Optional[MessageHandler] = None) -> Channelmarket_updates: created and updated for the markets you
watch. Nothing arrives until you watch something; pass watch= or
call channel.watch([...]). Prices are converted from cents to
dollar strings here.
| Parameter | Type | Description |
|---|---|---|
watch |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
markets()
Section titled “markets()”markets(*, rule_filters: Optional[Sequence[str]] = None, message_types: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelmarkets: market_created and market_updated for every
market. Each payload maps market id to a market object;
market_updated carries only the changed fields. Prices arrive in
cents on the wire and are converted to dollar strings here.
| Parameter | Type | Description |
|---|---|---|
rule_filters |
Optional[Sequence[str]] |
|
message_types |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
orderbook()
Section titled “orderbook()”orderbook(market_ids: Sequence[str], *, on_message: Optional[MessageHandler] = None) -> Channelorderbook: the aggregated book, one book push per market.
Each push is a full snapshot of that market’s book; replace what you
hold rather than merging. market_ids is required.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Sequence[str] |
|
on_message |
Optional[MessageHandler] |
orders()
Section titled “orders()”orders(*, market_ids: Optional[Sequence[str]] = None, cancel_on_disconnect: bool = False, ping_timeout: Optional[int] = None, on_message: Optional[MessageHandler] = None) -> Channelorders:{user_id}: all_orders on join, then new_open_order.
cancel_on_disconnect=True arms cancel-on-disconnect for orders
placed with cancel_on_disconnect=True. ping_timeout is in
milliseconds, clamped by the server to 5000 to 20000; the granted
value is in channel.reply["ping_timeout"] and the SDK pings at
a third of it for as long as the channel is joined.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
cancel_on_disconnect |
bool |
|
ping_timeout |
Optional[int] |
|
on_message |
Optional[MessageHandler] |
positions()
Section titled “positions()”positions(*, market_ids: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelpositions:{user_id}: all_positions on join, then
updated_positions deltas with only the changed positions.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
rfq(*, on_message: Optional[MessageHandler] = None) -> RfqChannelrfq:{user_id}: your player channel for combos.
Returns an stx.combos.RfqChannel, which sends
request_quote, cash_out, accept, cancel and
snapshot. Needs a read_write key.
Raises stx.STXCombosUnavailableException on an environment
without combos, and stx.STXComboException with the reason
for any other refusal.
| Parameter | Type | Description |
|---|---|---|
on_message |
Optional[MessageHandler] |
run_forever()
Section titled “run_forever()”run_forever() -> NoneBlock until close is called (or reconnects are exhausted).
settlements()
Section titled “settlements()”settlements(*, market_ids: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelsettlements:{user_id}: new_settlements as they are recorded.
No snapshot; history is AsyncSTX.settlements().
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
ticker()
Section titled “ticker()”ticker(*, sports: Optional[Sequence[str]] = None, competitions: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channelticker: a ticker push whenever a market’s price, top of
book, volume or open interest moves. No snapshot on join.
| Parameter | Type | Description |
|---|---|---|
sports |
Optional[Sequence[str]] |
|
competitions |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
trades()
Section titled “trades()”trades(*, market_ids: Optional[Sequence[str]] = None, event_ids: Optional[Sequence[str]] = None, on_message: Optional[MessageHandler] = None) -> Channeltrades: every execution on the exchange, anonymised. action
is the taker’s side. Not your fills: see fills.
| Parameter | Type | Description |
|---|---|---|
market_ids |
Optional[Sequence[str]] |
|
event_ids |
Optional[Sequence[str]] |
|
on_message |
Optional[MessageHandler] |
user_id()
Section titled “user_id()”user_id() -> strYour user id, from user_id= or GET /api/v1/me.
user_info()
Section titled “user_info()”user_info(*, on_message: Optional[MessageHandler] = None) -> Channeluser_info:{user_id}: user_updated right after joining, then
on every profile change.
| Parameter | Type | Description |
|---|---|---|
on_message |
Optional[MessageHandler] |

