r4lph.io

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.

TestReturn a year, after costsWorst 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

Load it in TradingView

About two minutes. You do not need to code.

  1. Open a chart at tradingview.com. A free account is enough.
  2. Load the symbol SPY and set the timeframe to daily (1D).
  3. Turn on dividend adjustment so the prices match the backtest. Click ADJ at the bottom right of the chart.
  4. Open the Pine Editor tab at the bottom of the screen and delete the text in the editor.
  5. Open turtle-rules.pine in a text editor, copy all of it, and paste it into the Pine Editor.
  6. 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

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 YouTube

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