matthewgilbert / pdblp

pandas wrapper for Bloomberg Open API
MIT License
241 stars 68 forks source link

bdh() sometimes gets data arbitrary #89

Open utorell opened 4 years ago

utorell commented 4 years ago

Code Sample, a copy-pastable example if possible

I get bloomberg data by calling the class BBHist and the function bbhist (see further below)

data = a.bbhist(names_sec[sec], 'day_to_day_tot_return_gross_dvds', names_date.date().strftime("%Y%m%d"), names_date.date().strftime("%Y%m%d"), freq, setting_list = [])

Here is the class BBHist

class BBHist: def init(self): self.BBHist = 'Bloomberg Data'

def bbhist(self,full_ticker,fields,startdate,enddate, d_freq,setting_list=[]):    

    if d_freq=="DAILY":
        setting_list=[["periodicitySelection","DAILY"],["nonTradingDayFillMethod","PREVIOUS_VALUE"],["nonTradingDayFillOption","ACTIVE_DAYS_ONLY"],["periodicityAdjustment","ACTUAL"]]
    else:
        setting_list=[["periodicitySelection","MONTHLY"],["nonTradingDayFillMethod","PREVIOUS_VALUE"],["nonTradingDayFillOption","ACTIVE_DAYS_ONLY"],["periodicityAdjustment","ACTUAL"]]

    '''Full ticker example: ERICA SS Equity'''
    if type(full_ticker) is list:
        full_ticker_list=full_ticker
    else:
        full_ticker_list=[]
        full_ticker_list.append(full_ticker)

    if type(fields) is list:
        field_list=fields
    else:
        field_list=[]
        field_list.append(fields)

    session = blpapi.Session()
    session.start()
    session.openService("//blp/refdata")
    service = session.getService("//blp/refdata")
    request = service.createRequest("HistoricalDataRequest")
    for ticker in full_ticker_list:
        request.getElement("securities").appendValue(ticker)

    for field in field_list:
        request.getElement("fields").appendValue(field)
    for setting in setting_list:
        request.set(setting[0], setting[1])

    request.set("startDate", startdate)
    request.set("endDate", enddate)
    request.set("maxDataPoints", 1000000)
    request.set("pricingOption","PRICING_OPTION_PRICE") #PX_BID always Clean Price and not yield. "PRICING_OPTION_YIELD"
    overrides = request.getElement("overrides")
    override = overrides.appendElement()
    override.setElement("fieldId", "EQY_FUND_CRNCY")
    override.setElement("value", "EUR")     #Sets the currency for all data

    session.sendRequest(request)
    endReached = False
    df_list=[]
    while endReached == False:
        ev = session.nextEvent()
        if ev.eventType() == blpapi.Event.RESPONSE or ev.eventType() == blpapi.Event.PARTIAL_RESPONSE:
            for msg in ev:
                #print(msg)
                sec_name=msg.getElement('securityData').getElementAsString('security')
                fieldDataArray=msg.getElement('securityData').getElement('fieldData')
                size = fieldDataArray.numValues()
                fieldDataList = [fieldDataArray.getValueAsElement(i) for i in range(0,size)]
                outDates = [x.getElementAsDatetime('date') for x in fieldDataList]
                dftempoutput = pd.DataFrame(columns=['Date','Ticker']+field_list)
                dftempoutput['Date']=outDates
                dftempoutput['Ticker']=sec_name
                for fld in field_list:
                    outData = [x.getElementAsFloat(fld) if x.hasElement(fld) else pd.np.nan for x in fieldDataList]
                    dftempoutput[fld] = outData
                    df_list.append(dftempoutput)
        if ev.eventType() == blpapi.Event.RESPONSE:
            endReached = True
    dfOutPut=pd.concat([pd.DataFrame(x) for x in df_list])
    dfOutPut.replace('#N/A History',pd.np.nan,inplace=True)
    #dfOutPut.set_index('Date',inplace=True)
    return dfOutPut
# Your code here, this should be a minimal reproducible example, see https://stackoverflow.com/help/mcve

Problem description

I try to pull small amounts of data - throught the bdh() function - many times (I am looping over 10 0000 times). It works fine most of the times but sometimes it does not generate any data (empty dataframe). However if I use the exact same code in the console window it's able to pull data from Bloomberg. How can this happend ? Clearly it's nothing wrong with the code and the error statement does not say anything about that it takes too long "time out".

Expected Output

     Date         Ticker  day_to_day_tot_return_gross_dvds

0 2015-12-31 WHR UN Equity -0.7348

Version Information

[paste the output of pdblp.__version__ here below this line]