Greetings,
I want to compare a particular equity's PE to its industry and sector PE. My theory is that if an equity's PE is greater than it's industry and sector, it is overvalued.
I was able to create this metric by manipulating the resulting DataFrame
after running the Pipeline
. I would like to create a factor (inheriting CustomFactor
) for this instead. My question is how I might do this.
# Forward Price to Earnings Ratio (MORNINGSTAR)
class Price_To_Forward_Earnings(CustomFactor):
...
def filter_universe():
"""
Modified from Nathan Wolfe's Algorithm
"""
...
universe = Q500US()
pipe = Pipeline(
columns={
'Price_To_Forward_Earnings': Price_To_Forward_Earnings(mask=universe),
'morningstar_industry_group_code': asset_classification.morningstar_industry_group_code.latest,
'morningstar_sector_code': asset_classification.morningstar_sector_code.latest
},
screen=filter_universe()
)
results = run_pipeline(pipe, '2014-01-01', '2014-06-30')
results = results.fillna(value=0.0)
sector_forward_pe = results.groupby(['morningstar_sector_code'])['Price_To_Forward_Earnings'].mean()
results['sector_forward_pe'] = results['morningstar_sector_code'].map(sector_forward_pe)
industry_forward_pe = results.groupby(['morningstar_industry_group_code'])['Price_To_Forward_Earnings'].mean()
results['industry_forward_pd'] = results['morningstar_industry_group_code'].map(industry_forward_pe)
results['ForwardPEvsSector'] = results['Price_To_Forward_Earnings']/results['sector_forward_pe']
results['ForwardPEvsIndustry'] = results['Price_To_Forward_Earnings']/results['industry_forward_pd']
The result
DataFrame
now contains ForwardPEvsSector
and ForwardPEvsIndustry
.
How might I accomplish this within a custom factor?