Example #1
0
        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);
        }
Example #2
0
        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();
        }
Example #3
0
        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);
        }
Example #4
0
        public async Task PositionsController_Should_ReturnAListOfPositions()
        {
            // Setup
            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 = 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 <PositionStatusEventArgs> positionStatusEvents = twsController.RequestPositions().ConfigureAwait(false).GetAwaiter().GetResult();

            // Assert
            positionStatusEvents.Count.Should().BeGreaterOrEqualTo(0);
            PositionStatusEventArgs searchedPositions = positionStatusEvents.Where(eventArgs => eventArgs.Contract.Symbol == contract.Symbol).FirstOrDefault();

            searchedPositions.Position.Should().BeGreaterOrEqualTo(order.TotalQuantity);

            // Tear down
            await twsController.DisconnectAsync();
        }
Example #5
0
        public async Task TwsExecutionController_Should_ReturnOpenOrdersTwice()
        {
            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);

            Thread.Sleep(5005);

            var openOrders2 = await twsController.RequestOpenOrders();

            openOrders2.Count.Should().BeGreaterOrEqualTo(1);

            await twsController.DisconnectAsync();
        }
Example #6
0
        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();
        }
Example #7
0
        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();
        }
Example #8
0
        public async Task OrderPlacementController_Should_HandleErrorCallback()
        {
            // Setup
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.EnsureConnectedAsync();

            // Initialize the contract
            Contract contract = new Contract();

            contract.SecType     = TwsContractSecType.Stock;
            contract.Symbol      = "MSFT";
            contract.Exchange    = TwsExchange.Island;
            contract.PrimaryExch = TwsExchange.Island;

            // Initialize the order
            Order order = new Order()
            {
                Action        = TwsOrderActions.Buy,
                OrderType     = "PEG MID",
                TotalQuantity = 1,
                LmtPrice      = 200,
                AuxPrice      = 0,
            };

            // Call the API
            int orderId = await twsController.GetNextValidIdAsync();

            Action orderFunc = () => twsController.PlaceOrderAsync(orderId, contract, order).ConfigureAwait(false).GetAwaiter().GetResult();

            // Assert
            orderFunc.Should().Throw <TwsException>();

            // Tear down
            await twsController.DisconnectAsync();
        }
Example #9
0
        public async Task OrderPlacementController_Should_PlaceOrderSuccessfully2()
        {
            // 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      = "GRWG",
                Exchange    = TwsExchange.Smart,
                PrimaryExch = TwsExchange.Island,
                Currency    = TwsCurrency.Usd,
            };

            // Initialize the order
            Order order = new Order
            {
                Action        = "BUY",
                OrderType     = "MKT",
                TotalQuantity = 1
            };

            // Call the API
            int orderId = await twsController.GetNextValidIdAsync();

            bool successfullyPlaced = await twsController.PlaceOrderAsync(orderId, contract, order);

            // Assert
            successfullyPlaced.Should().BeTrue();

            // Tear down
            await twsController.DisconnectAsync();
        }
Example #10
0
        public async Task OrderPlacementController_Should_PlaceOrderSuccessfully()
        {
            // Setup
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", 7462, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.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 twsController.GetNextValidIdAsync();

            bool successfullyPlaced = await twsController.PlaceOrderAsync(orderId, contract, order);

            // Assert
            successfullyPlaced.Should().BeTrue();

            // Tear down
            await twsController.DisconnectAsync();
        }
Example #11
0
        public async Task OrderPlacementController_Should_PlacePegToMidpointOrder()
        {
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.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 twsController.GetNextValidIdAsync();

            var orderAck = await twsController.PlaceOrderAsync(orderId, contract, order);

            // Assert
            orderAck.Should().BeTrue();

            // Tear down
            await twsController.DisconnectAsync();
        }
Example #12
0
        public async Task OrderPlacementController_Should_PlaceBracketOrder()
        {
            // Setup
            TwsObjectFactory   twsObjectFactory = new TwsObjectFactory("localhost", TestConstants.Port, 1);
            ITwsControllerBase twsController    = twsObjectFactory.TwsControllerBase;

            await twsController.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 twsController.GetNextValidIdAsync();

            var takeProfitOrderId = await twsController.GetNextValidIdAsync();

            var stopOrderId = await twsController.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      = twsController.PlaceOrderAsync(entryOrderId, contract, entryOrder);
            var takeProfitOrderAckTask = twsController.PlaceOrderAsync(takeProfitOrderId, contract, takeProfit);
            var stopOrderAckTask       = twsController.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 twsController.DisconnectAsync();
        }