Loop mutual funds holdings

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
Answers
-
Thank you for reaching out to us.
You can try asynchronous requests, as mentioned in the discussion.
0 -
Thank you for your reply. I still can not understand much about the discussion you sent me (I am new to APIs) can you explain me more about asynchronous requests and how to build them please?
0 -
The asynchronous requests use the Python asyncio library. This allows users to send requests in parallel.
You need to import the asyncio module.
import asyncio
You can refer to this example.
0 -
Thank you, but in the examples you provided me, they are downloading just three stocks. I must handle thousands of mutual funds. How can I do that?
0 -
You may need to apply asynchronous code to the batch logic. This requires proficiency in Python programming.
If you need assistance implementing this logic, please contact your LSEG account team or sales representative directly. The account team can coordinate with the solution team to support you in implementing the logic.
1
Categories
- All Categories
- 3 Polls
- 6 AHS
- 37 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 697 Datastream
- 1.5K DSS
- 632 Eikon COM
- 5.2K Eikon Data APIs
- 14 Electronic Trading
- 1 Generic FIX
- 7 Local Bank Node API
- 6 Trading API
- 2.9K Elektron
- 1.5K EMA
- 256 ETA
- 563 WebSocket API
- 39 FX Venues
- 15 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 25 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 283 Open PermID
- 47 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 23 RDMS
- 2K Refinitiv Data Platform
- 757 Refinitiv Data Platform Libraries
- 4 LSEG Due Diligence
- LSEG Due Diligence Portal API
- 4 Refinitiv Due Dilligence Centre
- Rose's Space
- 1.2K Screening
- 18 Qual-ID API
- 13 Screening Deployed
- 23 Screening Online
- 12 World-Check Customer Risk Screener
- 1K World-Check One
- 46 World-Check One Zero Footprint
- 45 Side by Side Integration API
- 2 Test Space
- 3 Thomson One Smart
- 10 TR Knowledge Graph
- 151 Transactions
- 143 REDI API
- 1.8K TREP APIs
- 4 CAT
- 27 DACS Station
- 122 Open DACS
- 1.1K RFA
- 107 UPA
- 194 TREP Infrastructure
- 229 TRKD
- 918 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 97 Workspace SDK
- 11 Element Framework
- 5 Grid
- 19 World-Check Data File
- 1 Yield Book Analytics
- 48 中文论坛