// ------------------------------------------------------------------------- private Request composeRequest( final String identifier, final String dataSource, final String dataProvider, final String field, LocalDate startDate, LocalDate endDate, Integer maxPoints) { ArgumentChecker.notNull(identifier, "identifier must not be null or empty"); Request request = getRefDataService().createRequest(BLOOMBERG_HISTORICAL_DATA_REQUEST); Element securitiesElem = request.getElement(BLOOMBERG_SECURITIES_REQUEST); securitiesElem.appendValue(identifier); Element fieldElem = request.getElement(BLOOMBERG_FIELDS_REQUEST); fieldElem.appendValue(field == null ? "PX_LAST" : field); request.set("periodicityAdjustment", "ACTUAL"); request.set("periodicitySelection", "DAILY"); request.set("startDate", DateUtils.printYYYYMMDD(startDate)); request.set("endDate", DateUtils.printYYYYMMDD(endDate)); request.set("returnEids", true); if (maxPoints != null && maxPoints <= 0) { request.set("maxDataPoints", -maxPoints); } return request; }
public EARN_ANN_DT_TIME_HIST_WITH_EPS(Element e) { this.year_period = e.getElementAsString("Year/Period"); this.announsment_date = e.getElementAsString("Announcement Date"); this.announsment_time = e.getElementAsString("Announcement Time"); this.earnings_EPS = e.getElementAsString("Earnings EPS"); this.comparable_EPS = e.getElementAsString("Comparable EPS"); this.estimate_EPS = e.getElementAsString("Estimate EPS"); }
protected void processError(Element element) { int code = element.getElementAsInt32("code"); String category = element.getElementAsString("category"); String subcategory = element.getElementAsString("subcategory"); String message = element.getElementAsString("message"); String errorMessage = MessageFormat.format(ERROR_MESSAGE_FORMAT, code, category, subcategory, message); s_logger.warn(errorMessage); }
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); } }
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); } }
public BEST_ANALYST_RECS_BULK(Element element) { this.firm = element.getElementAsString("Firm Name"); this.analyst = element.getElementAsString("Analyst"); this.recom = element.getElementAsString("Recommendation"); this.rating = element.getElementAsString("Rating"); this.action_code = element.getElementAsString("Action Code"); this.target_price = element.getElementAsString("Target Price"); this.period = element.getElementAsString("Period"); this.date = element.getElementAsString("Date"); this.barr = element.getElementAsString("BARR"); this.year_return = element.getElementAsString("1 Year Return"); }
/** * 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 } }
@Override public void execute(String name) { final BloombergSession bs = new BloombergSession(name); bs.start(); try { final Request request = bs.createRequest("//blp/refdata", "ReferenceDataRequest"); Element _securities = request.getElement("securities"); for (String security : securities) { _securities.appendValue(security); } Element _fields = request.getElement("fields"); for (String field : fields) { _fields.appendValue(field); } bs.sendRequest(request, this); if (!peers.isEmpty()) { final Request requestP = bs.createRequest("//blp/refdata", "ReferenceDataRequest"); final Element securitiesP = requestP.getElement("securities"); for (String peer : peers) { securitiesP.appendValue(peer + " Equity"); } final Element fieldsP = requestP.getElement("fields"); fieldsP.appendValue("CUR_MKT_CAP"); fieldsP.appendValue("OPER_ROE"); fieldsP.appendValue("BS_TOT_LIAB2"); fieldsP.appendValue("PE_RATIO"); fieldsP.appendValue("INDUSTRY_subGROUP"); fieldsP.appendValue("INDUSTRY_GROUP"); fieldsP.appendValue("EBITDA"); bs.sendRequest( requestP, new MessageHandler() { @Override public void processMessage(Message message) { Element ReferenceDataResponse = message.asElement(); if (ReferenceDataResponse.hasElement("responseError")) { log.error("*****ResponceError*****"); } Element securityDataArray = ReferenceDataResponse.getElement("securityData"); int numItems = securityDataArray.numValues(); for (int i = 0; i < numItems; ++i) { Element securityData = securityDataArray.getValueAsElement(i); String security = securityData.getElementAsString("security"); if (securityData.hasElement("securityError")) { log.error("SecurityError:" + securityData.getElement("securityError")); return; } else { Element fieldData = securityData.getElement("fieldData"); final PeerData item = new PeerData(security, fieldData); peersData.add(item); } } } }, new CorrelationID(1)); } } finally { bs.stop(); } }
@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); } } } } }
/** @param e */ public ERN_ANN_DT_AND_PER(Element e) { this.ead = e.getElementAsString("Earnings Announcement Date"); this.eyap = e.getElementAsString("Earnings Year and Period"); }
// ------------------------------------------------------------------------- @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; }
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 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); } } } } }
@Override public void execute(String name) { final BloombergSession bs = new BloombergSession(name); bs.start(); try { final Service service = bs.getService("//blp/refdata"); for (String crncy : currency) { final String idq[] = {"1CQ", "2CQ", "3CQ", "4CQ", "1CS", "2CS", "1CY", "2CY"}; for (final String quarter : idq) { final Request request = service.createRequest("ReferenceDataRequest"); final Element _securities = request.getElement("securities"); for (String s : securities) { final int p = s.indexOf('|'); if (crncy.equals(s.substring(p + 1))) { _securities.appendValue(s.substring(0, p)); } } final Element _fields = request.getElement("fields"); for (String field : fields) { _fields.appendValue(field); } final Element overrides = request.getElement("overrides"); final Element overridePeriod = overrides.appendElement(); overridePeriod.setElement("fieldId", "BEST_FPERIOD_OVERRIDE"); overridePeriod.setElement("value", period = quarter); final Element overrideCrncy = overrides.appendElement(); overrideCrncy.setElement("fieldId", "EQY_FUND_CRNCY"); overrideCrncy.setElement("value", crncy); if (null != override) { final Element overrideDataSource = overrides.appendElement(); overrideDataSource.setElement("fieldId", "BEST_DATA_SOURCE_OVERRIDE"); overrideDataSource.setElement("value", override); } bs.sendRequest(request, this); } } } finally { bs.stop(); } }