public async Task CancelPnLSingleController_Should_StopPnLEventSingle() { int waitDelayInMs = 5000; TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); PnLSingleEventArgs pnlSingleEventArgs = null; DateTime pnlEventTriggerDateTime = DateTime.MaxValue; twsObjectFactory.TwsCallbackHandler.PnLSingleEvent += (sender, args) => { pnlSingleEventArgs = args; pnlEventTriggerDateTime = DateTime.Now; }; // Create a position Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; Order order = new Order { Action = "BUY", OrderType = "MKT", TotalQuantity = 1, }; int orderId = await twsController.GetNextValidIdAsync(); bool successfullyPlaced = twsController.PlaceOrderAsync(orderId, contract, order).ConfigureAwait(false).GetAwaiter().GetResult(); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Call List <ExecutionDetailsEventArgs> executionDetailEvents = twsController.RequestExecutions().ConfigureAwait(false).GetAwaiter().GetResult(); // Assert executionDetailEvents.Count.Should().BeGreaterOrEqualTo(0); var pnlSingleResult = twsObjectFactory.TwsControllerBase.RequestPnLSingle( "DU1052488", "", executionDetailEvents.First().Contract.ConId).ConfigureAwait(false).GetAwaiter().GetResult(); pnlSingleEventArgs.Should().NotBeNull(); pnlSingleResult.Should().NotBeNull(); pnlSingleEventArgs.RequestId.Should().IsSameOrEqualTo(pnlSingleResult.RequestId); twsObjectFactory.TwsControllerBase.CancelPnLSingle(pnlSingleResult.RequestId); await Task.Delay(waitDelayInMs); pnlEventTriggerDateTime.Ticks.Should().BeLessThan(DateTime.Now.AddMilliseconds(-waitDelayInMs).Ticks); }
public async Task PnLSingleController_Should_ReturnPnLSingle() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); PnLSingleEventArgs pnlSingleEventArgs = null; twsObjectFactory.TwsCallbackHandler.PnLSingleEvent += (sender, args) => { pnlSingleEventArgs = args; }; // Create a position Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; Order order = new Order { Action = "BUY", OrderType = "MKT", TotalQuantity = 1, }; int orderId = await twsController.GetNextValidIdAsync(); bool successfullyPlaced = await twsController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Call List <ExecutionDetailsEventArgs> executionDetailEvents = await twsController.RequestExecutions(); // Assert executionDetailEvents.Count.Should().BeGreaterOrEqualTo(0); var pnlSingleResult = await twsObjectFactory.TwsControllerBase.RequestPnLSingle( "DU1052488", "", executionDetailEvents.First().Contract.ConId); pnlSingleEventArgs.Should().NotBeNull(); pnlSingleResult.Should().NotBeNull(); pnlSingleEventArgs.RequestId.Should().IsSameOrEqualTo(pnlSingleResult.RequestId); }
public async Task Should_PlaceOrder() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); // Create a position Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; Order order = new Order { Action = "BUY", OrderType = "MKT", TotalQuantity = 1 }; int orderId = await twsController.GetNextValidIdAsync(); bool successfullyPlaced = await twsController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Call List <ExecutionDetailsEventArgs> executionDetailEvents = await twsController.RequestExecutions(); // Assert executionDetailEvents.Count.Should().BeGreaterOrEqualTo(0); // Tear down await twsController.DisconnectAsync(); }