Bloomberg Equity Option Chain 通过 API

Bloomberg Equity Option Chain through API

我目前正在进行一个项目,该项目可以帮助我为给定股票创建隐含波动率表面。为此,我正在编写一个脚本,它将下载该特定股票的所有可用选项 - 根据我收集到的信息,这可以通过使用批量 fields/overrides 通过彭博社 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)

现在我有几个issues/questions:

  1. 代码编译没有问题,但是当运行它在彭博终端上时,会打印出以下错误: 我将如何解决这个问题?我假设我在覆盖字段的某个地方犯了一个错误..

  2. 我需要如何调整我的代码才能下载特定期限内可用的所有选项,即我想获得直到今天 + 15 年的所有选项的列表。

  3. 我该如何下载每个期权的隐含波动率?我是否需要将代码存储在一个数组中,然后为每个选项发送字段 "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。

如另一个答案所示,问题出在事件处理上,因此我使用 Bloomberg API emulator.

中的一些示例重写了该部分
session.sendRequest(request, cid);
bool continueToLoop = true;
while (continueToLoop)
{
    Event evt = session.nextEvent();
    switch (evt.eventType())
    {
        case Event::RESPONSE:
            continueToLoop = false; //fall through
        case Event::PARTIAL_RESPONSE:
            ProcessReferenceDataEvent(evt);
        break;
     }
}

void ProcessReferenceDataEvent(Event evt)
{
    const string level1 = "";
    const string level2 = "\t";
    const string level3 = "\t\t";
    const string level4 = "\t\t\t";

    std::cout << endl << endl;
    std::cout << level1 << "EventType = " << evt.eventType();

    MessageIterator iter(evt);
    while (iter.next())
    {
        Message msg = iter.message();

        std::cout << endl << endl;
        std::cout << level1 << "correlationID = " << msg.correlationId().asInteger() << endl;
        std::cout << level1 << "messageType = " << msg.messageType().string() << endl;
        std::cout << endl << endl;
        Element SecurityDataArray = msg.getElement(SECURITY_DATA);
        int numSecurities = SecurityDataArray.numValues();
        for (int valueIndex = 0; valueIndex < numSecurities; valueIndex++)
        {
            Element SecurityData = SecurityDataArray.getValueAsElement(valueIndex);

            string Security = SecurityData.getElementAsString(SECURITY);
            std::cout << level2 << Security << endl;

            bool hasFieldErrors = SecurityData.hasElement("fieldExceptions", true);
            if (hasFieldErrors)
            {
                Element FieldErrors = SecurityData.getElement(FIELD_EXCEPTIONS);
                for (size_t errorIndex = 0; errorIndex < FieldErrors.numValues(); errorIndex++)
                {
                    Element fieldError = FieldErrors.getValueAsElement(errorIndex);
                    string fieldId = fieldError.getElementAsString(FIELD_ID);

                    Element errorInfo = fieldError.getElement(ERROR_INFO);
                    string source = errorInfo.getElementAsString("source");
                    int code = errorInfo.getElementAsInt32("code");
                    string category = errorInfo.getElementAsString("category");
                    string strMessage = errorInfo.getElementAsString("message");
                    string subCategory = errorInfo.getElementAsString("subcategory");

                    cerr << level3 << "field error:" << endl;
                    cerr << level4 << "fieldId = " << fieldId << endl;
                    cerr << level4 << "source = " << source << endl;
                    cerr << level4 << "code = " << code << endl;
                    cerr << level4 << "category = " << category << endl;
                    cerr << level4 << "errorMessage = " << strMessage << endl;
                    cerr << level4 << "subCategory = " << subCategory << endl;
                }
            }

            bool isSecurityError = SecurityData.hasElement("securityError", true);
            if (isSecurityError)
            {
                Element secError = SecurityData.getElement("securityError");
                string source = secError.getElementAsString("source");
                int code = secError.getElementAsInt32("code");
                string category = secError.getElementAsString("category");
                string errorMessage = secError.getElementAsString("message");
                string subCategory = secError.getElementAsString("subcategory");

                cerr << level3 << "security error:" << endl;
                cerr << level4 << "source = " << source << endl;
                cerr << level4 << "code = " << code << endl;
                cerr << level4 << "category = " << category << endl;
                cerr << level4 << "errorMessage = " << errorMessage << endl;
                cerr << level4 << "subCategory = " << subCategory << endl;
            }
            else
            {
                Element FieldData = SecurityData.getElement(FIELD_DATA);


                double pxLast = FieldData.getElementAsFloat64("PX_LAST");
                double bid = FieldData.getElementAsFloat64("BID");
                double ask = FieldData.getElementAsFloat64("ASK");
                string ticker = FieldData.getElementAsString("TICKER");

                std::cout << level3 << "fields: " << endl;
                std::cout << level4 << "PX_LAST = " << pxLast << endl;
                std::cout << level4 << "BID = " << bid << endl;
                std::cout << level4 << "ASK = " << ask << endl;
                std::cout << level4 << "TICKER = " << ticker << endl;

                bool excludeNullElements = true;
                if (FieldData.hasElement("CHAIN_TICKERS", excludeNullElements))
                {
                    Element chainTickers = FieldData.getElement("CHAIN_TICKERS");
                    for (size_t chainTickerValueIndex = 0; chainTickerValueIndex < chainTickers.numValues(); chainTickerValueIndex++)
                    {
                        Element chainTicker = chainTickers.getValueAsElement(chainTickerValueIndex);
                        string strChainTicker = chainTicker.getElementAsString("Ticker");

                        std::cout << level4 << "CHAIN_TICKER = " << strChainTicker << endl;
                    }
                }
                else
                {
                    std::cout << level4 << "NO CHAIN_TICKER information" << endl;
                }
            }
        }
    }
}

关于第二个问题,Bloomberg 支持人员建议我只选择一个任意大的数字,以便下载所有选项,即

override2.setElement("fieldId", "CHAIN_POINTS_OVRD");
override2.setElement("value", 50000);

对于第三个问题,可以通过将 "CHAIN_EXP_DT_OVRD" 覆盖设置为 'ALL' 来下载所有到期日的链代码(这部分目前未测试):

    Element override3 = overrides.appendElement();
    override3.setElement("fieldId", "CHAIN_EXP_DT_OVRD");
    override3.setElement("value", 'ALL');