Bloomberg Equity Option Chain通过API

时间:2015-02-04 04:36:14

标签: c++ bloomberg

我目前正在开展一个项目,可以帮助我为给定的股票创建隐含的波动率表面。为此,我正在编写一个脚本,将下载此特定股票的所有可用选项 - 从我收集的内容中,可以通过使用批量字段/覆盖通过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)

现在我有几个问题/问题:

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

  2. 我需要调整代码以下载特定成熟度下的所有可用选项,即我希望获得所有选项的列表,直到今天+ 15年。

  3. 我如何下载每个选项的隐含波动率?我是否需要将代码存储在一个数组中,然后发送一个字段请求&#34; IVOL_MID&#34;对于每个选项还是有某种方法可以立即获得所有波动率?

  4. 编辑:这是我的事件处理程序的代码,因为这似乎是问题所在。

        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;
        }
    

2 个答案:

答案 0 :(得分:2)

问题在于您未显示的事件处理代码。你可能错误地解析它。

运行查询我得到以下结果:

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。

答案 1 :(得分:1)

正如在另一个答案中所看到的,问题在于事件处理,所以我使用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;
                }
            }
        }
    }
}

关于第二个问题,彭博支持人员建议我选择一个非常高的数字,以便下载所有选项,即

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

对于第三个问题,可以通过设置&#34; CHAIN_EXP_DT_OVRD&#34;来下载所有到期日的链代码。覆盖&#39; ALL&#39; (这部分目前尚未经过测试):

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