Moving Averages Stuff
Motivated by e-mail from Robert B.

I get this e-mail asking about the Hull Moving Average (HMA) and ...

>And you never heard of it before.
Uh ... that's right. In fact, when I googled I discovered lots of moving averages which I'd never heard of, such as:

>So?
So I thought we'd talk about moving averages and ...

>Haven't you done that before, like here and here and here and here and ...
Yes, yes, but that was before I knew of all these other moving averages.
In fact, the only ones I played with were these, where P1, P2, ... Pn are the last n stock prices (Pn being the most recent):

1. Simple Moving Average (SMA) = (P1 + P2 + ... + Pn) / K   where K = n.
2. Weighted Moving Average (WMA) = (P1 + 2 P2 + 3 P3 + ... + n Pn) / K   where K = (1+2+...+n) = n(n+1)/2.
3. Exponential Moving Average (EMA) = (Pn + α Pn-1 + α2 Pn-2 + α3 Pn-3 + ... ) / K   where K = 1+ α+α2+... = 1/(1-α).
>Whoa! I've never seen that EMA formula before. I always thoguht it was ...
Yeah, it's normally written differently, but I wanted to show that these three have similar prescriptions. (See the EMA stuff here and here.)
Indeed, they all look like:
 the general Weighted Average = (w1P1 + w2P2 + w3P3 + ...) / K   where K = the sum of the weights = w1 + w2 + w3 + ...
Note that, if all the Ps are equal to, say, Po, then the moving average equals Po as well ... and that's the way any self-respecting average should behave.

>So which is best?
Define "best".

Here are a few moving averages, attempting to track a series of stock prices that vary in a sinusoidal fashion:

>Stock prices that follow a sine curve? Where did you find a stock like that?
Pay attention!
Notice that the commonly used moving averages (SMA, WMA and EMA) reach their maximum later than the sine curve. That's lag and ...

>But what about that HMA guy. He looks pretty good?
Yeah, and that's what we want to talk about. Indeed ...

>And what's that 6 in HMA(6) and I see something called MMA(36) and ... ?
Patience.

 Hull Moving Average

We start by calculating the 16-day Weighted Moving Average (WMA) like so:
[1]       WMA(16) = (P1 + 2 P2 + 3 P3 + ... + 16 Pn) / K with K = 1+2+...+16 = 136.
Though it's nice and smoooth, it'll have a lag larger than we'd like:

So we look at the 8-day WMA:

>I like it!
Yes, it follows the price variations quite nicely ... but there's more.
While WMA(8) looks at more recent prices, it still has a lag, so we see how much the WMA has changed when going from 8-day to 16-day.
That difference would look like this:

In a sense, that difference gives some indication of how WMA is changing ... so we add this change to our earlier WMA(8) to give:
[2]       MMA(16) = WMA(8) + [ WMA(8) - WMA(16) ] = 2 WMA(8) - WMA(16).

>MMA? Why call it MMA?
I stutter.

Anyway, MMA(16) would look like this:

>I'll take it!
Patience ... there's more.
Now we introduce the magic transformation and get ... ta-DUM!

>That's Hull?
Yes ... as I understand it

>But what's the magic ritual?
Having generated a series of MMAs involving the 8-day and 16-day weighted moving averages, we stare intently at this sequence of numbers.
Then we calculate the WMA over the past 4 days.
That gives the Hull Moving Average that we've called HMA(4).

>Huh? 16 days then 8 days then 4 days. Do you toss a coin to see how many ... ?
You pick some number of days, like n = 16.
Then you look at WMA(n) and WMA(n/2) and calculate MMA = 2 WMA(n/2) - WMA(n).   (In our example, that'd be 2 WMA(8) - WMA(16).
Then you calculate WMA(sqrt(n)) using just the last sqrt(n) numbers from the MMA series.   (In our example, that'd be calculating a WMA(4), using the MMA series.)

>And for that funny SINE chart? How'd it do?

I'm still working on it:
MA-stuff.xls
It's interesting to see how the various moving averages react to spikes:

>Is HMA really a "weighted" moving average?
Well, let's see:

• We have:
MMA = 2 WMA(8) - WMA(16) = 2 [ (P1 + 2 P2 + 3 P3 + ... + 8 Pn) / 36] - (P1 + 2 P2 + 3 P3 + ... + 16 Pn) / 136
or
MMA = [2 (1/36) - (1/136)] [ P1 + 2 P2 + ... + 8 P8] - (1/136)[9 P9 + 10 P10 + ... + 16 P16]
• For sanitary reasons, we'll write this like so:
MMA = [ w1P1 + w2P2 + ... w16P16 ].
Note that all the weights add to 1.
Further, wk = [2 (1/36) - (1/136)
] K for K = 1, 2, ... 8
and wk = -(1/136)] K for K = 9, 10, ... 16.
• Then, doing the magic square-root ritual (where sqrt(16) = 4), we have (recalling that P16 is the most recent value):
HMA = the 4-day WMA of the above MMAs
=[ ( w1P1 + w2P2 + ... w16P16 ) + 2( w1P0 + w2P1 + ... w16P15 ) + 3( w1P-1 + w2P0 + ... w16P14 ) + 4( w1P-2 + w2P-1 + ... w16P13 ) ] / 10
(noting that 1+2+3=4 = 10).
>Huh? P0? P-1? What ...?
The MMA(16) uses the last 16 days, back to the price we're callling P1.
If we calculate the 4-day weighted average of them thar MMAs, we'll be using yesterday's MMA (and that goes back 1 day before P1) and the day before that, the MMA goes back to 2 days before P1 and the day before that ...

>Okay, so you're calling them prices P0, P-1 etc.etc.
You got it.

>So a 16-day HMA actually uses info which goes back more than 16 days, right?
You got it.

>But there are negative weights for them old prices! Is that legal?
The proof is in the ...

>Yeah, yeah ... the proof is in the pudding. So what does the spreadsheet do?
So far it looks like this: (Click on the picture to download.)

You can choose a SINE series or a RANDOM series of stock prices. For the latter, each time you click a button you get another set of prices.
Then you can choose the number of days: that's our n. (For example, we used n = 16 for our example, above.)
Further, if you choose the SINE series, you can introduce "spikes" and move them along the chart ... like this.

Note that we've used n = 16 and n = 36 (in the picture of the spreadsheet) 'cause n/2 and sqrt(n) are both integers.
If you use something like n = 15 then the spreadsheet uses the INTeger part of n/2 and sqrt(n), namely 7 and 3.

>So, is the Hull Moving Average the best?
Define "best".

I know nothing about it. It proprietary and you gotta pay to use it ... however, let's play with moving averages.

 Another Moving Average

Suppose that, instead of the Weighted Moving Average (where the weights are proportional to 1, 2, 3 ...), we use the magic Hull ritual with the Exponential Moving Average.
That is, we consider:
 MAg = 2 EMA(n/2) - EMA(n)

>MAg?
Yes, that's Moving Average gimmick or Moving Average generalized or Moving Average grand or ...

>Or Moving Average gummy?
Pay attention!
We pick our favourite number of days, like n = 16, and calculate MAg(n, α, k) = α EMA(n/k) - (1-α)EMA(n).
We can play with α and k and see what we get:
For example, here are a few MAgs (where we're sticking to 16 days but changing the values of α and k):
 MAg(16) = 2 EMA(4) - EMA(16) MAg(16) = 1.5 EMA(5) - 0.5 EMA(16)

Note that when we pick k = 3 we get n/k = 16/3 = 5.333 which we change to plain-and-simple 5.0.

>Why don't you stick with Hull's choices: α = 2 and k = 2?
Good idea. We'd get this:
 MAg(16) = 2 EMA(8) - EMA(16)
>Looks like the chart with α = 1.5 and k = 3.
It does, doesn't it?
>Did you goof ... again!
Possibly ...
>So what about that square-root ritual?
I leave that as an exercise ... for you!

Okay, while playing with that MAg thing I find that Hull's k = 2 works quite well ... so we'll stick to that.
However, we often get a pretty nice average when we add just a little piece of the "change": EMA(n/2) - EMA(n).
In fact, we'll add just a fraction β of that change.
That'd give: MAg(n,β) = EMA(n/2) + β [ EMA(n/2) - EMA(n) ].
That is, we choose β = 0.5 or maybe just β = 0.25 or whatever and use:
 MAg(n,β) = (1+β) EMA(n/2) - β EMA(n)

For example, if we compare our gaggle of moving averages as they track a STEP function, we get this, where we add (for MAg) only β = 1/2 of the change::

>Yeah, but what's the best value of beta?
Define "best":

Note that beta = 1 is the Hull choice ... except we're using EMAs instead of WMAs.
>And you leave out that square-root thing.
Uh, yes ... I forgot that.

Note: The spreadsheet changes from hour to hour. It currently looks like this

 Something to Play With

You pick a stock and click a button and get a year's worth of daily prices.
The you choose either HMA or MAg, changing the number of days and, for MAg, the parameter, and see when you should BUY ro SELL.

>When? Based upon what criteria?
If the moving average is DOWN x% from its maximum over the last 2 days, you BUY. (In the example, x% = 1.0%)
If it's UP y% from its minimum over the last 2 days, you SELL. (In the example, y% = 1.5%)
You can change the values of x and y.

>Is it any good ... these criteria?
I said it was something to play with.

There's this other "smoothing" technique called the Hodrick-Prescott Filter.
With the help of Ron McEwan, it's now included in this spreadsheet:

>Is it any good?
Play with it !! You'll notice that there's a parameter you can change in cell M3 ... and BUY and SELL signals.