title: Bloomberg Equity Option Chain through API
body: <p>I am currently working on a project that should help me create an implied volatility surface for a given stock. For this purpose, I am writing a script that will download all the available options for this specific stock - from what I've gathered, this is possible by sending a request through the Bloomberg API using bulks fields/overrides. Here is my current code:</p>
  
  <pre><code>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 &lt;&lt; "Sending Request: " &lt;&lt; request &lt;&lt; std::endl;
  CorrelationId cid(this);
  session.sendRequest(request, cid);
  
  (followed by event handling)
  </code></pre>
  
  <p>Now I have several issues/questions:</p>
  
  <ol>
  <li><p>The code compiles without problems, but when running it on the Bloomberg terminal,the following error is printed:
  <img src="https://i.imgur.com/MJgbDjj.png" alt="bloomberg error">
  How would I go about fixing this problem? I assume I made a mistake somewhere in the override fields..</p></li>
  <li><p>How would I need to adjust my code to download all options available given a specific maturity, i.e. I want to get a list of all the options until today + 15 years.</p></li>
  <li><p>How would I then download the implied volatility for each option? Would I need to store the tickers in an array and then send a request for the field "IVOL_MID" for each option or is there some kind of way to obtain all the volatilities at once?</p></li>
  </ol>
  
  <p>Edit: Here is the code of my event handler, since that seems to be the problem.</p>
  
  <pre><code>    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 &amp;msg)
  {
      Element securityDataArray = msg.getElement(SECURITY_DATA);
      int numSecurities = securityDataArray.numValues();
      for (int i = 0; i &lt; numSecurities; ++i) {
          Element securityData = securityDataArray.getValueAsElement(i);
          std::cout &lt;&lt; securityData.getElementAsString(SECURITY)
              &lt;&lt; std::endl;
          const Element fieldData = securityData.getElement(FIELD_DATA);
          for (size_t j = 0; j &lt; fieldData.numElements(); ++j) {
              Element field = fieldData.getElement(j);
              if (!field.isValid()) {
                  std::cout &lt;&lt; field.name() &lt;&lt; " is NULL." &lt;&lt; std::endl;
              }
              else {
                  std::cout &lt;&lt; field.name() &lt;&lt; " = "
                      &lt;&lt; field.getValueAsString() &lt;&lt; std::endl;
              }
          }
  
          Element fieldExceptionArray =
              securityData.getElement(FIELD_EXCEPTIONS);
          for (size_t k = 0; k &lt; fieldExceptionArray.numValues(); ++k) {
              Element fieldException =
                  fieldExceptionArray.getValueAsElement(k);
              std::cout &lt;&lt;
                  fieldException.getElement(ERROR_INFO).getElementAsString(
                  "category")
                  &lt;&lt; ": " &lt;&lt; fieldException.getElementAsString(FIELD_ID);
          }
          std::cout &lt;&lt; std::endl;
      }
  </code></pre>