Exemplo n.º 1
0
    /*****************************************************************************
    *  Function    : processSubscriptionStatus
    *  Description : Processes subscription status messages returned from Bloomberg
    *  Arguments   : Event, Session
    *  Returns     : void
    *****************************************************************************/
    private void processSubscriptionStatus(Event eventObj, Session session)
    {
        System.Console.WriteLine("Processing SUBSCRIPTION_STATUS");
        foreach (Message msg in eventObj)
        {
            string topic = (string)msg.CorrelationID.Object;
            System.Console.WriteLine(System.DateTime.Now.ToString("s") +
                                     ": " + topic + " - " + msg.MessageType);

            if (msg.HasElement(REASON))
            {
                // This occurs if a bad security is subscribed to
                Element reason = msg.GetElement(REASON);
                System.Console.WriteLine("\t" +
                                         reason.GetElement(CATEGORY).GetValueAsString() +
                                         ": " + reason.GetElement(DESCRIPTION).GetValueAsString());
            }

            if (msg.HasElement(EXCEPTIONS))
            {
                // This can occur on SubscriptionStarted if an
                // invalid field is passed in
                Element exceptions = msg.GetElement(EXCEPTIONS);
                for (int i = 0; i < exceptions.NumValues; ++i)
                {
                    Element exInfo  = exceptions.GetValueAsElement(i);
                    Element fieldId = exInfo.GetElement(FIELD_ID);
                    Element reason  = exInfo.GetElement(REASON);
                    System.Console.WriteLine("\t" + fieldId.GetValueAsString() +
                                             ": " + reason.GetElement(CATEGORY).GetValueAsString());
                }
            }
            System.Console.WriteLine("");
        }
    }
Exemplo n.º 2
0
        private void GetData(BloombergDataInstrument instrument, BB.Element fields, BB.Element secData)
        {
            #region security errors
            if (secData.HasElement(SECURITY_ERROR))
            {
                instrument.IsSecurityValid = false;
                BB.Element error = secData.GetElement(SECURITY_ERROR);
                UpdateStatus(string.Format("Security error for ticker {0} : {1}", instrument.Ticker, error.GetElementAsString(MESSAGE)));
                instrument.SecurityErrors += (error.GetElementAsString(MESSAGE) + "; ");
            }
            #endregion

            #region field errors
            if (secData.HasElement(FIELD_EXCEPTIONS))
            {
                instrument.HasFieldErrors = true;
                // process error
                BB.Element error = secData.GetElement(FIELD_EXCEPTIONS);
                for (int errorIndex = 0; errorIndex < error.NumValues; errorIndex++)
                {
                    BB.Element errorException = error.GetValueAsElement(errorIndex);
                    BB.Element errorInfo      = errorException.GetElement(ERROR_INFO);

                    instrument.BBFields[errorException.GetElementAsString(FIELD_ID)].Error = errorInfo.GetElementAsString(MESSAGE);
                    instrument.HasFieldErrors = true;
                    string msg = string.Format("Field error for ticker {0} : Field {1}: {2}",
                                               instrument.Ticker,
                                               errorException.GetElementAsString(FIELD_ID),
                                               errorInfo.GetElementAsString(MESSAGE));
                    UpdateStatus(msg);
                }
            }
            #endregion

            #region get the data
            if (instrument.BBFields != null)
            {
                lock (lockObject) {
                    foreach (string bbField in instrument.BBFields.Keys.ToList())
                    {
                        if (fields.HasElement(bbField))
                        {
                            BB.Element item = fields.GetElement(bbField);
                            if (item.IsArray)
                            {
                                instrument.BBFields[bbField].Value = processBulkData(item);
                            }
                            else
                            {
                                // set the value in the instrument field item
                                instrument.BBFields[bbField].Value = item.GetValue();
                            }
                        }
                    }
                }
            }
            #endregion
        }
Exemplo n.º 3
0
        private List <T> ParseEvent_OLD(BbergAPI.Event response, List <T> outputContainer)
        {
            foreach (BbergAPI.Message message in response.GetMessages())
            {
                // Extract security
                BbergAPI.Element security   = message.GetElement(SECURITY_DATA);
                string           currentSec = (string)security.GetElementAsString(TICKER);

                // Extract fields
                BbergAPI.Element fields = security.GetElement(FIELD_DATA);

                int sequenceNumber = security.GetElementAsInt32(SEQUENCE_NUMBER);

                Dictionary <string, int> skipFields = this.initializeSkipFields();

                // Loop through all observation dates
                for (int i = 0; i < fields.NumValues; i++)
                {
                    // Determine type of <T> and create instance
                    var Ttype    = typeof(T);
                    var thisLine = (T)Activator.CreateInstance(Ttype);

                    // extract all field data for a single observation date
                    BbergAPI.Element observationDateFields = fields.GetValueAsElement(i);

                    string   currentStringDate = observationDateFields.GetElementAsString(DATE);
                    DateTime currentDate       = DateTime.ParseExact(currentStringDate, "yyyy-MM-dd", CultureInfo.CurrentCulture);

                    // Determine type of <T> and create instance
                    thisLine.SetDate(currentDate);
                    thisLine.SetDBID(dbid);

                    // Fill the line
                    skipFields = thisLine.SetFromBloomberg(ref observationDateFields, skipFields);

                    // Add to output container
                    outputContainer.Add(thisLine);
                }
            }

            // This kills the data inside the response object...
            this.CloseConnection();

            return(outputContainer);
        }
Exemplo n.º 4
0
        private List <T> ParseEvent(BbergAPI.Event response, List <T> outputContainer)
        {
            foreach (BbergAPI.Message message in response.GetMessages())
            {
                // Extract security
                BbergAPI.Element security   = message.GetElement(SECURITY_DATA);
                string           currentSec = (string)security.GetElementAsString(TICKER);

                // Extract fields
                BbergAPI.Element fields = security.GetElement(FIELD_DATA);

                int sequenceNumber = security.GetElementAsInt32(SEQUENCE_NUMBER);

                // Loop through all observation dates
                for (int i = 0; i < fields.NumValues; i++)
                {
                    // Determine type of <T> and create instance
                    var Ttype    = typeof(T);
                    var thisLine = (T)Activator.CreateInstance(Ttype);

                    // extract all field data for a single observation date
                    BbergAPI.Element observationDateFields = fields.GetValueAsElement(i);

                    string   currentStringDate = observationDateFields.GetElementAsString(DATE);
                    DateTime currentDate       = DateTime.ParseExact(currentStringDate, "yyyy-MM-dd", CultureInfo.CurrentCulture);

                    // Set the date and DBID to identify the line
                    thisLine.SetDate(currentDate);
                    thisLine.SetDBID(dbid);

                    // Fill the line with data
                    foreach (string localKey in this.fieldNames.Keys)
                    {
                        if (skip[localKey] < 10000)
                        {
                            try
                            {
                                // Warning : Bloomberg displays interest rates in percentage terms
                                thisLine[localKey] = scaling[localKey] * (double)observationDateFields.GetElementAsFloat64(fieldNames[localKey]);
                            }

                            catch
                            {
                                // Some log ?
                                skip[localKey] += 1;
                                // thisLine[localKey] = System.DBNull.Value;
                                thisLine[localKey] = null;
                                //thisLine[localKey] = 0.0;
                            }
                        }
                    }

                    // Add to output container
                    outputContainer.Add(thisLine);
                }
            }

            // This kills the data inside the response object...
            this.CloseConnection();

            return(outputContainer);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Processes the Bloomberg subscription data event.
        /// </summary>
        /// <param name="eventObj">The event obj.</param>
        /// <param name="session">The session.</param>
        private void processSubscriptionDataEvent(BB.Event eventObj, BB.Session session)
        {
            try {
                // process message
                foreach (BB.Message msg in eventObj.GetMessages())
                {
                    #region find instrument
                    Guid reqGUID = (Guid)msg.CorrelationID.Object;

                    // check for duplicate replies just in case
                    if (guids.Contains(reqGUID))
                    {
                        return;
                    }
                    else
                    {
                        guids.Add(reqGUID);
                    }

                    // find the correct instrument
                    BloombergDataInstrument bbdi = FindSentInstrumentByGuid(reqGUID);
                    if (bbdi == null)
                    {
                        UpdateStatus("Unable to find received instrument by Guid - " + reqGUID.ToString());
                        continue;
                    }
                    #endregion

                    UpdateStatus(string.Format("Received {0} of {1} requests : {2}", guids.Count, sentToBB.Count, bbdi.Ticker));

                    if (msg.HasElement("responseError"))
                    {
                        BB.Element error         = msg.GetElement(RESPONSE_ERROR);
                        string     responseError = error.GetElementAsString(SUBCATEGORY);
                        UpdateStatus("Response error : " + error.GetElementAsString(MESSAGE));
                        CheckForLimits(responseError);
                        bbdi.HasFieldErrors = true;
                        continue;
                    }
                    BB.Element secDataArray = msg.GetElement(SECURITY_DATA);

                    #region process security data

                    // process security data
                    int numberOfSecurities = secDataArray.NumValues;
                    for (int index = 0; index < numberOfSecurities; index++)
                    {
                        if (msg.MessageType.Equals(Bloomberglp.Blpapi.Name.GetName(BLP_REFERENCE_RESPONSE)))
                        {
                            // just contains the one element, which is actually a sequence
                            BB.Element secData = secDataArray.GetValueAsElement(index);
                            BB.Element fields  = secData.GetElement(FIELD_DATA);

                            GetData(bbdi, fields, secData);
                        }
                        else if (msg.MessageType.Equals(Bloomberglp.Blpapi.Name.GetName(BLP_HISTORICAL_RESPONSE)))
                        {
                            // Historical is handled slightly different.  Can contain many elements each with possibly multiple values
                            foreach (BB.Element secData in secDataArray.Elements)
                            {
                                if (secData.Name != FIELD_DATA)
                                {
                                    continue;
                                }

                                for (int pointIndex = 0; pointIndex < secData.NumValues; pointIndex++)
                                {
                                    BB.Element fields = secData.GetValueAsElement(pointIndex);

                                    GetData(bbdi, fields, secData);
                                }
                            }
                        }
                    }
                    #endregion

                    ShowCompletionPercentage(guids.Count, sentToBB.Count);
                    ShowCompletedInstrument(bbdi);
                }
            } catch (Exception ex) {
                UpdateStatus("Error occurred processing reply : " + ex.Message);
            } finally {
                if (guids.Count == sentToBB.Count)                   // we are done
                {
                    ShowCompletionPercentage(1, 1);
                    ProcessComplete();
                }
            }
        }