Skip to content

Trading

Placing and cancelling need a read_write key. The examples run on the US demo exchange with 1-cent buy orders, which usually rest unfilled; one can still fill if someone sells at that price before it is cancelled. 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, and whose event has not started:

using STX.Sdk;
using STX.Sdk.Models;
using var client = new STXClient();
Market? market = null;
await foreach (var m in client.GetAllMarketsAsync(status: ["open"], trading: true, sortBy: "event_start", sortDirection: "desc"))
{
if (m.EventStatus == "scheduled") { market = m; break; }
}
var marketId = market!.MarketId!; // "21ce76c1-52aa-492f-a20e-59fb82ad0552"
var order = await client.PlaceOrderAsync(marketId, STXOrderAction.Buy, STXOrderType.Limit,
quantity: "1", price: "0.01", clientOrderId: "my-first-order");
Console.WriteLine($"{order.Id} {order.Status} {order.Quantity} @ {order.Price}");
// 49c15a69-791c-4480-8719-9b222e4e0a5a accepted 1.00 @ 0.0100
await client.CancelOrderAsync(order.Id!);

action is buy or sell (STXOrderAction) and orderType limit or market (STXOrderType). The other parameters:

Parameter Type
quantity Contract string, e.g. "100"
price Dollar string, e.g. "0.58". Required for limit, left out for market
clientOrderId Your own id, echoed back and filterable
expiration, expirationTime See Expiration
cancelOnDisconnect See Cancel-on-disconnect

PlaceOrderAsync(new STXOrderRequest { ... }) takes the same fields as a record, plus FillMode; see Post-only and fill-or-kill.

If the exchange refuses the order, the call throws with the exchange’s message:

try
{
await client.PlaceOrderAsync(marketId, "buy", "limit", quantity: "1.5", price: "0.01");
}
catch (STXApiException ex) when (ex is STXRejectedException or STXValidationException)
{
Console.WriteLine($"{ex.StatusCode} {ex.Message}");
// 422 Invalid order fields: quantity - Fractional quantity is not allowed.
}

A limit order’s FillMode sets how it matches. The modes are described in Order types.

FillMode The order
STXFillMode.Normal (normal, the default) matches what it can and rests the rest
STXFillMode.PostOnly (post_only) rests only as a maker; it is not placed when it would match on arrival
STXFillMode.FillOrKill (fill_or_kill) fills in full or not at all
try
{
var maker = await client.PlaceOrderAsync(new STXOrderRequest
{
MarketId = marketId, Action = STXOrderAction.Buy, OrderType = STXOrderType.Limit,
Quantity = "1", Price = "0.01", FillMode = STXFillMode.PostOnly,
});
Console.WriteLine($"{maker.Id} {maker.Status} {maker.FillMode} @ {maker.Price}");
// 0f7d3a52-58c4-4f6b-a3e3-2b8e0c5b8e61 open post_only @ 0.0100
}
catch (STXPostOnlyWouldMatchException ex)
{
// Nothing traded: re-price below the best offer and place again.
Console.WriteLine($"would have matched ({ex.Reason})"); // would have matched (auto_matching)
}
catch (STXNotAvailableException ex)
{
Console.WriteLine($"{ex.Feature}: {ex.Message}"); // orders.post_only: Post-only orders are not available on stxapp.io yet.
}

What else can come back:

Outcome What you get
A post-only order would match another member’s order STXPostOnlyWouldMatchException, Reason auto_matching; Order is the cancelled order
A post-only order would match your own order STXPostOnlyWouldMatchException, Reason match_with_self (422)
The market does not take the mode, such as a combination market STXFillModeNotAllowedException (422), FillMode
A fill-or-kill order is placed the order accepted (or delayed); it then ends filled, or cancelled with nothing filled. Follow it on the orders channel or with GetOrderAsync
A market order with a fill mode STXValidationException (400)

Both exceptions derive from STXRejectedException. In PlaceOrdersAsync a mode that is switched off throws STXNotAvailableException for the batch, and a post-only order that would match comes back as its own result rather than an exception.

var results = await client.PlaceOrdersAsync([
new STXOrderRequest { MarketId = marketId, Action = "buy", OrderType = "limit", Price = "0.01", Quantity = "1" },
new STXOrderRequest { MarketId = marketId, Action = "buy", OrderType = "limit", Price = "0.02", Quantity = "1" },
]);
foreach (var r in results)
Console.WriteLine(r.Ok ? r.Order!.Id : string.Join("; ", r.Errors!));

Each result holds either Order or Errors, in the order you sent them. One rejection does not stop the rest.

await client.CancelOrderAsync(orderId); // one
await client.CancelOrdersAsync([idA, idB]); // several
await client.CancelAllOrdersAsync(); // everything on the account

Each returns cancellations with OrderId and Status: the order’s status after cancelling (cancelled, or partially_cancelled when part of it had already filled), or the reason it was not cancelled. An order can still fill while a cancel is in flight, so reconcile against fills.

await foreach (var o in client.GetAllOrdersAsync(status: ["created", "requested", "accepted", "delayed", "open"]))
Console.WriteLine($"{o.Id} {o.Price} {o.Filled} / {o.Quantity}");
var one = await client.GetOrderAsync(orderId);
Console.WriteLine($"{one.ClientOrderId} {one.Status} {one.AvgPrice}");

GetOrdersAsync filters on orderIds, clientOrderIds, marketIds and status. The five statuses above are every order still working: an order on a pre_open market waits at accepted, so open alone misses it. See order statuses.

The client never resends a POST after a server error or a dropped connection, because the order may already be on the book. Send a clientOrderId and look it up before trying again:

var clientOrderId = Guid.NewGuid().ToString();
Order? order;
try
{
order = await client.PlaceOrderAsync(marketId, "buy", "limit", quantity: "1", price: "0.01", clientOrderId: clientOrderId);
}
catch (STXException ex) when (ex is STXServerException or STXTransportException)
{
order = (await client.GetOrdersAsync(clientOrderIds: [clientOrderId])).FirstOrDefault();
}

The two expiration modes are described in Risk controls. In the SDK:

await client.PlaceOrderAsync(marketId, "buy", "limit", quantity: "1", price: "0.01", expiration: "good_till_start");
await client.PlaceOrderAsync(marketId, "buy", "limit", quantity: "1", price: "0.01",
expiration: "good_till_time",
expirationTime: DateTimeOffset.UtcNow.AddMinutes(10).ToUnixTimeMilliseconds() * 1000); // microseconds

How cancel-on-disconnect works is described in Risk controls. In the SDK, arm it on the orders channel and opt each order in. The socket then pings the channel for you, at a third of the timeout the exchange granted (every 1.7 seconds for 5000 ms), for as long as it is joined. If the exchange ends the channel anyway, the socket rejoins it and calls OnReconnect, so you can check which orders were cancelled.

await using var ws = client.CreateWebSocket();
var orders = await ws.OrdersAsync(cancelOnDisconnect: true, pingTimeout: 5000);
Console.WriteLine($"granted timeout (ms): {orders.Reply["ping_timeout"]}"); // granted timeout (ms): 5000
await client.PlaceOrderAsync(marketId, "buy", "limit", quantity: "1", price: "0.01", cancelOnDisconnect: true);

If the socket drops, the exchange cancels the orders placed with cancelOnDisconnect: true. Orders placed without it stay on the book.

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