Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Counter trend (limit orders, Zipline)

This is an example of my learning to use limit and stop orders. In normal operation, there is a limit order to buy at a lower price and a limit order to sell at a higher price. Here is the description:

Long-only counter-trend algorithm that adjusts every X multiple of the standard deviation of price over a lookback period (using limit and stop orders). It allows one price step per bar and increases the long position according to the bar's down price steps. The position is reduced back to a single long if a bar with an up price step occurs before downSteps exceeds downLimit. When downLimit is exceeded, a reset closes all positions. To try to avoid being long during downturns, trading after a reset is restarted when a dual moving average state becomes positive (fast > slow). The algorithm checks if the position size has exceeded cash, or if the bet is too big, although the share size is calculated to try to be fully invested when downSteps equals downLimit. The last trade price, from which steps are calculated, follows price steps up while holding the single long position.

It doesn't use some of the latest features for Quantopian since it is also trying to be compatible with Zipline, and I am hesitant to put it forward as recommended practice (the blind leading the blind). It was also an excuse to throw stuff in to see how things work between Quantopian and Zipline. Nevertheless, there should be some useful info in there for other beginners.

Best regards,

Bob Schmidt

5 responses

Thanks Bob, long example but well put together with a lot of info and comments. Everything here is still up-to-date and working obviously; I think the new features would just make the code more compact. Still a useful example for sure, thanks for sharing!

Gus

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Bob, Cool...Zipline and Quantopian. I like to use Visual Studio with PTVS. Does Python not have a compiler directive/ options so that you can define something like:

#if QUANTOPIAN  
...do this
#else  
...do this.

See here:
http://msdn.microsoft.com/en-us/library/aa691099(v=vs.71).aspx
or here:
http://msdn.microsoft.com/en-us/library/aa691094(v=vs.71).aspx

instead of the try-catch and uncommenting shows a mismatch between proper overriding/ wrapping/ inheritance of Quantopian on top of ZipLine...a simple copy past of code from zipline into quantopian would be the ultimate goal...

Quant Trader, Yes, when I tried to use an if statement, the builder said that the imports in the Zipline code weren't in the whitelist. That's way beyond my pay grade, lol. My simple solution was the comment blocks to display the Zipline code in Quantopian. Normally, when I have the uncommented Zipline file open in Spyder, I can copy/paste just the initialize and handle_data functions between them and the try/except block makes it work pretty well. I think the recent changes that the devs have made makes it alot easier to switch between them and they are probably not done yet.

Best regards,

Bob

Very cool. Good to see people writing zipline compatible algorithms. Two quick suggestions, you can use the new symbols() feature:

    context.stocks = symbols('SPY',  # S+P 500 ETF  
                             'XLY',  # Consumer Discrectionary SPDR Fund  
                             'XLF',  # Financial SPDR Fund  
                             'XLK',  # Technology SPDR Fund  
                             'XLE',  # Energy SPDR Fund  
                             'XLV',  # Health Care SPDR Fund  
                             'XLI',  # Industrial SPDR Fund  
                             'XLP',  # Consumer Staples SPDR Fund  
                             'XLB',  # Materials SPDR Fund  
                             'XLU',  # Utilities SPDR Fund  
                             'TLT')  # Long term treasury bond ETF  

And, you can use history() since zipline will have support for it in the upcoming 0.7 release.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Hi Bob,
You enspired me to update zipline to latest version and see if I can get your code running as well. I have more experience with C# and Microsoft IDE. I am using: http://pytools.codeplex.com/ . Which has also the Package Console Manager with Python integrated. So you can run "pip install ..." directly from the IDE..
J.