FindGartley
Background
The Gartley is a five-point zig-zag pattern, usually labelled X-A-B-C-D, where each leg retraces the previous one by a specific Fibonacci ratio. When the legs line up with those ratios the final point, D, is taken as a likely turning point: a place to buy in a bullish Gartley or sell in a bearish one. It is one of the classic "harmonic" patterns, and the appeal is that it gives you a precise entry zone rather than a vague area.
FindGartley checks whether the most recent peaks and troughs form a valid
Gartley ending at the bar you specify. It measures the legs, compares their ratios against
the Gartley ratios (0.618, 0.786, 1.27, 1.618) within a small tolerance, and confirms the
current price sits in the expected reversal zone. It returns 0 if there is no pattern, 1 for
a bullish Gartley and 2 for a bearish one. This is the scriptable version of the
Gartley pattern scanner.
Included formula
The toolbox ships a ready-made Gartley Pattern Scanner built on this function, so you can use it without writing any code. It runs as an Exploration: in AmiBroker open the Analysis window, pick Gartley Pattern Scanner from the WiseTraderToolbox formulas, choose a watchlist and click Explore to list every symbol that currently completes a Gartley, marking each one bullish or bearish. Applied to a chart, the same formula draws the pattern's legs.
The trend size is set from its parameters. The toolbox's Combined Pattern Search looks for this pattern alongside the others.
The exported function
If you would rather build your own scan, the toolbox exports the calculation as a single AFL function you can call directly — it is the engine inside that scanner.
FindGartley(TrendSize, Bar)
Parameters
| Parameter | Description |
|---|---|
| TrendSize | The amount in percent a price has to move for a point to be counted as a peak or trough. This sets how big the swings that make up the pattern have to be. |
| Bar | Which price bar to test as the completion point (point D) of the pattern. The current last bar is the total number of bars minus 1. |
Return Value
The function returns 0 if no pattern was found, 1 if a bullish Gartley was found, and 2 if a bearish Gartley was found. It also sets AFL variables (P1, P2, T1, T2 and their bar positions) describing the peaks and troughs it used, which you can read back if you want to draw the pattern.
Usage
// Test the most recent bar for a Gartley
result = FindGartley( 5, BarCount - 1 );
Filter = result > 0;
AddColumn( IIf( result == 1, 1, 0 ), "Bullish" );
AddColumn( IIf( result == 2, 1, 0 ), "Bearish" );
This tests one bar at a time, so to scan history you call it inside a loop over the bars you care about. A larger TrendSize looks for bigger, slower patterns; a smaller one finds more, shorter-term ones.