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.
| Test | Gann levels | Random levels, median | Random sets Gann beat |
|---|---|---|---|
| Hold rate, 25 markets | 40.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% |
- The test used 24 ETFs from March 2007 to November 2026, and Bitcoin from September 2014. The levels are 45 degrees apart and start from the latest swing high or low. Costs are 0.05% of the traded value on each side.
- The random levels have the same spacing as the Gann levels. Only the starting point is different. So the test asks one question: is Gann's starting point better than a random one?
- A touch holds when the day closes back on the side it came from. A fade trade sells at the level above and buys at the level below. A break trade does the opposite.
- Bitcoin's break trades made 21.2% a year after costs, but random levels made 18.8%, and Bitcoin bought and held made 50.3% a year. The profit came from Bitcoin's trend, not from the levels.
- On SPY alone, the Gann levels held more often than 97% of the random sets. On the utilities fund XLU they were in the bottom 5%. With 25 markets, luck gives a few of each, so do not judge the method on one market.
- This is a basic version of the textbook method. It tests price levels only, not Gann's time cycles, angles or hand-picked anchors. The swing is a plain pivot: the highest high or lowest low of 10 days on each side. It is not a Gann swing chart.
- 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). -
Leave dividend adjustment off. The
ADJbutton at the bottom right of the chart must not be highlighted. - Open the Pine Editor tab at the bottom of the screen and delete the text in the editor.
-
Open
gann-square-of-nine.pinein a text editor, copy all of it, and paste it into the Pine Editor. - 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
- A second implementation, written separately in plain Python, makes the same 460 SPY trades as the engine and counts the same 2,059 touches.
- Cutting the data at 40 random dates changes no earlier trade, so no trade uses future prices.
- The account rebuilt from each trade's prices and costs matches the engine in all 25 markets.
- The agent caught a bug in its first version. That version used prices from later in the same day, and it made break trades look better than they were. The fixed engine decides the order of prices from the open only.
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 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.