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();
        }
Пример #2
0
        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();
        }
Пример #3
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);
        }
Пример #4
0
        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 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();
        }
        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_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 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 OrderPlacementController_Should_PlaceBracketOrder()
        {
            // Setup
            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);

            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;

            int entryOrderId = await nextOrderIdController.GetNextValidIdAsync();

            var takeProfitOrderId = await nextOrderIdController.GetNextValidIdAsync();

            var stopOrderId = await nextOrderIdController.GetNextValidIdAsync();

            // Initialize the order
            Order entryOrder = new Order()
            {
                Action        = TwsOrderActions.Buy,
                OrderType     = TwsOrderType.Market,
                TotalQuantity = 1,
                Transmit      = false
            };

            Order takeProfit = new Order()
            {
                Action        = TwsOrderActions.Sell,
                OrderType     = TwsOrderType.Limit,
                TotalQuantity = 1,
                LmtPrice      = 190,
                ParentId      = entryOrderId,
                Transmit      = false,
            };

            Order stopLoss = new Order()
            {
                Action        = TwsOrderActions.Sell,
                OrderType     = TwsOrderType.StopLoss,
                TotalQuantity = 1,
                AuxPrice      = 100,
                ParentId      = entryOrderId,
                Transmit      = true,
            };

            // Call the API
            var entryOrderAckTask      = orderPlacementController.PlaceOrderAsync(entryOrderId, contract, entryOrder);
            var takeProfitOrderAckTask = orderPlacementController.PlaceOrderAsync(takeProfitOrderId, contract, takeProfit);
            var stopOrderAckTask       = orderPlacementController.PlaceOrderAsync(stopOrderId, contract, stopLoss);

            Task.WaitAll(entryOrderAckTask, takeProfitOrderAckTask, stopOrderAckTask);

            // Assert
            entryOrderAckTask.Result.Should().BeTrue();
            takeProfitOrderAckTask.Result.Should().BeTrue();
            stopOrderAckTask.Result.Should().BeTrue();

            // Tear down
            await connectionController.DisconnectAsync();
        }