How to to gather daily returns data for all US-traded equities from a period starting from the ea...

...rly 2010s to the present day using CODEBOOK API
How to to gather daily returns data for all US-traded equities from a period starting from the early 2010s to the present day using CODEBOOK API
Best Answer
-
Thank you for reaching out to us.
You can get the list of RICs by using the following code.
lse_equities["RIC"].to_list()
However, the get_data method is not desinged to retrieve a lot of data at once. Therefore, you need to split to multiple requests, such as requesting data for ten RICs at a time.
The sample code looks like this. This is just a sample so we will not support it.
from refinitiv.data.errors import RDError
def GetDataRetry(universe, fields,parameters):
retry = True
retry_max = 5
retry_count = 1
while retry==True:
try:
retry=False
df = rd.get_data(
universe,
fields,
parameters)
return df
except RDError as err:
if "Backend error. 400 Bad Request" in err.message:
retry_count = retry_count + 1
if retry_count<=retry_max:
print("Retry "+universe)
retry=True
else:
print("Retry reach max and unable to get the data for "+universe)
return None
else:
print(str(err.code)+" "+err.message)
return None
def split_instruments(instruments, chunk_size=5):
"""Yield successive chunk_size chunks from instruments."""
for i in range(0, len(instruments), chunk_size):
yield instruments[i:i + chunk_size]
instrument_chunks = list(split_instruments(lse_equities["RIC"].to_list(), 10))
dfs = []
for chunk in instrument_chunks:
# Fetch data for the current chunk of instruments
print(chunk)
df_chunk = GetDataRetry(chunk,
[
'TR.TotalReturn.date',
'TR.TotalReturn'
],
{'SDate':'2010-01-01','EDate':'2024-09-24','Frq':'D'})
dfs.append(df_chunk)0
Answers
-
Formulated the code, however this is only for one RIC:
import refinitiv.data as rd
rd.open_session()
df = rd.get_data(
universe = ['AAPL.O'],
fields = [
'TR.TotalReturn(SDate=2010-01-01,EDate=2024-09-24,Frq=D).date',
'TR.TotalReturn(SDate=2010-01-01,EDate=2024-09-24,Frq=D)'
]
)display(df)
Managed to get the list of all the securities for an exchange using the search function, however I am having troubles incorporating this list into the following data item of total return.
lse_equities = equity.search(exchange_name="NASDAQ Stock Exchange Capital Market")
display(lse_equities)0
Categories
- All Categories
- 3 Polls
- 6 AHS
- 36 Alpha
- 166 App Studio
- 6 Block Chain
- 4 Bot Platform
- 18 Connected Risk APIs
- 47 Data Fusion
- 34 Data Model Discovery
- 684 Datastream
- 1.4K DSS
- 615 Eikon COM
- 5.2K Eikon Data APIs
- 10 Electronic Trading
- Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 251 ETA
- 555 WebSocket API
- 37 FX Venues
- 14 FX Market Data
- 1 FX Post Trade
- 1 FX Trading - Matching
- 12 FX Trading – RFQ Maker
- 5 Intelligent Tagging
- 2 Legal One
- 23 Messenger Bot
- 3 Messenger Side by Side
- 9 ONESOURCE
- 7 Indirect Tax
- 60 Open Calais
- 275 Open PermID
- 44 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 22 RDMS
- 1.9K Refinitiv Data Platform
- 643 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
- 121 Open DACS
- 1.1K RFA
- 104 UPA
- 193 TREP Infrastructure
- 228 TRKD
- 916 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 90 Workspace SDK
- 11 Element Framework
- 5 Grid
- 18 World-Check Data File
- 1 Yield Book Analytics
- 46 中文论坛