Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Using CustomFactors in CustomFactors and passing in numerical values in CustomFactors

I would like to be able to use CustomFactors inside other CustomFactors. It allows for code reuse, high cohesion and low coupling. No copy paste is needed.

I prepared a notebook with some code examples. For me it makes sense to work with CustomFactors as I explain in the notebook.
Is this the way you look at it, now or for the future. I am correct in assuming that you can not:

  1. Use CustomFactors within other CustomFactors?
  2. Pass in numerical arguments into the CustomFactor

Thanks Rene

5 responses

+1 I always wanted to use EWMA, EWMSTD and SimpleMovingAverage on other Factors but we can't.

Q any feedback on this topic? This would improve performance dramatically in certain scenarios where complicated factors are in place.

Would also love to see this implemented!

Q explained here why this is not possible:

"In general we still do not support the use of factors as inputs, but with the release of these new correlation/regression factors we have deemed that a select few factors can safely be used as inputs (such as Returns). The main difficulty of using other factors as inputs here is accounting for splits. For example, if we try to use SimpleMovingAverage as the input to our regression factor, and our target asset undergoes a split, the regression output would be distorted for any regressions computed over a window containing the date of the split. Factors that are comparable across splits, such as zscore and rank, are other examples (besides Returns) of factors that could be used as inputs."

I spotted this solution for passing in parameters into a custom factor.

def PPO(fast_period=12, slow_period=26):  
    """  
    Function to produce CustomFactor of Percentage Price Oscillator  
    Called in same way as a normal class, but used in order to give variable  
    fast- and slow- periods  
    Parameters  
    ----------  
    fast_period : int > 0  
        shorter period moving average  
    slow_period : int > 0 (> fast_period)  
        longer period moving average

    Returns  
    -------  
    zipline.CustomFactor : Percentage Price Oscillator factor

    **Default Inputs:**  12, 26

    http://www.investopedia.com/terms/p/ppo.asp  
    """  
    class PPO_(CustomFactor):

        inputs=[USEquityPricing.close]  
        window_length = slow_period

        def compute(self, today, assets, out, close):  
            slowMA = np.mean(close, axis=0)  
            fastMA = np.mean(close[-fast_period:], axis=0)  
            out[:] = ((fastMA - slowMA) / slowMA) * 100.

    return PPO_()