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
1214
Loading...
Backtest from to with initial capital ( data)
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
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.
37 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?

thanks

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.

Rui,
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.

Gus

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?

Thanks

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).

symbol_lookup

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.

Hi Bob,

What were some of your lessons learned while using the Turtle System with stocks and ETFs?

Gus this is a great start. I was reading the book The Complete Turtle Trader and I stumble upon your your post. However I have notice that in your algo you are not adding units if you are in the good side of the trend. This is what it would make the strategy more solid and move the stops as the trend is moving up or down. Do you have any plans to complete the algo to include these rules? or have anyone implement such rules ?

Thanks,
EG

EG, I am not a programmer and usually only trade the long side. It pays well, but requires more patience. Others might be interested in seeing it both ways.

Thanks,

Bob

Hey Erick, the algorithm does that, but maybe not to the level you desire. You can see in this line from my original code:

#compute how many units to buy or sell  
trade_amt = math.floor(account_size*.01/N)  

As our account grows, the amount we buy or sell should grow too. If you want to change the amount bought, you could add an additional multiplier in there — maybe something like current_value/context.portfolio.starting_cash, or a multiple of that.

Looking back, this code is a bit sloppy, sorry if it's tough to interpret :)

No problem. I will play with the algo to see if can improve it.

Thanks

if anyone is interested, I can help explain the strategy in simple step by step terms ( I am currently trading it using FX)
I am really curious to see how this can/will work on index and futures

I would love to this worked on more. The system is really cool. Ordered a book on it, reading the pdf tonight.

Hopefully I'll have somthing to add !

To see* :(

I expanded on this for my first algo here.

I used System 2 for entries (55 day breakout) and exits (20 day breakout.) Here is my understanding of the Turtle Trading System (System 2)...

  • Used 20 Day ATR (aka N) for Money Management and Risk Control via Position Sizing
  • 1 N = 1% of portfolio equity
  • Use 55-day breakouts as entries (high = long, low = short)
  • Loser exit (stop) if price moves 2 N against you (lose 2% of account equity)
  • Winning exit when price hits a 20 day high / low against you (after hopefully making significant moves with you)

Some things not included...
-More ETFs
-Strategy 1 (the 20 day breakout model, 10 day exit)
-The Turtles traded up to 4N per position, including adding units or pyramiding after 1N initial entry
-Limits per correlated markets
-Alternative stop strategy -Whipsaw
-Favoring 'stronger' entry signals over 'weaker' ones

Clone Algorithm
28
Loading...
Backtest from to with initial capital ( data)
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Information Ratio
--
Benchmark Returns
--
Volatility
--
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
There was a runtime error.

@Jim
It goes 14x leverage.

The problem I always found with the position sizing for the Turtle strategies was that for things which didn't move much, it required an obscene amount of cash to be placed in one position, preventing other positions from being open (and hence, the possibility for uncorrelated returns). I assume this is fine for futures (since its already leveraged) but is not the case for stocks.

Michael Covel has said that trend following strategies do work for ETFs, but I've yet to work out how. He did link to a nice paper. They give a definition for a trend following signal -- if I remember, the position size is relative to the strength of the signal, based on the theoretical returns for trading the previous month.

If anyone makes progress, I for one would be happy to see it ;)

@james yes, but the lot sizes were volatility based, so the raw dollar amount was irrelevant, it should be based on risk per lot being equal to risk per lot for other assets. The important part here is that you don't have too many lots of the same type of highly correlated assets.

@James thanks for sharing the paper. I agree, the cash required makes the strategy difficult because it limits the number of entry signals you can take, which drastically effects the results of the strategy. The leverage can be reduced by trading only lower priced ETFs with a higher ATR. The currency ETFs I used are priced very high relative to their N. In some cases, 3x the entire portfolio is needed to risk 1% per N. That being said, I believe @Leigh is right that the 'risk' is the same per position because they are equalized by the 20-day ATR.

I'm new to trading, what leverage should I aim to use? What's viable for live trading? I see the Quantopian contest sets a limit at 3.

I will give the paper a read. My next step is to play with the strength of signals based on other trends and to reduce the taking of multiple signals in highly correlated markets.

The Turtle trading strategy was not designed to be used on ETFs, it was specifically designed to be used on futures contracts given the nature of the leverage and cost of that leverage. I'm not sure this works with ETFs frankly.

The turtle trading strategy is nothing more than a channel breakout system. Turtle trading is simple trend following. Which stock traders usually call momentum. Trend following/ momentum work on any instrument. I trade ETfs on momentum. There are COUNTLESS ways you can get your algo to follow the trend and the channel breakout is one of them. The original parameters are useless in the futures markets but easily adapted:

https://anthonyfjgarner.files.wordpress.com/2015/06/garner0210.pdf

@Leigh, I understand the equal risk but if N is small then trade_amt becomes quite big, eating a larger portion of the portfolio than I would like. When the position size is big, it stops you from taking out other positions without increasing leverage. So the problem (out of futures) was how to position size correctly, which is where I got stuck (fixed fraction, relative, didn't seem to work reliably).

@Jim,
I would keep the algo to 1x leverage and develop the algo to make sure it does not go above it.

You can swap order for order_target and that will help a lot. Losing positions can be exited with order_target(sid, 0).

When there are lots of positions that it could have open, the decision is then which to take, or otherwise what weighting to assign them.

That's not quite accurate. Yes it is a momentum strategy and that is where most of the returns come from. But much a lot of the alpha was also created through the risk management strategy dealing with volatility and risk weighted lot sizes and the number of lots of like assets that you can hold at any one time. This is incredibly important to your return curve because it prevents massive drawdowns by being too heavily exposed to any one asset type. This is especially important given that signals will fail for extended periods of time before catching 1 big trend and if you're not using the correct risk management strategy here your drawdowns will be too large.

Perhaps you might like to read the following strategy paper I wrote which covers risk parity trading in the futures markets. This is what futures traders usually mean by money management. You can use risk parity with stocks.....but you will need leverage. This is all very, very well trodden ground for those of us that trade the futures markets.
https://anthonyfjgarner.files.wordpress.com/2015/05/strategy.pdf

@james the original strategy had a cap on the total # of lots that you were allowed to hold at any one time, as well as the total absolute exposure in # of lots long or short at any one time. You should be able to figure out what that total lot risk allowable equals in dollars, back that into the 3X leverage, and then increase lot sizes accordingly by playing with the % risk on each trade.

This document was published by original turtle Curtis Faith to bust the crap marketed out there: http://www.dailystocks.com/turtlerules.pdf
Its all very , very simple stuff.
See Curtis Faith's Way of the Turtle which I proofread and commented on.

@Anthony, those were all fantastic reads, thanks for sharing them. The strategy laid out for the Contrapuntal Fund overlaps a lot of the points I learned doing the Turtle Strategy. Faith's principles of a "Complete Trading System" are all mirrored in your paper. FYI minor typo in "The Fund calls it* strategy “Systematic Global Macro”." Interesting to read your analysis of the benefits of only going long as well as your brief discussion of the zero sum game in commodity markets. Are there any ballpark numbers out there on the percentage of participants in the commodity market who only enter to hedge their farming or production of the materials? I would assume that their numbers are quite a bit lower than participants hoping to gain from the market.

Your improvements to the Turtle strategy are brilliant as well. Amazing how easy it is to tweak those parameters in Quantopian.