FindDoublePattern
Background
A double top is two peaks at roughly the same height with a dip in between, like the letter M. The price tries to push above a level twice, fails both times, and that failure is read as a sign the uptrend is tired and may reverse down. A double bottom is the mirror image, a W shape, where the price tests a low twice and holds, hinting at a turn back up. They are common and easy to recognise, which is part of why traders watch them.
FindDoublePattern checks whether the recent peaks and troughs form a double top
or double bottom at the bar you specify. It confirms the two tops (or bottoms) are close
enough in height and that the swing between them is large enough to matter. It returns 0 if
there is no pattern, 1 for a double bottom (bullish) and 2 for a double top (bearish). This is
the scriptable version of the Double Bottom & Top
pattern scanner.
Included formula
The toolbox ships a ready-made Double Bottom Pattern Finder 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 Double Bottom Pattern Finder from the WiseTraderToolbox formulas, choose a watchlist and click Explore to list every symbol currently forming a double top or double bottom. Applied to a chart, the same formula draws the two tops or bottoms and the support/resistance between them.
The trend sizes and the threshold are 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.
FindDoublePattern(TrendSize, TrendSizeConfirm, Bar, Threshold)
Parameters
| Parameter | Description |
|---|---|
| TrendSize | The amount in percent a price has to move for a point to be counted as a pivot. This sets the size of the swings the pattern is built from. |
| TrendSizeConfirm | A second, confirming trend size used when locating the peaks and troughs that make up the pattern. It lets you require the turning points to also stand out over this swing size. |
| Bar | Which price bar to test for a completed double top or double bottom. The current last bar is the total number of bars minus 1. |
| Threshold | The maximum percentage difference between the two tops (or two bottoms) for them to count as level. A smaller value insists the two be very close in price. Must be between 0 and 50. |
Return Value
The function returns 0 if no pattern was found, 1 if a double bottom (bullish) was found, and 2 if a double top (bearish) was found. It also sets AFL variables describing the peaks, troughs and their bar positions that it used.
Usage
result = FindDoublePattern( 5, 5, BarCount - 1, 2.0 );
Filter = result > 0;
AddColumn( IIf( result == 1, 1, 0 ), "Double Bottom" );
AddColumn( IIf( result == 2, 1, 0 ), "Double Top" );
This tests one bar at a time, so to scan history call it in a loop over the bars you want to check. A tighter Threshold finds cleaner, more even patterns but fewer of them.