/// <summary> /// Process SubscriptionStatus event /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processSubscriptionStatus(Event eventObj, Session session) { System.Console.WriteLine("Processing SUBSCRIPTION_STATUS"); foreach (Message msg in eventObj.GetMessages()) { string topic = (string)msg.CorrelationID.Object; System.Console.WriteLine(System.DateTime.Now.ToString("s") + ": " + topic + " - " + msg.MessageType); if (msg.HasElement(REASON)) { // This can occur on SubscriptionFailure. 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 at least // one field is good while the rest are bad. 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(""); } }
private void eventLoop(Session session) { bool done = false; while (!done) { Event eventObj = session.NextEvent(); if (eventObj.Type == Event.EventType.PARTIAL_RESPONSE) { System.Console.WriteLine("Processing Partial Response"); processResponseEvent(eventObj); } else if (eventObj.Type == Event.EventType.RESPONSE) { System.Console.WriteLine("Processing Response"); processResponseEvent(eventObj); done = true; } else { foreach (Message msg in eventObj.GetMessages()) { System.Console.WriteLine(msg.AsElement); if (eventObj.Type == Event.EventType.SESSION_STATUS) { if (msg.MessageType.Equals("SessionTerminated")) { done = true; } } } } } }
/// <summary> /// Process events other than subscription data/status event /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processMiscEvents(Event eventObj, Session session) { System.Console.WriteLine("Processing " + eventObj.Type); foreach (Message msg in eventObj.GetMessages()) { System.Console.WriteLine(System.DateTime.Now.ToString("s") + ": " + msg.MessageType + "\n"); } }
// return true if processing is completed, false otherwise private void processResponseEvent(Event eventObj) { foreach (Message msg in eventObj.GetMessages()) { if (msg.HasElement(RESPONSE_ERROR)) { printErrorInfo("REQUEST FAILED: ", msg.GetElement(RESPONSE_ERROR)); continue; } Element data = msg.GetElement(DATA); Element securities = data.GetElement(SECURITY_DATA); int numSecurities = securities.NumValues; System.Console.WriteLine("Processing " + numSecurities + " securities:"); for (int i = 0; i < numSecurities; ++i) { Element security = securities.GetValueAsElement(i); string ticker = security.GetElementAsString(SECURITY); System.Console.WriteLine("\nTicker: " + ticker); if (security.HasElement("securityError")) { printErrorInfo("\tSECURITY FAILED: ", security.GetElement(SECURITY_ERROR)); continue; } Element fields = security.GetElement(FIELD_DATA); if (fields.NumElements > 0) { System.Console.WriteLine("FIELD\t\tVALUE"); System.Console.WriteLine("-----\t\t-----"); int numElements = fields.NumElements; for (int j = 0; j < numElements; ++j) { Element field = fields.GetElement(j); System.Console.WriteLine(field.Name + "\t\t" + field.GetValueAsString()); } } System.Console.WriteLine(""); Element fieldExceptions = security.GetElement(FIELD_EXCEPTIONS); if (fieldExceptions.NumValues > 0) { System.Console.WriteLine("FIELD\t\tEXCEPTION"); System.Console.WriteLine("-----\t\t---------"); for (int k = 0; k < fieldExceptions.NumValues; ++k) { Element fieldException = fieldExceptions.GetValueAsElement(k); printErrorInfo(fieldException.GetElementAsString(FIELD_ID) + "\t\t", fieldException.GetElement(ERROR_INFO)); } } } } }
/// <summary> /// Process SubscriptionData event /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processSubscriptionDataEvent(Event eventObj, Session session) { System.Console.WriteLine("Processing SUBSCRIPTION_DATA"); foreach (Message msg in eventObj.GetMessages()) { string topic = (string)msg.CorrelationID.Object; System.Console.WriteLine(System.DateTime.Now.ToString("s") + ": " + topic + " - " + msg.MessageType); //System.Console.WriteLine(msg.AsElement); if (msg.MessageType.Equals("PageUpdate")) { processPageElement(msg.AsElement, topic); } else if (msg.MessageType.Equals("RowUpdate")) { processRowElement(msg.AsElement, topic); } //showUpdatedPage(topic); } }
/// <summary> /// Polls for an event or a message in an event loop /// & Processes the event generated /// </summary> /// <param name="session"></param> private void eventLoop(Session session) { while (true) { Event eventObj = session.NextEvent(); foreach (Message msg in eventObj.GetMessages()) { if (eventObj.Type == Event.EventType.SUBSCRIPTION_STATUS) { System.Console.WriteLine("Processing SUBSCRIPTION_STATUS"); string topic = (string)msg.CorrelationID.Object; System.Console.WriteLine(System.DateTime.Now.ToString("s") + ": " + topic + ": " + msg.AsElement); } else if (eventObj.Type == Event.EventType.SUBSCRIPTION_DATA) { System.Console.WriteLine("\nProcessing SUBSCRIPTION_DATA"); string topic = (string)msg.CorrelationID.Object; System.Console.WriteLine(System.DateTime.Now.ToString("s") + ": " + topic + " - " + msg.MessageType); foreach (Element field in msg.Elements) { if (!field.IsNull) { System.Console.WriteLine("\t\t" + field.Name + " = " + field.GetValueAsString()); } } } else { System.Console.WriteLine(msg.AsElement); } } } }
/// <summary> /// Process subscription data /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processRequestDataEvent(Event eventObj, Session session) { string securityName = string.Empty; // process message foreach (Message msg in eventObj.GetMessages()) { if (msg.MessageType.Equals(Bloomberglp.Blpapi.Name.GetName("ReferenceDataResponse"))) { // process errors if (msg.HasElement(RESPONSE_ERROR)) { Element error = msg.GetElement(RESPONSE_ERROR); Error(String.Format("{0}, {1}", kKospiIndex, error.GetElementAsString(MESSAGE))); } else { Element secDataArray = msg.GetElement(SECURITY_DATA); int numberOfSecurities = secDataArray.NumValues; if (secDataArray.HasElement(SECURITY_ERROR)) { // security error Element secError = secDataArray.GetElement(SECURITY_ERROR); Element security = secDataArray.GetElement(SECURITY); Error(secError.GetElementAsString(MESSAGE)); Error(security.ToString()); } if (secDataArray.HasElement(FIELD_EXCEPTIONS)) { // field error Element error = secDataArray.GetElement(FIELD_EXCEPTIONS); for (int errorIndex = 0; errorIndex < error.NumValues; errorIndex++) { Element errorException = error.GetValueAsElement(errorIndex); string field = errorException.GetElementAsString(FIELD_ID); Element errorInfo = errorException.GetElement(ERROR_INFO); string message = errorInfo.GetElementAsString(MESSAGE); logger.Warn("{0} has field error.", kKospiIndex); } // end for } // end if // process securities for (int index = 0; index < numberOfSecurities; index++) { Element secData = secDataArray.GetValueAsElement(index); Element fieldData = secData.GetElement("fieldData"); if (fieldData.HasElement(kIndexWeight)) { Element weightField = fieldData.GetElement(kIndexWeight); for (int i = 0; i < weightField.NumValues; ++i) { String memberTicker = ""; double weight = 0.0; Element row = weightField.GetValueAsElement(i); if (row.HasElement("Member Ticker and Exchange Code")) { Element item = row.GetElement("Member Ticker and Exchange Code"); memberTicker = item.GetValueAsString(); } else { continue; } if (row.HasElement("Percentage Weight")) { Element item = row.GetElement("Percentage Weight"); weight = item.GetValueAsFloat64(); } else { continue; } _resultDict.Add(memberTicker + " Equity", weight); } // logger.Info(fieldStr); } } // end for } // end else } // end if } // end foreach }
/// <summary> /// Request status event /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processMiscEvents(Event eventObj, Session session) { foreach (Message msg in eventObj.GetMessages()) { switch (msg.MessageType.ToString()) { case "SessionStarted": // "Session Started" break; case "SessionTerminated": case "SessionStopped": // "Session Terminated" break; case "ServiceOpened": // "Reference Service Opened" break; case "RequestFailure": Element reason = msg.GetElement(REASON); string message = string.Concat("Error: Source-", reason.GetElementAsString(SOURCE), ", Code-", reason.GetElementAsString(ERROR_CODE), ", category-", reason.GetElementAsString(CATEGORY), ", desc-", reason.GetElementAsString(DESCRIPTION)); logger.Info(message); break; default: logger.Info(msg.MessageType.ToString()); break; } } }
/// <summary> /// Process subscription data /// </summary> /// <param name="eventObj"></param> /// <param name="session"></param> private void processRequestDataEvent(Event eventObj, Session session) { string securityName = string.Empty; Boolean hasFieldError = false; // clear column tag of field error message. foreach (DataGridViewColumn col in dataGridViewData.Columns) { col.Tag = null; } // process message foreach (Message msg in eventObj.GetMessages()) { if (msg.MessageType.Equals(Bloomberglp.Blpapi.Name.GetName("HistoricalDataResponse"))) { // process errors if (msg.HasElement(RESPONSE_ERROR)) { Element error = msg.GetElement(RESPONSE_ERROR); Error(error.GetElementAsString(MESSAGE)); } else { Element secDataArray = msg.GetElement(SECURITY_DATA); int numberOfSecurities = secDataArray.NumValues; if (secDataArray.HasElement(SECURITY_ERROR)) { // security error Element secError = secDataArray.GetElement(SECURITY_ERROR); Element security = secDataArray.GetElement(SECURITY); Error(secError.GetElementAsString(MESSAGE)); Error(security.ToString()); } if (secDataArray.HasElement(FIELD_EXCEPTIONS)) { // field error Element error = secDataArray.GetElement(FIELD_EXCEPTIONS); for (int errorIndex = 0; errorIndex < error.NumValues; errorIndex++) { Element errorException = error.GetValueAsElement(errorIndex); string field = errorException.GetElementAsString(FIELD_ID); Element errorInfo = errorException.GetElement(ERROR_INFO); string message = errorInfo.GetElementAsString(MESSAGE); dataGridViewData.Columns[field].Tag = message; hasFieldError = true; } // end for } // end if // process securities for (int index = 0; index < numberOfSecurities; index++) { foreach (Element secData in secDataArray.Elements) { switch (secData.Name.ToString()) { case "eidsData": // process security eid data here break; case "security": // security name securityName = secData.GetValueAsString(); break; case "fieldData": if (hasFieldError && secData.NumValues == 0) { // no data but have field error object[] dataValues = new object[dataGridViewData.ColumnCount]; dataValues[0] = securityName; int fieldIndex = 0; foreach (DataGridViewColumn col in dataGridViewData.Columns) { if (col.Tag != null) { dataValues[fieldIndex] = col.Tag.ToString(); } fieldIndex++; } d_data.Rows.Add(dataValues); } else { // get field data d_data.BeginLoadData(); for (int pointIndex = 0; pointIndex < secData.NumValues; pointIndex++) { int fieldIndex = 0; object[] dataValues = new object[dataGridViewData.ColumnCount]; Element fields = secData.GetValueAsElement(pointIndex); foreach (DataGridViewColumn col in dataGridViewData.Columns) { try { if (col.Name == "security") dataValues[fieldIndex] = securityName; else { if (fields.HasElement(col.Name)) { Element item = fields.GetElement(col.Name); if (item.IsArray) { // bulk field data dataValues[fieldIndex] = "Bulk Data"; } else { // field data dataValues[fieldIndex] = item.GetValueAsString(); } } else { // no field value if (col.Tag.ToString().Length > 0) { // field has error dataValues[fieldIndex] = col.Tag.ToString(); } else dataValues[fieldIndex] = DBNull.Value; } } // end if } catch (Exception ex) { // display error dataValues[fieldIndex] = ex.Message; } finally { fieldIndex++; } } // end foreach // add data to data table d_data.Rows.Add(dataValues); } // end for d_data.EndLoadData(); } break; } // end switch } // end foreach } // end for } // end else } // end if } // end foreach }