Skip to main content
This page builds a program that places a post-only bid, replaces it with a larger one, and cancels it. Any command that gets no answer is reconciled instead of sent again, and every command is paced to the budget of your account’s type. It picks up from the Quickstart.

Place an Order

The request’s fields are those of Order Creation, and every answer about the order carries its ClientRequestID. Commands from any number of goroutines can share one connection.

Modify an Order

A modify replaces the order under a new id, and the result describes the replacement:

Cancel an Order

Cancelling is idempotent by order id, and CancelOrder resends in the one case where the exchange itself asks for the cancel again.

Cancel Every Order

To cancel every order on a market, or on all of them with perps.MarketAll, call client.CancelAllOrders. It goes over HTTP, so it works even when the WebSocket can’t be dialled during a soft ban.

When a Command Gets No Answer

After rails.ErrTimeout or rails.ErrConnClosed the command may still have been applied, and its error carries the clientRequestId. Either look the order up with it, as the example does, or send the command again with that ClientRequestID set: within 60 seconds the exchange refuses a repeat with DUPLICATE_REQUEST instead of executing it twice. Only the lookup says what happened to the order.

Clean Up After a Failure

Closing a connection doesn’t cancel its orders. The program keeps the id of the order it has on the book, set to the replacement’s id after the modify and cleared after the cancel, and cancels it if it stops before cancelling it itself. The cleanup gets a context of its own, since the program’s may be what ran out:

Run the Example

The complete program is examples/perps-orders in the SDK repository. Run it with a price well below the market: