Back to Community
Industry PE Ratio

Hi any ideas on how to create a particular stock's industry PE ratio as a parameter for factor investing? I tried creating a custom factor but it doesn't work. Thanks!

class Sector_Per(CustomFactor):  
    inputs = [valuation_ratios.pe_ratio, asset_classification.morningstar_sector_code, USEquityPricing.close, valuation.shares_outstanding]  
    window_length = 252

    # Compute sector per  
    def compute(self, today, assets, out, per, sector, close, shares):  
        sector_code = query(sector).filter(assets)  
        out[:] = query(per, sector).filter(sector == sector_code).mean()  
2 responses

Hello, enjoy !

class IndustryMeanPE(CustomFactor):  
inputs = [Fundamentals.pe_ratio, Fundamentals.morningstar_industry_group_code]  
window_length = 252

    def compute(self, today, assets, out, pe, industry_codes):  
        df = pd.DataFrame(index=assets, data={"pe_ratio": pe[-1], "industry_codes": industry_codes[-1]}) 

        out[:] = df.groupby("industry_codes").transform(np.mean).values.flatten()  

Many thanks Mathieu!