Would it be possible to combined the Factors used in your algo and mask the below "Make_Filters" coding over them? Basically I would want to use them to filter on the factors which can produce the alpha.
def make_filters():
class STA(CustomFactor):
inputs = [Fundamentals.operating_cash_flow,
Fundamentals.net_income_continuous_operations,
Fundamentals.total_assets]
window_length = 1
def compute(self, today, assets, out, ocf, ni, ta):
ta = np.where(np.isnan(ta), 0, ta)
ocf = np.where(np.isnan(ocf), 0, ocf)
ni = np.where(np.isnan(ni), 0, ni)
out[:] = preprocess(abs(ni[-1] - ocf[-1])/ ta[-1])
class SNOA(CustomFactor):
inputs = [Fundamentals.total_assets,
Fundamentals.cash_and_cash_equivalents,
Fundamentals.current_debt, # same as short-term debt?
Fundamentals.minority_interest_balance_sheet,
Fundamentals.long_term_debt, # check same?
Fundamentals.preferred_stock] # check same?
window_length = 1
def compute(self, today, assets, out, ta, cace, cd, mi, ltd, ps):
ta = np.where(np.isnan(ta), 0, ta)
cace = np.where(np.isnan(cace), 0, cace)
cd = np.where(np.isnan(cd), 0, cd)
mi = np.where(np.isnan(mi), 0, mi)
ltd = np.where(np.isnan(ltd), 0, ltd)
ps = np.where(np.isnan(ps), 0, ps)
results = ((ta[-1]-cace[-1])-(ta[-1]-cace[-1]-ltd[-1]-cd[-1]-ps[-1]-mi[-1]))/ta[-1]
out[:] = preprocess(np.where(np.isnan(results),0,results))
class ROA(CustomFactor):
inputs = [Fundamentals.roa]
window_length = 1
def compute(self, today, assets, out, roa):
out[:] = preprocess(np.where(roa[-1]>0,1,0))
class FCFTA(CustomFactor):
inputs = [Fundamentals.free_cash_flow,
Fundamentals.total_assets]
window_length = 1
def compute(self, today, assets, out, fcf, ta):
out[:] = preprocess(np.where(fcf[-1]/ta[-1]>0,1,0))
class ROA_GROWTH(CustomFactor):
inputs = [Fundamentals.roa]
window_length = 252
def compute(self, today, assets, out, roa):
out[:] = np.where(roa[-1]>roa[-252],1,0)
class FCFTA_ROA(CustomFactor):
inputs = [Fundamentals.free_cash_flow,
Fundamentals.total_assets,
Fundamentals.roa]
window_length = 1
def compute(self, today, assets, out, fcf, ta, roa):
out[:] = preprocess(np.where(fcf[-1]/ta[-1]>roa[-1],1,0))
class FCFTA_GROWTH(CustomFactor):
inputs = [Fundamentals.free_cash_flow,
Fundamentals.total_assets]
window_length = 252
def compute(self, today, assets, out, fcf, ta):
out[:] = preprocess(np.where(fcf[-1]/ta[-1]>fcf[-252]/ta[-252],1,0))
class LTD_GROWTH(CustomFactor):
inputs = [Fundamentals.total_assets,
Fundamentals.long_term_debt]
window_length = 252
def compute(self, today, assets, out, ta, ltd):
out[:] = preprocess(np.where(ltd[-1]/ta[-1]<ltd[-252]/ta[-252],1,0))
class CR_GROWTH(CustomFactor):
inputs = [Fundamentals.current_ratio]
window_length = 252
def compute(self, today, assets, out, cr):
out[:] = preprocess(np.where(cr[-1]>cr[-252],1,0))
class GM_GROWTH(CustomFactor):
inputs = [Fundamentals.gross_margin]
window_length = 252
def compute(self, today, assets, out, gm):
out[:] = preprocess(np.where(gm[-1]>gm[-252],1,0))
class ATR_GROWTH(CustomFactor):
inputs = [Fundamentals.assets_turnover]
window_length = 252
def compute(self, today, assets, out, atr):
out[:] = preprocess(np.where(atr[-1]>atr[-252],1,0))
class NEQISS(CustomFactor):
inputs = [Fundamentals.shares_outstanding]
window_length = 252
def compute(self, today, assets, out, so):
out[:] = preprocess(np.where(so[-1]-so[-252]<1,1,0))
class GM_GROWTH_2YR(CustomFactor):
inputs = [Fundamentals.gross_margin]
window_length = 504
def compute(self, today, assets, out, gm):
out[:] = preprocess(gmean([gm[-1]+1, gm[-252]+1,gm[-504]+1])-1)
class GM_STABILITY_2YR(CustomFactor):
inputs = [Fundamentals.gross_margin]
window_length = 504
def compute(self, today, assets, out, gm):
out[:] = preprocess(np.std([gm[-1]-gm[-252],gm[-252]-gm[-504]],axis=0))
class ROA_GROWTH_2YR(CustomFactor):
inputs = [Fundamentals.roa]
window_length = 504
def compute(self, today, assets, out, roa):
out[:] = preprocess(gmean([roa[-1]+1, roa[-252]+1,roa[-504]+1])-1)
class ROIC_GROWTH_2YR(CustomFactor):
inputs = [Fundamentals.roic]
window_length = 504
def compute(self, today, assets, out, roic):
out[:] = preprocess(gmean([roic[-1]+1, roic[-252]+1,roic[-504]+1])-1)
class GM_GROWTH_8YR(CustomFactor):
inputs = [Fundamentals.gross_margin]
window_length = 8
def compute(self, today, assets, out, gm):
out[:] = preprocess(gmean([gm[-1]+1, gm[-2]+1, gm[-3]+1, gm[-4]+1, gm[-5]+1, gm[-6]+1, gm[-7]+1, gm[-8]+1])-1)
class GM_STABILITY_8YR(CustomFactor):
inputs = [Fundamentals.gross_margin]
window_length = 9
def compute(self, today, assets, out, gm):
out[:] = preprocess(gm[-8])
class ROA_GROWTH_8YR(CustomFactor):
inputs = [Fundamentals.roa]
window_length = 9
def compute(self, today, assets, out, roa):
out[:] = preprocess(gmean([roa[-1]/100+1, roa[-2]/100+1,roa[-3]/100+1,roa[-4]/100+1,roa[-5]/100+1,roa[-6]/100+1,roa[-7]/100+1,roa[-8]/100+1])-1)
class ROIC_GROWTH_8YR(CustomFactor):
inputs = [Fundamentals.roic]
window_length = 9
def compute(self, today, assets, out, roic):
out[:] = preprocess(gmean([roic[-1]/100+1, roic[-2]/100+1,roic[-3]/100+1,roic[-4]/100+1,roic[-5]/100+1,roic[-6]/100+1,roic[-7]/100+1,roic[-8]/100+1])-1)
return {
'STA': STA,
'SNOA': SNOA,
'ROA': ROA,
'FCFTA': FCFTA,
'ROA_GROWTH': ROA_GROWTH,
'FCFTA_ROA': FCFTA_ROA,
'FCFTA_GROWTH': FCFTA_GROWTH,
'LTD_GROWTH': LTD_GROWTH,
'CR_GROWTH': CR_GROWTH,
'GM_GROWTH': GM_GROWTH,
'ATR_GROWTH': ATR_GROWTH,
'NEQISS': NEQISS,
'GM_GROWTH_2YR': GM_GROWTH_2YR,
'GM_STABILITY_2YR': GM_STABILITY_2YR,
'ROA_GROWTH_2YR': ROA_GROWTH_2YR,
'ROIC_GROWTH_2YR': ROIC_GROWTH_2YR,
'GM_STABILITY_8YR': GM_STABILITY_8YR,
'ROA_GROWTH_8YR': ROA_GROWTH_8YR,
'ROIC_GROWTH_8YR': ROIC_GROWTH_8YR,
}