Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to implement pipeline?

Here I have an algorithm that takes the daily return for certain stocks. If the daily return of the stock is between 1% and 7%, it will short the stock. If it is between -1% and -7%, it will long. Its more or less saying that the next day is going to be different than the previous day.

What I want to figure out is how to make it so it can use the pipeline and have a more diverse range of stocks. I want to use the Q1500 but I have no clue on how to do it. Could anyone help me figure this out? Thanks!

2 responses

There's a pretty good tutorial on how (and why) to use pipeline in the tutorials section https://www.quantopian.com/tutorials/pipeline.

To get you started maybe an algorithm something like the one that's attached. It places orders at different times than the original and because pipeline always gets the previous days prices, it looks at a bit different returns. Also, it shorts positive returns and goes long on negative returns. This is the stated goal in the post however the code that was attached was just reverse (more like a momentum strategy). Note that the leverage is about 2 because of the weighting that was defined in the original code.

Also, the code in the original algorithm probably isn't working exactly as expected. The ordering lines

        order_target_percent(longs, 1/(len(context.securities_long)))  
and  
        order_target_percent(shorts, 1/(len(context.securities_short)))

Should be as follows (notice the floating point 1.0) and also in order to short a stock one needs to order a negative qty.

        order_target_percent(longs, 1.0/(len(context.securities_long)))  
and  
        order_target_percent(shorts, -1.0/(len(context.securities_short)))

Thank you that explains it pretty well!