Dear Expert,
I am trying to create a custom factor using GRANT's template but I encountered the following error. Seem like having a class within a class can generate this error?
NonWindowSafeInput: Can't compute windowed expression SimpleMovingAverage([TEST(...)], 3) with windowed input TEST([NumExprFactor(...)], 252).
def preprocess(a):
a = a.astype(np.float64)
a[np.isinf(a)] = np.nan
a = np.nan_to_num(a - np.nanmean(a))
a = winsorize(a, limits=[WIN_LIMIT,WIN_LIMIT])
return preprocessing.scale(a)
def make_factors(rng):
class LastTwoQuarters(CustomFactor):
# Get the last 2 reported values of the given asof_date + field.
outputs = ['q2', 'q1']
window_length = 130
def compute(self, today, assets, out, asof_date, values):
for column_ix in range(asof_date.shape[1]):
_, unique_indices = np.unique(asof_date[:, column_ix], return_index=True)
quarterly_values = values[unique_indices, column_ix]
if len(quarterly_values) < 2:
quarterly_values = np.hstack([
np.repeat([np.nan], 2 - len(quarterly_values)),
quarterly_values,
])
quarterly_values = quarterly_values[-2:]
out[column_ix] = quarterly_values
class TEST(CustomFactor):
FCFYLD = LastTwoQuarters(inputs = [Fundamentals.fcf_yield_asof_date, Fundamentals.fcf_yield], mask=universe)
# FCFYLD.window_safe = True
# FCFYLD.q1.window_safe = True
FCF_YLD = FCFYLD.q1 + FCFYLD.q2
FCF_YLD.window_safe = True
inputs = [FCF_YLD]
window_length = 252
def compute(self, today, assets, out, FCF_YLD):
out[:] = preprocess(np.nan_to_num(FCF_YLD))
factors = [
TEST
]
return factors
def make_pipeline_1():
universe = QTradableStocksUS()
factors = make_factors((0,30))
combined_alpha = None
for f in factors:
if combined_alpha == None:
combined_alpha = SimpleMovingAverage([f(mask=universe)],window_length=FACTOR_AVG_WINDOW)
else:
combined_alpha += SimpleMovingAverage([f(mask=universe)],window_length=FACTOR_AVG_WINDOW)
beta = SimpleBeta(target=sid(8554),regression_length=260,
allowed_missing_percentage=1.0
)
pipe = Pipeline(columns = {
'combined_alpha':combined_alpha,
'beta':beta,
},
screen = universe)
return pipe
thanks