developers:websocket:accounts

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
developers:websocket:accounts [2025/03/14 23:04] chaddevelopers:websocket:accounts [2025/03/15 13:18] (current) chad
Line 23: Line 23:
  
 Once subscribed, the server will begin streaming real-time updates for the selected accounts. Once subscribed, the server will begin streaming real-time updates for the selected accounts.
 +
 +
  
 ===== Handling Account Updates ===== ===== Handling Account Updates =====
Line 28: Line 30:
 The server sends account-related updates as separate messages. These include: The server sends account-related updates as separate messages. These include:
  
 +  * **AccountSubscribeResponse** – A response message indicating whether the account subscription was successful or not.
 +  * **AccountSnapshot** – A batch message containing the latest state of the account.
   * **AccountDetails** – Provides static information about the account.   * **AccountDetails** – Provides static information about the account.
   * **AccountUpdate** – Sends real-time updates for account values.   * **AccountUpdate** – Sends real-time updates for account values.
   * **AccountPosition** – Reports current open positions.   * **AccountPosition** – Reports current open positions.
-  * **AccountSnapshot** – A batch message containing the latest state of the account. 
   * **OrderUpdateMulti** – Sends batch updates for a position.   * **OrderUpdateMulti** – Sends batch updates for a position.
 +  * **MarketDetails** – Market details are always sent before a new position is sent to ensure the client has the altest pricing details for the market.
  
 Example message handling structure: Example message handling structure:
  
-private void HandleAccountMessage(ServerMessage serverMessage) { switch (serverMessage.PayloadCase) { case ServerMessage.PayloadOneofCase.AccountSubscribeResponse: _logger.LogInformation("Received account subscribe response. Success: {Success}", serverMessage.AccountSubscribeResponse.Success); break;+<code c#> 
 +private void HandleAccountMessage(ServerMessage serverMessage) 
 +{ 
 +    switch (serverMessage.PayloadCase) 
 +    { 
 +        case ServerMessage.PayloadOneofCase.AccountSubscribeResponse: 
 +            _logger.LogInformation("Received account subscribe response. Success: {Success}", serverMessage.AccountSubscribeResponse.Success); 
 +            serverMessage.AccountSubscribeResponse.Errors.ToList().ForEach(e => _logger.LogWarning(" * " + e)); 
 +            break;
  
-cpp +        case ServerMessage.PayloadOneofCase.AccountDetails: 
-Copy +            ProcessAccountDetails(serverMessage.AccountDetails); 
-Edit +            break;
-    case ServerMessage.PayloadOneofCase.AccountDetails: +
-    case ServerMessage.PayloadOneofCase.AccountUpdate: +
-    case ServerMessage.PayloadOneofCase.AccountPosition: +
-    case ServerMessage.PayloadOneofCase.AccountSnapshot: +
-    case ServerMessage.PayloadOneofCase.OrderUpdateMulti: +
-        HandleAccountUpdate(serverMessage); +
-        break;+
  
-    case ServerMessage.PayloadOneofCase.MarketDetails+        case ServerMessage.PayloadOneofCase.AccountUpdate
-        _logger.LogInformation("Received market details: {MarketId}", serverMessage.MarketDetails.MarketId); +            ProcessAccountUpdate(serverMessage.AccountUpdate); 
-        break;+            break;
  
-    default+        case ServerMessage.PayloadOneofCase.AccountPosition: 
-        _logger.LogWarning("Unhandled Account Message: {PayloadCase}", serverMessage.PayloadCase); +            ProcessAccountPosition(serverMessage.AccountPosition); 
-        break; +            break; 
-}+ 
 +        case ServerMessage.PayloadOneofCase.AccountSnapshot: 
 +            ProcessAccountSnapshot(serverMessage.AccountSnapshot); 
 +            break; 
 + 
 +        case ServerMessage.PayloadOneofCase.OrderUpdateMulti: 
 +            ProcessOrderUpdateMulti(serverMessage.OrderUpdateMulti); 
 +            break; 
 + 
 +        case ServerMessage.PayloadOneofCase.MarketDetails: 
 +            ProcessMarketDetails(serverMessage.MarketDetails); 
 +            break; 
 + 
 +        default: 
 +            _logger.LogWarning("Unhandled Account Message: {PayloadCase}", serverMessage.PayloadCase); 
 +            break; 
 +    }
 } }
 +</code>
 +
  
-markdown 
-Copy 
-Edit 
  
 ===== Account Snapshot ===== ===== Account Snapshot =====
  
-The **AccountSnapshot** message is a **batch message** that contains the latest state of an account. Instead of sending individual updates for each position or order, the snapshot provides a consolidated view of:+The **AccountSnapshot** message is a **batch message** that contains the latest state of an account including:
  
   * Account balances   * Account balances
Line 71: Line 91:
   * Working orders   * Working orders
  
-Clients should process **AccountSnapshot** messages carefully as they represent the most recent known state of +<code c#> 
 +private void ProcessAccountSnapshot(AccountSnapshot snapshotMessage) 
 +
 +      switch (snapshotMessage.PayloadCase) 
 +      { 
 +            case AccountSnapshotMessage.PayloadOneofCase.AccountDetails: 
 +                 _details = snapshotMessage.AccountDetails; 
 +                 break; 
 + 
 +            case AccountSnapshotMessage.PayloadOneofCase.AccountUpdate: 
 +                 _update = snapshotMessage.AccountUpdate; 
 +                 break; 
 + 
 +            case AccountSnapshotMessage.PayloadOneofCase.AccountPosition: 
 +                 { 
 +                      var position = _positions.AddOrUpdate( 
 +                           snapshotMessage.AccountPosition.MarketId, 
 +                           // Add new Position if key doesn't exist 
 +                           _ => new Position(this, snapshotMessage.AccountPosition, _loggerFactory.CreateLogger<Position>()), 
 +                           // Update existing Position 
 +                           (_, existingPosition) => existingPosition); 
 + 
 +                      if (position != null) 
 +                      { 
 +                           position.UpdateWithMessage(snapshotMessage); 
 +                           updatedPositions.Add(position); 
 +                      } 
 + 
 +                      _logger.LogInformation("=> SNAPSHOT => AccountPosition: {AccountId} / {MarketID}", snapshotMessage.AccountPosition.AccountId, snapshotMessage.AccountPosition.MarketId); 
 +                 } 
 +                 break; 
 + 
 +            case AccountSnapshotMessage.PayloadOneofCase.OrderUpdateMulti: 
 +                 var orderMultiUpdates = ProcessOrderUpdateMulti(snapshotMessage.OrderUpdateMulti); 
 +                 updatedOrders.AddRange(orderMultiUpdates.Select(res => res.Order)); 
 +                 updatedTrades.AddRange(orderMultiUpdates.SelectMany(res => res.Trades)); 
 +                 break; 
 + 
 +            default: 
 +                 _logger.LogInformation($" > Unhandled Snapshot Message: {snapshotMessage.PayloadCase}"); 
 +                 break; 
 +     } 
 + 
 +
 +</code> 
 + 
  • developers/websocket/accounts.1741993467.txt.gz
  • Last modified: 2025/03/14 23:04
  • by chad