public async Task Should_LiquidateShortPosition() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsOpenOrdersController twsOpenOrdersController = new TwsOpenOrdersController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderCancelationController orderCancellationController = new TwsOrderCancelationController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsPortfolioOrderCancellationController twsPortfolioOrderCancellationController = new TwsPortfolioOrderCancellationController(connectionController, twsOpenOrdersController, orderCancellationController); TwsPositionsController positionsController = new TwsPositionsController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsPositionLiquidationController positionLiquidationController = new TwsPositionLiquidationController( connectionController, positionsController, nextOrderIdController, orderPlacementController, twsPortfolioOrderCancellationController); await connectionController.EnsureConnectedAsync(); // Create a position Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; Order order = new Order { Action = TwsOrderActions.Sell, OrderType = "MKT", TotalQuantity = 1, }; // Place a couple of orders int orderId = await nextOrderIdController.GetNextValidIdAsync(); await orderPlacementController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Liquidate the position bool success = await positionLiquidationController.LiquidatePosition("MSFT", TwsExchange.Smart); success.Should().BeTrue(); }
public async Task Should_CancelOrders() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsOpenOrdersController twsOpenOrdersController = new TwsOpenOrdersController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderCancelationController orderCancellationController = new TwsOrderCancelationController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsPortfolioOrderCancellationController twsPortfolioOrderCancellationController = new TwsPortfolioOrderCancellationController(connectionController, twsOpenOrdersController, orderCancellationController); await connectionController.EnsureConnectedAsync(); // Create a position Contract contract = new Contract(); contract.SecType = TwsContractSecType.Stock; contract.Symbol = "MSFT"; contract.Exchange = TwsExchange.Smart; contract.Currency = TwsCurrency.Usd; contract.PrimaryExch = TwsExchange.Island; Order order = new Order { Action = "BUY", OrderType = "LMT", TotalQuantity = 1, LmtPrice = 1, }; // Placea couple of orders int orderId = await nextOrderIdController.GetNextValidIdAsync(); await orderPlacementController.PlaceOrderAsync(orderId, contract, order); orderId = await nextOrderIdController.GetNextValidIdAsync(); await orderPlacementController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Cancel them all var success = await twsPortfolioOrderCancellationController.CancelOrders("MSFT"); success.Should().BeTrue(); }
public async Task CancelOrder_Should_CancelOrder() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); // This should be fixed a bit to be injectable // It's a bit dirty because you need to run ConfigureTws before you have access to the client socket and callback handler TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderCancelationController orderCancelationController = new TwsOrderCancelationController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); await connectionController.EnsureConnectedAsync(); // Initialize the contract Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; // Initialize the order Order order = new Order { Action = "BUY", OrderType = "LMT", TotalQuantity = 1, LmtPrice = 1, }; // Place an order int orderId = await nextOrderIdController.GetNextValidIdAsync(); bool orderAcknowledged = await orderPlacementController.PlaceOrderAsync(orderId, contract, order); orderAcknowledged.Should().BeTrue(); // Call the API bool cancelationAcknowledged = await orderCancelationController.CancelOrderAsync(orderId); // Assert cancelationAcknowledged.Should().BeTrue(); }