In my pipeline, I have a custom factor that assesses whether a particular long term trend is detected.
It returns nan if the trend is not detected and a float if detected. My customfactors take quite a bit of computation time. However, I only need to run the next custom factors for those assets for which a value was returned by the first custom factor. In fact, the other factors take the output of the first factor as input, so running it for assets for which np.nan was returned does not make any sense....
So I was hoping that I could simply create a list of assets for which the first custom factor returned a value that was not null and use that list as a mask going forward for subsequent computations in my pipeline. However, this does not work.... The code below produces an error
So my question is... is there a way to create a mask and limit the assets on which computations are run, by the output of another factor? And if so, how should this be done?
def make_pipeline():
# test_securities = symbols(['AAPL', 'TSLA', 'AMZN'])
# base_universe = StaticAssets(test_securities)
base_universe = Q500US()
universe = base_universe
# Here we limit out universe to four assets
weeklySignal = weeklySignals(window_length = 400, mask = base_universe)
df1 = weeklySignal
df2=[pd.notnull(df1['Weekly signal'])]
maskResults = (pd.notnull(df2['Weekly signal']) == True)
maskInput = list(maskResults.index.get_level_values(1))
# maskInput is a list of assets for which subsequent computations should take place
weeklyConfirmations = weeklyConfirmations(inputs = [weeklySignal], window_length = 400, mask = maskInput)
weeklyOBV = weeklyOBV(inputs = [weeklySignal], window_length = 400, mask = maskInput)
# return Pipeline(columns={'Weekly signal': weeklySignal,
# 'Weekly confirmations': weeklyConfirmations,
# 'weeklyOBV': weeklyOBV
# }, screen=base_universe)
return Pipeline(columns={'Weekly signal': weeklySignal,
'Weekly confirmations': weeklyConfirmations,
'weekly OBV': weeklyOBV}, screen = base_universe)
result = run_pipeline(make_pipeline(), '2015-01-05', '2015-01-08')
result