I got a NonWindowSafeInput error and resolved it by simply setting window_safe=True. But I don't understand why I got the error and how forcing window_safe to true fixes it. Hoping someone may explain further? Please see the code below for reference:
from quantopian.pipeline.data.builtin import USEquityPricing
from quantopian.pipeline.factors import CustomFactor
import numpy as np
class Momentum(CustomFactor):
# Compute momentum
def compute(self, today, assets, out, close):
out[:] = close[-1] / close[0]
def MeanMom(mwl, wl, mom_input):
class MeanMomFact(CustomFactor):
mom = Momentum(window_length=wl, inputs=[mom_input])
# Fix NonWindowSafeInput error
mom.window_safe = True
inputs = [mom]
window_length = mwl
def compute(self, today, asset_ids, out, mom):
# Calculates the mean momentum over window length mwl
out[:] = np.nanmean(mom, axis=0)
return MeanMomFact()
def make_pipeline():
MM = MeanMom(mwl=5, wl=5, mom_input=USEquityPricing.close)
return Pipeline(columns={'MeanMom': MM})
result = run_pipeline(make_pipeline(), start_date = '2015-01-01', end_date = '2016-01-01')
result.head(10)
Thanks ahead.