I need help translating the below BDH formula from excel into python code using the BBG API. Specifically, I need to query only the weekly data for the particular index but my current python code is only returning daily. Is there a way to set specific criteria in python similar to how you query in Excel?
=@BDH(AAIIBEAR Index,PX_LAST,Start Date,End DAte,"Dir=V","fill=#N/A","Per=W","Days=A","Dts=H","cols=1;rows=1783")
Currently, I have built this Python code which queries the data in daily increments but need it to return weekly.
import os
from blp import blp
import pdblp
import blpapi
import datetime
import xlwings
import pandas as pd
with open(os.path.join(os.getenv("TEMP"), "Bloomberg", "log", "bbcomm.log"),"r") as f:
try:
port = f.read().split("BLOOMBERG COMMUNICATION SERVER READY on Port: ")[-1].split("\n")[0]
except:
port = 8194
con = pdblp.BCon(debug = False, port = 8194, timeout = 100000)
con.start()
today = datetime.datetime.today()
#assign tickers
bbg = con.bdh(['AAIIBEAR Index','PX_LAST', start_date="20240210",end_date= "20240220")
bbg = bbg.resample('D').ffill()
print (bbg)
#print data to excel
with pd.ExcelWriter(r'X:\Public\Employee folders\fvan\Market.xlsx') as writer:
bbg.to_excel(writer, sheet_name='data', startcol=1)
I am expecting to return bloomberg data in weekly increments rather than daily. Is there a way to set this kind of criteria when querying in Python?
The OP has serendipitously chosen a security that is only updated weekly (on a Thursday), hence there is no adjustment needed. The line
bbg = bbg.resample('D').ffill()is then coercing this weekly data into daily data, which is not what is wanted here.If a security does actually have daily data, then the options are passed to the
bdh()call by theelmsparameter. The Excel=BDH()call uses different option names compared topdblp(which uses the underlying Bloomberg API).The mapping can be found on Page 92 of the Bloomberg API Guide, but I've extracted the relevant ones here (the doc also gives the alternative values for these parameters for different manipulations).
Putting this together:
Note: The final parameter I have added is
periodicityAdjustment. This governs whether the start or the end date is used for the weekly reference.Using
periodicityAdjustmentset toACTUAL, the last day of the period is the reference, and the weekly days are counted back from that:Using
periodicityAdjustmentset toCALENDAR, the starting date is the reference and the weeks are counted forward from that: