无法使用xbbg调用blpapi函数(InstrumentsListRequest)

问题描述 投票:0回答:1

我正在尝试使用 xbbg 调用 blpapi 的示例

from xbbg.core import conn, process
from xbbg import blp
from datetime import date

def allInstruments():  # Return all govts with the given ticker, matured or not
    req = process.create_request(service='//blp/instruments', request='instrumentListRequest')
    req.set('query', "Dhaka")
    req.set('maxResults', 10)

    def _process_instruments(msg):  # Process the response
            print(msg)
            pass

    conn.send_request(request=req)
    
    processed_elements_list = list(process.rec_events(func=_process_instruments))

    for i in processed_elements_list:
        print(i)

allInstruments()

它给了我以下错误,但我仍然得到结果

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
h:\tanjin-work\xbbg\Examples.ipynb Cell 45 line 2
     18     for i in processed_elements_list:
     19         print(i)
---> 21 allGovts()

h:\tanjin-work\xbbg\Examples.ipynb Cell 45 line 1
     14 conn.send_request(request=req)
     16 # Process events and get the list of elements
---> 17 processed_elements_list = list(process.rec_events(func=_process_instruments))
     19 # Iterate over the processed elements
     20 for i in processed_elements_list:

File h:\tanjin-work\xbbg\xbbg\core\process.py:197, in rec_events(func, **kwargs)
    195 if ev.eventType() in responses:
    196     for msg in ev:
--> 197         for r in func(msg=msg, **kwargs):
    198             yield r
    199     if ev.eventType() == blpapi.Event.RESPONSE:

TypeError: 'NoneType' object is not iterable

结果快照

CID: {[ valueType=AUTOGEN classId=0 value=65 ]}
RequestId: 2838b8e0-83d5-4fb7-855f-d2122184f5c2
InstrumentListResponse = {
    results[] = {
        results = {
            security = "MBL BD<equity>"
            description = "Marico Bangladesh Ltd (Dhaka)"
        }
        results = {
            security = "BATBC BD<equity>"
            description = "British American Tobacco Bangladesh Co Ltd (Dhaka)"
        }
        results = {
            security = "DSEX<index>"
            description = "Bangladesh Dhaka Stock Exchange Broad Index"
        }
        results = {
            security = "BRAC BD<equity>"
            description = "BRAC Bank PLC (Dhaka)"
        }
        results = {
            security = "SQUARE BD<equity>"
            description = "Square Pharmaceuticals PLC (Dhaka)"
        } 
.....

它显示了最大值不起作用的所有数据

python bloomberg quantitative-finance blpapi
1个回答
0
投票

OP 的代码并未对

yield
函数中的任何内容进行
_process_instruments
操作,因此无法迭代结果。

替代代码:

from xbbg.core import conn,process
import pandas as pd
    
def allSecurities(ticker,yellowKey, maxResults): 
    req = process.create_request(service='//blp/instruments',request='instrumentListRequest')
    req.set('query',ticker)

    if yellowKey is not None: req.set('yellowKeyFilter',yellowKey)

    req.set('languageOverride','LANG_OVERRIDE_NONE')
    req.set('maxResults',maxResults)
        
    def _process_instruments(msg): #Process the response
        for elt in msg.asElement().getElement('results').values():
            yield elt.getElementAsString('security'),elt.getElementAsString('description')
    
    conn.send_request(request=req)
    return process.rec_events(func=_process_instruments)

def allStocks(ticker,maxResults=1000):
    return pd.DataFrame(allSecurities(ticker,'YK_FILTER_EQTY',maxResults),
                        columns=['Ticker','Description'])

print(allStocks("Dhaka",10))

输出:

                Ticker                                        Description
0    SQUARE BD<equity>                 Square Pharmaceuticals PLC (Dhaka)
1      GRAM BD<equity>                           GrameenPhone Ltd (Dhaka)
2      BRAC BD<equity>                              BRAC Bank PLC (Dhaka)
3    BXPHAR BD<equity>                Beximco Pharmaceuticals Ltd (Dhaka)
4    OLYMPI BD<equity>                     Olympic Industries Ltd (Dhaka)
5  TITASGAS BD<equity>  Titas Gas Transmission & Distribution Co Ltd (...
6      DBHF BD<equity>                            DBH Finance PLC (Dhaka)
7        PB BD<equity>                             Prime Bank PLC (Dhaka)
8      UPGO BD<equity>  United Power Generation and Distribution Co Lt...
9    ALARAB BD<equity>                  Al-Arafah Islami Bank PLC (Dhaka)

yellowKeyFilter
允许您仅过滤所需的证券类型。替代方案有
YK_FILTER_INDX
YK_FILTER_CORP
等。如果未设置,您将获得所有匹配项(我认为)。

//blp/instruments
服务有点变化无常,彭博文档也很薄弱。

© www.soinside.com 2019 - 2024. All rights reserved.