【问题标题】:Finding SPX Index Option Data using xbbg or blpapi使用 xbbg 或 blpapi 查找 SPX 指数期权数据
【发布时间】:2021-08-18 13:57:50
【问题描述】:

我正在尝试使用 Python 从 Bloomberg 查找 SPX INDEX 选项数据。我想查找给定日期所有期权的现货价格、买入价、卖出价和隐含波动率。

我知道如何在 Excel 中执行此操作。在那里,我找到了针对给定日期列出的所有选项 (BQL("filter(options('SPX Index'),expire_dt=='"08/15/21"')","ID_SECURITY_DES,px_bid().value","showids=false","cols=2;rows=495"))。这将创建包含选项名称、价格、执行价格以及它们是否是看涨期权的列。对于每一行,我可以使用 BDH 找到所有选项信息(例如 BDH('SPX US 08/15/21 4440P Index','Ivol_MID','08/18/21','08/18/21') 并为我想要查找的每个字段重复)。但是,我尝试使用 blpapi 查找此信息,如下所示:

import blpapi
import pandas
import csv
options = blpapi.SessionOptions()
options.setServerHost('localhost')
options.setServerPort(8194)

SECURITY_DATA = blpapi.Name("securityData")
SECURITY = blpapi.Name("security")
FIELD_DATA = blpapi.Name("fieldData")
FIELD_ID = blpapi.Name("fieldId")
OPT_CHAIN = blpapi.Name("OPT_CHAIN")
SECURITY_DES = blpapi.Name("Security Description")


def getOptionChain (sec_list):
    import pdb;pdb.set_trace()
    session = blpapi.Session(options)
    session.start()
    session.openService('//blp/refdata')
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    for s in sec_list:
        request.append("securities",s)
    request.append("fields", "OPT_CHAIN")
    cid = session.sendRequest(request)
    try:
        # Process received events
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            response = []
            for msg in ev:
                if cid in msg.correlationIds():
                    securityDataArray = msg.getElement(SECURITY_DATA)
                    for securityData in securityDataArray.values():
                        fieldData = securityData.getElement(FIELD_DATA)
                        for field in fieldData.elements():
                            for n in range(field.numValues()):
                                fld = field.getValueAsElement(n)
                                response.append (fld.getElement(SECURITY_DES).getValueAsString())
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
    return response



def getPX (sec_list, fld_list):
    opt_chain_list = getOptionChain(sec_list)
    session = blpapi.Session(options)
    session.start()
    session.openService('//blp/refdata')
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    for s in opt_chain_list:
        request.append("securities",s)
    for f in fld_list:
        request.append("fields",f)
    cid = session.sendRequest(request)
    try:
        # Process received events
        response = {}
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)

            for msg in ev:
                if cid in msg.correlationIds():
                    securityDataArray = msg.getElement(SECURITY_DATA)
                    for securityData in securityDataArray.values():
                        secName = securityData.getElementAsString(SECURITY)
                        fieldData = securityData.getElement(FIELD_DATA)
                        response[secName] = {}
                        for field in fieldData.elements():
                            response[secName][field.name()] = field.getValueAsFloat()
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()                   
    tempdict = {}
    for r in response:
        tempdict[r] = pandas.Series(response[r])
        data = pandas.DataFrame(tempdict)
    return data
                

sec = ["SPX INDEX Equity"]
fld = ["PX_ASK","PX_BID"]
print(getPX(sec,fld))

这样做会给我以下错误:

blpapi.exception.InvalidArgumentException: Choice sub-element not found for name 'securityData'. (0x00020002)

尝试使用 xbbg

blp.bdh(tickers='SPX INDEX',flds=['PX_LAST','Bid','Ask'],start_date='09-26-20',end_date='09-30-20')

我得到一个空数据框。

我知道如果我像在 excel 中那样列出特定选项,我可以找到我想要的信息,但我如何才能找到一个完整的选项列表呢?此外,我还想查找期权到期列表,以便查询给定的到期时间。

关于如何查找 SPX INDEX 期权历史期权数据的任何建议

编辑 08/19/21

更改为正确的安全名称。仍然无法正常工作。

使用sec = ["SPX US 08/15/21 4440P Index"]fld = ["IVOL"] 查询时,我收到错误blpapi.exception.InvalidArgumentException: Choice sub-element not found for name 'securityData'.

【问题讨论】:

  • 尝试使用正确的安全名称,例如 SPX US 08/15/21 4440P IndexSPX Index
  • 您想查找过去给定日期可用的所有选项,还是仅查找今天可用的选项?您的 BQL 按到期日期查询,这不是(我认为)对 python API 开放的功能。如果您有特定的到期日期,那么您可以选择之前的参考日期(比如 1 天前)并提取该参考日期可用的所有选项(这将是很多 ~ 8k)。然后,您可以按 MATURITY 过滤,仅获取您选择的到期日期的那些。虽然你冒着达到安全下载限制的风险......
  • 嗨@DS_London。我想找到在过去给定日期和给定到期日可用的所有选项。我想我可以过滤一下就好了。如何找到过去某个日期给出的所有选项?
  • xbbg 函数与 Excel 中的函数非常相似(毕竟它们封装了相同的底层 API),因此您可以先在 Excel 中尝试。此函数会带回 20 年 1 月 1 日可用选项的所有选项 =BDS("SPX Index","OPT_CHAIN","SINGLE_DATE_OVERRIDE","20200101") 的 ID。或者至少有 8000 个。布隆伯格警告名单被截断。不幸的是,只有 Bloomberg 自己的 ID 会返回(当您使用日期覆盖时),这对于过滤毫无用处。您可以致电=BDP(BbgTkr,"SECURITY_DES") 获取描述性名称。

标签: python bloomberg blpapi


【解决方案1】:

要查找历史值,您需要按照 DS_London 的建议包含单个日期覆盖。列出了 getOptionChain 的代码,其中 queryDate 是一个日期时间对象,它是所需的日期,而 sec_l​​ist 是我们要查询的安全性

 SECURITY_DATA = blpapi.Name("securityData")
SECURITY = blpapi.Name("security")
FIELD_DATA = blpapi.Name("fieldData")
FIELD_ID = blpapi.Name("fieldId")
OPT_CHAIN = blpapi.Name("OPT_CHAIN")
SECURITY_DES = blpapi.Name("Security Description")

def getOptionChain (sec_list,queryDate):
    session = blpapi.Session(options)
    session.start()
    session.openService('//blp/refdata')
    refDataService = session.getService("//blp/refdata")
    request = refDataService.createRequest("ReferenceDataRequest")
    for s in sec_list:
        request.append("securities",s)
    request.append("fields", "OPT_CHAIN")
    requestOverride = request.getElement('overrides').appendElement()
    requestOverride.setElement("fieldId","SINGLE_DATE_OVERRIDE")
        requestOverride.setElement('value',queryDate.strftime('%Y%m%d'))
    cid = session.sendRequest(request)
    try:
        # Process received events
        while(True):
            # We provide timeout to give the chance to Ctrl+C handling:
            ev = session.nextEvent(500)
            response = []
            for msg in ev:
                if cid in msg.correlationIds():
                    securityDataArray = msg.getElement(SECURITY_DATA)
                    for securityData in securityDataArray.values():
                        fieldData = securityData.getElement(FIELD_DATA)
                        for field in fieldData.elements():
                            for n in range(field.numValues()):
                                fld = field.getValueAsElement(n)
                                response.append(fld.getElement(SECURITY_DES).getValueAsString())
            # Response completely received, so we could exit
            if ev.eventType() == blpapi.Event.RESPONSE:
                break
    finally:
        # Stop the session
        session.stop()
    return response

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多