Exemplo n.º 1
0
        /// <summary>
        /// Checks that a connection to IB is established.
        /// </summary>
        /// <param name="tws">The instance to check the connection for.</param>
        /// <param name="token">The token to check to cancel the task.</param>
        /// <exception cref="ConnectionException">If a connection cannot be
        /// established.</exception>
        /// <exception cref="TaskCanceledException">If the task is canceled before
        /// completion.</exception>
        private static async Task CheckConnection(
            ITwsControllerBase tws, CancellationToken token)
        {
            try
            {
                await tws.EnsureConnectedAsync(token);
            }
            catch (TimeoutException e)
            {
                if (tws.Connected)
                {
                    await tws.DisconnectAsync();
                }
                throw new ConnectionException(
                          "Timed out waiting for connection to establish", e);
            }
            catch (TaskCanceledException)
            {
                if (tws.Connected)
                {
                    await tws.DisconnectAsync();
                }
                throw;
            }
            catch (Exception e)
            {
                if (tws.Connected)
                {
                    await tws.DisconnectAsync();
                }
                throw new ConnectionException(e.Message, e);
            }

            try
            {
                await tws.RequestPositions();
            }
            catch (TaskCanceledException e)
            {
                if (tws.Connected)
                {
                    await tws.DisconnectAsync();
                }
                throw new ConnectionException(
                          "Timed out waiting for response from establish", e);
            }
            catch (Exception e)
            {
                if (tws.Connected)
                {
                    await tws.DisconnectAsync();
                }
                throw new ConnectionException(e.Message, e);
            }
        }
Exemplo n.º 2
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();
        }