private void processFieldData( Element securityElem, String field, Map<String, ExternalIdBundle> bbgSecDomainMap, Map<ExternalIdBundle, HistoricalTimeSeries> result) { String secDes = securityElem.getElementAsString(BloombergConstants.SECURITY); ExternalIdBundle identifiers = bbgSecDomainMap.get(secDes); if (identifiers == null) { String message = "Found time series data for unrecognized security" + secDes + " " + bbgSecDomainMap; throw new OpenGammaRuntimeException(message); } HistoricalTimeSeries hts = result.get(identifiers); MapLocalDateDoubleTimeSeries timeSeries; if (hts == null) { timeSeries = new MapLocalDateDoubleTimeSeries(); hts = new SimpleHistoricalTimeSeries(UID_SUPPLIER.get(), timeSeries); result.put(identifiers, hts); } else { timeSeries = (MapLocalDateDoubleTimeSeries) hts.getTimeSeries(); } Element fieldDataArray = securityElem.getElement(FIELD_DATA); int numValues = fieldDataArray.numValues(); for (int i = 0; i < numValues; i++) { Element fieldData = fieldDataArray.getValueAsElement(i); Datetime date = fieldData.getElementAsDate("date"); double lastPrice = fieldData.getElementAsFloat64(field); int year = date.year(); int month = date.month(); int day = date.dayOfMonth(); timeSeries.putDataPoint(LocalDate.of(year, month, day), lastPrice); } }
/** * Transforms a Bloomberg Element into the most specific Object (for example: Double, Float, * Integer, DateTime, String etc.).<br> * Complex types are returned in the form of collections ({@code List<Object>} for arrays and * {@code Map<String, Object>} for sequences). */ public static Object getSpecificObjectOf(Element field) { if (field.datatype() == Schema.Datatype.FLOAT64) { // likeliest data type return field.getValueAsFloat64(); } else if (field.datatype() == Schema.Datatype.FLOAT32) { return field.getValueAsFloat32(); } else if (field.datatype() == Schema.Datatype.BOOL) { return field.getValueAsBool(); } else if (field.datatype() == Schema.Datatype.CHAR) { return field.getValueAsChar(); } else if (field.datatype() == Schema.Datatype.INT32) { return field.getValueAsInt32(); } else if (field.datatype() == Schema.Datatype.INT64) { return field.getValueAsInt64(); } else if (field.datatype() == Schema.Datatype.STRING) { return field.getValueAsString(); } else if (field.datatype() == Schema.Datatype.DATE || field.datatype() == Schema.Datatype.DATETIME || field.datatype() == Schema.Datatype.TIME) { return new DateTime(field.getValueAsDate().calendar()); } else if (field.isArray()) { List<Object> list = new ArrayList<>(field.numValues()); for (int i = 0; i < field.numValues(); i++) { list.add(getSpecificObjectOf(field.getValueAsElement(i))); } return list; } else if (field.datatype() == Schema.Datatype.SEQUENCE || field.datatype() == Schema.Datatype.CHOICE) { // has to be after array because arrays are sequences... ElementIterator it = field.elementIterator(); Map<String, Object> map = new LinkedHashMap<>(field.numElements(), 1.0f); while (it.hasNext()) { Element e = it.next(); map.put(e.name().toString(), getSpecificObjectOf(e)); } return map; } else { return field.toString(); // always works } }
protected void processFieldData( final Element fieldDataArray, String field, final List<LocalDate> dates, final List<Double> values) { int numValues = fieldDataArray.numValues(); for (int i = 0; i < numValues; i++) { Element fieldData = fieldDataArray.getValueAsElement(i); Datetime date = fieldData.getElementAsDate("date"); double fieldValue = fieldData.getElementAsFloat64(field); dates.add(LocalDate.of(date.year(), date.month(), date.dayOfMonth())); values.add(fieldValue); } }
@Override public void processMessage(Message msg) { final Element arraySecurityData = msg.getElement("securityData"); final int numItems = arraySecurityData.numValues(); for (int i = 0; i < numItems; ++i) { final Element securityData = arraySecurityData.getValueAsElement(i); final String security = securityData.getElementAsString("security"); Map<String, Map<String, String>> pv; if (answer.containsKey(security)) { pv = answer.get(security); } else { pv = new HashMap<>(); answer.put(security, pv); } final Map<String, String> values = new HashMap<>(); pv.put(period, values); if (securityData.hasElement("securityError")) { final String value = securityData.getElementAsString("securityError"); log.error("SecurityError:" + value); values.put("securityError", value); continue; } final Element fieldData = securityData.getElement("fieldData"); for (String field : fields) { if (fieldData.hasElement(field)) { try { String value = fieldData.getElementAsString(field); values.put(field, value); } catch (Exception e) { log.error(e); } } } } }
private LocalDateDoubleTimeSeries processRequest( String identifier, Request request, String field) { CorrelationID cid = submitBloombergRequest(request); BlockingQueue<Element> resultElements = getResultElement(cid); if (resultElements == null || resultElements.isEmpty()) { s_logger.info("Unable to get HistoricalTimeSeries for {}", identifier); return null; } List<LocalDate> dates = new ArrayList<LocalDate>(); List<Double> values = new ArrayList<Double>(); for (Element resultElem : resultElements) { if (resultElem.hasElement(RESPONSE_ERROR)) { s_logger.warn("Response error"); processError(resultElem.getElement(RESPONSE_ERROR)); } Element securityElem = resultElem.getElement(SECURITY_DATA); if (securityElem.hasElement(SECURITY_ERROR)) { processError(securityElem.getElement(SECURITY_ERROR)); return null; } if (securityElem.hasElement(FIELD_EXCEPTIONS)) { Element fieldExceptions = securityElem.getElement(FIELD_EXCEPTIONS); for (int i = 0; i < fieldExceptions.numValues(); i++) { Element fieldException = fieldExceptions.getValueAsElement(i); String fieldId = fieldException.getElementAsString(FIELD_ID); s_logger.warn("Field error on {}", fieldId); Element errorInfo = fieldException.getElement(ERROR_INFO); processError(errorInfo); } } if (securityElem.hasElement(FIELD_DATA)) { processFieldData(securityElem.getElement(FIELD_DATA), field, dates, values); } } return new ArrayLocalDateDoubleTimeSeries(dates, values); }
@Override public void processMessage(Message message) { Element ReferenceDataResponse = message.asElement(); Element securityDataArray = ReferenceDataResponse.getElement("securityData"); for (int i = 0; i < securityDataArray.numValues(); ++i) { final Element securityData = securityDataArray.getValueAsElement(i); final String security = securityData.getElementAsString("security"); if (securityData.hasElement("securityError")) { log.error("SecurityError:" + securityData.getElement("securityError")); continue; } final Element fieldData = securityData.getElement("fieldData"); for (String p : fields) { if (p.equals("BEST_ANALYST_RECS_BULK")) { final List<BEST_ANALYST_RECS_BULK> values = new ArrayList<>(); bestAnalyst.put(security, values); final Element best_anal_recs_bulk = fieldData.getElement("BEST_ANALYST_RECS_BULK"); for (int m = 0; m < best_anal_recs_bulk.numValues(); m++) { final Element e = best_anal_recs_bulk.getValueAsElement(m); values.add(new BEST_ANALYST_RECS_BULK(e)); } } if (p.equals("EARN_ANN_DT_TIME_HIST_WITH_EPS")) { final List<EARN_ANN_DT_TIME_HIST_WITH_EPS> values = new ArrayList<>(); earnHistWithEps.put(security, values); final Element element = fieldData.getElement("EARN_ANN_DT_TIME_HIST_WITH_EPS"); for (int t = 0; t < element.numValues(); t++) { final Element e = element.getValueAsElement(t); values.add(new EARN_ANN_DT_TIME_HIST_WITH_EPS(e)); } } if (p.equals("ERN_ANN_DT_AND_PER")) { final List<ERN_ANN_DT_AND_PER> values = new ArrayList<>(); ernAnnDTandPer.put(security, values); final Element element = fieldData.getElement("ERN_ANN_DT_AND_PER"); final int ernItems = element.numValues(); for (int j = 0; j < ernItems; ++j) { final Element e = element.getValueAsElement(j); values.add(new ERN_ANN_DT_AND_PER(e)); } } if (p.equals("BLOOMBERG_PEERS")) { final List<String> values = new ArrayList<String>(); peerTicker.put(security, values); final Element blm_peers = fieldData.getElement("BLOOMBERG_PEERS"); for (int j = 0; j < blm_peers.numValues(); ++j) { final Element e = blm_peers.getValueAsElement(j); final String peer = e.getElementAsString("Peer Ticker"); values.add(peer); peers.add(peer); } } } } }
// ------------------------------------------------------------------------- @Override public Map<ExternalIdBundle, HistoricalTimeSeries> getHistoricalTimeSeries( Set<ExternalIdBundle> identifierSet, String dataSource, String dataProvider, String dataField, LocalDate start, boolean includeStart, LocalDate end, boolean includeEnd) { ArgumentChecker.notNull(identifierSet, "identifierSet"); ArgumentChecker.notNull(dataField, "dataField"); ArgumentChecker.notNull(start, "start"); ArgumentChecker.notNull(end, "end"); Validate.isTrue( ObjectUtils.equals(dataSource, BLOOMBERG_DATA_SOURCE_NAME), getClass().getName() + "cannot support " + dataSource); if (end.isBefore(start)) { throw new IllegalArgumentException("end must be after start"); } ensureStarted(); s_logger.debug("Getting historical data for {}", identifierSet); if (identifierSet.isEmpty()) { s_logger.info("Historical data request for empty identifier set"); return Collections.emptyMap(); } Map<String, ExternalIdBundle> bbgSecDomainMap = new HashMap<String, ExternalIdBundle>(); Request request = getRefDataService().createRequest(BLOOMBERG_HISTORICAL_DATA_REQUEST); Element securitiesElem = request.getElement(BLOOMBERG_SECURITIES_REQUEST); for (ExternalIdBundle identifiers : identifierSet) { ExternalId preferredIdentifier = BloombergDomainIdentifierResolver.resolvePreferredIdentifier(identifiers); s_logger.debug( "Resolved preferred identifier {} from identifier bundle {}", preferredIdentifier, identifiers); String bbgKey = BloombergDomainIdentifierResolver.toBloombergKeyWithDataProvider( preferredIdentifier, dataProvider); securitiesElem.appendValue(bbgKey); bbgSecDomainMap.put(bbgKey, identifiers); } Element fieldElem = request.getElement(BLOOMBERG_FIELDS_REQUEST); fieldElem.appendValue(dataField); // TODO: inclusive start / exclusive end request.set("periodicityAdjustment", "ACTUAL"); request.set("periodicitySelection", "DAILY"); request.set("startDate", printYYYYMMDD(start)); request.set("endDate", printYYYYMMDD(end)); request.set("adjustmentSplit", true); _statistics.gotFields(bbgSecDomainMap.keySet(), Collections.singleton(dataField)); CorrelationID cid = submitBloombergRequest(request); BlockingQueue<Element> resultElements = getResultElement(cid); if (resultElements == null || resultElements.isEmpty()) { s_logger.warn("Unable to get historical data for {}", identifierSet); return null; } // REVIEW simon 2011/11/01: should this be deduped with the single case? Map<ExternalIdBundle, HistoricalTimeSeries> result = new HashMap<ExternalIdBundle, HistoricalTimeSeries>(); for (Element resultElem : resultElements) { if (resultElem.hasElement(RESPONSE_ERROR)) { s_logger.warn("Response error"); processError(resultElem.getElement(RESPONSE_ERROR)); continue; } Element securityElem = resultElem.getElement(SECURITY_DATA); if (securityElem.hasElement(SECURITY_ERROR)) { processError(securityElem.getElement(SECURITY_ERROR)); } if (securityElem.hasElement(FIELD_EXCEPTIONS)) { Element fieldExceptions = securityElem.getElement(FIELD_EXCEPTIONS); for (int i = 0; i < fieldExceptions.numValues(); i++) { Element fieldException = fieldExceptions.getValueAsElement(i); String fieldId = fieldException.getElementAsString(FIELD_ID); s_logger.warn("Field error on {}", fieldId); Element errorInfo = fieldException.getElement(ERROR_INFO); processError(errorInfo); } } if (securityElem.hasElement(FIELD_DATA)) { processFieldData(securityElem, dataField, bbgSecDomainMap, result); } } if (identifierSet.size() != result.size()) { s_logger.warn( "Failed to get time series results for ({}/{}) {}", new Object[] { identifierSet.size() - result.size(), identifierSet.size(), Sets.difference(identifierSet, result.keySet()) }); } return result; }