FindFibRetracement
Background
When a market makes a big move and then pulls back, the pullback often stops at one of a handful of well-known levels: 23.6%, 38.2%, 50% and 61.8% of the original move. These are the Fibonacci retracement levels, and many traders watch them as likely places for the price to find support and resume the trend. The idea is simple even if the maths behind the ratios isn't: draw a move from a low to a high, divide it up at those percentages, and see whether the price respects those lines.
FindFibRetracement does this search for you. It takes the most recent peaks and
troughs, tries pairing them up into possible moves, lays the Fibonacci levels over each one,
and checks whether the recent price has actually touched one of those levels. When it finds a
match it keeps the largest move and writes the level prices out as AFL variables. It returns 1
if a retracement was found and 0 if not. This is the scriptable version of the
Fibonacci pattern scanner.
Included formula
The toolbox ships a ready-made Fibonacci 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 Fibonacci Pattern Finder from the WiseTraderToolbox formulas, choose a watchlist and click Explore to list every symbol that currently has a valid retracement. Applied to a chart, the same formula draws the Fibonacci levels it found. The trend size and tolerance are set from its parameters.
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.
FindFibRetracement(ID, PeaksBack, BarsBack, Threshold, TrendSize)
Parameters
| Parameter | Description |
|---|---|
| ID | The function creates AFL variables. So you can run it more than once without overwriting earlier results, a prefix taken from this string is added to each variable name. |
| PeaksBack | Fibonacci retracements are built from peaks and troughs. This is how many of the most recent peaks (and troughs) to consider when searching. Must be between 1 and 5. |
| BarsBack | How many of the most recent price bars to check for a touch of a Fibonacci level. If this is 1, only patterns where the current bar is touching a level are found. |
| Threshold | A percentage. How close the price has to come to a level to count as touching it. |
| TrendSize | The percent change in price needed for a point to be counted as a peak or trough. |
Exported Variables
When a pattern is found the function writes the following variables, each prefixed with the string you passed as ID.
| Variable | Description |
|---|---|
| _fibStart | The bar where the pattern begins. |
| _line0, _line236, _line382, _line50, _line618, _line100 | The price of each Fibonacci level. The number in the name is the retracement percentage, so _line382 is the 38.2% level and _line618 is the 61.8% level. |
Usage
found = FindFibRetracement( "fib", 5, 1, 1.0, 5 );
Filter = found == 1;
AddColumn( StaticVarGet( "fib_line618" ), "61.8% Level" );
AddColumn( StaticVarGet( "fib_line50" ), "50% Level" );
If no retracement is found the function returns 0 and the exported level variables are left at 0. PeaksBack is capped at 5, so widen TrendSize rather than PeaksBack if you want to look at larger swings.