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();

            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 OrderPlacementController_Should_PlaceOrdersSuccessfully()
        {
            // Setup
            // Define the contract input to the contract details controller
            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 = "201806";

            // The following ContractDetails is the output of the call.
            ContractDetails expectedContractDetails = new ContractDetails();

            // Create a fake request Id generator which always returns a request Id of 1
            int fakeRequestId = 1;
            Mock <ITwsRequestIdGenerator> mockTwsRequestIdGenerator = new Mock <ITwsRequestIdGenerator>();

            mockTwsRequestIdGenerator.Setup(mock => mock.GetNextRequestId()).Returns(fakeRequestId);

            // Mock the callback handler, this one is a bit complex thanks to Tws APIs
            // It is saying that at any point that TWSCallbackHandler.contractDetails is called, propagate a new event through the TwsCallbackHandler.ContractDetailsEvent event.
            Mock <ITwsCallbackHandler> mockTwsCallbackHandler = new Mock <ITwsCallbackHandler>();

            mockTwsCallbackHandler.Setup(mock => mock.contractDetails(It.IsAny <int>(), It.IsAny <ContractDetails>())).Callback <int, ContractDetails>((requestId, contractDetails) =>
            {
                mockTwsCallbackHandler.Raise(mock => mock.ContractDetailsEvent += null, null, new ContractDetailsEventArgs(requestId, contractDetails));
            });

            mockTwsCallbackHandler.Setup(mock => mock.contractDetailsEnd(It.IsAny <int>())).Callback <int>((requestId) =>
            {
                mockTwsCallbackHandler.Raise(mock => mock.ContractDetailsEndEvent += null, null, new ContractDetailsEndEventArgs(requestId));
            });

            // Mock the client socket
            Mock <ITwsClientSocket> mockTwsClientSocket = new Mock <ITwsClientSocket>();

            mockTwsClientSocket.Setup(mock => mock.ReqContractDetails(fakeRequestId, contract)).Callback(
                () =>
            {
                mockTwsCallbackHandler.Object.contractDetails(fakeRequestId, expectedContractDetails);
                mockTwsCallbackHandler.Object.contractDetailsEnd(fakeRequestId);
            });

            TwsContractDetailsController contractDetailsController = new TwsContractDetailsController(mockTwsClientSocket.Object, mockTwsCallbackHandler.Object, mockTwsRequestIdGenerator.Object);

            // Call
            List <ContractDetails> actualContractDetails = await contractDetailsController.GetContractAsync(contract);

            // Assert
            actualContractDetails[0].Should().Be(expectedContractDetails);
        }
        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);
        }