/// <summary> /// Send a request to the BLP API. Called by Query(). /// </summary> /// <param name="session"></param> /// <param name="identity"></param> private void SendRequest(Session session, Identity identity) { Console.WriteLine("Sending Request: {0}", d_requestType.ToString()); Service instrumentService = session.GetService(INSTRUMENT_SERVICE); Request request; try { request = instrumentService.CreateRequest(d_requestType.ToString()); } catch (NotFoundException e) { throw new Exception( string.Format("Request Type not found: {0}", d_requestType), e); } request.Set(QUERY_ELEMENT, d_queryString); request.Set(MAX_RESULTS_ELEMENT, d_maxResults); foreach (KeyValuePair <string, string> entry in d_filters) { try { request.Set(entry.Key, entry.Value); } catch (NotFoundException e) { throw new Exception(string.Format("Filter not found: {0}", entry.Key), e); } catch (InvalidConversionException e) { throw new Exception( string.Format( "Invalid value: {0} for filter: {1}", entry.Value, entry.Key), e); } } request.Print(Console.Out); Console.WriteLine(); session.SendRequest(request, identity, null); }
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); }
public IEnumerable <RefDataItem> GetRefData(IList <string> tickers, IList <string> fields) { var req = this.service.CreateRequest("ReferenceDataRequest"); var rsecurities = req.GetElement("securities"); foreach (var tk in tickers) { rsecurities.AppendValue(tk.Replace("_", " ")); } var rfields = req.GetElement("fields"); foreach (var f in fields) { rfields.AppendValue(f); } session.SendRequest(req, null); while (true) { var e = session.NextEvent(); if (e.Type == Event.EventType.RESPONSE || e.Type == Event.EventType.PARTIAL_RESPONSE) { foreach (var msg in e) { Log("--" + msg.MessageType.ToString()); var securities = msg.GetElement(SECURITY_DATA); for (int i = 0; i < securities.NumValues; i++) { var sec = securities.GetValueAsElement(i); var tk = sec.GetElementAsString(SECURITY); if (sec.HasElement("securityError")) { Log("Invalid Security: " + tk, InfoLevel.Warn); continue; } var ffs = sec.GetElement(FIELD_DATA); if (ffs.NumElements > 0) { for (int j = 0; j < ffs.NumElements; j++) { var fieldData = ffs.GetElement(j); var fieldName = fieldData.Name.ToString(); var fieldValue = fieldData.GetValueAsString(); Log("---- " + fieldName + ": " + fieldValue); yield return(new RefDataItem() { Security = tk, Field = fieldName, Value = fieldValue }); } } var ferrs = sec.GetElement(FIELD_EXCEPTIONS); for (int j = 0; j < ferrs.NumValues; j++) { var ferr = ferrs.GetValueAsElement(j); var ferrName = ferr.GetElementAsString(FIELD_ID); var ferrInfo = ferr.GetElement(ERROR_INFO) .GetElementAsString("message"); Log("Field Error for '" + tk + "': [" + ferrName + "] " + ferrInfo, InfoLevel.Error); } } } } //--- this means the complete message is done if (e.Type == Event.EventType.RESPONSE) { yield break; } } }
/// <summary> /// Gets the Bloomberg data via the specified request type /// </summary> /// <param name="instruments">The list of instruments to retrieve data for</param> /// <param name="dataFromDate">The start date of the data being requested</param> /// <param name="dataToDate">The end date of the data being requested</param> /// <param name="requestType">The type of request i.e. Reference/Historical</param> private void GetBloombergData(List <BloombergDataInstrument> instruments, DateTime dataFromDate, DateTime dataToDate, string requestType) { try { sentLimitEmail = false; Initialise(BLP_REF, instruments.Count); BB.Service service = session.GetService(BLP_REF); guids = new List <Guid>(); sentToBB = instruments; // new List<BloombergDataInstrument>(); ShowCompletionPercentage(0, instruments.Count); foreach (BloombergDataInstrument bbdi in instruments) { BB.Request request = service.CreateRequest(requestType); if (requestType == BLP_HISTORICAL_REQUEST) { request.Set("startDate", dataFromDate.ToString("yyyyMMdd")); request.Set("endDate", dataToDate.ToString("yyyyMMdd")); } BB.Element securities = request.GetElement(BLP_SECURITIES); // check for sedol ticker which must be in the correct format string ticker = bbdi.Ticker; if (ticker.EndsWith("SEDOL1")) { ticker = @"/SEDOL1/" + ticker.Replace(" SEDOL1", string.Empty); } // set all the securities to fetch securities.AppendValue(ticker); // set all the fields BB.Element fields = request.GetElement(BLP_FIELDS); foreach (string field in bbdi.BBFields.Keys) { fields.AppendValue(field); // now do the overrides - if they exist if (bbdi.BBFields[field].FieldOverrides != null) { BB.Element requestOverrides = request.GetElement(BLP_OVERRIDES); foreach (string oField in bbdi.BBFields[field].FieldOverrides.Keys) { object oValue = bbdi.BBFields[field].FieldOverrides[oField]; // now add in the override oField and oValue BB.Element ovr = requestOverrides.AppendElement(); ovr.SetElement(FIELD_ID, oField); ovr.SetElement(VALUE, oValue.ToString()); } } } session.SendRequest(request, new BB.CorrelationID(bbdi.GUID)); } UpdateStatus(string.Format("Sent {0} instruments\\requests to Bloomberg", sentToBB.Count)); } catch (Exception ex) { UpdateStatus(ex.Message); throw new Exception("An error occurred whilst sending requests to Bloomberg - " + ex.Message, ex); } }