Skip to main content
This page builds a program that reads your account over HTTP, then follows the nearest expiry of a market over the WebSocket: its quotes, mark prices and Greeks, and your positions and open orders. It picks up from the Quickstart.

Read the Account

The account reads are methods on the client:
The fields are those of Get Account Summary and Get Positions, and monetary amounts are USDT. Amounts are strings, as on the wire, so no precision is lost. OpenOrders, RecentOrders, Deposits and Withdrawals return one options.Page at a time. Pass Next back as the PageToken until it comes back empty. A page can hold fewer items than asked for and still not be the last.

Subscribe to Streams

A connection follows the contracts of its options.Scope, the SDK’s contract filter: a market, narrowed in turn to an expiration, a strike and a type, or every market with options.ScopeAll(). The program follows the nearest expiry, found as in the Quickstart. options.WithSubscriptions adds streams beyond the Order Management Stream for the connection’s scope:
conn.Subscribe and conn.Unsubscribe change the streams later, for the connection’s scope or any other. A broader scope replaces the narrower ones under it. The account summary is account-wide, so any scope subscribes it for every market. Subscriptions survive reconnects. One the exchange refuses to restore, such as one over the connection’s limit, arrives as an options.SubscriptionLost event and is no longer held.

Handle Events

Everything the connection receives arrives as a typed event on conn.Events(). The channel applies backpressure instead of dropping events, so it must be read: a consumer that stops reading stalls the connection.
Each stream has its event: A position that closes arrives once in a PositionsDelta, with Side options.PositionSideFlat. For the contracts the Order Management Stream covers, orders you placed elsewhere arrive too: a fill as options.OrderFilled, a refusal as options.OrderRejected, a cancellation as options.OrderCancelled.

Take Snapshots

A snapshot is the state a stream continues from, and it is delivered on its stream. Each Emit method therefore needs that stream held for a scope that covers what it names. Without one it returns options.ErrNotSubscribed, having sent nothing. Only the Order Management Stream is replayed after a reconnect, so take a new snapshot of every other stream on each options.Connected, which also arrives for the first socket. EmitQuotes returns one page of an expiry’s quotes at a time. A snapshot is a request on the connection. It is asked for off the event loop, which has to keep reading events while it waits:
The event loop reads the snapshot pages alongside conn.Events(). Updates keep arriving while a snapshot is on its way, so a quote from the snapshot replaces the one held only if it is newer:

After a Reconnect

options.SessionBackfill reports how the session was recovered, on the first connection and after each reconnect. Contracts the replay could not cover (limits) are listed in ExpiredMarkets. When the session could not be resumed, or any contract is listed there, rebuild your order state over HTTP, following Next through every page as above; the example reads the first:

Run the Example

The complete program is examples/options-streams-and-account in the SDK repository. Run it for as long as you want to watch:
The first rebuilt line is the fresh session reporting that nothing was resumed.