Example #1
0
        public Task SubscribeTrades(string symbol, int limit, Action <TradeEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <object>();

            var binanceApi = new BinanceApi();
            var tradeCache = new TradeCache(binanceApi, new TradeWebSocketClient());

            tradeCache.Subscribe(symbol, limit, e =>
            {
                if (cancellationToken.IsCancellationRequested)
                {
                    tradeCache.Unsubscribe();
                    return;
                }

                var trades = e.Trades.Select(t => NewTrade(t)).ToList();

                try
                {
                    callback.Invoke(new TradeEventArgs {
                        Trades = trades
                    });
                }
                catch (Exception ex)
                {
                    tradeCache.Unsubscribe();
                    exception.Invoke(ex);
                    return;
                }
            });

            tcs.SetResult(null);

            return(tcs.Task);
        }
Example #2
0
        public void SubscribeTrades(string symbol, int limit, Action <TradeEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            try
            {
                var tradeCache = new TradeCache(binanceApi, new TradeWebSocketClient());
                tradeCache.Subscribe(symbol, limit, e =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        tradeCache.Unsubscribe();
                        return;
                    }

                    try
                    {
                        var trades = e.Trades.Select(t => NewTrade(t)).ToList();
                        callback.Invoke(new TradeEventArgs {
                            Trades = trades
                        });
                    }
                    catch (Exception ex)
                    {
                        tradeCache.Unsubscribe();
                        exception.Invoke(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                exception.Invoke(ex);
            }
        }
Example #3
0
        public void Unsubscribe()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ITradeClient>().Object;

            var cache = new TradeCache(api, client);

            // Can call unsubscribe before subscribe or multiple times without fail.
            cache.Unsubscribe();
            cache.Unsubscribe();
        }