I'm having difficulties running the below code and run into error message: BadBinaryOperator: Can't compute Latest < list. What I'm trying to do is combined the factors in "make_factors" then rank them based on their industry mean pe ratio to start.
def make_factors():
class Intraday(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.open]
window_length = 6
window_safe = True
def compute(self, today, assets, out, close, open):
x = np.cumprod(open[:] / close[:], axis=0)[-1] - 1
out[:] = preprocess(x)
class Reversion_Volume(CustomFactor):
inputs = [USEquityPricing.volume]
window_length = 14
window_safe = True
def compute(self, today, assets, out, volumes):
x = -volumes[-1] / np.mean(volumes, axis=0)
out[:] = preprocess(x)
class BullBearIntensity(CustomFactor):
inputs = [stocktwits.bull_minus_bear]
window_length = 4
window_safe= True
def compute(self, today, assets, out, bull_minus_bear):
x= np.nanmean(bull_minus_bear, axis = 0)
out[:] = preprocess(x)
class PsychSignalMessages(CustomFactor):
inputs = [stocktwits.bull_scored_messages,stocktwits.bear_scored_messages,stocktwits.total_scanned_messages]
window_length = 30
def compute(self, today, assets, out, bull_msgs, bear_msgs, ts):
x = np.nanmean(((bull_msgs + bear_msgs)/ts), axis = 0)
out[:] = preprocess(x)
factors = [
Intraday,
Reversion_Volume,
BullBearIntensity,
PsychSignalMessages,
]
return factors
def IndustryAVG():
class IndustryMeanPE(CustomFactor):
inputs = [Fundamentals.pe_ratio, Fundamentals.morningstar_industry_group_code]
window_length = 252
def compute(self, today, assets, out, pe_ratio, industry_codes):
df = pd.DataFrame(index=assets, data={"pe_ratio": pe_ratio[-1], "industry_codes": industry_codes[-1]})
out[:] = df.groupby("industry_codes").transform(np.mean).values.flatten()
indavg = [
IndustryMeanPE,
]
return indavg
def make_pipeline():
universe = QTradableStocksUS()
factors = make_factors()
beta = SimpleBeta(target=sid(8554),regression_length=260,
allowed_missing_percentage=1.0
)
indavg = IndustryAVG()
#____________________________Combined Alphas___________________________
combined_alpha = None
for f in factors:
if combined_alpha == None:
combined_alpha = f(mask=universe).rank(mask=indavg)
else:
combined_alpha += f(mask=universe).rank(mask=indavg)
#combined_alpha_sma = None
#for f in factors:
#if combined_alpha_sma == None:
#combined_alpha_sma = SimpleMA([f(mask=universe)],window_length=FACTOR_AVG_WINDOW)
#else:
#combined_alpha_sma += SimpleMA([f(mask=universe)],window_length=FACTOR_AVG_WINDOW)
pipe = Pipeline(columns = {
'combined_alpha':combined_alpha,
'beta':beta,
},
screen = universe)# & screens)
return pipe