Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Combining factors in pipeline for non overlapping security sets

I'd like to combine two factor runs that have numerical values for different securities so that they form a single pipeline column. I'm hoping that this is possible but suspect it isn't? I've attached a notebook where I use a filtering factor to drop securities that don't have data, then run a bog standard 12_2 momentum factor and then pass the top and bottom stocks separately into a factor to generate volatility weights for the longs and the shorts independently. I'd like to merge the results so that I have a single pipeline column with the desired weights.

2 responses

Try this:


    class JoinFactors(CustomFactor):  
        #inputs = [factor1, factor2, ...]  
        window_length = 1

        def compute(self, today, assets, out, *inputs):  
            array = np.concatenate(inputs, axis=0)  
            out[:] = np.nansum(array, axis=0)  
            out[ np.all(np.isnan(array), axis=0) ] = np.nan

    top_bottom_scores = (top_scores | bottom_scores)

    long_weights.window_safe = True  
    short_weights.window_safe = True

    final_weights = JoinFactors(inputs=[long_weights, short_weights], mask=top_bottom_scores)  
    universe = final_weights.notnan()

Thanks Luca

Exactly what I was looking for.