Ejemplo n.º 1
0
        private static async Task <ICryptoOrderBook> StartBitmex(string pair, bool optimized, bool l2Optimized)
        {
            var url          = BitmexValues.ApiWebsocketUrl;
            var communicator = new BitmexWebsocketCommunicator(url)
            {
                Name = "Bitmex"
            };
            var client = new BitmexWebsocketClient(communicator);

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

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

            _ = communicator.Start();

            // Send subscription request to order book data
            client.Send(new Bitmex.Client.Websocket.Requests.BookSubscribeRequest(pair));

            return(orderBook);
        }
        public async Task AutoSnapshotReloading_ShouldWorkAfterTimeout()
        {
            var url = BitmexValues.ApiWebsocketUrl;

            using (var communicator = new BitmexWebsocketCommunicator(url))
            {
                using (var client = new BitmexWebsocketClient(communicator))
                {
                    var pair = "XBTUSD";

                    var source = new BitmexOrderBookSource(client)
                    {
                        LoadSnapshotEnabled = true
                    };
                    var orderBook = new CryptoOrderBook(pair, source)
                    {
                        SnapshotReloadTimeout = TimeSpan.FromSeconds(5),
                        SnapshotReloadEnabled = true
                    };

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

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

                    Assert.NotEmpty(orderBook.BidLevels);
                    Assert.NotEmpty(orderBook.AskLevels);
                }
            }
        }
        public async Task ConnectToSource_ShouldHandleOrderBookCorrectly()
        {
            var url = BitmexValues.ApiWebsocketUrl;

            using (var communicator = new BitmexWebsocketCommunicator(url))
            {
                using (var client = new BitmexWebsocketClient(communicator))
                {
                    var pair = "XBTUSD";

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

                    await communicator.Start();

                    client.Send(new BookSubscribeRequest(pair));

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

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

                    Assert.NotEmpty(orderBook.BidLevels);
                    Assert.NotEmpty(orderBook.AskLevels);
                }
            }
        }
        public async Task ConnectToSource_ShouldHandleOrderBookOneByOne()
        {
            var url = BitmexValues.ApiWebsocketUrl;

            using (var communicator = new BitmexWebsocketCommunicator(url))
            {
                using (var client = new BitmexWebsocketClient(communicator))
                {
                    var pair   = "XBTUSD";
                    var called = 0;

                    var source    = new BitmexOrderBookSource(client);
                    var orderBook = new CryptoOrderBook(pair, source)
                    {
                        DebugEnabled = true
                    };

                    orderBook.OrderBookUpdatedStream.Subscribe(x =>
                    {
                        called++;
                        Thread.Sleep(2000);
                    });

                    await communicator.Start();

                    client.Send(new BookSubscribeRequest(pair));

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

                    Assert.Equal(2, called);

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

                    Assert.Equal(3, called);
                }
            }
        }
Ejemplo n.º 5
0
        public async Task StreamFromFile_ShouldBeFast()
        {
            var pair         = "XBTUSD";
            var communicator = new RawFileCommunicator();

            communicator.FileNames = _rawFiles;

            var client = new BitmexWebsocketClient(communicator);
            var source = new BitmexOrderBookSource(client);

            source.LoadSnapshotEnabled = false;
            source.BufferEnabled       = false;

            var orderBook = new CryptoOrderBookL2(pair, source);

            orderBook.SnapshotReloadEnabled = false;
            orderBook.ValidityCheckEnabled  = false;

            var snapshotCount = 0;
            var diffCount     = 0;

            source.OrderBookSnapshotStream.Subscribe(x => snapshotCount++);
            source.OrderBookStream.Subscribe(x => diffCount++);

            var sw = Stopwatch.StartNew();
            await communicator.Start();

            sw.Stop();

            var elapsedMs = sw.ElapsedMilliseconds;
            var msg       = $"Processed snapshots: {snapshotCount}, diffs: {diffCount}, elapsed time was: {elapsedMs} ms";

            _output.WriteLine(msg);

            Assert.True(elapsedMs < 7000, msg);
        }
        public async Task StreamingFromFile_ShouldHandleCorrectly()
        {
            var pair         = "XBTUSD";
            var communicator = new RawFileCommunicator();

            communicator.FileNames = _rawFiles;

            var client = new BitmexWebsocketClient(communicator);
            var source = new BitmexOrderBookSource(client);

            source.LoadSnapshotEnabled = false;
            source.BufferEnabled       = false;

            var orderBook = new CryptoOrderBook(pair, source);

            orderBook.SnapshotReloadEnabled = false;
            orderBook.ValidityCheckEnabled  = false;

            var receivedUpdate = 0;
            IOrderBookChangeInfo lastReceivedUpdate = null;

            var receivedTopLevel = 0;
            IOrderBookChangeInfo lastReceivedTopLevel = null;

            var receivedBidAskUpdate = 0;
            IOrderBookChangeInfo lastReceivedBidAskUpdate = null;


            orderBook.OrderBookUpdatedStream.Subscribe(x =>
            {
                receivedUpdate++;
                lastReceivedUpdate = x;
            });
            orderBook.TopLevelUpdatedStream.Subscribe(x =>
            {
                receivedTopLevel++;
                lastReceivedTopLevel = x;
            });
            orderBook.BidAskUpdatedStream.Subscribe(x =>
            {
                receivedBidAskUpdate++;
                lastReceivedBidAskUpdate = x;
            });


            await communicator.Start();

            Assert.Equal(3284, orderBook.BidLevels.Length);
            Assert.Equal(4035, orderBook.AskLevels.Length);

            Assert.Equal(6249, orderBook.BidPrice);
            Assert.Equal(163556, orderBook.BidLevels.First().Amount);

            Assert.Equal(6249.5, orderBook.AskPrice);
            Assert.Equal(270526, orderBook.AskLevels.First().Amount);

            Assert.Equal(322703, receivedUpdate);
            Assert.Equal(6249, lastReceivedUpdate?.Quotes?.Bid);
            Assert.Equal(6249.5, lastReceivedUpdate?.Quotes?.Ask);
            Assert.Equal("8799374800", lastReceivedUpdate.Sources[0].Levels[0].Id);
            Assert.Equal(6252, lastReceivedUpdate.Sources[0].Levels[0].Price);

            Assert.Equal(79020, receivedTopLevel);
            Assert.Equal(6249, lastReceivedTopLevel?.Quotes?.Bid);
            Assert.Equal(6249.5, lastReceivedTopLevel?.Quotes?.Ask);
            Assert.Equal("8799375100", lastReceivedTopLevel.Sources[0].Levels[0].Id);
            Assert.Equal(6249, lastReceivedTopLevel.Sources[0].Levels[0].Price);

            Assert.Equal(584, receivedBidAskUpdate);
            Assert.Equal(6249, lastReceivedBidAskUpdate?.Quotes?.Bid);
            Assert.Equal(6249.5, lastReceivedBidAskUpdate?.Quotes?.Ask);
            Assert.Equal("8799375050", lastReceivedBidAskUpdate.Sources[0].Levels[0].Id);
            Assert.Equal(6249.5, lastReceivedBidAskUpdate.Sources[0].Levels[0].Price);

            var levels = orderBook.Levels;

            foreach (var level in levels)
            {
                Assert.Equal(CryptoPairsHelper.Clean(pair), level.Pair);
            }
        }