Hi everyone,
I am trying to download mutual fund holdings data using the following code in codebook:
import refinitiv.data as rd
from refinitiv.data.errors import RDError
import pandas as pd
import time
LP=[ 'LP40215045', 'LP40212543', 'LP40235060', 'LP40209239', 'LP40209247', 'LP40215141', 'LP40212555', 'LP40209256', 'LP40187035', 'LP40189201']
fields = ['TR.FdInvestorPctPortfolio.Date',
'TR.FdInvestorPctPortfolio.SecurityOwnedRIC',
'TR.FdInvestorPctPortfolio'
]
retry_max=5
retry_count=1
chunks=[]
chunk_size=5
df_holdings = pd.DataFrame()
for index in range(0, len(LP), chunk_size):
chunk=LP[index:index+chunk_size]
retry=True
retry_count=1
while retry==True:
try:
retry=False
time.sleep(5)
rd.open_session()
df = rd.get_data(
universe = chunk,
fields = fields,
parameters={'SDate':'2004-01-01','EDate':'2024-12-31','Frq':'FQ','StartNum':1,'EndNum':316})
if len(df):
df_holdings=pd.concat([df_holdings,df],axis=0,ignore_index=True)
else:
df_holdings=df
except RDError as err:
if 'Backed error. 400 Bad Request' in err.message:
retry_count=retry_count+1
if retry_count<=retry_max:
print('Retry'+chunk)
retry=True
else:
print('Retry reach max and unable to get the data for'+chunk)
else:
print(f"{err.code}: {err.message}")
display(df_holdings)
df_holdings.dropna()
df_holdings.to_excel('provats.xlsx')
However, this code appears to be too slow and fails when I request data for more than 10 mutual funds at once.
Since I need to download data for thousands of mutual funds, I would like to ask how I can optimize this code to handle larger batches more efficiently.
Thank you in advance for your help.
Mema Myftar