-
Notifications
You must be signed in to change notification settings - Fork 77
Realtime Caching
The in-memory cache classes are high-level implementations that utilize the respective WebSocket client to provide a local copy of the order book, trade history, price chart, etc. for a symbol that is also updated in real-time. Applications are notified of updates via an event handler, callback, or both.
NOTE: Multiple event listener handlers can be linked to the ...Cache
implementations though the Update
event.
As shown, each of the web socket caches can be instantiated using the dependency injection ServiceProvider
.
var cache = serviceProvider.GetService<IAggregateTradeWebSocketCache>();
For simplicity, the following examples use the default constructors to instantiate services.
The web socket caches use the web socket clients which include automatic stream control triggered by calls to subscribe/unsubscribe. Streaming begins with subscribe and ends with unsubscribe.
An Error
event handler can be added to receive ErrorEventArgs
events.
cache.Error += (s, e) => { /* ... */ };
Use an IOrderBookCache
(with an IDepthWebSocketClient
) to create a real-time, synchronized order book for a symbol. Refer to the BinanceMarketDepth sample for an additional example.
// Initialize cache.
var cache = new DepthWebSocketCache();
// Subscribe cache to symbol with limit and callback (begin streaming).
// NOTE: If no limit is provided (or limit = 0) then the order book is initialized with
// limit = 1000 and the diff. depth stream is used to keep order book up-to-date.
cache.Subscribe(symbol, limit, evt => {
// Handle order book event for symbol.
});
// ...
// Unsubscribe cache (end streaming).
cache.Unsubscribe();
Optionally, add an Update
event handler that receives OrderBookCacheEventArgs
events.
cache.Update += (s, e) => { /* ... */ };
View an example of instantiating the internal components of the web socket cache.
// Initialize cache.
var cache = new OrderBookCache();
// Initialize stream.
var webSocket = new BinanceWebSocketStream();
// Initialize controller.
using (var controller = new RetryTaskController(webSocket.StreamAsync))
{
// Optionally, add error event handler.
controller.Error += (s, e) => { /* ... */ };
// Subscribe to symbol with optional callback.
cache.Subscribe(symbol, limit, evt => {
// Handle order book event for symbol.
});
// Set stream URI using cache subscribed streams.
webSocket.Uri = BinanceWebSocketStream.CreateUri(cache);
// NOTE: This must be done after cache subscribe.
// Route stream messages to cache.
webSocket.Message += (s, e) => cache.HandleMessage(e.Subject, e.Json);
// Begin streaming.
controller.Begin();
// ...
// End streaming.
await controller.CancelAsync();
}
Use an IAggregateTradeCache
(with an IAggregateTradeWebSocketClient
) to create a real-time, synchronized trade history for a symbol. Refer to the BinanceTradeHistory sample for an additional example.
// Initialize cache.
var cache = new AggregateTradeWebSocketCache();
// Subscribe cache to symbol with limit and callback (begin streaming).
cache.Subscribe(symbol, limit, evt => {
// Handle aggregate trade event for symbol.
});
// ...
// Unsubscribe cache (end streaming).
cache.Unsubscribe();
Optionally, add an Update
event handler that receives AggregateTradeCacheEventArgs
events.
cache.Update += (s, e) => { /* ... */ };
View an example of instantiating the internal components of the web socket cache.
// Initialize cache.
var cache = new AggregateTradeCache();
// Initialize stream.
var webSocket = new BinanceWebSocketStream();
// Initialize controller.
using (var controller = new RetryTaskController(webSocket.StreamAsync))
{
// Optionally, add error event handler.
controller.Error += (s, e) => { /* ... */ };
// Subscribe to symbol with optional callback.
cache.Subscribe(symbol, limit, evt => {
// Handle aggregate trade event for symbol.
});
// Set stream URI using cache subscribed streams.
webSocket.Uri = BinanceWebSocketStream.CreateUri(cache);
// NOTE: This must be done after cache subscribe.
// Route stream messages to cache.
webSocket.Message += (s, e) => cache.HandleMessage(e.Subject, e.Json);
// Begin streaming.
controller.Begin();
// ...
// End streaming.
await controller.CancelAsync();
}
Use an ITradeCache
(with an ITradeWebSocketClient
) to create a real-time, synchronized trade history for a symbol.
// Initialize cache.
var cache = new TradeWebSocketCache();
// Subscribe cache to symbol with limit and callback (begin streaming).
cache.Subscribe(symbol, limit, evt => {
// Handle trade event for symbol.
});
// ...
// Unsubscribe cache (end streaming).
cache.Unsubscribe();
Optionally, add an Update
event handler that receives TradeCacheEventArgs
events.
cache.Update += (s, e) => { /* ... */ };
View an example of instantiating the internal components of the web socket cache.
// Initialize cache.
var cache = new TradeCache();
// Initialize stream.
var webSocket = new BinanceWebSocketStream();
// Initialize controller.
using (var controller = new RetryTaskController(webSocket.StreamAsync))
{
// Optionally, add error event handler.
controller.Error += (s, e) => { /* ... */ };
// Subscribe to symbol with optional callback.
cache.Subscribe(symbol, limit, evt => {
// Handle trade event for symbol.
});
// Set stream URI using cache subscribed streams.
webSocket.Uri = BinanceWebSocketStream.CreateUri(cache);
// NOTE: This must be done after cache subscribe.
// Route stream messages to cache.
webSocket.Message += (s, e) => cache.HandleMessage(e.Subject, e.Json);
// Begin streaming.
controller.Begin();
// ...
// End streaming.
await controller.CancelAsync();
}
Use an ICandlestickCache
(with an ICandlestickWebSocketClient
) to create a real-time, synchronized price chart for a symbol. Refer to the BinancePriceChart sample for an additional example.
// Initialize cache.
var cache = new CandlestickWebSocketCache();
// Subscribe cache to symbol with interval and callback (begin streaming).
cache.Subscribe(symbol, interval, evt => {
// Handle candlestick event for symbol.
});
// ...
// Unsubscribe cache (end streaming).
cache.Unsubscribe();
Optionally, add an Update
event handler that receives CandlestickCacheEventArgs
events.
cache.Update += (s, e) => { /* ... */ };
View an example of instantiating the internal components of the web socket cache.
// Initialize cache.
var cache = new CandlestickCache();
// Initialize stream.
var webSocket = new BinanceWebSocketStream();
// Initialize controller.
using (var controller = new RetryTaskController(webSocket.StreamAsync))
{
// Optionally, add error event handler.
controller.Error += (s, e) => { /* ... */ };
// Subscribe to symbol with optional callback.
cache.Subscribe(symbol, interval, evt => {
// Handle candlestick event for symbol.
});
// Set stream URI using cache subscribed streams.
webSocket.Uri = BinanceWebSocketStream.CreateUri(cache);
// NOTE: This must be done after cache subscribe.
// Route stream messages to cache.
webSocket.Message += (s, e) => cache.HandleMessage(e.Subject, e.Json);
// Begin streaming.
controller.Begin();
// ...
// End streaming.
await controller.CancelAsync();
}
Use an ISymbolStatisticsCache
(with an ISymbolStatisticsWebSocketClient
) to create a real-time, synchronized 24-hour statistics history for a symbol. Refer to the Binance24HourStatistics sample for an additional example.
// Initialize cache.
var cache = new SymbolStatisticsWebSocketCache();
// Subscribe cache to symbol with limit and callback (begin streaming).
cache.Subscribe(symbol, evt => {
// Handle 24-hour statistics event for symbol.
});
// ...
// Unsubscribe cache (end streaming).
cache.Unsubscribe();
Optionally, add an Update
event handler that receives SymbolStatisticsCacheEventArgs
events.
cache.Update += (s, e) => { /* ... */ };
View an example of instantiating the internal components of the web socket cache.
// Initialize cache.
var cache = new SymbolStatisticsCache();
// Initialize stream.
var webSocket = new BinanceWebSocketStream();
// Initialize controller.
using (var controller = new RetryTaskController(webSocket.StreamAsync))
{
// Optionally, add error event handler.
controller.Error += (s, e) => { /* ... */ };
// Subscribe to symbol with optional callback.
cache.Subscribe(symbol, evt => {
// Handle symbol statistics event for symbol.
});
// Set stream URI using cache subscribed streams.
webSocket.Uri = BinanceWebSocketStream.CreateUri(cache);
// NOTE: This must be done after cache subscribe.
// Route stream messages to cache.
webSocket.Message += (s, e) => cache.HandleMessage(e.Subject, e.Json);
// Begin streaming.
controller.Begin();
// ...
// End streaming.
await controller.CancelAsync();
}
- Verify connection to the Binance server (minimal examples).
- Get the market depth (order book) for a symbol.
- Maintain a real-time order book cache for a symbol.
- Get the aggregate trades for a symbol.
- Maintain a real-time trade history cache for a symbol.
- Get the candlesticks for a symbol.
- Maintain a real-time price chart cache for a symbol.
- Get the 24-hour statistics for a symbol.
- Get current prices for all symbols for a price ticker.
- Get best price and quantity on the order book for all symbols.
- Get a list of all current symbols.
- Place a LIMIT order.
- Place a MARKET order.
- Place a TEST order to verify client order properties.
- Look-up an existing order to check status.
- Cancel an open order.
- Get all open orders for a symbol.
- Get all orders for a symbol.
- Get current account information.
- Get account trades for a symbol.
- Submit a withdraw request.
- Get deposit history.
- Get withdraw history.
- Donate BTC to the creator of this library.