Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Seeking help: how to find market PE?

I was trying to find a market PE by using SPY's PE ratio. (Q500US would be nicer but not working and sid(8554) not working either)
The algo then pick stocks with its stock_PE > market_PE using the eval and return a boolean to pipeline.

def make_pipeline():  
    base_universe = Q500US()  
    market_pe = Fundamentals.pe_ratio(input = symbol('SPY'), window_length = 1)  
    stock_pe = Fundamentals.pe_ratio.latest  
    eval = stock_pe > marekt_pe

    return Pipeline(  
              columns={  
                'eval': eval  
              },screen=(base_universe)  
          )  

It come back with symbol not defined.
Checked the tutorials and browsed over the posts in forum, didn't seem to find a solution.

Question: how to get the market PE?

2 responses

ETFs like SPY usually don't have fundamental data. You can use a custom factor to calculate the mean PE ratio of all Q500 constituants:

class MarketPE(CustomFactor):  
    inputs = [Fundamentals.pe_ratio]  
    window_length = 1

    def compute(self, today, assets, out, pe_ratio):  
        out[:] = np.nanmean(pe_ratio)


def make_pipeline():  
    base_universe = Q500US()  
    market_pe = MarketPE(mask=base_universe)  
    stock_pe = Fundamentals.pe_ratio.latest  
    eval_ = stock_pe > market_pe # 'eval' is a builtin name in python, best not to use such names as variable

    return Pipeline(  
              columns={  
                'eval_': eval_  
              },screen=(base_universe)  
          )

@Tentor Testivis, Thanks you!