r4lph.io

Free code Gann square of nine

My AI Tests Famous Trading Systems

Gann's square of nine levels, free for TradingView and Python

My AI coding agent built a basic, textbook version of W. D. Gann's square of nine price levels from public sources. It tested the levels on 25 markets against random levels with the same spacing, and checked its own work. This is the code it wrote: a TradingView indicator and the full Python test.

Download the code (zip, 22 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

Gann's levels did no better than random lines. When price touched a Gann level, the level held 40.6% of the time. Random levels with the same spacing also held 40.6% of the time. Trades at the Gann levels lost money after costs, about the same as trades at random levels.

40.6%

of touches held at a Gann level. Average of 25 markets.

40.6%

of touches held at a random level with the same spacing. Median of 200 random sets.

−0.76%

a year after costs. Fade trades at Gann levels on 24 ETFs. Random levels made −0.75%.

13 of 25

markets where Gann beat the middle random set. A coin flip gives 12 or 13.

TestGann levelsRandom levels, medianRandom sets Gann beat
Hold rate, 25 markets40.6%40.6%56.5%
Fade trades, 24 ETFs, return a year after costs−0.76%−0.75%49.5%
Break trades, 24 ETFs, return a year after costs−1.80%−1.89%55.5%
Fade trades, Bitcoin, return a year after costs−56.8%−55.9%28%
Break trades, Bitcoin, return a year after costs+21.2%+18.8%71.5%

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. Leave dividend adjustment off. The ADJ button at the bottom right of the chart must not be highlighted.
  4. Open the Pine Editor tab at the bottom of the screen and delete the text in the editor.
  5. Open gann-square-of-nine.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 indicator draws a line every 45 degrees above and below the latest swing high or low. Solid lines are full turns of the square, and dashed lines are the steps between them. To check it, open the indicator's settings, set Anchor to Manual price and type 348.11, the SPY low of 13 October 2022. The first three levels above it are 357.50, 367.02 and 376.66. spy-check.md in the zip has the full table.

What is in the zip

gann-square-of-nine.pine
The TradingView indicator in Pine Script v6. It draws the levels from the latest swing, or from a price you type in.
README.md
How to load the indicator, change the settings and read the lines. It lists the sources for the formula.
spy-check.md
The exact levels to expect on SPY, and the test on SPY alone.
python/
The full test: the engine, the data download, the comparison with random levels 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 .
python spy_check.py data
python run_tests.py .

How the agent checked its work

The formula is level = (square root of the anchor price + degrees / 180) squared. It comes from public sources, which the README lists: LuxAlgo, Strike Money, Optuma, Tradingsim, and Patrick Mikula's book on the square of nine (2003).

The video

My AI Tested Gann's Square of Nine 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.