Turtle Trading Strategy

Turtle trading is a well known trend following strategy that was originally taught by Richard Dennis. The basic strategy is to buy futures on a 20-day high (breakout) and sell on a 20-day low, although the full set of rules is more intricate. I've modeled the meat of the strategy in Quantopian and used it to trade exchange-traded funds (ETFs), in this case just some silver and copper securities.

I used rules from here. From what I have seen, the rules of turtle trading slightly vary from source to source, however what's outlined in that PDF seems well-guided and reliable. If you want to adjust the rules you can clone this and it should be fairly straightforward from there. I've also added an option in the code if you only want to long and not short. To trigger buys and sells, the code calculates the goal amount of shares then works from there to determine how many to buy or sell. This method for determining order amount works well for things like risk-adjusted portfolio sizes.

This is a pretty fundamental strategy and it seems to work well. There are a few different parameters to play with, so clone this and see if you can get some good results or even add to the code in any way.

If you want to experiment with adding different ETFs, you can get ideas from a list of futures like this one. From there just Google for whatever ETFs, like "corn etfs", and add the respective symbols to the code.

Clone Algorithm
Backtest from to with initial capital ( data)
Custom data:
Total Returns
Information Ratio
Benchmark Returns
Max Drawdown
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Information Ratio 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
17 responses

Great work Gus, thanks for forwarding this my way. I used to run a strategy that was very similar to the Turtle Trading strategy. The most important part of the strategy though is the pyramiding of the position coupled with the matrix of which assets you could hold together and in what size. The money management aspect of the strategy was almost more important than the basic momentum algo. I'm wondering when Quantopian will allow that kind of detailed money management. Either way, this is a great start, and I'm sure others will build on it in the future, that's the value of this platform, people working together to build interesting stuff. Thanks!

Sure, no problem. Unfortunately that stuff you mentioned is difficult to implement in a dynamic way. I think there would have to be a categorization of securities/futures/ETFs, which I suppose is actually possible with fetcher or extra code.

IMO the order of importance of designing this system is 1) the markets you will trade, 2) position sizing, 3) exit strategy, 4) entry strategy. It needs to be well diversified to work well across stock markets, commodities, currencies and bonds. You'd also probably want to keep each category from taking up more than 25 percent of the portfolio. Keep the position size no more than 1-2 percent of equity. The problem I've run into using the turtle strategy on ETFs is leverage. You may not be able to take all the signals because of margin requirements, which you don't have with futures.

In any case, thanks a lot for the algo. I'll have fun playing with it.

Hi Gus,

This is great and I have tried to work with your sample as a way to learn how to use the interface and Quantopian.

I've adapted parts to make a modified strategy with an EMA requirement and also a trailing stop loss. Somewhere in my modification it went wrong and I get a run time error. Runtime exception: ValueError: max() arg is an empty sequence - it appears my sequence is not inserting the price highs....any ideas what is going on with my backtester and why its failing? Your help would be greatly appreciated.

Where do I post my code without taking up so much space on your page?


Hey Mason, glad to hear you like this. I'm not sure where that error is coming from. You can do as Dan suggested and I'll try to help.

Hi Gus,

Thanks for sharing this Turtle strategy. I am quite new to coding and having trouble implementing a few more rules --- specifically with regards to placing sell stop orders when a trade is triggered and placed.
For example, if a security triggers a new long entry, I would like a sell stop order to be placed, with the stop at (entry price - 2*N) and vice versa for shorts.

Thanks in advance!

SJ - I suggest that you attempt to make the code, and then make a new post that explains what is working and what isn't working. You'll get more help that way. Asking a question on an old thread, unfortunately, doesn't work very well.

Thanks Dan -- will do.

Thanks SJ! I'll look out for your thread, Dan is right, making a new one is good.

I'll try to get you started though in case you just need a bump to get going. You can call your stop order by doing something like:

order(security, amt_to_buy, stop_price=entry_price-2*N)  

(see here for more: https://www.quantopian.com/help#ide-ordering)

So if you just want that to trigger when something happens (when some random variable a is greater than 3, for example):
if a > 3: order(security, amt_to_buy, stop_price=entry_price-2*N)

For shorts, you should just be able to use a negative amt_to_buy, if I'm understanding correctly.

Great -- thanks so much Gus! Will certainly post the code on a new thread when it is complete.

Hey SJ - are you still working on this code? I would be interested to see how it works

Does anyone ever look at this source code closely?

First, the testing commodity is uranium? Is WW3 going to happen so Uranium is a liquid commodity to trade?

Second, most of system components are missing: the adding unit part, stop, or exit. I was amazed first by Turtle system can be implemented with 101 lines of concise code and then I figure out it's probably just an intern who wants to get his assignment done.

Conclusion: The result is misleading and the code is not even half cooked. DO NOT USE IT.

The ETFs I used are simply random examples, that can be customized. Because of how Quantopian works right now, it is not possible to use the exact turtle trading rules. As I said in the code comments, this is a sample template to share with the community.


I am new to this and was playing with your code but I am lost on the tickers. How does sid(37732), # Euro translate to the ticker EUO If I wanted to use say XBI where do I find the sid number?


Hi Mitch,

Welcome to Quantopian!

There are two easy lookup methods to find stocks in the IDE. You can use the sid() method or the symbol() method, either of which should pop up an autocomplete window for you on the open parenthesis. The sid number is a unique identifier on our platform, as trading symbols can be reused on the exchanges. Then just start typing the ticker you are looking for and you should see it show up in the autocomplete list (see screenshot below).


Let me know if this answers your question. Best wishes, Jess

I have been trading the Turtle System with stocks and ETFs for over 10 years. (I'm not a programmer. I have traded futures and options but prefer stocks and ETFs.) There was a misconception a couple of comments back that needs correcting. All Turtle System 1 entries are at 20 days and exits at 10 days. All Turtle System 2 entries are at 55 days and exits at 20 days against your position; long or short. I didn't look back on this thread to see if anyone has mentioned it before. Hope this is helpful to someone.