Chains on multiple stock indexes

Hello all
I am reaching out to try to solve an API problem, i am trying to retrieve a unique list of RIC/CUSIPS for multiple indexes using Chains. However i am getting different errors for different methods, could you help me with understanding of the feasibility of retrieving data through such methods?
import lseg.data as ld
import pandas as pd
from lseg.data.discovery import Chain
import datetime
from IPython.display import display, clear_output
from lseg.data.content import symbol_conversion
try:
ld.open_session()
print("Session opened successfully.")
except Exception as e:
print(f"Error opening session: {e}")
exit(1) # Exit if the session cannot be established
NASDAQ=Chain("0#.IXIC")
SP500=Chain("0#.SPX")
NYSE = ld.get_data(NYSE, fields=['TR.REVENUE']) # NYSE
NASDAQ = ld.get_data(NASDAQ, fields=['TR.REVENUE']) # NASDAQ
SP500 = ld.get_data(SP500, fields=['TR.REVENUE']) # S&P 500
DOWJONES = ld.get_data(DOWJONES, fields=['TR.REVENUE']) # Dow Jones
csv_file_path=r"xxxx"
Combine all data into a single DataFramerics_df = pd.concat([NYSE, NASDAQ, SP500, DOWJONES], ignore_index=True)
Remove duplicatesrics_df.drop_duplicates(subset=['CUSIP'], inplace=True)
Convert to listrics = rics_df['CUSIP'].tolist()
Extract the first two and last two RICsfirst_two = rics[:2]
last_two = rics[-2:]
print(f"Number of items in the rics list: {len(rics)}")
print(f"First two items: {first_two}")
print(f"Last two items: {last_two}")
#Save the complete DataFrame as a CSV file at the end
rics_df.to_csv(csv_file_path, index=False)
print(f"Final data saved successfully to {csv_file_path}")
2nd try :
import pandas as pd
import lseg.data as ld
from lseg.data.discovery import Chain
NYSE = Chain("0#.NYA")
NASDAQ = Chain("0#.IXIC")
SP500 = Chain("0#.SPX")
DOWJONES = Chain("0#.DJA")
def fetch_chain_constituents(chain):
# If the Chain object is iterable
constituents = []
for item in chain: # Assuming Chain
is iterable
constituents.append(item)
return constituents
nyse_constituents = fetch_chain_constituents(NYSE)
nasdaq_constituents = fetch_chain_constituents(NASDAQ)
sp500_constituents = fetch_chain_constituents(SP500)
dowjones_constituents = fetch_chain_constituents(DOWJONES)
all_constituents = (
nyse_constituents +
nasdaq_constituents +
sp500_constituents +
dowjones_constituents
)
stocks_df = pd.DataFrame(all_constituents, columns=["Symbol"])
Save to CSVstocks_df.to_csv
print(f"Stocks data saved successfully to {csv_file_path}")
3rd try:
Define the chainsNYSE = Chain("0#.NYA")
NASDAQ = Chain("0#.IXIC")
SP500 = Chain("0#.SPX")
DOWJONES = Chain("0#.DJA")
def fetch_chain_constituents(chain):
constituents = [item for item in chain] # If Chain is iterable
return constituents
try:
nyse_constituents = fetch_chain_constituents(NYSE)
nasdaq_constituents = fetch_chain_constituents(NASDAQ)
sp500_constituents = fetch_chain_constituents(SP500)
dowjones_constituents = fetch_chain_constituents(DOWJONES)
all_constituents = (
nyse_constituents +
nasdaq_constituents +
sp500_constituents +
dowjones_constituents
)
# Convert to DataFrame and save
stocks_df = pd.DataFrame(all_constituents, columns=["Symbol"])
csv_file_path = r"C:\Users\r_cas\OneDrive\Ambiente de Trabalho\green\thesis\data_and_code\stocks_data.csv"
stocks_df.to_csv(csv_file_path, index=False)
print(f"Stocks data saved to {csv_file_path}")
except Exception as e:
print(f"Error fetching data: {e}")
ld.close_session()
Best Answer
-
Certain indexes, such as Nasdaq, is returning me an error of "No values to unpack" is there any way around to get Nasdaq stocks?.
See the code:
NYSE = Chain("0#.NYA")
NASDAQ = Chain("0#.IXIC")
SP500 = Chain("0#.SPX")
DOWJONES = Chain("0#.DJA")def fetch_chain_constituents(chain):
# If the Chain object is iterable
constituents = []
for item in chain.constituents: # Assuming Chain is iterableconstituents.append(item)return constituentsnyse_constituents = fetch_chain_constituents(NYSE)
nasdaq_constituents = fetch_chain_constituents(NASDAQ)
sp500_constituents = fetch_chain_constituents(SP500)
dowjones_constituents = fetch_chain_constituents(DOWJONES)all_constituents = (….
)
stocks_df=pd.DataFrame(all_constituents, columns=["Symbol"])
stocks_df0
Answers
-
The code should be pasted in Code Block for readability.
A list of RICs can be retrieved from the constituents property of the Chain object so the code in the fetch_chain_constituents method should look like this:
def fetch_chain_constituents(chain): # If the Chain object is iterable constituents = [] for item in chain.constituents: # Assuming Chain is iterable constituents.append(item) return constituents
If you have any further questions, please share the code and errors.
0 -
Is the issue only happen to 0#.IXIC? It should be a permission issue. Please contact your local LSEG account team to verify the account's permission.
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
- 690 Datastream
- 1.4K DSS
- 629 Eikon COM
- 5.2K Eikon Data APIs
- 11 Electronic Trading
- 1 Generic FIX
- 7 Local Bank Node API
- 3 Trading API
- 2.9K Elektron
- 1.4K EMA
- 255 ETA
- 559 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
- 279 Open PermID
- 45 Entity Search
- 2 Org ID
- 1 PAM
- PAM - Logging
- 6 Product Insight
- Project Tracking
- ProView
- ProView Internal
- 23 RDMS
- 2K Refinitiv Data Platform
- 716 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
- 106 UPA
- 194 TREP Infrastructure
- 229 TRKD
- 918 TRTH
- 5 Velocity Analytics
- 9 Wealth Management Web Services
- 95 Workspace SDK
- 11 Element Framework
- 5 Grid
- 19 World-Check Data File
- 1 Yield Book Analytics
- 48 中文论坛