Example #1
0
        private string processBulkData(BB.Element data)
        {
            string ret = null;

            try {
                if (data.NumValues > 0)
                {
                    for (int index = 0; index < data.NumValues; index++)
                    {
                        BB.Element bulk = data.GetValueAsElement(index);

                        if (bulk.NumElements > 1)
                        {
                            ret += "{";
                        }
                        foreach (BB.Element item in bulk.Elements)
                        {
                            ret += item.GetValueAsString() + arrayDelimiter;
                        }
                        if (bulk.NumElements > 1)
                        {
                            ret += "}";
                        }
                    }
                }
            } catch (Exception ex) {
                UpdateStatus("Error occurred processing array field : " + ex.Message);
                ret = null;
            }
            return(ret);
        }
Example #2
0
        /// <summary>
        /// Process a list of curves in a response from the BLP API.
        /// Not fully implemented yet - does not collect results
        /// </summary>
        /// <param name="msg"></param>
        private void ProcessCurveListResponse(Message msg)
        {
            Element results    = msg.GetElement(RESULTS_ELEMENT);
            int     numResults = results.NumValues;

            Console.WriteLine("Processing " + numResults + " results:");
            for (int i = 0; i < numResults; ++i)
            {
                Element       result = results.GetValueAsElement(i);
                StringBuilder sb     = new StringBuilder();
                foreach (Name n in CURVE_RESPONSE_ELEMENTS)
                {
                    if (sb.Length != 0)
                    {
                        sb.Append(" ");
                    }
                    sb.Append(n).Append("=").Append(result.GetElementAsString(n));
                }
                Console.WriteLine(
                    "\t{0} {1} - {2} '{3}'",
                    i + 1,
                    result.GetElementAsString(CURVE_ELEMENT),
                    result.GetElementAsString(DESCRIPTION_ELEMENT),
                    sb.ToString());
            }
        }
Example #3
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
        }
Example #4
0
        /// <summary>
        /// Process a list of instruments in a response from the BLP API.
        /// </summary>
        /// <param name="msg"></param>
        private void ProcessInstrumentListResponse(Message msg)
        {
            Element results = msg.GetElement(RESULTS_ELEMENT);

            for (int i = 0; i < results.NumValues; i++)
            {
                instrument_results.Add(results.GetValueAsElement(i).GetElementAsString(SECURITY_ELEMENT));
            }
        }
Example #5
0
        /// <summary>
        /// Process a list of Govt instances in a response from the BLP API.
        /// Not fully implemented yet - does not collect results
        /// </summary>
        /// <param name="msg"></param>
        private void ProcessGovtListResponse(Message msg)
        {
            Element results    = msg.GetElement(RESULTS_ELEMENT);
            int     numResults = results.NumValues;

            Console.WriteLine("Processing " + numResults + " results:");
            for (int i = 0; i < numResults; ++i)
            {
                Element result = results.GetValueAsElement(i);
                Console.WriteLine(
                    "\t{0} {1}, {2} - {3}",
                    i + 1,
                    result.GetElementAsString(PARSEKY_ELEMENT),
                    result.GetElementAsString(NAME_ELEMENT),
                    result.GetElementAsString(TICKER_ELEMENT));
            }
        }
Example #6
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);
        }
Example #7
0
        private List <Bloomberglp.Blpapi.Element> ConvertElementArrayToList(Bloomberglp.Blpapi.Element argvElement)
        {
            List <Bloomberglp.Blpapi.Element> tReturnValue = new List <Element>();

            if (argvElement.IsArray)
            {
                for (int i = 0; i < argvElement.NumValues; i++)
                {
                    tReturnValue.Add(argvElement.GetValueAsElement(i));
                }
            }
            else
            {
                tReturnValue.Add(argvElement);
            }

            return(tReturnValue);
        }
        public List <String> SearchTicker(String ticker)
        {
            List <String> listTicker = new List <string>();

            // request.AsElement.SetElement("partialMatch", true);
            request.AsElement.SetElement("query", ticker);// this plus the previous line permits to retrieve all the thicker that begins with T
            request.AsElement.SetElement("languageOverride", "LANG_OVERRIDE_NONE");
            request.AsElement.SetElement("maxResults", 10);
            session.SendRequest(request, null);

            bool done = false;

            while (!done)
            {
                // Grab the next Event object
                Event eventObject = session.NextEvent();
                // If this event type is Response then process the messages
                if (eventObject.Type == Event.EventType.RESPONSE)
                {
                    // Loop over all of the messages in this Event
                    foreach (Message msg in eventObject.GetMessages())
                    {
                        Console.WriteLine(msg);
                        Element secDataArray = msg.GetElement("results");

                        for (int index = 0; index < secDataArray.NumValues - 1; index++)
                        {
                            Element fieldData = secDataArray.GetValueAsElement(index);
                            if (fieldData.HasElement("security"))
                            {
                                listTicker.Add(fieldData.GetElementAsString("security"));
                            }
                        }
                    }
                    done = true;
                }
            }

            return(listTicker);
        }
Example #9
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);
        }
Example #10
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();
                }
            }
        }