Subscribe to live order book data using ExchangeOrderBookFactory
ExchangeOrderBookFactory creates locally-maintained order book instances (ISymbolOrderBook) for any supported exchange. Each order book opens a WebSocket subscription and continuously applies incremental updates, so the Asks and Bids collections always reflect the current state of the market.
The Create(string exchange, SharedSymbol symbol, ...) overload resolves the correct depth and options for the target exchange automatically.
using CryptoExchange.Net.SharedApis;var symbol = new SharedSymbol(TradingMode.Spot, "ETH", "USDT");// Create a Binance order book for ETH/USDTISymbolOrderBook? book = factory.Create("Binance", symbol, minimalDepth: 10);if (book is null){ Console.WriteLine("Exchange or symbol not supported"); return;}
The minimalDepth parameter is a hint: the factory selects the smallest supported depth that satisfies the request. For example, Binance supports depths of 5, 10, and 20 — requesting minimalDepth: 10 results in a 10-level book.
var symbol = new SharedSymbol(TradingMode.Spot, "BTC", "USDT");// Returns an ISymbolOrderBook[] for every exchange that supports this symbolISymbolOrderBook[] books = factory.Create(symbol, minimalDepth: 10);Console.WriteLine($"Created {books.Length} order books");
using CryptoClients.Net;using CryptoExchange.Net.SharedApis;var factory = new ExchangeOrderBookFactory(/* inject or construct per-exchange factories */);var symbol = new SharedSymbol(TradingMode.Spot, "ETH", "USDT");var book = factory.Create("Binance", symbol, minimalDepth: 10)!;book.OnOrderBookUpdate += (_) =>{ Console.WriteLine($"Spread: {book.BestAsk?.Price - book.BestBid?.Price:F2}");};var result = await book.StartAsync();if (!result.Success){ Console.WriteLine($"Error: {result.Error}"); return;}Console.WriteLine($"Order book started — {book.Asks.Count()} ask levels, {book.Bids.Count()} bid levels");Console.ReadLine();await book.StopAsync();
Order books maintain synchronization automatically. If the WebSocket connection drops, the book will reconnect and resynchronize without any manual intervention.
Use minimalDepth rather than hardcoding exchange-specific depth values. The factory knows which depths each exchange actually supports and will pick the closest valid option.