Example #1
0
        public void OrderIdConverterSimpleTest(ulong apex, int market, OrderSide side)
        {
            var decoded = OrderIdConverter.Decode(OrderIdConverter.Encode(apex, market, side));

            Assert.AreEqual(apex, decoded.Apex);
            Assert.AreEqual(market, decoded.Asset);
            Assert.AreEqual(side, decoded.Side);
        }
Example #2
0
        public void OrderbookRemoveTest(bool isOrderedByPrice)
        {
            var orders = new List <Order>();
            var random = new Random();
            var price  = 1D;
            var side   = OrderSide.Buy;
            var asset  = 1;

            var orderbook   = Global.Exchange.GetOrderbook(asset, side);
            var ordersCount = 1000;

            for (var i = 1; i <= ordersCount; i++)
            {
                if (isOrderedByPrice)
                {
                    price = price * 1.01;
                }
                else
                {
                    price = 1 + random.NextDouble();
                }
                var orderId = OrderIdConverter.Encode((ulong)i, asset, side);
                var amount  = 1000;
                var order   = new Order {
                    OrderId = orderId, Amount = 1000, Price = price, QuoteAmount = OrderMatcher.EstimateQuoteAmount(amount, price, side)
                };
                orders.Add(order);
                orderbook.InsertOrder(order);
            }

            Func <int> getOrdersCount = () =>
            {
                var ordersCounter = 0;
                foreach (var o in orderbook)
                {
                    ordersCounter++;
                }
                return(ordersCounter);
            };

            var count = getOrdersCount();

            Assert.AreEqual(orderbook.Count, count, "Orderbook.Count and order-book items count are not equal.");
            Assert.AreEqual(ordersCount, count);

            foreach (var order in orders)
            {
                orderbook.RemoveOrder(order.OrderId);
                ordersCount--;
                Assert.AreEqual(ordersCount, orderbook.Count);
                Assert.AreEqual(ordersCount, getOrdersCount());
            }
            Assert.IsNull(orderbook.Head);
            Assert.IsNull(orderbook.Tail);
        }
        public async Task OrderCancellationQuantumTest(int asset, int amount, OrderSide side, int apexMod, bool useFakeSigner, Type excpectedException)
        {
            var acc = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair);

            var order = new OrderRequest
            {
                Account        = acc.Account.Id,
                RequestId      = 1,
                Amount         = amount,
                Asset          = asset,
                Price          = 100,
                Side           = side,
                AccountWrapper = acc
            };

            var envelope = order.CreateEnvelope().Sign(useFakeSigner ? TestEnvironment.Client2KeyPair : TestEnvironment.Client1KeyPair);

            if (!context.IsAlpha)
            {
                var quantum = new RequestQuantum {
                    Apex = context.QuantumStorage.CurrentApex + 1, RequestEnvelope = envelope, Timestamp = DateTime.UtcNow.Ticks
                };
                envelope = quantum.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            var submitResult = await AssertQuantumHandling(envelope, excpectedException);

            if (excpectedException != null)
            {
                return;
            }

            var apex = ((Quantum)submitResult.OriginalMessage.Message).Apex + apexMod;

            var orderCancellation = new OrderCancellationRequest
            {
                Account        = acc.Account.Id,
                RequestId      = 2,
                OrderId        = OrderIdConverter.Encode((ulong)apex, asset, side),
                AccountWrapper = context.AccountStorage.GetAccount(TestEnvironment.Client1KeyPair)
            };

            envelope = orderCancellation.CreateEnvelope().Sign(TestEnvironment.Client1KeyPair);

            if (!context.IsAlpha)
            {
                var quantum = new RequestQuantum {
                    Apex = context.QuantumStorage.CurrentApex + 1, RequestEnvelope = envelope, Timestamp = DateTime.UtcNow.Ticks
                };
                envelope = quantum.CreateEnvelope().Sign(TestEnvironment.AlphaKeyPair);
            }

            var cancelResult = await AssertQuantumHandling(envelope, excpectedException);

            if (excpectedException != null)
            {
                return;
            }
            var currentMarket = context.Exchange.GetMarket(asset);

            Assert.IsTrue(currentMarket != null);

            var requests = side == OrderSide.Buy ? currentMarket.Bids : currentMarket.Asks;

            Assert.AreEqual(requests.Count, 0);
        }