Exemplo n.º 1
0
        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 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();
        }