Upgrade from Eikon -> Workspace. Learn about programming differences.

For a deeper look into our Eikon Data API, look into:

Overview |  Quickstart |  Documentation |  Downloads |  Tutorials |  Articles

question

Upvotes
Accepted
3 0 3 4

How to program in python the time series of the percentage of stocks in an index

Can you advise how program in python the time series of the percentage of stocks in an index? Let's say the S&P 500, which their end of day prices are above or below their 50 (100 or 200) days moving average.

eikoneikon-data-apipythonrefinitiv-dataplatform-eikonworkspaceworkspace-data-api
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello @ben.victorio

Thank you for your participation in the forum. Is the reply below satisfactory in resolving your query?


If so please can you click the 'Accept' text next to the appropriate reply? This will guide all community members who have a similar question.

Thanks,


AHS

@ben.victorio

Hi,

Please be informed that a reply has been verified as correct in answering the question, and marked as such.

Thanks,

AHS

1 Answer

· Write an Answer
Upvotes
Accepted
79.2k 251 52 74

@ben.victorio

It is better to contact the Eikon Excel support team to verify how to do it in Eikon Excel with the =TR function.

Then, we can convert the formula used in Eikon Excel to Python code.

For example, "Let's say the S&P 500, which their end of day prices are above or below their 50 (100 or 200)". We can use the following code to get 50-Day SMA.

df, err = ek.get_data("0#.SPX",["TR.Price50DayAverage","TR.PriceClose"])
df


1624252704232.png


Then, we can calculate the percentage of stocks that have end-of-day prices above 50-day SMA.

(len(df[df["Price Close"] > df["50-day SMA"]])/len(df))*100

The output is 30.89%.

In conclusion, please directly the Eikon Excel support team via MyRefinitv (https://my.refinitiv.com/content/mytr/en/helpandsupport.html) to verify how to do it in Eikon Excel. Then, we can convert the Excel formula to the Python code that uses Eikon Data API to retrieve the data.


1624252704232.png (23.5 KiB)
icon clock
10 |1500

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.