Describes github.com/stxapp/stx-go v0.3.1, the current release.
Placing and cancelling need 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, and whose event has not started:
var marketID stringfor m, err := range client.Markets.All(ctx, &stx.MarketsQuery{ Status: []string{"open"}, Trading: stx.Bool(true), SortBy: "event_start", SortDirection: "desc",}) { if err != nil { log.Fatal(err) } if m.EventStatus == "scheduled" { marketID = m.MarketID // "e00651a3-10cb-4bc1-9e1f-c5ea1796c83b" break }}Place an order
Section titled “Place an order”order, err := client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), ClientOrderID: "my-first-order",})if err != nil { log.Fatal(err)}fmt.Println(order.ID, order.Status)// 8f023ead-6f2d-4cf5-9931-cd217e44a2ee accepted
if _, err := client.Orders.Cancel(ctx, order.ID); err != nil { log.Fatal(err)}stx.OrderRequest fields:
| Field | Meaning |
|---|---|
MarketID |
The market |
Action |
stx.Buy or stx.Sell |
OrderType |
stx.Limit or stx.MarketOrder |
Price |
Dollars, e.g. stx.MustDecimal("0.58"). Required for a limit order, left empty for a market order |
Quantity |
Contracts, e.g. stx.MustDecimal("100"). Required |
ClientOrderID |
Your own id, echoed back and filterable |
Expiration, ExpirationTime |
See Expiration |
CancelOnDisconnect |
See Cancel-on-disconnect |
DeviceID |
Optional label |
FillMode |
stx.PostOnly or stx.FillOrKill on a limit order; see Post-only and fill-or-kill |
A missing market, action, order type or quantity, a limit order without a price, or a value that is not a decimal is refused before anything is sent. If the exchange refuses the order, the error carries the exchange’s message:
_, err := client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1.5"),})var apiErr *stx.APIErrorif errors.As(err, &apiErr) && (errors.Is(err, stx.ErrRejected) || errors.Is(err, stx.ErrBadRequest)) { fmt.Println(apiErr.StatusCode, apiErr.Message) // 422 Invalid order fields: quantity - Fractional quantity is not allowed.}A market order leaves out Price:
_, err := client.Orders.Place(ctx, stx.OrderRequest{MarketID: marketID, Action: stx.Buy, OrderType: stx.MarketOrder, Quantity: stx.MustDecimal("1")})Post-only and fill-or-kill
Section titled “Post-only and fill-or-kill”A limit order’s FillMode sets how it matches. The modes are described in Order types.
FillMode |
The order |
|---|---|
stx.FillNormal (normal, the default) |
matches what it can and rests the rest |
stx.PostOnly (post_only) |
rests only as a maker; it is not placed when it would match on arrival |
stx.FillOrKill (fill_or_kill) |
fills in full or not at all |
maker, err := client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), FillMode: stx.PostOnly,})var wouldMatch *stx.PostOnlyWouldMatchErrorswitch {case err == nil: fmt.Println(maker.ID, maker.Status, maker.FillMode, maker.Price) // 0f7d3a52-58c4-4f6b-a3e3-2b8e0c5b8e61 open post_only 0.0100case errors.As(err, &wouldMatch): // Nothing traded: re-price below the best offer and place again. fmt.Println("would have matched:", wouldMatch.Reason) // would have matched: auto_matchingcase errors.Is(err, stx.ErrNotAvailable): fmt.Println(err) // stx: Post-only orders are not available on stxapp.io yet.default: log.Fatal(err)}The client sends the order as it is and the exchange decides. Where a mode is not enabled, the exchange rejects the order and the call returns a *stx.NotAvailableError (Feature orders.post_only or orders.fill_or_kill, matching stx.ErrNotAvailable). Which exchanges offer each mode is listed in Order types.
Warning: an exchange without fill modes may place the order as a normal limit order instead of rejecting it. A post-only order can then match and trade as a taker, and a fill-or-kill order can fill in part and rest the remainder. Check the returned order’s FillMode: when it is not the mode you sent, the exchange treated it as a normal order, so cancel it if that is not what you want.
o, err := client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), FillMode: stx.PostOnly,})if err == nil && o.FillMode != stx.PostOnly { // Placed as a normal order: cancel it. _, err = client.Orders.Cancel(ctx, o.ID)}client.SupportsFillMode and the capability ids orders.post_only and orders.fill_or_kill are deprecated: the exchange does not publish which fill modes it takes, so SupportsFillMode always returns true and Capabilities reports both ids as unknown (nil). Send the order and handle the answer as above.
What else can come back:
| Outcome | What you get |
|---|---|
| A post-only order would match another member’s order | *stx.PostOnlyWouldMatchError, Reason auto_matching; Order is the cancelled order |
| A post-only order would match your own order | *stx.PostOnlyWouldMatchError, Reason match_with_self; it wraps the 422 |
| The market does not take the mode, such as a combination market | an *stx.APIError (422) matching stx.ErrFillModeNotAllowed |
| 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 Orders.Get |
| A market order with a fill mode | an error matching stx.ErrBadRequest |
*stx.PostOnlyWouldMatchError matches stx.ErrPostOnlyWouldMatch and stx.ErrRejected. In Orders.PlaceBatch a mode that is not enabled returns the same *stx.NotAvailableError for the batch, and a post-only order that would match comes back as its own result rather than an error.
Place several orders
Section titled “Place several orders”results, err := client.Orders.PlaceBatch(ctx, []stx.OrderRequest{ {MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1")}, {MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.02"), Quantity: stx.MustDecimal("1")},})if err != nil { log.Fatal(err)}for _, r := range results { if r.OK() { fmt.Println("placed", r.Order.ID) } else { fmt.Println("refused", r.Errors) }}Each result holds either Order or Errors, in the order you sent them. One rejection does not stop the rest.
Cancel
Section titled “Cancel”one, err := client.Orders.Cancel(ctx, orderID)fmt.Println(one.OrderID, one.Status)// 8f023ead-6f2d-4cf5-9931-cd217e44a2ee cancelled
several, err := client.Orders.CancelBatch(ctx, []string{"8f023ead-6f2d-4cf5-9931-cd217e44a2ee", "09b3c058-f9c0-4b0d-a6e9-f1295b7f0b33"})for _, c := range several { fmt.Println(c.OrderID, c.Status)}// 8f023ead-6f2d-4cf5-9931-cd217e44a2ee cancelled// 09b3c058-f9c0-4b0d-a6e9-f1295b7f0b33 Order not found
everything, err := client.Orders.CancelAll(ctx)fmt.Println(everything)// [{4702e9b6-211a-4805-a605-0eb0817ee9b3 partially_cancelled}]Each returns cancellations with OrderID and Status: the order’s status after cancelling (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.
Read orders
Section titled “Read orders”working := &stx.OrdersQuery{Status: []string{"created", "requested", "accepted", "delayed", "open"}}for o, err := range client.Orders.All(ctx, working) { if err != nil { log.Fatal(err) } fmt.Println(o.ID, o.Price, o.Filled, "/", o.Quantity)}// 4702e9b6-211a-4805-a605-0eb0817ee9b3 0.5800 40.00 / 100.00
one, err := client.Orders.Get(ctx, orderID)if err != nil { log.Fatal(err)}fmt.Println(one.ClientOrderID, one.Status, one.AvgPrice) // my-order-001 open 0.5800stx.OrdersQuery 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.
Avoid duplicate orders
Section titled “Avoid duplicate orders”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:
req := stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), ClientOrderID: "rebalance-2026-11-11-0001",}order, err := client.Orders.Place(ctx, req)if errors.Is(err, stx.ErrServer) || errors.Is(err, stx.ErrTransport) { page, lookupErr := client.Orders.List(ctx, &stx.OrdersQuery{ClientOrderIDs: []string{req.ClientOrderID}}) if lookupErr != nil { log.Fatal(lookupErr) } if len(page.Items) > 0 { order, err = &page.Items[0], nil // it was placed after all }}if err != nil { log.Fatal(err)}fmt.Println(order.ID)Expiration
Section titled “Expiration”The two expiration modes are described in Risk controls. In the SDK:
_, err := client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), Expiration: stx.GoodTillStart,})
_, err = client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), Expiration: stx.GoodTillTime, ExpirationTime: time.Now().Add(10 * time.Minute).UnixMicro(),})Cancel-on-disconnect
Section titled “Cancel-on-disconnect”How cancel-on-disconnect works is described in Risk controls. In the SDK, arm it on the orders channel of a stream and opt each order in. The stream then keeps pinging the channel for you while the socket is up, at a third of the granted timeout, so two pings can be lost before the exchange cancels your orders.
stream, err := client.Stream(ctx)if err != nil { log.Fatal(err)}defer stream.Close()
orders, err := stream.Subscribe(ctx, stx.OrdersChannel(stx.OrdersFilter{CancelOnDisconnect: true, PingTimeout: 5 * time.Second}))if err != nil { log.Fatal(err)}var granted struct { PingTimeout int `json:"ping_timeout"`}if err := orders.DecodeReply(&granted); err != nil { log.Fatal(err)}fmt.Println("granted timeout (ms):", granted.PingTimeout) // granted timeout (ms): 5000
_, err = client.Orders.Place(ctx, stx.OrderRequest{ MarketID: marketID, Action: stx.Buy, OrderType: stx.Limit, Price: stx.MustDecimal("0.01"), Quantity: stx.MustDecimal("1"), CancelOnDisconnect: stx.Bool(true),})
