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)); }
public void WhenAnIcebergOrderActivatesItCreatesCorrectOrderDetails() { var expected = DefaultFakeOrderRecord(); var createdOrders = new List <OrderRecord>(); var mockServer = new Mock <IServerFacade>(); mockServer.Setup(s => s.CreateOrder(It.IsAny <OrderRecord>())) .Returns(true) .Callback((OrderRecord o) => createdOrders.Add(o)); 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()); Assert.AreEqual(1, createdOrders.Count, "Incorrect number of market orders created"); var actual = createdOrders[0]; CompareOrderRecords(expected, actual); }
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()); }
public void ThrowsIfActivatedWhenPendingMarketOrderAcceptance() { 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()); Assert.Throws <ApplicationException>(iceberg.Activate); }
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()); }
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"); }
public void ShouldBeAbleToSetNewValuesAfterMarketOrderAccepted() { const decimal totalQ = 25; 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(); iceberg.ActivatedMarketOrderAccepted("OrderID"); Assert.IsTrue(iceberg.CanSetNewValues(), "Should be able to set values after market order is accepted"); }
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"); }
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"); }
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"); }
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"); }
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"); }
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"); }
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"); }
public void PartialFillThenTotalFillOnlyRemovesAClipSizeOfQuantity() { const decimal totalQ = 25; 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.OnPartialFill(5); iceberg.OnTotalFill(); mockServer.Verify(s => s.CreateOrder(It.IsAny <OrderRecord>()), Times.Exactly(2)); Assert.AreEqual(clipQ, iceberg.CurrentQuantity, "Order not refilled to correct clip size"); Assert.AreEqual(totalQ - clipQ, iceberg.RemainingQuantity, "Remaining quantity should be total minus one clip"); }
public void SettingNewPriceWhileWaitingForMarketOrderAcceptanceThrows() { const decimal totalQ = 25; 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(); Assert.Throws <ApplicationException>(() => iceberg.SetNewOrderPrice(100)); }
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"); }
private void Activate() { _icebergOrder.Activate(); }