Skip to content

Realtime Caching

sonvister edited this page Mar 16, 2018 · 11 revisions

General Information

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.

Dependency Injection

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.

Automatic Streaming

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.

Error Handling

An Error event handler can be added to receive ErrorEventArgs events.

cache.Error += (s, e) => { /* ... */ };

Order Book Cache

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) => { /* ... */ };

Advanced Usage

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();
}

Aggregate Trade Cache

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) => { /* ... */ };

Advanced Usage

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();
}

Trade Cache

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) => { /* ... */ };

Advanced Usage

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();
}

Candlestick Cache

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) => { /* ... */ };

Advanced Usage

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();
}

24-Hour Statistics Cache

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) => { /* ... */ };

Advanced Usage

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();
}
Clone this wiki locally