Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sector average values

Hi,

I have posted this question earlier but was not able to figure out how to query for a sector average value of a fundamental ratio.

A code example would be great, thanks.

Suppose I have some stock that has been returned and I have updated my universe with the results of the get_fundamentals, now how do I query for the sector average of p/e ratio of the stock's sector?

4 responses

I just wrote up this example that will get you the average value of the p/e ratio for the AAPL stock and its sector. This should give you a good enough basis so that you can use it for other stocks! Let me know if you have questions.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank you Jamie! Much appreciated! This does not average out the p/e ratios of the stocks in the same sector as returned by the query does it?

What I mean is if you run the get_fundamentals, you only get 500 stocks, I have noticed that second query that is run makes sure that all 500 are the stocks of the same sector, but we are still not getting the sector mean, are we? Only approximation, because the actual total number of stocks in the sector may be different than 500, is this correct?

This calculation is actually being performed on 779 securities. Since I'm not actually adding these securities to my universe, I'm not blocked by the 500 security limit. Since you didn't want to actually trade all of these securities, there's no need to add them to the universe and our calculation done in before_trading_start is on all 779 securities (you can print fundamental_df2.shape to confirm).

Does this make sense?

Yeap, it does. I suppose If I wanted to compute variability of a particular ratio, I would do the similar process as above but would "collect" the fundamentals each year and append it to my dataframe and then do the computations on that DF. Thanks