Auto Fibonacci From Last Swing
Drawing a Fibonacci retracement by hand means picking the last swing point and the swing before it, then dragging the tool between them. It takes a few seconds and it is easy to mis-click the anchor, especially on a fast chart or a small screen. This script does the same job mechanically: it finds the most recent confirmed swing point, finds the nearest opposite swing point further back, and draws the retracement between them. A swing point here is a fractal: a bar whose high (or low) is strictly more extreme than a fixed number of bars on both sides of it, set by InpFractalK. This is a lagging, mechanical definition, not a discretionary one - it will not always agree with where a trader's eye would put the swing, particularly on a five-bar fractal in a slow-grinding trend where the "obvious" swing has not yet produced two clean rejections around it. The trap that matters most is not the fractal logic, it is the anchor order. MT4's Fibonacci tool convention places the 0% level at the SECOND anchor point (time2/price2) regardless of which point came first in time. Anchor the two swing points in the wrong order and the tool still draws something - a full set of levels, on the chart, at plausible-looking prices - it is just upside down: 0% sits on the older point and 100% on the newer one, and every retracement level is mirrored. Nothing about the drawing looks obviously broken unless you already know where 0% should be, which is exactly what makes this trap worth naming. This script always writes the newer (more recent) swing point as point 2 and the older one as point 1, so 0% lands on the most recent swing - the same result a trader dragging the tool by hand, newer point last, would get. The two swing points are not independently chosen. The script first finds the single most recent confirmed fractal on the chart, of either type, then searches further back in time for the nearest fractal of the opposite type. That pairing is what makes the two points a swing rather than two arbitrary extremes: a fractal high paired with the fractal low immediately before it, or the reverse. Notes and limitations - Fractal-based swing detection is a lagging, mechanical rule, not a discretionary read of the chart. A confirmed fractal needs InpFractalK bars to close on the newer side before it counts, so the script can never point at the very last bar, and on a strongly trending market it can pick a swing further back than a trader would. - The script can refuse outright. On a very quiet chart, a very short history, or a large InpFractalK relative to InpLookbackBars, no qualifying fractal (or no opposite-type partner for the nearest one) may exist in the search window. When that happens nothing is drawn, and the log and alert state which side - high or low - came up empty along with the K value and lookback window used. - Repainting is not a concern here the way it is for a live fractal indicator: this script runs once, finds already-confirmed fractals (bars with InpFractalK newer bars closed beyond them), and draws once. It does not update the drawing as new bars form - run it again to refresh. - Only one swing pair is drawn per run. It does not attempt to chain multiple swings or draw more than one Fibonacci object. - The search only looks at High[] and Low[] on the chart's current timeframe; it does not consider higher timeframe structure.
Script · MT4 · v1.0 · Free
About Auto Fibonacci From Last Swing
Auto Fibonacci From Last Swing is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Fractal k, Lookback bars and Prefix. MQL source code included.
Files in the download: AutoFibonacci.ex4, AutoFibonacci.mq4, AutoFibonacci.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Fractal k | 5 | Bars required on each side to confirm a fractal |
| Lookback bars | 300 | How far back to search, in bars |
| Prefix | afib_ | Prefix for chart object names |
| Cleanup first | true | Delete existing afib_ objects before drawing |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Fibonacci · Fractals · Alerts · MT4 Script · MQL4 Source Code