Fractal Adaptive Moving Average Formula (FrAMA)
The fractal adaptive moving average formula, the division two pages drop, and the gap case where its smoothing factor comes out at 99 instead of 1.
The fractal adaptive moving average is an exponential moving average that re-picks its own smoothing factor on every bar. The factor comes from a fractal dimension, which the formula works out by setting the window's high-low range against the ranges of its two halves. Price that ran in a straight line scores 1, and the line sits right on it. Price that churned back and forth scores 2, and the line barely moves.
The formula is short and easy to get wrong. Two published versions drop a division, which turns the dimension of a straight line from 1 into 0 and the smoothing factor from 1 into 99. The indicator also has nothing to do with Bill Williams' five-bar fractals, the ones that put an arrow above a high or below a low. For those, see the Williams fractals indicator.
MetaTrader's help page says it "was developed by John Ehlers" and "is constructed based on the algorithm of the Exponential Moving Average, in which the smoothing factor is calculated based on the current fractal dimension of the price series". The open-source TA-Lib library dates Ehlers' article to Technical Analysis of Stocks & Commodities V.23:10 (October 2005).
What is the FrAMA formula?
MetaTrader's version of the calculation, written out:
FRAMA(i) = A(i) * Price(i) + (1 - A(i)) * FRAMA(i-1)
A(i) = EXP(-4.6 * (D(i) - 1))
D = (LOG(N1 + N2) - LOG(N3)) / LOG(2)
N(Length,i) = (HighestPrice(i) - LowestPrice(i)) / Length
N1(i) = N(Length,i)
N2(i) = N(Length,i + Length)
N3(i) = N(2 * Length,i)
The last four lines are where published versions diverge. N is not a price range. It is a price range divided by the number of bars it covers, so it is average movement per bar: N1 for the newer half of the window, N2 for the older half, and N3 for the whole window, which is why N3 divides by 2 * Length rather than by Length.
On a straight line each half covers half of the window's total range, so N1, N2 and N3 all equal the slope per bar, N1 + N2 is exactly twice N3, and the dimension is LOG(2)/LOG(2) = 1.
The same step can be written in a form where the division cannot get lost, and the same library prints it that way. Call the range of the newer half R1, the range of the older half R2 and the range of the whole window R. For the dimension D, add R1 and R2, divide by R, take the base-2 logarithm of that ratio and add 1. For the smoothing factor, alpha, subtract 1 from D, multiply by minus 4.6 and raise e to that power. Alpha is capped at 1, and it is set to 1 when R1 or R2 is 0. Each new value of the line is the price P times alpha plus the previous value times 1 minus alpha, and the line starts from P on the bar before its first output.
The two agree exactly: (N1 + N2)/N3 = 2(R1 + R2)/R, so MetaTrader's log2((N1+N2)/N3) is 1 + log2((R1+R2)/R).
That form also pins down what P is: the library and Ehlers both take the median price, (High + Low) / 2, and so does the ChartLabs built-in by default. MetaTrader does not fix it. The MQL5 signature exposes applied_price, "The price used", and its reference adds: "Can be any of the price constants ENUM_APPLIED_PRICE or a handle of another indicator." No default is published, so two charts with the same period can draw different lines for that reason alone.
What dropping the division does
Two pages print the three terms as raw ranges, or divide all three by the same number, which is the same mistake because a constant divisor cancels out of a ratio. aemmtrader.com prints them with no division at all, as "N1 = (Highmax − Lowmin) over the first half of the period" and the same for the second half and "the entire period". alphax.trading divides all three by one window length, including the whole-window term where MetaTrader divides by twice as many bars: "Calculate N3 = (Highest High(shifted2) - Lowest Low(shifted2)) / WindowLength".
Either version fails on a straight line. Each half's range is half the whole window's, so R1 + R2 = R, the ratio inside the logarithm is 1 rather than 2, the dimension comes out at 0 instead of 1, and the smoothing factor is exp(-4.6 x (0 - 1)) = exp(4.6) = 99.48 instead of 1. aemmtrader.com's own text states the right answer a line later, "When D ≈ 1 (strong trend), α ≈ 1", which its own formula cannot produce.
wallstreetmojo.com heads a section "How To Calculate?", prints the two recursion lines correctly, and never prints the fractal dimension. Its worked example treats the exponential function as an input with a value, opening "If, suppose, D= 1, and EXP = 1" and then printing "At = 1 [-4.6 X (1 – 1)] = EXP(0) = 1". The same page lists as a drawback that "The indicator uses arrows that point out to the high or low points", which belongs to Bill Williams' fractals rather than to this average.
Which version does ChartLabs draw?
Ehlers' own, built in as Fractal Adaptive Moving Average. Open the chart's Indicators menu, go to the Standard list, and it is under the Moving Averages heading, where it draws as one line on the price pane over the candles.
Two settings. Length is 16, and that number is the whole window, so it splits into two halves of 8; the window has to be even, and an odd length uses the even number below it. Source is the median price, (H + L) / 2, which is Ehlers' input, and it only sets the price being averaged: the ranges behind the dimension always come from the bars' highs and lows whatever you set it to.
Both of the edge cases the published formula leaves open are answered in the build. The smoothing factor is held between 0.01 and 1, so a gapped window cannot send the line past the price, and where either half of the window is flat it carries the previous bar's dimension forward, as Ehlers' listing does.
What does the fractal dimension measure?
How much wandering the price did to get where it got, nominally as a number between 1 and 2.
A dimension of 1 is a straight line. MetaTrader's help page says "Fractal dimension of a straight line is equal to one", so A = EXP(0) = 1, the recursion becomes FRAMA(i) = 1 x Price(i) + 0 x FRAMA(i-1), and in its words "the indicator exactly follows the price".
A dimension of 2 is a filled plane. MetaTrader's help page says "The fractal dimension of a plane is equal to two", which gives A = EXP(-4.6) = 0.01, and that smoothing, in its words, "corresponds to approximately 200-period simple moving average". That is sound: a smoothing factor of 0.010052 is the factor of a 198-bar exponential moving average, with a half-life of 69 bars.
In between the scale is steep, because the factor is an exponential of the dimension: a dimension of 1.3 already costs most of the responsiveness.
A worked example with round numbers
An example, not a recommended setting. A window of 16 bars in two halves of 8. The older half ran between 100 and 104, so R2 = 4; the newer half between 102 and 108, so R1 = 6; the whole 16 bars between 100 and 108, so R = 8.
Then (R1 + R2) / R = 10 / 8 = 1.25, so D = 1 + log2(1.25) = 1.3219 and alpha = exp(-4.6 x 0.3219) = 0.2274, the factor of a 7.8-bar EMA. With the current bar's high at 108 and its low at 106, the median price P = 107, and from a previous value of 104.0000 the line goes to FRAMA = 104 + 0.2274 x (107 - 104) = 104.6823. Halves that overlap heavily, as these do, are the ordinary case.
What are the best FrAMA settings?
One number, and the sources do not agree on it.
| Source | Period published | Notes |
|---|---|---|
| The open-source TA-Lib library | Default 16, accepted 2 to 100000 | The window, "split into two equal halves". States that "The period must be even; an odd period is rejected" |
| MetaTrader 5 help | None | Defines Length as the half-window, with the dimension taken over 2 * Length bars |
| MQL5 reference | None | ma_period, no default published |
| aemmtrader.com | "usually 16 or 20" | No source given for either |
The even-period rule follows from the construction: the window splits into two equal halves, and an odd number of bars will not split evenly.
The word "period" means different things on the two pages. In the open-source library's notation it is the whole window, as it is in ChartLabs, so 16 means two halves of 8. In MetaTrader's help the Length is the half, and the dimension is computed over 2 * Length bars, so the same 16 describes a 16-bar window on one page and a 32-bar window on the other.
Why does my FrAMA jump past the price?
Because the published formula has no lower bound on the dimension. On a gapped window the dimension goes below 1, which sends the smoothing factor above 1. This is arithmetic on the formula as printed, not a measurement of any market.
The condition is exact: D is below 1 precisely when R1 + R2 < R, the two halves together covering less ground than the whole window. That happens only when they sit at prices far apart relative to their own ranges, which is to say there is a gap at the join.
| Older half | Newer half | R2 | R1 | R | D | Smoothing factor |
|---|---|---|---|---|---|---|
| 100 to 101 | 103 to 104 | 1 | 1 | 4 | 0.0000 | 99.4843 |
| 100 to 101 | 110 to 111 | 1 | 1 | 11 | -1.4594 | 81,910.74 |
In the second row the recursion reads FRAMA = 81,910.74 x Price - 81,909.74 x previous, which is not a moving average. The open-source library clamps it, as the ChartLabs build does, giving alpha = exp(-4.6 * (D - 1)), "at most 1". MetaTrader's help page does not mention a cap, so what your own platform draws on such a bar is something you have to look at and see.
The ceiling of 2 is safe: it would take R1 + R2 > 2R, and the whole window's range is at least as large as either half's, hence at least their average. The floor of 1 does not hold, and aemmtrader.com, which states the range, hedges it: "The value of D typically ranges from 1.0 to 2.0".
A flat half is the other edge case. With one half flat, R1 or R2 is 0 but N1 + N2 is not, so the logarithm is still defined; it takes a window that is flat throughout to make it the logarithm of zero. The reference behaviours disagree there. The open-source library documents both: "Where either half of the window is flat, alpha is 1 and the output is the price; Ehlers' listing keeps the previous bar's dimension there instead". One pins the line to the price, the other carries the previous bar's smoothing forward, so on a thin session the same data gives two different lines from two correct implementations.
Is FrAMA on MT4 or MT5?
MT5 has it built in, in MetaTrader's Trend Indicators list alongside the Adaptive Moving Average and the Variable Index Dynamic Average. MQL5's reference lists it as iFrAMA(symbol, period, ma_period, ma_shift, applied_price), with "only one buffer". MT4 does not have it, nor any other adaptive moving average: the "Fractals" in its index of 29 technical indicators is the Bill Williams one.
That single buffer matters, because theforexgeek.com builds a strategy section on a pair of lines that do not exist: "The two lines that represent the FRAMA illustrates buy and sell signals. If the red line crosses the blue line from above, it's considered a buy signal", with matching buy and sell rule lists. There is no red line and no blue line to cross. What the one line gives you, in MetaTrader's framing, is that "All types of analysis used for Moving Averages can be applied to this indicator".
How is it different from a KAMA or a VIDyA?
All three are an EMA that resets its own smoothing factor every bar, and they differ in what they measure to do it. A KAMA uses an efficiency ratio, net movement divided by total movement: see the Kaufman adaptive moving average. A VIDyA scales its factor by the Chande Momentum Oscillator: see the variable index dynamic average. A FrAMA uses the fractal dimension, so it reacts to how much the price doubled back on itself rather than to which way it went.
How do you test a FrAMA rule?
Fix the symbol, the timeframe, the period and whether that number is the window or the half-window, and the applied price, before the first bar. If a bar in your sample had a gap at the middle of the window, record what the line did there: your platform's answer to the undefined case is part of your result. alphax.trading names the other risk in the settings: "Parameter Sensitivity: The indicator's performance is highly dependent on the initial window length, which can lead to over-optimization during backtesting".
On ChartLabs you replay real historical candles one at a time and place the trades yourself, with per-symbol commission and slippage feeding every trade's P&L, balances and analytics. Our build answers the gapped window and the flat half the same way every time, so that column in your log is a constant you can write down once instead of a surprise halfway through. Nothing here backtests it, so the counting stays yours to do on the replay.
Next step
Settle on the version of the dimension you mean, then step a year of real candles past it one bar at a time. One plan, $20 a month or $200 a year, with a 7-day free trial and no credit card required: start the free trial.
Related reading: how to backtest a strategy step by step, forex backtesting, and why backtest results and live results drift apart.
Related Topics
Related
Backtesting a Trading Strategy: A Beginner's Guide
What backtesting is, why it matters, and how to run one step by step: defining your rules, choosing clean historical data, stepping through the chart trade by trade, logging results, and reading the metrics that tell you whether your strategy has a real edge.
Forex Backtesting: Test Your Strategy on Real Data
A practical guide to backtesting forex strategies: why session dynamics, spread variation, and pair-specific behavior change your results, what clean historical data actually means, and a five-step process for testing a strategy before you risk real capital.
Backtesting Accuracy: How Slippage and Data Quality Matter
Why backtest results and live results drift apart, and what actually closes the gap: clean tick data, honest spread assumptions, and commission and slippage modelled directly on every trade you log.