#Create the classes for many rav variables and factorsclassLowVol(CustomFactor):inputs=[Returns(window_length=2)]window_length=25defcompute(self,today,assets,out,close):out[:]=-np.nanstd(close,axis=0)classgrowth(CustomFactor):window_length=1inputs=[morningstar.operation_ratios.revenue_growth]defcompute(self,today,assets,out,growth):out[:]=growthclasssus_growth(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.sustainable_growth_rate]defcompute(self,today,assets,out,sustainable_growth_rate):out[:]=sustainable_growth_rate#Qualityclassturns(CustomFactor):window_length=1inputs=[morningstar.operation_ratios.assets_turnover]defcompute(self,today,assets,out,assets_turnover):out[:]=assets_turnoverclassroic(CustomFactor):window_length=1inputs=[morningstar.operation_ratios.roic]defcompute(self,today,assets,out,roic):out[:]=roicclassmargins(CustomFactor):window_length=1inputs=[morningstar.operation_ratios.ebitda_margin]defcompute(self,today,assets,out,ebitda_margin):out[:]=ebitda_margin#Valuationclasspeg_ratio(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.peg_ratio]defcompute(self,today,assets,out,peg_ratio):out[:]=np.where(peg_ratio<=0,0,1/peg_ratio)classpe_ratio(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.pe_ratio]defcompute(self,today,assets,out,pe_ratio):out[:]=np.where(pe_ratio<=0,0,1/pe_ratio)classpb_ratio(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.pb_ratio]defcompute(self,today,assets,out,pb_ratio):out[:]=np.where(pb_ratio<=0,0,1/pb_ratio)classtotal_yield(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.total_yield]defcompute(self,today,assets,out,total_yield):out[:]=total_yieldclassev_ebitda(CustomFactor):window_length=1inputs=[morningstar.valuation_ratios.ev_to_ebitda]defcompute(self,today,assets,out,ev_to_ebitda):out[:]=np.where(ev_to_ebitda<=0,0,1/ev_to_ebitda)classliquidity(CustomFactor):inputs=[USEquityPricing.volume,USEquityPricing.close]window_length=21defcompute(self,today,assets,out,volume,close):out[:]=(volume*close).mean(axis=0)classsector(CustomFactor):inputs=[morningstar.asset_classification.morningstar_sector_code]window_length=1defcompute(self,today,assets,out,morningstar_sector_code):out[:]=morningstar_sector_code[-1]classAvgDailyDollarVolumeTraded(CustomFactor):inputs=[USEquityPricing.close,USEquityPricing.volume]window_length=20defcompute(self,today,assets,out,close_price,volume):out[:]=np.mean(close_price*volume,axis=0)classPriceReturn(CustomFactor):inputs=[USEquityPricing.close]window_length=2defcompute(self,today,assets,out,close):out[:]=close[-1]/close[0]

assets=results.index.levels[1].unique()# Make sure to extend the pricing a bit to get a full set of returns.pricing=get_pricing(assets,start_date='2012-06-30',end_date='2016-07-31',fields='open_price')