Blog
Account stateWebSockets

Initializing and Recovering Account State

Initialize account balances, positions, and orders without losing private stream updates, overwriting newer facts, or treating an unchanged value as stale.

Siebly.io3 min readMarkdown

Initialize Each Required Value

A wallet subscription can succeed without reporting any balances. If an application initializes its cache to zero and enables order sizing immediately, it treats missing data as an actual account value. A successful connection has established neither the balance nor the funds available for a new order.

Account State needs to track which required values have been initialized, alongside the values themselves.

A private subscription does not necessarily send an initial snapshot. For example, Bybit's wallet channel publishes changes and requires a separate initial read when the application needs an existing balance.

Attach handlers before subscribing. Track whether authentication and the required subscription succeeded, then obtain the missing values through the appropriate REST methods. During that read, incoming events must remain available to the initializer instead of being discarded.

Applying the response and then replaying every buffered event is safe only when the endpoint and stream provide rules that establish which update is newer. Without comparable versions, a snapshot can already include the effect of a buffered event. Adding that event again can double-count it. Absolute balance or position values also need different handling from incremental executions.

Distinguish Missing Balances from Zero

Consider an application that needs both USDT and BTC balances. It first requests only USDT. That response cannot establish the BTC balance, even if the response is otherwise successful:

ObservationUSDT stateBTC state
Subscription succeeds without a snapshotNot initializedNot initialized
Filtered wallet read returns USDT 500Known: 500Not initialized
Separate BTC read explicitly returns 0Known: 500Known: 0

A filtered read replaces only the scope it actually covers. An asset missing from a response becomes zero only if the endpoint documents that omission as zero for that request. A failed read leaves the required value unresolved.

The same rule applies to stream messages: a message replaces an entire collection only when the channel promises a complete snapshot. If a channel sends changed entries, merge those entries by their documented account and asset keys. Empty arrays and absent fields need the channel's own interpretation.

Orders have a separate lifecycle. The order-state article covers acceptance responses arriving after fills and repeated cumulative updates.

Recover the Affected Scope

A disconnect marks the affected subscription as needing recovery. A balance read may repair a balance; it does not prove that open orders or execution history are complete. Query the facts required by the consumer and apply the endpoint's ordering rules.

Each recovery attempt needs an identity. If a second attempt starts while an old request is running, its late response must not replace the new attempt's state. Reconnect recovery covers attempt tracking and bounded buffering.

An unchanged position does not need a new event every few seconds to remain readable. Missing initialization, a known gap, a failed subscription, or conflicting observations are reasons to repair it. Marked equity and current prices can have additional requirements because they change independently of a position's quantity.

Keep the Failure Visible

When ordering cannot be established, retain the uncertainty for that account and consumer. Do not manufacture a complete snapshot by combining responses from unrelated moments. Log the affected field, source, request attempt, and native identifiers so recovery can be investigated without exposing account secrets.

The exchange-state workflow provides the shared model. The exchange tutorial supplies its actual REST methods and topics; one venue's snapshot procedure should not be copied onto another.

Related articles

Continue from here

Related Siebly resources

All articles

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.