Read the Account
The account reads are methods on the client: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 itsoptions.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 onconn.Events(). Read it steadily. The connection never waits for it: what does not fit in the channel’s buffer waits in the SDK, and once that backlog passes 16 times the buffer (16,384 events at the default WithEventBuffer of 1024), market data is dropped until it has been read. A MarketDataDropped event then says how many were dropped. Order events, account streams and connection events are never dropped.
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. EachEmit 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. Take one after options.MarketDataDropped too: it follows contract, quote, order book and public trade updates dropped while events were not read. 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:
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 isexamples/options-streams-and-account in the SDK repository. Run it for as long as you want to watch:
rebuilt line is the fresh session reporting that nothing was resumed.