How to get all the Rics of a sector (ex. consumer discretionary) and then calculate with its numbers

import pandas as pd

# Example test data for 5 stocks in the Consumer Discretionary sector

df = pd.DataFrame({

"Instrument": ["A", "B", "C", "D", "E"],

"TR.TotalNetIncome": [100, 200, 150, -50, 300], # Net income

"TR.AverageTotalEquity": [1000, 2000, 1200, 500, 2500] # Equity

})

# Calculate ROE (Return on Equity = Net Income / Equity)

df["ROE"] = df["TR.TotalNetIncome"] / df["TR.AverageTotalEquity"]

# Calculate average ROE for the entire sector

average_roe = df["ROE"].mean()

print("ROE per stock:")

print(df[["Instrument", "ROE"]])

print("\nAverage Return on Equity of the sector: {:.2%}".format(average_roe))

i want something like this, where i'm getting the Dataframe from the API and then I calculate all the results and make the average for the whole sector.

Answers

  • Seems like you need to use Screener API to find the companies. Please take a look at this article which describes how to use it.

    If you need help building the correct screener query - you can ask for support at LSEG MyAccount.