Пример #1
0
        public void PartialFillAdjustsLastTradedTime()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 10;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            const string orderID = "FakeOrderID";

            iceberg.ActivatedMarketOrderAccepted(orderID);

            const decimal fillQ = 5;

            Assert.IsNull(iceberg.LastTradedTime);
            iceberg.OnPartialFill(fillQ);
            Assert.NotNull(iceberg.LastTradedTime);

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());
        }
Пример #2
0
        public void SecondTotalFillBeforeFirstFillMarketOrderAcceptedThrows()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 5;
            const decimal delta  = 1;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var iceberg = new IcebergOrder(mockServer.Object,
                                           expected.Symbol,
                                           expected.ClOrdID,
                                           expected.Side,
                                           expected.Quantity,
                                           clipQ,
                                           expected.Price,
                                           delta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            Assert.Throws <ApplicationException>(iceberg.OnTotalFill);

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));
        }
Пример #3
0
        public void PartialFillForGreaterThanOrEqualToCurrentQuantityThrows(decimal orderQty,
                                                                            decimal fillQty)
        {
            var totalQ = orderQty;
            var clipQ  = orderQty;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            const string orderID = "FakeOrderID";

            iceberg.ActivatedMarketOrderAccepted(orderID);

            Assert.Throws <ApplicationException>(() => iceberg.OnPartialFill(fillQty));

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());
        }
Пример #4
0
        public void ThrowsIfActivatedWhenAlreadyActive()
        {
            var expected   = DefaultFakeOrderRecord();
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var           clipSize   = expected.Quantity;
            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipSize,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            Assert.Throws <ApplicationException>(iceberg.Activate);
        }
Пример #5
0
        public void WhenMarketOrderAcceptedOrderIDOfIcebergIsUpdated()
        {
            var expected   = DefaultFakeOrderRecord();
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var           clipSize   = expected.Quantity;
            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipSize,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());

            const string orderID = "FakeOrderID";

            iceberg.ActivatedMarketOrderAccepted(orderID);
            Assert.AreEqual(IcebergOrder.ActivationState.Active,
                            iceberg.State,
                            "Order should be active");
            Assert.AreEqual(orderID,
                            iceberg.OrderID,
                            "OrderID not set correct when market order accepted");
        }
Пример #6
0
        public void TotalFillWithDeltaAdjustsPriceAsWellAsRefilling(MarketSide side,
                                                                    decimal delta,
                                                                    decimal price,
                                                                    decimal priceAfterFill)
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 10;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ, price: price, side: side);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var iceberg = new IcebergOrder(mockServer.Object,
                                           expected.Symbol,
                                           expected.ClOrdID,
                                           expected.Side,
                                           expected.Quantity,
                                           clipQ,
                                           price,
                                           delta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));

            Assert.AreEqual(clipQ, iceberg.CurrentQuantity, "Order not refilled to clip size");
            Assert.AreEqual(totalQ - clipQ,
                            iceberg.RemainingQuantity,
                            "Remaining quantity should be total minus one clip");

            Assert.AreEqual(priceAfterFill,
                            iceberg.CurrentPrice,
                            "Current price not as expected after fill with delta");
            Assert.AreNotEqual(iceberg.InitialPrice,
                               iceberg.CurrentPrice,
                               "Current price should not equal initial price after fill with delta");
        }
Пример #7
0
        public void SettingNewPriceWhileOrderActiveResultsInMarketOrderCancelAndCreatesNewOrder()
        {
            const decimal totalQ = 25;
            const decimal clipQ  = 10;

            const string orderID = "OrderID";

            var expected = DefaultFakeOrderRecord(quantity: totalQ);

            var newPrice = expected.Price + 10;

            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var updatedOrders = new UpdatedOrders();

            mockServer.Setup(s => s.UpdateOrder(It.IsAny <OrderRecord>(), It.IsAny <OrderRecord>()))
            .Returns(true)
            .Callback <OrderRecord, OrderRecord>(updatedOrders.AddUpdate);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();
            iceberg.ActivatedMarketOrderAccepted(orderID);

            iceberg.SetNewOrderPrice(newPrice);

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());

            mockServer.Verify(s => s.UpdateOrder(It.IsAny <OrderRecord>(), It.IsAny <OrderRecord>()),
                              Times.Once());

            Assert.AreEqual(1,
                            updatedOrders.OldRecords.Count,
                            "Incorrect number of old orders in update callback");
            var old = updatedOrders.OldRecords[0];

            Assert.AreEqual(expected.ClOrdID,
                            old.ClOrdID,
                            "Incorrect ClOrdID for old record when updating");

            Assert.AreEqual(1,
                            updatedOrders.NewRecords.Count,
                            "Incorrect number of new orders in update callback");
            var update = updatedOrders.NewRecords[0];

            Assert.AreEqual(newPrice, update.Price, "Updated order price incorrect");
        }
Пример #8
0
        public void TwoRefillsWithDeltaAdjustsPriceTwiceAndReducesQtyTwice()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 5;
            const decimal delta  = 1;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var iceberg = new IcebergOrder(mockServer.Object,
                                           expected.Symbol,
                                           expected.ClOrdID,
                                           expected.Side,
                                           expected.Quantity,
                                           clipQ,
                                           expected.Price,
                                           delta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());
            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(3));

            Assert.AreEqual(expected.Price - (2 * delta),
                            iceberg.CurrentPrice,
                            "Current price not as expected after two fills with delta");
            Assert.AreEqual(totalQ - (2 * clipQ),
                            iceberg.RemainingQuantity,
                            "Remaining quantity not as expected after two fills");
            Assert.AreEqual(clipQ,
                            iceberg.CurrentQuantity,
                            "Current quantity not equal to clip size after two fills");
        }
Пример #9
0
        public void OrderNotRefilledAfterQuantityExhausted()
        {
            const decimal totalQ = 15;
            const decimal clipQ  = 10;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());
            iceberg.OnTotalFill();

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));

            Assert.AreEqual(IcebergOrder.ActivationState.Suspended,
                            iceberg.State,
                            "Order should not be active after being exhausted");
            Assert.AreEqual(0, iceberg.CurrentQuantity, "Order should have no quantity left");
            Assert.AreEqual(0,
                            iceberg.RemainingQuantity,
                            "Order should have no quantity left");
        }
Пример #10
0
        public void TotalFillWithNoDeltaReplenishesWithClipSizePriceStaysSame()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 10;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));

            Assert.AreEqual(clipQ, iceberg.CurrentQuantity, "Order not refilled to clip size");
            Assert.AreEqual(totalQ - clipQ,
                            iceberg.RemainingQuantity,
                            "Remaining quantity should be total minus one clip");
            Assert.AreEqual(expected.Price, iceberg.CurrentPrice, "Current price not as expected");
            Assert.AreEqual(iceberg.InitialPrice,
                            iceberg.CurrentPrice,
                            "Current price should equal initial price");
        }
Пример #11
0
        public void RefillWithLessRemainingQtyThanClipSizeRefillsToRemaining()
        {
            const decimal totalQ     = 15;
            const decimal clipQ      = 10;
            const decimal remainingQ = totalQ - clipQ;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));

            Assert.AreEqual(remainingQ, iceberg.CurrentQuantity, "Order not refilled to correct");
            Assert.AreEqual(remainingQ,
                            iceberg.RemainingQuantity,
                            "Remaining quantity should be total minus one clip");
        }
Пример #12
0
        public void RefillWithDeltaHasZeroPriceFloor()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 10;
            const decimal price  = 0.1m;
            const decimal delta  = 0.2m;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ, price: price);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            var iceberg = new IcebergOrder(mockServer.Object,
                                           expected.Symbol,
                                           expected.ClOrdID,
                                           expected.Side,
                                           expected.Quantity,
                                           clipQ,
                                           price,
                                           delta);

            iceberg.Activate();

            var idGen = new SimpleOrderIDGenerator();

            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            iceberg.OnTotalFill();
            iceberg.ActivatedMarketOrderAccepted(idGen.GetID());

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Exactly(2));

            Assert.AreEqual(0,
                            iceberg.CurrentPrice,
                            "Current price not oberying zero floor after fill with delta");
        }
Пример #13
0
        public void SuspendWhenAlreadySuspendedThrows()
        {
            const string orderID = "FakeOrderID";

            var expected   = DefaultFakeOrderRecord();
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);
            mockServer.Setup(s =>
                             s.CancelOrder(expected.Symbol, expected.ClOrdID, expected.Side, orderID))
            .Returns(true);

            var           clipSize   = expected.Quantity;
            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipSize,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());

            iceberg.ActivatedMarketOrderAccepted(orderID);
            Assert.AreEqual(IcebergOrder.ActivationState.Active,
                            iceberg.State,
                            "Order should be active");

            iceberg.Suspend();
            iceberg.MarketOrderCanceled();

            Assert.AreEqual(IcebergOrder.ActivationState.Suspended,
                            iceberg.State,
                            "Order should be suspended");

            Assert.Throws <ApplicationException>(iceberg.Suspend);

            mockServer.Verify(
                s => s.CancelOrder(expected.Symbol, expected.ClOrdID, expected.Side, orderID),
                Times.Once());
            Assert.AreEqual(IcebergOrder.ActivationState.Suspended,
                            iceberg.State,
                            "Order should be suspended");
        }
Пример #14
0
        public void PartialFillAdjustsCurrentAndRemainingQuantity()
        {
            const decimal totalQ = 20;
            const decimal clipQ  = 10;

            var expected   = DefaultFakeOrderRecord(quantity: totalQ);
            var mockServer = new Mock <IServerFacade>();

            mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())).Returns(true);

            const decimal priceDelta = 0;
            var           iceberg    = new IcebergOrder(mockServer.Object,
                                                        expected.Symbol,
                                                        expected.ClOrdID,
                                                        expected.Side,
                                                        expected.Quantity,
                                                        clipQ,
                                                        expected.Price,
                                                        priceDelta);

            iceberg.Activate();

            const string orderID = "FakeOrderID";

            iceberg.ActivatedMarketOrderAccepted(orderID);

            const decimal fillQ = 5;

            iceberg.OnPartialFill(fillQ);

            mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()),
                              Times.Once());

            Assert.AreEqual(clipQ - fillQ,
                            iceberg.CurrentQuantity,
                            "Current quantity not updated correctly");
            Assert.AreEqual(totalQ - fillQ,
                            iceberg.RemainingQuantity,
                            "Remaining quantity not updated correctly");
        }