Skip to main content
This page builds a program that places a post-only bid, modifies it and cancels it, then places two more and sweeps the contract with a cancel-all. Every command is paced to the budget of your account’s type. It picks up from the Quickstart.

Place an Order

CreateOrder takes an OrderID of your own, a ULID minted when empty. The program sets one and records it in live before the command goes out, so an order whose answer never arrives can still be found and cancelled. A refusal, or a command that never left, placed nothing:
An answer is not always a resting order, so read Order.Status. A command on a contract outside the connection’s scope is refused before it is sent, with options.ErrOutOfScope.

Modify an Order

A modify changes the order’s price and quantity in place, and keeps its id:

Cancel an Order

CancelOrder is answered without an acknowledgment, so its AckAt is zero. An order already gone is refused with options.CodeOrderNotFound. ViaHTTP is explained below.

Cancel Every Order

conn.CancelAllOrders sweeps one contract, or the connection’s whole scope when Contract is empty, and returns once the exchange has taken the sweep on; each cancel is then answered as its own options.OrderCancelled or options.OrderRejected event, and Count says how many to expect. The program matches them to its orders by id; the example also handles one filled before the sweep reached it:
With no connection at all, client.CancelAllOrders sweeps one contract, or the whole account when Contract is empty, over HTTP.

When the Socket Is Down

Cancels and reduce-only creates matter most when the connection is lost, so while the socket is down the SDK can send them over HTTP instead; other creates, and modifies, fail with the connection’s error. A command sent over HTTP has ViaHTTP set and is only queued: its outcome arrives as an event, as for a command another connection sent. The README has the exact conditions.

When a Command Gets No Answer

After rails.ErrTimeout or rails.ErrConnClosed a command may still have been applied, and its error carries the clientRequestId, a create’s also the OrderID it went out under. Either look the order up, as the example does, or send the command again with that ClientRequestID set: within 60 seconds the exchange refuses a repeat with a 409 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 cancels any it may still have on the book when it stops. Each cancel gets a context of its own, since the program’s may be what ran out:

Run the Example

The complete program is examples/options-orders in the SDK repository. Run it with a price well below the contract’s book: