Skip to content

Trading

Describes stx-python 0.10.1, the current release.

Placing and cancelling needs a read_write key. The examples run on the US demo exchange with 1-cent buy orders that will not fill. How orders behave on the exchange is covered in Order types and Risk controls; this page shows the SDK calls.

The examples use a market that is open, accepting orders (trading is True), and whose event has not started:

from stx import STX
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
print(market.market_id, market.symbol, market.max_price)

Sample output:

e00651a3-10cb-4bc1-9e1f-c5ea1796c83b STXNHL-26OCT101600EDMSJ-GAMEEDM 1.0000
from stx import STX
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
order = client.place_order(
market.market_id, "buy", "limit", price="0.01", quantity="1", client_order_id="my-first-order"
)
print(order.id, order.status, order.price, order.quantity, order.client_order_id)
client.cancel_order(order.id)

Sample output:

8f023ead-6f2d-4cf5-9931-cd217e44a2ee accepted 0.0100 1.00 my-first-order
Argument Meaning
market_id The market
action "buy" or "sell"
order_type "limit" or "market"
price Dollars as a string, e.g. "0.58". Required for a limit order, omitted for a market order
quantity Contracts as a string, e.g. "100"
client_order_id Your own id, echoed back and filterable
expiration, expiration_time See Expiration
cancel_on_disconnect See Cancel-on-disconnect
device_id Optional device label
fill_mode "normal" (the default), "post_only" or "fill_or_kill", for limit orders. See Post-only and fill-or-kill

price and quantity must be strings (or decimal.Decimal). A float raises TypeError before anything is sent, because a float cannot carry an exact decimal. The exchange validates everything else: a price off the tick, a price at or above max_price, or a fractional quantity comes back as STXRejectedException (422) or STXValidationException (400) carrying the exchange’s message:

from stx import STX, STXAPIException
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
try:
client.place_order(market.market_id, "buy", "limit", price="0.01", quantity="1.5")
except STXAPIException as exc:
print(exc.status_code, exc)

Sample output:

422 Invalid order fields: quantity - Fractional quantity is not allowed.

The returned Order shows the state at acceptance (accepted, open, filled…). Follow it on the orders channel or with client.order(order.id).

place_orders() sends one POST /api/v1/orders/batched. Each order is a dict with the place_order fields; the result has one entry per order, in order, holding either order or errors:

from stx import STX
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
results = client.place_orders(
[
{"market_id": market.market_id, "action": "buy", "order_type": "limit",
"price": "0.01", "quantity": "1"},
{"market_id": market.market_id, "action": "buy", "order_type": "limit",
"price": "0.02", "quantity": "1"},
]
)
for r in results:
print("placed" if r.ok else "rejected", r.order.id if r.ok else r.errors)
cancelled = client.cancel_orders([r.order.id for r in results if r.ok])
print("cancelled", len(cancelled))

One rejected order does not stop the others.

from stx import STX
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
a = client.place_order(market.market_id, "buy", "limit", price="0.01", quantity="1")
b = client.place_order(market.market_id, "buy", "limit", price="0.01", quantity="1")
c = client.place_order(market.market_id, "buy", "limit", price="0.01", quantity="1")
print(client.cancel_order(a.id)) # one
print(client.cancel_orders([b.id, c.id])) # several by id
print(client.cancel_all_orders()) # everything left on the account

Each returns Cancellation objects with order_id and status. A cancel is a request: an order can fill between sending the cancel and the exchange processing it, so reconcile against fills rather than assuming you are flat.

from stx import STX
with STX() as client:
for order in client.orders(status=["open", "delayed"], limit=20):
print(order.id, order.market_id, order.action, order.price, order.filled, "/", order.quantity)
recent = client.orders(limit=1)
if recent.items:
print(client.order(recent[0].id).status)

Sample output, for a limit buy of 100 at 0.58 with 40 filled:

4702e9b6-211a-4805-a605-0eb0817ee9b3 e00651a3-10cb-4bc1-9e1f-c5ea1796c83b buy 0.5800 40.00 / 100.00
open

orders() filters on order_ids, client_order_ids, market_ids and status (one or a list of created, requested, accepted, delayed, open, filled, rejected, cancelled, partially_cancelled).

A POST is never retried after a server error or a dropped connection: the order may already be on the book. If that happens, look it up by client_order_id before placing again:

import uuid
from stx import STX, STXServerException, STXTransportException
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
coid = str(uuid.uuid4())
try:
order = client.place_order(
market.market_id, "buy", "limit", price="0.01", quantity="1", client_order_id=coid
)
except (STXServerException, STXTransportException):
found = client.orders(client_order_ids=[coid])
order = found[0] if found.items else None
print(order.id if order else "not placed")
if order:
client.cancel_order(order.id)

fill_mode="post_only" places a limit order that only ever rests on the book as a maker. If it would match on arrival, the exchange does not place it and the SDK raises STXPostOnlyWouldMatchException: reason is "auto_matching" (the exchange cancelled it; exc.order is the cancelled order) or "match_with_self" (the first order it would match is your own).

fill_mode="fill_or_kill" fills in full at your price or better, or not at all. The call returns the order accepted (or delayed in play); it then becomes filled, or cancelled with cancellation_reason "auto_matching" and nothing filled. Follow it on the orders channel or with client.order(order.id). How both behave on the exchange is in Order types.

from stx import STX, STXNotAvailableError, STXPostOnlyWouldMatchException
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
try:
order = client.place_order(
market.market_id, "buy", "limit", price="0.01", quantity="1", fill_mode="post_only"
)
if order.fill_mode != "post_only":
print("placed as a normal limit order:", order.id)
print(order.id, order.status, order.fill_mode)
client.cancel_order(order.id)
except STXPostOnlyWouldMatchException as exc:
print("would have taken liquidity:", exc.reason)
except STXNotAvailableError as exc:
print(exc)

Sample output, on an exchange that offers post-only orders:

78b3fce3-4825-48cc-89d8-3fbdf2ba26c6 accepted post_only

And on one that does not:

Post-only orders are not available on demo.stxapp.io yet.

The exchange decides whether it takes either mode; Order types says which exchanges offer them. The SDK sends the order as given. Where a mode is switched off, the exchange rejects the order and the call raises STXNotAvailableError with feature "orders.post_only" or "orders.fill_or_kill". An exchange on a release from before fill modes places the order as a normal limit order, so check order.fill_mode on the order that comes back. A combination market accepts only normal orders: a fill mode there raises STXFillModeNotAllowedException, which carries fill_mode. place_orders() takes fill_mode on each order and maps the same rejections; there a post-only order that would have matched comes back cancelled in its result.

expiration="good_till_start" pulls the order when the event starts. expiration="good_till_time" pulls it at expiration_time, in Unix microseconds:

import time
from stx import STX
with STX() as client:
market = next(
m
for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
)
if m.event_status == "scheduled"
)
in_one_hour = int((time.time() + 3600) * 1_000_000)
order = client.place_order(
market.market_id, "buy", "limit", price="0.01", quantity="1",
expiration="good_till_time", expiration_time=in_one_hour,
)
print(order.id, order.status)
client.cancel_order(order.id)

Cancel-on-disconnect takes two halves: join the orders channel with it armed, and place orders with cancel_on_disconnect=True. While the socket is up, the SDK pings the channel at a third of the timeout the server granted, so your orders stay on the book even if two pings are lost. If your process dies, the exchange cancels them. The timeout limits and the grace period are on Risk controls.

import asyncio
from stx import AsyncSTX
async def main():
async with AsyncSTX() as client:
market = None
async for m in client.iter_markets(
status="open", trading=True, sort_by="event_start", sort_direction="desc"
):
if m.event_status == "scheduled":
market = m
break
async with client.websocket() as ws:
orders = await ws.orders(cancel_on_disconnect=True, ping_timeout=5000)
print("armed, server timeout (ms):", orders.reply["ping_timeout"])
order = await client.place_order(
market.market_id, "buy", "limit", price="0.01", quantity="1",
cancel_on_disconnect=True,
)
await asyncio.sleep(8) # longer than the timeout: the pings keep it alive
print((await client.order(order.id)).status)
await client.cancel_order(order.id)
asyncio.run(main())

STX is operated by XV Exchange, LLC, a Designated Contract Market regulated by the U.S. Commodity Futures Trading Commission (CFTC). Contracts traded on STX are cleared by XV Clearing, LLC, a CFTC-registered Derivatives Clearing Organization. Trading involves substantial risk of loss and may not be appropriate for everyone. Members risk losing the cost to enter any transaction, including fees. Consider whether trading is appropriate for you in light of your experience and financial resources. Nothing on this site is investment advice or a recommendation to trade. Past performance is not indicative of future results.

© 2026 STX. All rights reserved.llms.txtllms-full.txtView as Markdown
Docs version v1.10.5