예제 #1
0
        private static async Task<ICryptoOrderBook> StartBinance(string pair, bool optimized, bool l2Optimized)
        {
            var url = BinanceValues.ApiWebsocketUrl;
            var communicator = new BinanceWebsocketCommunicator(url) { Name = "Binance" };
            var client = new BinanceWebsocketClient(communicator);

            client.SetSubscriptions(
                new OrderBookDiffSubscription(pair)
            );

            var source = new BinanceOrderBookSource(client);
            var orderBook = l2Optimized ? 
                new CryptoOrderBookL2(pair, source) : 
                (ICryptoOrderBook)new CryptoOrderBook(pair, source);

            if (optimized)
            {
                ConfigureOptimized(source, orderBook);
            }

            _ = communicator.Start();

            // Binance is special
            // We need to load snapshot in advance manually via REST call
            _ = source.LoadSnapshot(communicator, pair);

            return orderBook;
        }
        public async Task ConnectToSource_ShouldHandleOrderBookCorrectly()
        {
            var url = BinanceValues.ApiWebsocketUrl;

            using (var communicator = new BinanceWebsocketCommunicator(url))
            {
                using (var client = new BinanceWebsocketClient(communicator))
                {
                    var pair = "BTCUSDT";

                    client.SetSubscriptions(
                        new OrderBookDiffSubscription(pair)
                        );

                    var source    = new BinanceOrderBookSource(client);
                    var orderBook = new CryptoOrderBook(pair, source);

                    await communicator.Start();

                    // Binance is special
                    // We need to load snapshot in advance manually via REST call
                    await source.LoadSnapshot(pair);

                    await Task.Delay(TimeSpan.FromSeconds(5));

                    Assert.True(orderBook.BidPrice > 0);
                    Assert.True(orderBook.AskPrice > 0);

                    Assert.NotEmpty(orderBook.BidLevels);
                    Assert.NotEmpty(orderBook.AskLevels);
                }
            }
        }