Back to Posts
Listen to Thread

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
377
Loading...
Backtest from to with initial capital ( data)
Cumulative performance:
Algorithm Benchmark
Custom data:
Week
Month
All
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.

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.

Log in to reply to this thread.
Not a member? Sign up!