Free code Turtle Trading rules
My AI Tests Famous Trading Systems
The Turtle Trading rules, free for TradingView and Python
My AI coding agent rebuilt the original Turtle Trading rules from the 2003 rulebook, tested them on 24 ETFs, and checked its own work. This is the code it wrote: a TradingView strategy and the full Python backtest.
Download the code (zip, 19 KB) Source on GitHub
Free, with no sign-up. This is an experiment in what AI agents can build and verify, not trading advice.
The honest result
At the original size, both Turtle systems lost almost all of the account after costs. At a tenth of that size, System 2 made 3.0% a year. SPY, bought and held, made 10.4% a year in the same period.
−27.5%
a year after costs. System 2 at the original size, 1% risk per unit.
−21.3%
a year after costs. System 1 at the original size.
+3.0%
a year after costs. System 2 at a tenth of the size, 0.1% risk per unit.
+10.4%
a year. SPY, bought and held, in the same period.
| Test | Return a year, after costs | Worst drop | $1 became |
|---|---|---|---|
| System 1 (20-day breakout), 1% risk | −21.3% | 99.6% | $0.0095 |
| System 2 (55-day breakout), 1% risk | −27.5% | 99.86% | $0.0019 |
| System 2, 0.1% risk | +3.0% | 37% | $1.79 |
| SPY, buy and hold | +10.4% | 55% | $6.88 |
- The test used 24 ETFs, daily prices from March 2007 to November 2026, and a $10,000,000 account. Costs are 0.05% of the traded value on each side. Cash earns the Treasury bill rate.
- The Turtle unit size was made for futures. On these ETFs, one unit of SPY was worth about 88% of the account, and System 2 held positions worth 13.5 times the account on average. That is why the original size failed here.
- ETFs stand in for the futures that the Turtles traded in the 1980s. The test covers one 19-year period, and that period was hard for trend following.
- Past results do not predict future results.
Load it in TradingView
About two minutes. You do not need to code.
- Open a chart at tradingview.com. A free account is enough.
-
Load the symbol
SPYand set the timeframe to daily (1D). -
Turn on dividend adjustment so the prices match the backtest. Click
ADJat the bottom right of the chart. - Open the Pine Editor tab at the bottom of the screen and delete the text in the editor.
-
Open
turtle-rules.pinein a text editor, copy all of it, and paste it into the Pine Editor. - Click Save, then click Add to chart.
The thick lines are the entry channel and the thin lines are the exit channel. Arrows mark each buy and sell. To check it on SPY, turn on Stop at an end date in the strategy's Inputs, then open the Strategy Tester. It shows about 377 trades, about +5.8% total PnL, and about 7% max drawdown. TradingView tests one symbol, so these numbers differ from the 24-ETF test above. spy-check.md in the zip explains each number.
What is in the zip
turtle-rules.pine- The TradingView strategy in Pine Script v6. It has both systems, and settings for the risk size and the dates.
README.md- How to load the script, read the Strategy Tester and change the settings.
spy-check.md- The numbers to expect on SPY, so you can check that the script works.
python/- The full backtest: the engine, the data download, the portfolio test and the checks.
Run the Python backtest
From the python/ folder, with Python 3.12 or later:
pip install -r requirements.txt
python fetch_data.py data
python validate.py data
python run_portfolio.py data
How the agent checked its work
- A second implementation, written separately, makes the same 103 SPY trades at the same prices. That check runs System 2 with one unit per trade.
- No entry fills inside the breakout range of earlier days, so no entry uses future prices.
- Closed trades, open positions and interest add up to the change in the account, to the cent.
The rules come from The Original Turtle Trading Rules by Curtis Faith and others (2003).
The video
My AI Backtested the Famous Turtle Trading Rules Overnight
The video is not live yet. The link appears here when it is.
The channel on YouTubeDownload the code All free code
This is an experiment in what AI agents can build and verify, not trading advice. The code is free to use, change and share.