Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Problem with peg_ratio in fundamentals

I'm very confused. I tried to use get_fundamentals to filter out some stocks.

There are two lines of "order_by" in the code snippet below.
#.order_by(fundamentals.valuation_ratios.pe_ratio)
.order_by(fundamentals.valuation_ratios.peg_ratio)

If I use it as is, it does not work. The debug message always shows 0 stocks in updated universe.
Instead, if comment out the "peg_ratio" line and uncomment the "pe_ratio" line, everything works just fine.

What did I do wrong with peg_ratio? I'd expect some outputs no matter what kind of value peg_ratio is.

Any advice?

import pandas as pd  
import numpy as np

def initialize(context):  
    context.num_stocks = 10  
    # Rebalance monthly on the first day of the month at market open  
    schedule_function(rebalance,  
                      date_rule=date_rules.month_start(),  
                      time_rule=time_rules.market_open())  
def rebalance(context, data):  
    # Exit all positions before starting new ones  
    for stock in context.portfolio.positions:  
        if stock not in context.fundamental_df and stock in data:  
            order_target_percent(stock, 0)

    # Rebalance all stocks to target weights  
    for stock in context.stocks:  
        pct = 1.0/len(context.stocks)  
        log.debug("stock = %s, pct = %f" % (stock.symbol, pct))  
        order_target_percent(stock, pct)

def before_trading_start(context, data):  
    context.fundamental_df = get_fundamentals(  
        query(  
            # put your query in here by typing "fundamentals."  
            fundamentals.valuation.market_cap  
        )  
        #.filter(fundamentals.valuation_ratios.peg_ratio != 0)  
        .filter(fundamentals.valuation.market_cap > 200000000)  
        .filter(fundamentals.valuation.market_cap < 10000000000)  
        #.order_by(fundamentals.valuation_ratios.pe_ratio)  
        .order_by(fundamentals.valuation_ratios.peg_ratio)  
        .limit(context.num_stocks)  
    )  
    context.stocks = [stock for stock in context.fundamental_df]  
    log.debug("number of stocks in updated universe : %d" % len(context.stocks))  
    update_universe(context.stocks)  

def handle_data(context, data):

    # track how many positions we're holding  
    record(num_positions = len(context.portfolio.positions))  
10 responses

Yi

Looks like you found a bug in our database for this field. As you've probably discovered it seems that we have null values for peg_ratio until around June 2014. So it could be a couple things, 1) Morningstar didn't start calculating the PEG ratio until June 2014, 2) we messed up. I'm fairly certain its #2 as this seems to correspond to when we integrated Morningstar fundamentals data onto the platform. I've filed an issue for this bug and hopefully we can get it resolved soon.

You can control to some extent the handling of null values with the get_fundamentals parameter regarding null values, try typing in research get_fundamentals? for more information. get_fundamentals(query, filter_ordered_nulls=False)

James

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.

James,

Thanks for prompt response. Now I can move on..... :-)

BTW, is there a way/place to track issues like so that I would get notified once the issue is solved?

Seems like this is still an issue.. any ETA on this?

Hi Yi, Ravi,

Sorry, we should have been a little more clear. Unfortunately it looks like Morningstar only started providing this particular field in 2014. There is no data for this in our database prior. I'll double check with them for a further explanation. Just looking at the definition, this looks to be a convenience field -- i.e. the raw data you need is provided and you can access multiple days worth of fundamentals in the Pipeline API (which wasn't the case when this thread started).

Hope that helps,
Josh

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.

Thanks Josh for the clarification

I need PEG also.
Definition in quantopian.com/help/fundamentals:

peg_ratio
ForwardPERatio / Long-term Average Earning Growth Rate

Please write me where can I find in fundamentals Forward PE and Long-term Average Earning Growth Rate, because I cannot find such data.

valuation_ratios.forward_pe_ratio and the earnings ratios namespace has various growth rates.

Thank you for your quick response. Is there a growth ratio which I can use to get the exact same value as in peg_ratio?

I mean
peg_ratio = (forward_pe_ratio / (????_growth * 100))

Hello,
If you are looking at the fundamental data you are going to find problems with using more than just the PEG. If you go all the way back to 2003 you will find problems with the FPE, Equity per share growth, Total Yield and a multitude of others. Best to stick with the most common metrics as these are the most reliable such as: PS, PCF, PFCF, GrossMargin, EVto EBITDA, PE, RevGrowth, DEaPSG, NIG, PB, LTDE, TDER, Debt to Assets, CR, ROA, ROE, ROIC, DilConEPSG, NormProfitMargin, BasicEPS, DilEPS, DivPSG and Div Yield. I`ve conducted multiple backtests on the metrics in that list and I was able to get from 2003 to 2016 with fewer problems.

Hi Darth,

Can you be more specific with the problems with those metrics?

Thanks
Josh