Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Long-only counter-trend with Zipline

Hi all,

First post. This is a mangled version of Dan's martingale example. I added a run loss limit and the ability to easily change the bet size function of the number of down steps. One can still discover what a disaster a pure martingale is by setting the down step limit to a large number.

This algorithm has two parameters: the price step percentage at which to adjust it's position, and the down step loss limit. It is always long and increases position on down step losses according to the bet size. When the down step limit is exceeded, the position is reset to a single long position. When an up step is exceeded, all positions except the single long are closed. If only the single long, then the last trade price is moved up a step without trading, so the single long follows prices up.

This will run in Zipline if the comment blocks around the code at the top and bottom are removed. Or else just delete them if you are not using Zipline.

best regards,

Bob Schmidt

5 responses

in my test the strategy on Zipline0.61 , the show_results didn't be invoke after algo.run, what was wrong for me ?

Hi Novice TAI,

I think that Quantopian/Zipline compatibility is a high priority for the developers right now based on recent releases, so we are probably pushing into areas that are in heavy development. But I am only guessing. It sounds like the algo.run worked for you. Did you get a long printout of all the trades? But no plots? Did you get an error message?

In previous runing through run_algo.py, the show_result method cannot be invoke,
If direct runing through paython commnad it running well ,please see the running result image,
but the reuslt seems have difference compare running in Quantopian, anyway thanks Bob share the strategy!

Thank you for finding the problem. I was running the file in Spyder. I'm not familiar with running Zipline that way. The results are a little different. Yahoo and Quantopian have somewhat different historical prices. There is a recent post about that.

I also realized that there was no need for the algorithm to crash with no information when the cash was used up or the bet size was too big. Instead it resets to a single long position and keeps on going. That way we can see better the effect on returns when the algorithm takes its loss.

I introduced a bug where it was allowing one position too many. Sigh . . .