Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Can i create a factor on the output of another factor (that has already been processed) in the pipeline?

I would like to create a factor, then run another factor on that output to make another factor. I know how to get the USEquityPricing in the factor but i can't work out how to get factor fields i have created.

3 responses

To use a factor as an input to another factor just pass the factor. Something like this.

    pctChange = PctChange(inputs=[USEquityPricing.close], window_length=2)

    # Use pctChange as an input to the SimpleMovingAverage factor  
    # Set the window length to be the number of trading days to average  
    pctChange_avg = SimpleMovingAverage(inputs=[pctChange], window_length=10)  

The one caveat is that the initial factor must be 'window_safe'. Take a look at this post for some explanation on that https://www.quantopian.com/posts/get-lagged-output-of-pipeline-custom-factor . Basically it means adding a line of code to the initial custom factor.

# Add the line below to be able to use this factor as an input to other factors  
window_safe = True  

A few comments... your custom factor 'PctChange' is really similar to the built-in factor Returns. May want to use that instead? Also, it's really not mathematically correct to average arithmetic returns. One should use log returns instead. Consider the case of a -10% return followed by a +10%. The net return is not 0%. However log(1-.1) + log(1+.1) (ie the log returns) gives -.105+.095 = -.01 which is a small loss and is correct.

Fortunately there is a built in method to get the log of a factor log1p() . (See Scott Sandersons post here https://www.quantopian.com/posts/log-normal-return-built-in-factor). So, this is perhaps the best way to get the average return over a given window.

    pctChange_log_avg = SimpleMovingAverage(inputs=[pctChange.log1p()], window_length=10)

Or by using the built in 'Returns' factor.

    pctChange_log_avg = SimpleMovingAverage(inputs=[Returns(window_length=2).log1p()], window_length=10)

Hope that helps. Maybe more info than needed? See attached notebook for changes.

For simplicity, would everybody please imagine an actual thumbs-up emoticon from me on all of Dan's posts. :)

Thanks Dan, great response!
Actually, my goal wasn't to get the returns, I was just using that as an example to keep it simple.

What I was really doing was getting the relative strength of a stock vs's the index over time (sma).
Stock / index (ratio)
percent change of that ratio each day
Add up the percent change each day to the last day
Get two moving averages of this arbitrary ratio percent change line
Do a moving average cross over of these lines.

I use this as one of my filters to get outperforming stocks.

If you want the code for this I'm happy to share once I've got it fully working.. I'm porting one of my algos from R (quantstrat) to quantopian.

thumbs up

http://blogs.vmware.com/management/files/2017/10/group-thumbs-up.jpg