public async Task Should_PlaceBracketOrderWithStopLimit() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", 7462, 1); TwsController twsController = twsObjectFactory.TwsController; await twsController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, Currency = TwsCurrency.Usd, }; bool placed = await twsController.PlaceBracketOrder(contract, TwsOrderActions.Buy, 1, 10, 1000, 1, 0.9); placed.Should().BeTrue(); await twsController.DisconnectAsync(); }
public void Should_PlaceSimpleBracketOrder() { // Setup // Initialize the contract to trade Contract contract = new Contract(); contract.SecType = TwsContractSecType.Stock; contract.Symbol = "MSFT"; contract.Exchange = TwsExchange.Smart; contract.PrimaryExch = TwsExchange.Island; // Initialize the expected orders to be placed Order entryOrder = new Order() { Action = TwsOrderActions.Buy, OrderType = TwsOrderType.PegToMarket, TotalQuantity = 100, Transmit = false, Tif = "GTC", PermId = 234234, // Set a random perm id unique per order, it's only for the unit test - normally TWS sets this value }; Order takeProfit = new Order() { Action = TwsOrderActions.Sell, OrderType = TwsOrderType.Limit, TotalQuantity = 100, LmtPrice = 190, ParentId = 1, Transmit = false, Tif = "GTC", PermId = 52345, }; Order stopLoss = new Order() { Action = TwsOrderActions.Sell, OrderType = TwsOrderType.StopLoss, TotalQuantity = 100, AuxPrice = 150, ParentId = 1, Transmit = true, Tif = "GTC", PermId = 615, }; // Initialize the controllers Mock <ITwsControllerBase> mockControllerBase = new Mock <ITwsControllerBase>(); mockControllerBase.SetupSequence(mock => mock.GetNextValidIdAsync()) .ReturnsAsync(1) .ReturnsAsync(2) .ReturnsAsync(3); mockControllerBase.Setup(mock => mock.PlaceOrderAsync(1, contract, entryOrder)).ReturnsAsync(true); mockControllerBase.Setup(mock => mock.PlaceOrderAsync(2, contract, takeProfit)).ReturnsAsync(true); mockControllerBase.Setup(mock => mock.PlaceOrderAsync(3, contract, stopLoss)).ReturnsAsync(true); mockControllerBase.Setup(mock => mock.EnsureConnectedAsync()).Returns(Task.CompletedTask); // Initialize the order details string entryAction = TwsOrderActions.Buy; double quantity = 100; double takePrice = 190; double stopPrice = 150; var twsController = new TwsController(mockControllerBase.Object); // Call bool orderAck = twsController.PlaceBracketOrder(contract, entryAction, quantity, takePrice, stopPrice).ConfigureAwait(false).GetAwaiter().GetResult(); // Asssert orderAck.Should().BeTrue(); mockControllerBase.VerifyAll(); }