public void StreamingSnapshot_DifferentPairs_ShouldHandleCorrectly()
        {
            var pair1    = "BTC/USD";
            var pair2    = "ETH/BTC";
            var data1    = GetOrderBookSnapshotMockData(pair1, 500);
            var data2    = GetOrderBookSnapshotMockData(pair2, 200);
            var data     = data2.Concat(data1).ToArray();
            var now      = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123451);
            var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, data, CryptoOrderBookType.L2)
            {
                ExchangeName    = "test",
                ServerSequence  = 3,
                ServerTimestamp = now
            };
            var source = new OrderBookSourceMock(snapshot);

            ICryptoOrderBook orderBook1 = new CryptoOrderBook(pair1, source);
            ICryptoOrderBook orderBook2 = new CryptoOrderBook(pair2, source);

            orderBook1.OrderBookUpdatedStream.Subscribe(x =>
            {
                Assert.Equal("test", x.ExchangeName);
                Assert.Equal(3, x.ServerSequence);
                Assert.Equal(now, x.ServerTimestamp);
            });

            orderBook2.OrderBookUpdatedStream.Subscribe(x =>
            {
                Assert.Equal("test", x.ExchangeName);
                Assert.Equal(3, x.ServerSequence);
                Assert.Equal(now, x.ServerTimestamp);
            });

            source.StreamSnapshot();

            Assert.Equal(500, orderBook1.BidLevels.Length);
            Assert.Equal(500, orderBook1.AskLevels.Length);

            Assert.Equal(200, orderBook2.BidLevels.Length);
            Assert.Equal(200, orderBook2.AskLevels.Length);

            Assert.Equal(499, orderBook1.BidLevels.First().Price);
            Assert.Equal(1499, orderBook1.BidLevels.First().Amount);

            Assert.Equal(199, orderBook2.BidLevels.First().Price);
            Assert.Equal(599, orderBook2.BidLevels.First().Amount);

            Assert.Equal(501, orderBook1.AskLevels.First().Price);
            Assert.Equal(2501, orderBook1.AskLevels.First().Amount);

            Assert.Equal(201, orderBook2.AskLevels.First().Price);
            Assert.Equal(1001, orderBook2.AskLevels.First().Amount);

            var levels = orderBook1.Levels;

            foreach (var level in levels)
            {
                Assert.Equal(CryptoPairsHelper.Clean(pair1), level.Pair);
            }
        }
        public async Task Buffering_Disable_ShouldWork()
        {
            var now = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123451);

            var snapshotLevels = new[]
            {
                new OrderBookLevel("1", CryptoOrderSide.Bid, 100, 5, 1, "BTCUSD"),
                new OrderBookLevel("2", CryptoOrderSide.Ask, 101, 50, 2, "BTCUSD"),
            };
            var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, snapshotLevels, CryptoOrderBookType.L2)
            {
                ServerSequence  = 3,
                ServerTimestamp = now,
                ExchangeName    = "test"
            };

            var bulks = new[]
            {
                new OrderBookLevelBulk(OrderBookAction.Update, snapshotLevels, CryptoOrderBookType.L2)
                {
                    ServerSequence  = 4,
                    ServerTimestamp = now.AddMilliseconds(1),
                    ExchangeName    = "test"
                },
                new OrderBookLevelBulk(OrderBookAction.Delete, snapshotLevels, CryptoOrderBookType.L2)
                {
                    ServerSequence  = 5,
                    ServerTimestamp = now.AddMilliseconds(2),
                    ExchangeName    = "test"
                }
            };

            var source = GetMock(snapshot, bulks);

            source.BufferEnabled = false;

            OrderBookLevelBulk receivedSnapshot = null;

            OrderBookLevelBulk[] receivedBulks = null;

            source.OrderBookSnapshotStream.Subscribe(x => receivedSnapshot = x);
            source.OrderBookStream.Subscribe(x => receivedBulks            = x);

            await source.LoadSnapshot("BTCUSD");

            source.StreamData();

            Assert.NotNull(receivedSnapshot);
            Assert.NotNull(receivedBulks);

            Assert.Equal("test", receivedSnapshot.ExchangeName);
            Assert.Equal(3, receivedSnapshot.ServerSequence);
            Assert.Equal(now, receivedSnapshot.ServerTimestamp);

            Assert.Equal("test", receivedBulks[1].ExchangeName);
            Assert.Equal(5, receivedBulks[1].ServerSequence);
            Assert.Equal("1577575307.125451", receivedBulks[1].ServerTimestamp.ToUnixSecondsString());
        }
Example #3
0
        private static void TestDecimal(decimal?timeInSec, string result)
        {
            var converted       = CryptoDateUtils.ConvertFromUnixSeconds(timeInSec);
            var convertedBack   = converted.ToUnixSecondsDecimal();
            var convertedString = converted.ToUnixSecondsString();

            Assert.Equal(timeInSec, convertedBack);
            Assert.Equal(result, convertedString);
        }
Example #4
0
        public void UnixTimeConversion_ShouldSupportSixDecimalMilliseconds(double?timeInSec, string result)
        {
            var converted       = CryptoDateUtils.ConvertFromUnixSeconds(timeInSec);
            var convertedBack   = converted.ToUnixSeconds();
            var convertedString = converted.ToUnixSecondsString();

            Assert.Equal(timeInSec, convertedBack);
            Assert.Equal(result, convertedString);
        }
        public async Task Buffering_ShouldStreamOneByOne()
        {
            var now = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123456);

            var snapshotLevels = new[]
            {
                new OrderBookLevel("1", CryptoOrderSide.Bid, 100, 5, 1, "BTCUSD"),
                new OrderBookLevel("2", CryptoOrderSide.Ask, 101, 50, 2, "BTCUSD"),
            };
            var snapshot = new OrderBookLevelBulk(OrderBookAction.Insert, snapshotLevels, CryptoOrderBookType.L2)
            {
                ServerSequence  = 3,
                ServerTimestamp = now,
                ExchangeName    = "test"
            };

            var bulks = new[]
            {
                new OrderBookLevelBulk(OrderBookAction.Update, snapshotLevels, CryptoOrderBookType.L2)
                {
                    ServerSequence  = 4,
                    ServerTimestamp = now.AddMilliseconds(1),
                    ExchangeName    = "test"
                },
                new OrderBookLevelBulk(OrderBookAction.Delete, snapshotLevels, CryptoOrderBookType.L2)
                {
                    ServerSequence  = 5,
                    ServerTimestamp = now.AddMilliseconds(2),
                    ExchangeName    = "test"
                }
            };

            var source = GetMock(snapshot, bulks);

            source.BufferInterval = TimeSpan.FromMilliseconds(50);

            OrderBookLevelBulk receivedSnapshot = null;

            OrderBookLevelBulk[] receivedBulks = null;
            var receivedCount = 0;

            source.OrderBookSnapshotStream.Subscribe(x => receivedSnapshot = x);
            source.OrderBookStream.Subscribe(x =>
            {
                receivedBulks = x;
                receivedCount++;
                Thread.Sleep(2000);
            });

            await source.LoadSnapshot("BTCUSD");

            source.StreamData();

            Assert.NotNull(receivedSnapshot);
            Assert.Null(receivedBulks);
            Assert.Equal(0, receivedCount);

            await Task.Delay(100);

            Assert.NotNull(receivedSnapshot);
            Assert.NotNull(receivedBulks);
            Assert.Equal(1, receivedCount);

            source.StreamData();
            source.StreamData();
            await Task.Delay(100);

            Assert.Equal(1, receivedCount);

            source.StreamData();
            await Task.Delay(2200);

            Assert.Equal(2, receivedCount);
        }
Example #6
0
        public void StreamingSnapshot_DifferentPairsSeparately_ShouldHandleCorrectly()
        {
            var pair1 = "BTC/USD";
            var pair2 = "ETH/BTC";
            var data1 = GetOrderBookSnapshotMockData(pair1, 500);
            var data2 = GetOrderBookSnapshotMockData(pair2, 200);
            var now   = CryptoDateUtils.ConvertFromUnixSeconds(1577575307.123451);

            var snapshot1 = new OrderBookLevelBulk(OrderBookAction.Insert, data1, CryptoOrderBookType.L2)
            {
                ExchangeName    = "test",
                ServerSequence  = 4,
                ServerTimestamp = now.AddMilliseconds(1)
            };
            var snapshot2 = new OrderBookLevelBulk(OrderBookAction.Insert, data2, CryptoOrderBookType.L2)
            {
                ExchangeName    = "test",
                ServerSequence  = 5,
                ServerTimestamp = now.AddMilliseconds(2)
            };

            var source = new OrderBookSourceMock();

            var orderBook1 = new CryptoOrderBookL2(pair1, source);
            var orderBook2 = new CryptoOrderBookL2(pair2, source);

            orderBook1.OrderBookUpdatedStream.Subscribe(x =>
            {
                Assert.Equal("test", x.ExchangeName);
                Assert.Equal(4, x.ServerSequence);
                Assert.Equal("1577575307.124451", x.ServerTimestamp.ToUnixSecondsString());
            });

            orderBook2.OrderBookUpdatedStream.Subscribe(x =>
            {
                Assert.Equal("test", x.ExchangeName);
                Assert.Equal(5, x.ServerSequence);
                Assert.Equal("1577575307.125451", x.ServerTimestamp.ToUnixSecondsString());
            });

            source.StreamSnapshotRaw(snapshot1);
            source.StreamSnapshotRaw(snapshot2);

            Assert.Equal(500, orderBook1.BidLevels.Length);
            Assert.Equal(500, orderBook1.AskLevels.Length);

            Assert.Equal(200, orderBook2.BidLevels.Length);
            Assert.Equal(200, orderBook2.AskLevels.Length);

            Assert.Equal(499, orderBook1.BidLevels.First().Price);
            Assert.Equal(1499, orderBook1.BidLevels.First().Amount);

            Assert.Equal(199, orderBook2.BidLevels.First().Price);
            Assert.Equal(599, orderBook2.BidLevels.First().Amount);

            Assert.Equal(501, orderBook1.AskLevels.First().Price);
            Assert.Equal(2501, orderBook1.AskLevels.First().Amount);

            Assert.Equal(201, orderBook2.AskLevels.First().Price);
            Assert.Equal(1001, orderBook2.AskLevels.First().Amount);

            var levels = orderBook1.Levels;

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