我目前正在开展一个项目,可以帮助我为给定的股票创建隐含的波动率表面.为此,我正在编写一个脚本,将下载此特定股票的所有可用选项 – 通过我收集的内容,可以通过Bloomberg API使用批量字段/覆盖发送请求.这是我目前的代码:
d_host = "localhost"; d_port = 8194; SessionOptions sessionOptions; sessionOptions.setServerHost(d_host.c_str()); sessionOptions.setServerPort(d_port); Session session(sessionOptions); Service refDataService = session.getService("//blp/refdata"); Request request = refDataService.createRequest("ReferenceDataRequest"); request.append("securities","MSFT US EQUITY"); request.append("fields","CHAIN_TICKERS"); // add overrides Element overrides = request.getElement("overrides"); Element override1 = overrides.appendElement(); override1.setElement("fieldId","CHAIN_PUT_CALL_TYPE_OVRD"); override1.setElement("value","C"); Element override2 = overrides.appendElement(); override2.setElement("fieldId","CHAIN_POINTS_OVRD"); override2.setElement("value",100); Element override3 = overrides.appendElement(); override3.setElement("fieldId","CHAIN_EXP_DT_OVRD"); override3.setElement("value","20250203"); std::cout << "Sending Request: " << request << std::endl; CorrelationId cid(this); session.sendRequest(request,cid); (followed by event handling)
现在我有几个问题/问题:
>代码编译没有问题,但在Bloomberg终端上运行时,会打印以下错误:
我该如何解决这个问题呢?我假设我在覆盖字段的某个地方犯了一个错误.
>我需要调整我的代码以下载特定成熟度下的所有可用选项,即我希望获得所有选项的列表,直到今天15年.
>然后,我如何下载每个期权的隐含波动率?我是否需要将代码存储在一个数组中,然后为每个选项发送一个“IVOL_MID”字段的请求,或者是否有某种方法可以立即获得所有的波动率?
编辑:这是我的事件处理程序的代码,因为这似乎是问题.
session.sendRequest(request,cid); while (true) { Event event = session.nextEvent(); MessageIterator msgIter(event); while (msgIter.next()) { Message msg = msgIter.message(); if (msg.correlationId() == cid) { processMessage(msg); } } if (event.eventType() == Event::RESPONSE) { break; } } void processMessage(Message &msg) { Element securityDataArray = msg.getElement(SECURITY_DATA); int numSecurities = securityDataArray.numValues(); for (int i = 0; i < numSecurities; ++i) { Element securityData = securityDataArray.getValueAsElement(i); std::cout << securityData.getElementAsString(SECURITY) << std::endl; const Element fieldData = securityData.getElement(FIELD_DATA); for (size_t j = 0; j < fieldData.numElements(); ++j) { Element field = fieldData.getElement(j); if (!field.isValid()) { std::cout << field.name() << " is NULL." << std::endl; } else { std::cout << field.name() << " = " << field.getValueAsString() << std::endl; } } Element fieldExceptionArray = securityData.getElement(FIELD_EXCEPTIONS); for (size_t k = 0; k < fieldExceptionArray.numValues(); ++k) { Element fieldException = fieldExceptionArray.getValueAsElement(k); std::cout << fieldException.getElement(ERROR_INFO).getElementAsString( "category") << ": " << fieldException.getElementAsString(FIELD_ID); } std::cout << std::endl; }
解决方法
问题出在您未显示的事件处理代码中.你可能错误地解析它.
运行您的查询我得到以下结果:
MSFT US 01/20/17 C23 MSFT US 01/20/17 C25 MSFT US 01/20/17 C30 MSFT US 01/20/17 C33 MSFT US 01/20/17 C35 MSFT US 01/20/17 C38 MSFT US 01/20/17 C40 MSFT US 01/20/17 C43 MSFT US 01/20/17 C45 MSFT US 01/20/17 C47 MSFT US 01/20/17 C50 MSFT US 01/20/17 C52.5 MSFT US 01/20/17 C55 MSFT US 01/20/17 C57.5 MSFT US 01/20/17 C60 MSFT US 01/20/17 C65 MSFT US 01/20/17 C70
注意:我使用的是Java API,但它基本相同.
更新:
您的代码不能正确解析字段数据数组元素:返回的数据包含一系列序列,因此您需要分两步解析它.而不是field.getValueAsString(),你应该有一个看起来像这样的代码(它在Java中,没有经过测试):
//... for (int i = 0; i < field.numValues(); i++) { Element sequence = field.getValueAsElement(i); ElementIterator it = sequence.elementIterator(); while (it.hasNext()) { Element e = it.next(); System.out.println(e.getValueAsString()); }
如果这不起作用,我建议您逐步调试代码并检查您收到的数据类型并相应地处理它.
有关详细信息,请阅读Developer’s guide,特别是A.2.3.