unable to call blpapi function (InstrumentsListRequest) using xbbg

108 views Asked by At

I am trying to call the example of blpapi using xbbg

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()

it gives me following error but still i get the result

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

result snap

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)"
        } 
.....

it shows all the data where max value is not working

1

There are 1 answers

5
DS_London On

The OP's code is not yielding anything in the _process_instruments function, so the result cannot be iterated over.

Alternative code:

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

    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(query,maxResults=1000):
    return pd.DataFrame(allSecurities(query,'YK_FILTER_EQTY',maxResults),
                        columns=['Security','Description'])

print(allStocks("Dhaka",10))

with the output:

              Security                                        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)

The yellowKeyFilter allows you to filter only the security types you want. Alternatives are YK_FILTER_INDX,YK_FILTER_CORP and others. If not set, you get all matches (I think).

The //blp/instruments service is a little fickle, and the Bloomberg documentation is quite thin. Eg, the query is not selecting by Ticker but seems to be matching on the Description field. An alternative is to use instrument searches saved by name in the Terminal (in the equivalent of the Excel BSRCH() function).