Exemple #1
0
        public void Connect()
        {
            try
            {
                ibClient = new EWrapperImpl();

                // subscribe to events triggered by wrapper, to update form
                ibClient.AccountValueUpdated    += UpdateAccountValue;
                ibClient.TickPriceUpdated       += UpdateTickPrice;
                ibClient.ErrorUpdated           += UpdateError;
                ibClient.MarketRuleUpdated      += UpdateMarketRule;
                ibClient.ContractDetailsUpdated += UpdateContractDetails;

                ibClient.ClientSocket.eConnect(iBGatewayClientConnectionData.Server,
                                               iBGatewayClientConnectionData.Port,
                                               iBGatewayClientConnectionData.ClientId);

                var reader = new EReader(ibClient.ClientSocket, ibClient.Signal);
                reader.Start();
                new Thread(() =>
                {
                    while (ibClient.ClientSocket.IsConnected())
                    {
                        ibClient.Signal.waitForSignal();
                        reader.processMsgs();
                    }
                })
                {
                    IsBackground = true
                }.Start();

                // Pause here until the connection is complete
                DateTime connectionTime = DateTime.Now;
                while (ibClient.NextOrderId <= 0)
                {
                    if (DateTime.Now > connectionTime.AddSeconds(1))
                    {
                        throw new Exception(strTimeOutMessage);
                    }
                }

                orderNumber = ibClient.NextOrderId;

                frmMain.SetConnectionStatus(true);

                SubscribeAccount(ibClient, frmMain.AccountNumber());
            }
            catch (Exception e)
            {
                if (e.Message == strTimeOutMessage)
                {
                    frmMain.AddNotification("Connection timeout.  Please check that IB Gateway or TWS is running.");
                    return;
                }
                throw;
            }
        }
Exemple #2
0
 private void SubscribeAccount(EWrapperImpl ibClient, string accountNumber)
 {
     ibClient.ClientSocket.reqAccountUpdates(true, accountNumber);
 }