Exemple #1
0
        public void SubscribeCandlesticks(string symbol, Interface.Model.CandlestickInterval candlestickInterval, int limit, Action <CandlestickEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            try
            {
                var interval         = candlestickInterval.ToBinanceCandlestickInterval();
                var canclestickCache = new CandlestickCache(binanceApi, new CandlestickWebSocketClient());
                canclestickCache.Subscribe(symbol, interval, limit, e =>
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        canclestickCache.Unsubscribe();
                        return;
                    }

                    try
                    {
                        var candlesticks = (from c in e.Candlesticks select NewCandlestick(c)).ToList();
                        callback.Invoke(new CandlestickEventArgs {
                            Candlesticks = candlesticks
                        });
                    }
                    catch (Exception ex)
                    {
                        canclestickCache.Unsubscribe();
                        exception.Invoke(ex);
                    }
                });
            }
            catch (Exception ex)
            {
                exception.Invoke(ex);
            }
        }
        public void SubscribeThrows()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ICandlestickWebSocketClient>().Object;

            var cache = new CandlestickCache(api, client);

            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(null, CandlestickInterval.Day));
        }
Exemple #3
0
        public void Unsubscribe()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ICandlestickClient>().Object;

            var cache = new CandlestickCache(api, client);

            // Can call unsubscribe before subscribe or multiple times without fail.
            cache.Unsubscribe();
            cache.Unsubscribe();
        }
        public async Task SubscribeThrows()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ICandlestickWebSocketClient>().Object;

            var cache = new CandlestickCache(api, client);

            await Assert.ThrowsAsync <ArgumentNullException>("symbol", () => cache.SubscribeAsync(null, CandlestickInterval.Day, new CancellationToken()));

            await Assert.ThrowsAsync <ArgumentException>("token", () => cache.SubscribeAsync(Symbol.BTC_USDT, CandlestickInterval.Day, CancellationToken.None));
        }
        public async Task StreamThrows()
        {
            var api    = new Mock <IBinanceApi>().Object;
            var client = new Mock <ICandlestickWebSocketClient>().Object;

            var cache = new CandlestickCache(api, client);

            using (var cts = new CancellationTokenSource())
            {
                await Assert.ThrowsAsync <ArgumentNullException>("symbol", () => cache.SubscribeAndStreamAsync(null, CandlestickInterval.Day, cts.Token));
            }
        }
Exemple #6
0
        public void SubscribeThrows()
        {
            var symbol   = Symbol.BTC_USDT;
            var interval = CandlestickInterval.Day;
            var api      = new Mock <IBinanceApi>().Object;
            var client   = new Mock <ICandlestickClient>().Object;

            var cache = new CandlestickCache(api, client);

            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(null, CandlestickInterval.Day));
            Assert.Throws <ArgumentNullException>("symbol", () => cache.Subscribe(string.Empty, CandlestickInterval.Day));

            cache.Subscribe(symbol, interval);

            Assert.Throws <InvalidOperationException>(() => cache.Subscribe(symbol, interval));
        }
Exemple #7
0
        public void LinkToClient()
        {
            var api      = new Mock <IBinanceApi>().Object;
            var client1  = new Mock <ICandlestickClient>().Object;
            var client2  = new CandlestickClient();
            var symbol1  = Symbol.BTC_USDT;
            var symbol2  = Symbol.LTC_BTC;
            var interval = CandlestickInterval.Hour;

            client2.Subscribe(symbol1, interval);

            var clientSubscribeStreams = client2.SubscribedStreams.ToArray();

            Assert.Equal(CandlestickClient.GetStreamName(symbol1, interval), clientSubscribeStreams.Single());

            var cache = new CandlestickCache(api, client1)
            {
                Client = client2 // link client.
            };

            // Client subscribed streams are unchanged after link to unsubscribed cache.
            Assert.Equal(clientSubscribeStreams, client2.SubscribedStreams);

            cache.Client = client1; // unlink client.

            // Subscribe cache to symbol.
            cache.Subscribe(symbol2, interval);

            // Cache is subscribed to symbol.
            Assert.Equal(CandlestickClient.GetStreamName(symbol2, interval), cache.SubscribedStreams.Single());

            cache.Client = client2; // link to client.

            // Client has second subscribed stream from cache.
            Assert.True(client2.SubscribedStreams.Count() == 2);
            Assert.Contains(CandlestickClient.GetStreamName(symbol2, interval), client2.SubscribedStreams);
        }
Exemple #8
0
        public Task SubscribeCandlesticks(string symbol, Core.Model.CandlestickInterval candlestickInterval, int limit, Action <CandlestickEventArgs> callback, Action <Exception> exception, CancellationToken cancellationToken)
        {
            var tcs = new TaskCompletionSource <object>();

            var binanceApi       = new BinanceApi();
            var interval         = candlestickInterval.ToBinanceCandlestickInterval();
            var candlestickCache = new CandlestickCache(binanceApi, new CandlestickWebSocketClient());

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

                var candlesticks = (from c in e.Candlesticks select NewCandlestick(c)).ToList();

                try
                {
                    callback.Invoke(new CandlestickEventArgs {
                        Candlesticks = candlesticks
                    });
                }
                catch (Exception ex)
                {
                    candlestickCache.Unsubscribe();
                    exception.Invoke(ex);
                    return;
                }
            });

            tcs.SetResult(null);

            return(tcs.Task);
        }