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.

35
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
import datetime

def initialize(context):
context.stock = sid(24)

# Query for specific security fundamentals (by symbol in this example).
fundamental_df = get_fundamentals(
# Retrieve sector code.
query(
fundamentals.asset_classification.morningstar_sector_code,
)

# Filter for specific stock (AAPL in this example)
.filter(fundamentals.share_class_reference.symbol == 'AAPL')
)

# Get the sector code of our stock.
sector_code = fundamental_df.at['morningstar_sector_code', sid(24)]
print sector_code

# Query for securities based on their economic sector
fundamental_df2 = get_fundamentals(
# Retrieve PE ratio based on economic sector code.
query(
fundamentals.valuation_ratios.pe_ratio,
fundamentals.asset_classification.morningstar_sector_code,
)

# Filter where the Sector code matches the sector code of our specific stock.
.filter(fundamentals.asset_classification.morningstar_sector_code == sector_code)
)

# Print the unweighted mean PE ratio of the sector
print 'Mean PE ratio for sector %d: %.2f' % (sector_code, fundamental_df2[fundamental_df2.index == 'pe_ratio'].transpose().mean())

def handle_data(context, data):
pass

There was a runtime error.
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