public async Task HistoricalDataController_Should_RetrieveHistoricalData() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsRequestIdGenerator twsRequestIdGenerator = new TwsRequestIdGenerator(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsHistoricalDataController historicalDataController = new TwsHistoricalDataController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator); await connectionController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; var queryTime = DateTime.Now.AddMonths(-6); // Call List <HistoricalDataEventArgs> historicalDataEvents = await historicalDataController.GetHistoricalDataAsync(contract, queryTime, TwsDuration.OneMonth, TwsBarSizeSetting.OneDay, TwsHistoricalDataRequestType.Midpoint); // Assert historicalDataEvents.Count.Should().BeGreaterThan(0); }
public async Task MarketDataController_Should_ReturnMarketDataType() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); MarketDataTypeEventArgs marketDataTypeEventArgs = null; TickPriceEventArgs tickPriceEventArgs = null; twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent += (sender, args) => { marketDataTypeEventArgs = args; }; twsObjectFactory.TwsCallbackHandler.TickPriceEvent += (sender, args) => { tickPriceEventArgs = args; }; // Only real-time data provided for this contract var contract = new Contract() { Symbol = TwsCurrency.Eur, Exchange = TwsExchange.Idealpro, SecType = TwsContractSecType.Cash, Currency = TwsCurrency.Usd }; var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, "233", false, false, null); marketDataResult.Should().NotBeNull(); tickPriceEventArgs.Should().NotBeNull(); tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId); marketDataTypeEventArgs.Should().NotBeNull(); marketDataTypeEventArgs.MarketDataType.Should().Be(1); }
public async Task HistoricalDataController_Should_RetrieveHistoricalData() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; var queryTime = DateTime.Now.AddMonths(-6); // Call List <HistoricalDataEventArgs> historicalDataEvents = await twsController.GetHistoricalDataAsync(contract, queryTime, TwsDuration.OneMonth, TwsBarSizeSetting.OneDay, TwsHistoricalDataRequestType.Midpoint); // Assert historicalDataEvents.Count.Should().BeGreaterThan(0); await twsController.DisconnectAsync(); }
public async Task ContractDetailsController_Should_ReturnValidContractAsync() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsContractDetailsController contractDetailsController = new TwsContractDetailsController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, new TwsRequestIdGenerator()); await connectionController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; // Call List <ContractDetails> contractDetails = await contractDetailsController.GetContractAsync(contract); // Assert contractDetails.Should().NotBeNull(); // Tear down await connectionController.DisconnectAsync(); }
public async Task MarketDataTypeController_Should_ReturnMarketDataType() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); MarketDataTypeEventArgs marketDataTypeEventArgs = null; TickPriceEventArgs tickPriceEventArgs = null; twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent += (sender, args) => { marketDataTypeEventArgs = args; }; twsObjectFactory.TwsCallbackHandler.TickPriceEvent += (sender, args) => { tickPriceEventArgs = args; }; // This contract provide delayed data when requested Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; // Request delayed data feed twsObjectFactory.TwsControllerBase.RequestMarketDataType(3); var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, "233", false, false, null); marketDataResult.Should().NotBeNull(); tickPriceEventArgs.Should().NotBeNull(); tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId); marketDataTypeEventArgs.Should().NotBeNull(); marketDataTypeEventArgs.MarketDataType.Should().Be(3); }
public async Task ContractDetailsController_Should_ReturnValidForexContractAsync() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsContractDetailsController contractDetailsController = new TwsContractDetailsController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, new TwsRequestIdGenerator()); await connectionController.EnsureConnectedAsync(); Contract contract = new Contract(); contract.Symbol = "EUR"; contract.SecType = "CASH"; contract.Currency = "GBP"; contract.Exchange = "IDEALPRO"; // Call List <ContractDetails> contractDetails = await contractDetailsController.GetContractAsync(contract); // Assert contractDetails.First().Should().NotBeNull(); // Tear down await connectionController.DisconnectAsync(); }
public async Task ContractDetailsController_Should_ReturnValidContractAsync() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, PrimaryExch = TwsExchange.Island, }; // Call List <ContractDetails> contractDetails = await twsController.GetContractAsync(contract); // Assert contractDetails.Should().NotBeNull(); // Tear down await twsController.DisconnectAsync(); }
public async Task CancelPnLController_Should_StopPnLEvent() { int waitDelayInMs = 5000; TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); PnLEventArgs pnlEventArgs = null; DateTime pnlEventTriggerDateTime = DateTime.MaxValue; twsObjectFactory.TwsCallbackHandler.PnLEvent += (sender, args) => { pnlEventArgs = args; pnlEventTriggerDateTime = DateTime.Now; }; var pnlResult = await twsObjectFactory.TwsControllerBase.RequestPnL("DU1052488", ""); pnlEventArgs.Should().NotBeNull(); pnlResult.Should().NotBeNull(); pnlEventArgs.RequestId.Should().IsSameOrEqualTo(pnlResult.RequestId); twsObjectFactory.TwsControllerBase.CancelPnL(pnlResult.RequestId); await Task.Delay(waitDelayInMs); pnlEventTriggerDateTime.Ticks.Should().BeLessThan(DateTime.Now.AddMilliseconds(-waitDelayInMs).Ticks); }
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 Should_CancelAllOrders() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsController twsController = twsObjectFactory.TwsController; await twsController.EnsureConnectedAsync(); bool success = await twsController.CancelAllOrders(); success.Should().BeTrue(); await twsController.DisconnectAsync(); }
public async Task Should_PlaceBracketForExistingPosition() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsController twsController = twsObjectFactory.TwsController; await twsController.EnsureConnectedAsync(); bool success = await twsController.PlaceBracketForExistingPosition("GRWG", "SMART", 6, 4, 3.9); success.Should().BeTrue(); await twsController.DisconnectAsync(); }
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 OrderPlacementController_Should_PlaceTwoOrdersSuccessfully() { // 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); 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 = "MKT", TotalQuantity = 1 }; // Call the API int orderId = await nextOrderIdController.GetNextValidIdAsync(); var firstOrderAcknowledgedTask = orderPlacementController.PlaceOrderAsync(orderId, contract, order); orderId = await nextOrderIdController.GetNextValidIdAsync(); var secondOrderAcknowledgedTask = orderPlacementController.PlaceOrderAsync(orderId, contract, order); Task.WaitAll(firstOrderAcknowledgedTask, secondOrderAcknowledgedTask); // Assert firstOrderAcknowledgedTask.Result.Should().BeTrue(); secondOrderAcknowledgedTask.Result.Should().BeTrue(); // Tear down await connectionController.DisconnectAsync(); }
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 TwsExecutionController_Should_ReturnOpenOrdersTwice() { 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); TwsRequestIdGenerator twsRequestIdGenerator = new TwsRequestIdGenerator(); TwsExecutionController executionController = new TwsExecutionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator); 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, }; int orderId = await nextOrderIdController.GetNextValidIdAsync(); bool successfullyPlaced = await orderPlacementController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. var openOrders = await twsOpenOrdersController.RequestOpenOrders(); openOrders.Count.Should().BeGreaterOrEqualTo(1); Thread.Sleep(5005); var openOrders2 = await twsOpenOrdersController.RequestOpenOrders(); openOrders2.Count.Should().BeGreaterOrEqualTo(1); }
public async Task PositionsController_Should_ReturnAListOfPositions() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsPositionsController positionsController = new TwsPositionsController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); 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 = "BUY", OrderType = "MKT", TotalQuantity = 1 }; int orderId = await nextOrderIdController.GetNextValidIdAsync(); bool successfullyPlaced = await orderPlacementController.PlaceOrderAsync(orderId, contract, order); Thread.Sleep(1000); // TWS takes some time to put the order in the portfolio. Wait for it. // Call List <PositionStatusEventArgs> positionStatusEvents = await positionsController.RequestPositions(); // Assert positionStatusEvents.Count.Should().BeGreaterOrEqualTo(0); PositionStatusEventArgs daxPositions = positionStatusEvents.Where(eventArgs => eventArgs.Contract.Symbol == contract.Symbol).FirstOrDefault(); daxPositions.Position.Should().BeGreaterOrEqualTo(order.TotalQuantity); // Tear down await connectionController.DisconnectAsync(); }
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(); }
public async Task PnLController_Should_ReturnPnL() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); PnLEventArgs pnlEventArgs = null; twsObjectFactory.TwsCallbackHandler.PnLEvent += (sender, args) => { pnlEventArgs = args; }; var pnlResult = await twsObjectFactory.TwsControllerBase.RequestPnL("DU1052488", ""); pnlEventArgs.Should().NotBeNull(); pnlResult.Should().NotBeNull(); pnlEventArgs.RequestId.Should().IsSameOrEqualTo(pnlResult.RequestId); }
public async Task AccountUpdatesController_Should_ReturnInformation() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsAccountUpdatesController accountUpdatesController = new TwsAccountUpdatesController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); await connectionController.EnsureConnectedAsync(); // Call string accountId = "DU1052488"; ConcurrentDictionary <string, string> accountUpdates = await accountUpdatesController.GetAccountDetailsAsync(accountId); // Assert accountUpdates.Count.Should().BeGreaterThan(0); // Tear down await connectionController.DisconnectAsync(); }
public async Task AccountUpdatesController_Should_ReturnInformation() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); // Call string accountId = "DU1052488"; ConcurrentDictionary <string, string> accountUpdates = twsController.GetAccountDetailsAsync(accountId).ConfigureAwait(false).GetAwaiter().GetResult(); // Assert accountUpdates.Count.Should().BeGreaterThan(0); // Tear down await twsController.DisconnectAsync(); }
public async Task TwsExecutionController_Should_ReturnExecutions() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); 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); TwsRequestIdGenerator twsRequestIdGenerator = new TwsRequestIdGenerator(); TwsExecutionController executionController = new TwsExecutionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator); await connectionController.EnsureConnectedAsync(); // Create a position Contract contract = new Contract(); contract.SecType = TwsContractSecType.Future; contract.Symbol = TwsSymbol.Dax; contract.Exchange = TwsExchange.Dtb; contract.Currency = TwsCurrency.Eur; contract.Multiplier = "25"; contract.LastTradeDateOrContractMonth = "201809"; Order order = new Order { Action = "BUY", OrderType = "MKT", TotalQuantity = 1 }; int orderId = await nextOrderIdController.GetNextValidIdAsync(); bool successfullyPlaced = await orderPlacementController.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 executionController.RequestExecutions(); // Assert executionDetailEvents.Count.Should().BeGreaterOrEqualTo(0); // Tear down await connectionController.DisconnectAsync(); }
public async Task OrderPlacementController_Should_PlaceTwoOrdersSuccessfully() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.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 = "MKT", TotalQuantity = 1 }; // Call the API int orderId = await twsController.GetNextValidIdAsync(); var firstOrderAcknowledgedTask = twsController.PlaceOrderAsync(orderId, contract, order); orderId = await twsController.GetNextValidIdAsync(); var secondOrderAcknowledgedTask = twsController.PlaceOrderAsync(orderId, contract, order); Task.WaitAll(firstOrderAcknowledgedTask, secondOrderAcknowledgedTask); // Assert firstOrderAcknowledgedTask.Result.Should().BeTrue(); secondOrderAcknowledgedTask.Result.Should().BeTrue(); // Tear down await twsController.DisconnectAsync(); }
public async Task OrderPlacementController_Should_PlacePegToMidpointOrder() { // 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); await connectionController.EnsureConnectedAsync(); // Initialize the contract Contract contract = new Contract(); contract.SecType = TwsContractSecType.Stock; contract.Symbol = "MSFT"; contract.Exchange = TwsExchange.Smart; contract.PrimaryExch = TwsExchange.Island; // Initialize the order Order order = new Order() { Action = TwsOrderActions.Buy, OrderType = "REL", TotalQuantity = 1, LmtPrice = 166, AuxPrice = 0.1, }; // Call the API int orderId = await nextOrderIdController.GetNextValidIdAsync(); var orderAck = await orderPlacementController.PlaceOrderAsync(orderId, contract, order); // Assert orderAck.Should().BeTrue(); // Tear down await connectionController.DisconnectAsync(); }
public async Task Should_GetOptionsContracts() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsRequestIdGenerator twsRequestIdGenerator = new TwsRequestIdGenerator(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 1); TwsSecurityDefinitionOptionParametersController securityDefinitionController = new TwsSecurityDefinitionOptionParametersController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator); TwsContractDetailsController twsContractDetailsController = new TwsContractDetailsController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, twsRequestIdGenerator); await connectionController.EnsureConnectedAsync(); Contract contract = new Contract { SecType = TwsContractSecType.Stock, Symbol = "MSFT", Exchange = TwsExchange.Smart, }; // Get the contract details of the STOCK so that you can find the underlying security ID, required for the security definitions call. var contractDetails = await twsContractDetailsController.GetContractAsync(contract); var securityDefinitions = await securityDefinitionController.RequestSecurityDefinitionOptionParameters("MSFT", "", "STK", contractDetails.First().Contract.ConId); securityDefinitions.Count.Should().BeGreaterThan(1); ////// If you want, you can request the contract details from this info or get historical data for it ////Contract option = new Contract() ////{ //// SecType = TwsContractSecType.Option, //// Symbol = "MSFT", //// Exchange = "SMART", //// Strike = 150, //// LastTradeDateOrContractMonth = securityDefinitions[0].Expirations.First(), // March 27, 20 //// Right = "C", //// Multiplier = securityDefinitions[0].Multiplier, //// Currency = TwsCurrency.Usd, ////}; ////var optionContractDetails = await twsContractDetailsController.GetContractAsync(option); ////var queryTime = DateTime.Now; ////List<HistoricalDataEventArgs> historicalDataEvents = await twsHistoricalDataController.GetHistoricalDataAsync(option, queryTime, TwsDuration.OneMonth, TwsBarSizeSetting.OneMinute, TwsHistoricalDataRequestType.Trades); }
public async Task OrderPlacementController_Should_PlaceOrderSuccessfully() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory(); TwsConnectionController connectionController = new TwsConnectionController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler, "localhost", 7462, 7); ITwsNextOrderIdController nextOrderIdController = new TwsNextOrderIdController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); TwsOrderPlacementController orderPlacementController = new TwsOrderPlacementController(twsObjectFactory.ClientSocket, twsObjectFactory.TwsCallbackHandler); await connectionController.EnsureConnectedAsync(); // Initialize the contract Contract contract = new Contract(); contract.SecType = TwsContractSecType.Future; contract.Symbol = TwsSymbol.Dax; contract.Exchange = TwsExchange.Dtb; contract.Currency = TwsCurrency.Eur; contract.Multiplier = "25"; contract.LastTradeDateOrContractMonth = "202009"; // Initialize the order Order order = new Order { Action = "BUY", OrderType = "MKT", TotalQuantity = 1 }; // Call the API int orderId = await nextOrderIdController.GetNextValidIdAsync(); bool successfullyPlaced = await orderPlacementController.PlaceOrderAsync(orderId, contract, order); // Assert successfullyPlaced.Should().BeTrue(); // Tear down // It appears something is wrong with the disconnection API, which could be screwing up all the integration tests if you don't run them individually. ////await connectionController.DisconnectAsync(); }
public async Task CancelOrder_Should_CancelOrder() { // Setup TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.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 twsController.GetNextValidIdAsync(); bool orderAcknowledged = await twsController.PlaceOrderAsync(orderId, contract, order); orderAcknowledged.Should().BeTrue(); // Call the API bool cancelationAcknowledged = await twsController.CancelOrderAsync(orderId); // Assert cancelationAcknowledged.Should().BeTrue(); await twsController.DisconnectAsync(); }
public async Task MarketOptionDataController_Should_ReturnMarketDataType() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", 7497, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.EnsureConnectedAsync(); MarketDataTypeEventArgs marketDataTypeEventArgs = null; TickPriceEventArgs tickPriceEventArgs = null; TickOptionComputationEventArgs tickOptionComputationEventArgs = null; twsObjectFactory.TwsCallbackHandler.MarketDataTypeEvent += (sender, args) => { marketDataTypeEventArgs = args; }; twsObjectFactory.TwsCallbackHandler.TickPriceEvent += (sender, args) => { tickPriceEventArgs = args; }; twsObjectFactory.TwsCallbackHandler.TickOptionComputationEvent += (sender, args) => { tickOptionComputationEventArgs = args; }; Contract contract = new Contract { SecType = TwsContractSecType.Option, Symbol = "MSFT", Exchange = TwsExchange.Smart, Multiplier = "100", Currency = "USD", Right = "C", Strike = 200, LastTradeDateOrContractMonth = "20201113" }; twsObjectFactory.TwsControllerBase.RequestMarketDataType(1); var marketDataResult = await twsObjectFactory.TwsControllerBase.RequestMarketDataAsync(contract, null, false, false, null); marketDataResult.Should().NotBeNull(); tickPriceEventArgs.Should().NotBeNull(); tickPriceEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId); tickOptionComputationEventArgs.Should().NotBeNull(); tickOptionComputationEventArgs.TickerId.Should().IsSameOrEqualTo(marketDataResult.TickerId); }
public async Task Should_ReturnOpenOrders() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1); ITwsControllerBase twsController = twsObjectFactory.TwsControllerBase; await twsController.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, }; 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. var openOrders = await twsController.RequestOpenOrders(); openOrders.Count.Should().BeGreaterOrEqualTo(1); var msftOrders = openOrders.Where(orderEvent => orderEvent.Contract.Symbol == "MSFT").ToList(); msftOrders.Count.Should().BeGreaterOrEqualTo(1); await twsController.DisconnectAsync(); }
public async Task Should_PlaceBracketOrderWithStopLimit() { TwsObjectFactory twsObjectFactory = new TwsObjectFactory("localhost", 7462, 1); ITwsController 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(); }