We don’t quite know what moving averages are

A couple of days ago I was reflecting on what the moving average of a price really is.
The idea stemmed from the consideration that any price can be expressed as a starting price plus the sum of subsequent returns. From this, it’s easy to see that:

MA(day 6,5) = (P1 + P2 + P3 + P4 + P5)/5

using the formalism MA(day X,5) = MA to be used to trade on day X based on previous 5 days prices.
In the example above, the next closing price would be P6, and of course we can’t use the information given by P6 to trade on day 6.

Then, given that:
P2 = P1 + r2
P3 = P2 + r3 = P1 + r2 + r3
….

We have:
MA(day 6,5) = P1 + (4*r2 + 3*r3 + 2*r4 + r5)/5

I.e. the simple 5 days moving average is given by 5-days-ago price plus a weighted average of the previous 4 days returns, with more weight given to the older returns.

Personally, I found this small result quite amusing. The fact that a non weighted moving average effectively gives more weight to the return occurred on a particular day (the oldest), it’s a good example of how even simple data transformations can be misleading.

If we apply the same reasoning to a Moving Average cross-over “system”, say MA(3) vs MA(5), we get:

MA(3) = (P3 + P4 + P5)/3
MA(5) = (P1 + … + P5)/5

Skipping the calculations (you can easily reproduce them yourself using the above substitution) we get:

MA(3) – MA(5) > 0       if

3*r2 + 6*r3 + 4*r4 + 2*r5 >0

That is, whenever this particularly weighted sum of previous 4 days returns is positive, we go long, otherwise we go short. From this, I think we can all agree that MA cross-over “system” are not a particularly clever (at best) way of building a system, doesn’t matter which combination of look -back periods we use.

Some further considerations:

1) the smoothing effect of a MA(N) comes from the fact that a particularly day return is used for a N number of days and with an increasing weight, so that the daily rate of change of the MA is kept small. E.g. observe how r5 is used in successive days:

MA(day 6, 5) = P1 + (4*r2 + 3*r3 + 2*r4 + r5)/5
MA(day 7, 5) = P2 + (4*r3 + 3*r4 + 2*r5 + r6)/5
MA(day 8, 5) = P3 + (4*r4 + 3*r5 + 2*r6 + r7)/5
MA(day 9, 5) = P4 + (4*r5 + 3*r6 + 2*r7 + r8)/5

2) based on the above, if we use an inversely weighted scheme for the returns to give more weight to recent returns, we would get a non smoothed series, as each day the MA values change significantly based on the newest return:

modified_MA(day 6, 5) = P1 + (r2 + 2*r3 + 3*r4 + 4*r5)/5
modified_MA(day 7, 5) = P2 + (r3 + 2*r4 + 3*r5 + 4*r6)/5

3) for MA-lovers, something that could be interesting to do is to project the moving average one day ahead. Given that a certain MA(N) assigns the lowest weight to most recent return, it’s easy to project the MA(N) one day forward with a low % error (with the error being lower the bigger is the look-back period N). If we are at the beginning of day 6, the MA(5) we would use to trade is:

MA(day 6, 5) = P1 + (4*r2 + 3*r3 + 2*r4 + r5)/5

However, if we look at what tomorrow’s MA(5) would look like:

MA(day 7, 5) = P2 + (4*r3 + 3*r4 + 2*r5 + r6)/5

we notice that all we are missing is r6 – which is the return having the lowest weight on tomorrow’s MA value. There are a number of ways in which we could proxy it, the simplest probably being considering it =0 (after all that’s the standard expected return value):

projected_MA(day 7, 5) =  P2 + (4*r3 + 3*r4 + 2*r5)/5

or alternatively one could use the average of the N-1 previous days returns (which would make the smoothing effect more substantial). Or really anything that comes to mind (e.g. some scheme based on volatility).

In the graph below I plotted the 10 days moving average of a price series and its one day ahead projection calculated using a 0 zero return for the missing day.

Projected Moving Average
Please note that I am in no way suggesting that this would make a better trading system than using MA cross-over, as it should be clear that I don’t consider MA cross-over a “system”. But it was nice to play around with it.
Interested readers can play around with the Matlab code I am attaching and see the effects of different weighting schemes:

movret_projection

(when I find some time I will figure out how to insert it directly into the post nicely formatted).
The function allows to specify the look-back period, the projection forward period and (optionally) whether the series is already in returns format or not.

PS: possibly all the above is quite obvious to some of you, probably even dull for those with a signal processing background…for those people, please don’t be offended by the catchy title.

Andrea

Advertisements

About mathtrading

My name is Andrea La Rosa and I am a quant trader based in the UK. In the past I worked as a quant in the prop desk of an investment bank, before deciding to fully dedicate myself to quantitative trading.
This entry was posted in Trading Strategies Design and tagged , , . Bookmark the permalink.

7 Responses to We don’t quite know what moving averages are

  1. Moreno says:

    if I understand well you propose a modify modified_MA() with an inverse weights scheme but why you don’t use it in the simulation?

  2. mathtrading says:

    Hi Moreno,

    Thanks for commenting. I am not really proposing a general use of a modified_MA, rather I am highlighting how a simple moving average doesn’t give equal weights to each point (under the form of “return”) in history. Knowing this, one can modify the moving average according to the application it’s being used for.
    The application I refer to in the post is the moving average “trading system”, which is widely in the industry (http://en.wikipedia.org/wiki/Moving_average_crossover)

    Andrea

  3. Interesting article Andrea! It kind of blew my mind until I realized that if you trace back where P2 = P1 + r2 that r2 must be defined not as returns but as price changes or price differences such that r2 = P2 – P1 leaving P2 = P1 + P2 – P1 or P2 = P2 as the starting point!

    And when studying shifting the MA “forward” I’ve failed to reproduce the forward shift effect and instead the MA has been shifted back. To shift the MA forward, wouldn’t you need to shift the values forward rather than back as in: (starting equation)
    MA(day 7, 5) = P2 + (4*r3 + 3*r4 + 2*r5 + r6)/5 = P2 + (-4*P2 + P3 + P4 + P5 + P6) /5 = (P2+P3+P4+P5+P6)/5
    becomes
    MA(day 7, 5) = P3 + (4*r4 + 3*r5 + 2*r6 + r7)/5 where r7 may be equal to zero. But if r7 = P7-P6 then P7 must be set equal to P6 as in: P3 + (-4*P3 + P4 + P5 + P6 + P6)/5
    or simply the forward shifted MA becomes (P3 + P4 + P5 + P6 *2 ) / 5. The forward shift comes from moving the series starting point ahead from P2 to P3 and from duplicating P6 twice to set r6 = 0 and weight the recent activity more heavily.

    At least this is what my charting package shows will shift the MA forward rather than backward.

  4. “and from duplicating P6 twice to set r6 = 0”

    should read:

    and from duplicating P6 twice to set r7 = 0

  5. mathtrading says:

    Hi Patrick, thanks for your comment.
    Well, I guess calling r_i return or price difference depends on which way you look at it!

    In regards to your comment, I see where your confusion comes from, but we are saying the same thing. I edited the post, hope it’s more clear now. What maybe wasn’t obvious is that in my example we are at the beginning of day 6 (in yours instead we are the beginning of day 7), hence we look at MA(day 7,5) and proxy r6 somehow. One way of doing this is to set it = 0, which is equivalent to assume that the missing price will be the same as the previous day price, so that projected_MA(7,5) = projected MA to be used in day 6 = (P2 + P3 + P4 + P5 + P5)/5 in price terms. If we were in day 7 then we would use the formula you presented -> (P3 + P4 + P5 + P6 +P6)/5.

    The advantage of using returns in general (which possibly was the main point of the post) is that you can have a clear view of what you are doing and so more easily apply some data transformation (e.g. look at returns distributions and assume the return will be the expected return from the empirical X days returns distribution, and e.g. only trade if the variance of returns is low enough).

    Andrea

  6. Pingback: Can LinkedIn Continue to Move Higher After Its Offering? – Wall St. Cheat Sheet | I am John Becker

  7. Pingback: Konsortium set to re-challenge immediate overhead resistance | I am John Becker

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s