Example #1
0
        private bool HandleSpecialApiMessages(APIMessageData data)
        {
            var errorCode = (EtwsErrorCode)data.ErrorCode;

            switch (errorCode)
            {
            case EtwsErrorCode.NoSecurityFound:
                var requestId = (int)data.AdditionalInfo;
                lock (SecurityDataDic)
                {
                    if (SecurityDataDic.ContainsKey(requestId))
                    {
                        Logger.Alert($"Request Id({requestId}) Not found. " +
                                     $" {SecurityDataDic[requestId].Data.GetContract()}");
                    }
                }
                return(true);

            case EtwsErrorCode.EntityIdNotFound:
                HandleEntityIdNotFound(data);
                return(true);

            case EtwsErrorCode.IbTWSConnectivityLost:
            case EtwsErrorCode.ConnectivityTwsServerBroken:
                if (_connectionStatus == ConnectionStatus.Connected)
                {
                    _connectionStatus = ConnectionStatus.Disconnected;
                    Consumer.Enqueue(new BrokerConnectionStatusMessage(
                                         ConnectionStatus.Disconnected, data));
                    Logger.Warn($"Connection status changed to disconnected:  {data}");
                }
                Consumer.Enqueue(data);
                return(true);

            case EtwsErrorCode.IbTWSConnectivityRestoredDataLost:
            case EtwsErrorCode.IbTWSConnectivityRestoredDataMaintained:
                if (_connectionStatus == ConnectionStatus.Disconnected)
                {
                    Consumer.Enqueue(new BrokerConnectionStatusMessage(ConnectionStatus.Connected, data));
                    Logger.Warn($"Connection status changed to connected: {data}");
                    Consumer.Enqueue(data);
                }
                ConnectivityIbTwsRestored?.Invoke();
                return(true);

            case EtwsErrorCode.MarketDataFarmConnected:
                Consumer.Enqueue(new BrokerConnectionStatusMessage(ConnectionStatus.Connected, data));
                ConnectivityIbTwsRestored?.Invoke();
                return(true);

            default:
                return(false);
            }
        }
Example #2
0
 private void PublishSecurities()
 {
     lock (SecurityDataDic)
     {
         SecurityDataDic.Where(a => a.Value.UpdatedSinceLastPubish).
         ForEach(a =>
         {
             Consumer.Enqueue(a.Value.Data);
             a.Value.UpdatedSinceLastPubish = false;
         });
     }
 }
Example #3
0
        private void HandleEntityIdNotFound(APIMessageData data)
        {
            int requestId = (int)data.AdditionalInfo;

            lock (SecurityDataDic)
            {
                if (!SecurityDataDic.ContainsKey(requestId))
                {
                    return;
                }
                Logger.Debug($"Request Id({requestId}) removed from SecurityDataDic, {data} {SecurityDataDic[requestId]}");
                SecurityDataDic.Remove(requestId);
            }
        }
Example #4
0
 public ContractBase RegisterContract(int requestId, ContractBase contract, ContractDetails IbContract)
 {
     lock (SecurityDataDic)
     {
         var optionContract = contract as OptionContract;
         BaseSecurityData securityData;
         if (optionContract != null)
         {
             securityData = new OptionData {
                 Account = AccountSummary.MainAccount
             };
         }
         else
         {
             securityData = new SecurityData {
                 Account = AccountSummary.MainAccount
             }
         };
         securityData.SetContract(contract);
         IbContract.UpdateSecurityData(contract);
         SecurityDataDic.Add(requestId, new SecuirtyDataWrapper(securityData));
         return(contract);
     }
 }