Hull Moving Average Formula: What to Do With the Fractions
The Hull moving average formula is HMA = WMA(2 x WMA(price, n/2) - WMA(price, n), sqrt(n)), and what to do when the fractions are not whole numbers.
The Hull moving average formula is HMA = WMA(2 x WMA(price, n/2) - WMA(price, n), sqrt(n)): one weighted moving average taken over a lag-corrected series built from two others. The catch is that n/2 and sqrt(n) are usually not whole numbers, and you cannot run the formula until you decide what to do with the fraction.
Alan Hull's own page answers that question twice, and the two answers disagree. The published instructions split: some floor both numbers, some leave the fraction in, and one rounds up. So the choice is yours: make it once, write it down, and keep it, because at almost every period the other reading draws a visibly different line on our 7,092 EUR/USD daily bars.
What is the Hull moving average?
A weighted moving average of a lag-corrected price series, built from two other weighted moving averages of different lengths.
Alan Hull created it in 2005 and put it in the public domain. In his words: "It was the result of an intellectual curiosity which I placed into the public domain by writing the following article." He describes the goal as "making a moving average more responsive to current price activity whilst maintaining curve smoothness".
The construction has three steps:
- Take a weighted moving average over half the period, and double it.
- Subtract a weighted moving average over the full period.
- Take a weighted moving average of that result, over the square root of the period.
TradingView's support page states those three steps in that order. Steps 1 and 3 are where the fractions live.
What is the Hull moving average formula?
In symbols, with m and s as the two derived lengths:
HMA = WMA( 2 x WMA(price, m) - WMA(price, n), s )
where m = n / 2 and s = sqrt(n)
In words: double the half-length weighted average, subtract the full-length weighted average, then smooth the result over the square root of the period.
The function libraries write the rounding into the formula itself. Tulip Indicators publishes m as the floor of n/2 and s as the floor of the square root of n, and its combined form, in plain operators, is hma = wma(2 * wma(in, m) - wma(in, n), s). That page carries its own warning at the top of the description: "This documentation is still a work in progress. It has omissions, and it probably has errors too."
Which rounding does ChartLabs use?
Both fractions floored. We ship this line as Hull Moving Average, and the code truncates n / 2 and the square root of n the way Hull's prose formula does. At a period of 9 the two inner lengths come out 4 and 3, where the round-up reading gives 5 and 3.
To put it on a chart, open the Indicators menu, go to the Standard list and pick Hull Moving Average under the Moving Averages heading. It draws on the price pane with the candles. Length defaults to 9 and Source to Close, and the shortest length it takes is 1, which draws the price itself.
What do you do when n/2 or the square root of n is not a whole number?
Pick one rule and keep it, because the published instructions conflict and two of them sit on the author's own page.
Hull's prose formula floors both numbers. In plain operators it is Integer(SquareRoot(Period)) WMA [2 x Integer(Period/2) WMA(Price) - Period WMA(Price)], and Integer() cuts the fraction off both lengths.
A few lines below, the same page gives the formula in MetaStock syntax, and that version floors neither. Hull's formula, written that way: Mov(2*Mov(C,period/2,W) - Mov(C,period,W),LastValue(sqrtperiod),W), with sqrtperiod set to Sqrt(period) and passed through whole.
Tulip Indicators marks the floor brackets explicitly on both lengths. MotiveWave's published code leaves the square root unrounded: it sets sqp to Math.sqrt(period) and hands that value straight to the averaging call. Its half-length is a plain division handed to the same call, and the page does not say whether that keeps the fraction. Samco goes the other way, and says so in plain words: "If this square root is a decimal, you can round it up to the next highest whole number."
The original formula never settled it, so the choice falls to you. Make it the same way every time, because of how far apart the two readings sit.
How much does the choice change the line?
At every period we tested except one, the two readings disagree on every single bar.
Floor against round-up, same price series, same period, on 7,092 EUR/USD daily bars of ours, from 2004-01-01 to 2026-08-31. Our data is sourced from Dukascopy, tick-level where it is available, cleaned to remove anomalies, and the detail is in clean forex historical data.
| n | floor (m, s) | round up (m, s) | bars where the two lines differ | mean gap | largest gap | days the line's direction differs |
|---|---|---|---|---|---|---|
| 9 | 4, 3 | 5, 3 | 7,082 of 7,082 | 14.38 pips | 160.27 pips | 482 of 7,081 |
| 14 | 7, 3 | 7, 4 | 7,076 of 7,076 | 8.23 pips | 88.77 pips | 310 of 7,075 |
| 16 | 8, 4 | 8, 4 | 0 of 7,074 | 0.00 | 0.00 | 0 of 7,073 |
| 20 | 10, 4 | 10, 5 | 7,069 of 7,069 | 6.98 pips | 80.44 pips | 223 of 7,068 |
| 21 | 10, 4 | 11, 5 | 7,068 of 7,068 | 16.14 pips | 176.73 pips | 410 of 7,067 |
| 50 | 25, 7 | 25, 8 | 7,036 of 7,036 | 4.60 pips | 41.24 pips | 98 of 7,035 |
| 55 | 27, 7 | 28, 8 | 7,031 of 7,031 | 10.19 pips | 74.77 pips | 171 of 7,030 |
The last column matters most, because the turning point is the reading Hull himself suggests: "A simple application for the HMA, given its superior smoothing, would be to employ the turning points as entry/exit signals." At a period of 9 the two readings of the same formula disagree about which way the line is pointing on 482 days out of 7,081.
The seven periods where the question goes away
Both fractions vanish only when n is even and also a perfect square. Hull's MetaStock input accepts any period from 1 to 200, and across the 197 periods from 4 to 200 that is true at exactly seven of them: 4, 16, 36, 64, 100, 144 and 196. At the other 190, floor and round-up hand you a different length for at least one of the three averages.
StockGro's step-by-step walkthrough picks 16 for its example, where half the period is 8 and the square root is 4, so both numbers come out whole and the question never arises on the page. At 16 the contradiction in Hull's own two formulas cannot show up at all. That is a reason to prefer 16 that has nothing to do with how price behaves.
Does the Hull moving average really have no lag?
No. It cuts lag a long way compared with a weighted moving average of the same period, and what is left depends on the period.
On a perfectly straight ramp, where lag has an exact meaning, the floored HMA sits this far behind the current price:
| n | HMA lag | WMA(n) lag |
|---|---|---|
| 9 | 0.0000 bars | 2.6667 bars |
| 16 | 0.6667 bars | 5.0000 bars |
| 20 | 0.6667 bars | 6.3333 bars |
| 200 | 4.0000 bars | 66.3333 bars |
Across the 57 periods from 4 to 60, the lag on that ramp is exactly zero at seven of them: 4, 6, 8, 9, 11, 13 and 15. At periods 5 and 7 it goes the other way, and the line sits a third of a bar ahead of the ramp.
Hull's "the HMA almost eliminates lag altogether" is fair as a comparison against a weighted average of the same period. It is a smaller claim than zero lag, and the table shows the gap. TradingView goes further and says the HMA "attempts to remove lag in its entirety". MotiveWave's reference manages both readings on one page: the HMA "makes a moving average more responsive while maintaining a curve smoothness" above the formula, and under the next heading, "The Hull Moving Average is a lagging trend indicator and may be used in conjunction with other studies." Whether any moving average can reach zero lag on real bars, and what the lines that claim it are doing, is the subject of our guide to zero lag moving averages.
What does the lower lag cost?
Hull's own worked example answers that, because the construction overshoots on purpose and he treats it as the point. Walking through ten numbers from 0 to 9, with 9 as the most recent, he adds the gap between the 10-period and the 5-period average back on top: "This gives a final answer of 9.5 (7 + 2.5) which is a slight overcompensation. But this overcompensation is very handy because it offsets the lagging effect of the nested averaging."
The highest number in that series was 9, and the line lands on 9.5.
Hull's example gives the mechanism. A square step gives the size: hold price at 100 for 300 bars and then at 101 for 300, so 101.000000 is the highest price anywhere in the series.
| n | HMA peak | overshoot, % of the step | WMA(n) peak |
|---|---|---|---|
| 9 | 101.266667 | 26.67% | 101.000000 |
| 16 | 101.251225 | 25.12% | 101.000000 |
| 20 | 101.262121 | 26.21% | 101.000000 |
| 21 | 101.285281 | 28.53% | 101.000000 |
| 55 | 101.283369 | 28.34% | 101.000000 |
A weighted moving average of the same period peaks at exactly 101.000000 in every row, because all of its weights are positive, and the HMA's are not. The inner series, 2 x WMA(price, m) - WMA(price, n), gives every bar older than m a negative weight, and a weighted sum with negative weights is not held between its own inputs.
It works downward just as well. Put a single 101 bar into an otherwise flat 100 series and the 20-period HMA dips to 99.960779 afterwards, below the lowest price the series ever held, following a move upward.
For scale, the same step puts a 14-period DEMA 11.69 per cent over the top and a 14-period TEMA 16.55 per cent over, measured in our guide to the DEMA and TEMA formulas. The HMA travels roughly twice as far past the step as DEMA does.
Hull's suggested reading is the turning point, and a turn is where the overshoot shows up. A level the HMA crossed going into one can be a level no bar traded.
How often does that happen on real bars?
On 0.8 to 4.4 per cent of days, depending on the period. Counting the days the HMA printed above the highest high of its own window or below the lowest low, on the same 7,092 EUR/USD daily bars:
- n = 9: 4.41% of days (149 above, 163 below, of 7,080). Worst excursion 72.03 pips.
- n = 16: 1.03% (38 above, 35 below, of 7,072). Worst 52.78 pips.
- n = 20: 1.16% (48 above, 34 below, of 7,068). Worst 50.09 pips.
- n = 55: 0.83% (26 above, 32 below, of 7,030). Worst 48.19 pips.
The worst excursion shrinks as the period grows, from 72.03 pips at 9 down to 48.19 at 55.
Should you use Hull moving average crossovers?
The author says not to. Hull: "However it shouldn't be used to generate crossover signals as this technique relies on lag."
Samco and StockGro each run a section headed "HMA Crossover Strategy" anyway. We are reporting the warning and the two strategies, and leaving the decision with you.
What are the best Hull moving average settings?
Two defaults are published, and both are 20. Everything else you will be handed is somebody's preference.
- Hull's own MetaStock listing defaults the period to 20, inside a range of 1 to 200.
- MotiveWave's reference also defaults to 20, with WMA as the averaging method.
- Samco reports that "Originally, Alan Hull developed the technical tool with a suggested time frame of 16 trading periods". Hull's page does open with a 16-week simple moving average, as an illustration, and then defaults its own HMA input to 20.
- StockGro suggests 9 or 16 for intraday work and 50 or higher for longer holds.
- Tulip Indicators calls its function with period 5, which is an example of calling the code.
Set against that list, 16 has one property none of the others has: it is one of the seven periods where the rounding question cannot arise.
Checking your code against a worked example
Tulip Indicators publishes a worked HMA table at period 5. Nine of its ten printed values reproduce to the cent from the page's own formula. One does not.
For 2005-11-14 the page prints 84.84. The page's own formula, with both lengths floored as its spec says, gives 84.834667, which is 84.83 to the nearest cent and 0.0053 below the printed figure.
Tulip Indicators says itself that the page probably has errors. That table is something a reader can check an implementation against, and anyone whose code returns 84.83 should know it is not their bug.
How do you test the Hull moving average yourself?
Build the line twice, once with both lengths floored and once with the square root rounded up, and count the days the two point in different directions on your instrument at your period. That count is what the unstated choice is costing you.
- Pick your period, your instrument and your timeframe.
- Write the HMA twice, floored and rounded up. Give the two lines different colours.
- Step the chart forward one bar at a time, so you cannot see what came next.
- Log every day the two lines point in different directions.
- Where one line would have triggered your rule and the other would not, you have the cost of the choice in your own terms rather than in pips.
The built-in Hull Moving Average is your floored line. The rounded-up one you write in the built-in Pine Script editor, where the Examples on the Pine Scripts tab are a starting point. Every moving average can be given its own colour and line style, and one indicator instance can be edited without changing the others, which is what telling the two apart needs. There is no Pine backtesting: the stepping and the logging are yours to do.
Stepping forward with the next bar hidden is what manual backtesting means in practice. Our beginner's guide to backtesting a strategy covers the whole loop, and forex backtesting covers the session and spread behaviour that moves a count like this one on a currency pair.
Next step
The rounding choice costs something specific on your instrument at your period, and the only way to know the number is to count it. ChartLabs runs a 7-day free trial with no card, then one plan at $20 a month or $200 a year. Start a free trial of ChartLabs and put the floored and the rounded-up line on the same chart.
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.