Moving Averages: A Powerful Tool in Data Science

What are moving averages and how can Data Scientists use them to spot the trend?

J.P. Rinfret
5 min readOct 19, 2019

When trying to predict a trend, whether it is in retail, finance, hospitality, etc., there are many powerful tools backed by complex mathematical theories and daunting equations. That being said, would you be surprised to hear that one of the easiest indicators to understand is also one of the most powerful?

Meet moving averages.

Let’s stick our toes in the water. A simple Wikipedia search on moving averages yields the following result:

“A moving average (aka. rolling average or running average) is a calculation to analyze data points by creating series of averages of different subsets of the full data set”

In layman’s terms, moving averages “smooth” your data to make trend spotting more obvious.

It is important to note that the length of the subset used in calculating a moving average is fixed. For instance, if time (specifically days) is your independent variable and you’re interested in calculating a rolling 20-day moving average, your subset does not grow to 21 elements one day later. Rather, your subset drops the oldest element and gains a new more recent element — all the while remaining at 20 elements.

Different types of moving averages

Let’s now discuss three different moving averages: Simple Moving Average (SMA), Weighted Moving Average (WMA), and Exponential Moving Average (EMA).

A simple moving average is, simply, the arithmetic mean of your subset. Period.

The weighted moving average follows the same idea of the simple moving average but weights each element of the subset using a linearly increasing (or decreasing) factor. Once the elements are weighted, the weighted moving average returns the simple moving average of the new subset. For example, calculating a X-day weighted moving average could assign a weighting factor of x to the most recent data point, x-1 to next most recent data point, all the way down to 1 for the oldest data point. This method puts a higher value on more recent data.

Similarly to the weighted moving average, the exponential moving average weights each element of the subset. However, the exponential moving average uses an exponentially increasing (or decreasing) weighting factor (see exponential smoothing). This method puts the highest value on more recent data points.

Using moving averages

While moving averages can be used by data scientists in many, many ways (e.g., forecasting seasonality at hotel chains, inventory management at retail warehouses, or real-time patient data analysis in healthcare), the remainder of this blog will focus on the basics of technical analysis as it relates to stock trading.

Moving averages are at the foundation of some of the most complicated trading strategies utilized on the street today. For now, we will keep it simple and explore a relatively common stock price trend indicator called a crossover. To explain what a crossover is, let us consider a M-period moving average, a N-period moving average (let’s assume M > N), an independent variable equal to time (delineated by the same periods used to define the moving averages), and a dependent variable equal to the price of the underlying security we are trying to analyze. A crossover occurs when N’s rolling average price is greater than (or less than) M’s rolling average price. Colloquially, this is called “crossing above (below)”.

A golden cross occurs when the shorter-term moving average (a common short-term moving average in technical analysis is 50-days) crosses above a long-term moving average (a common long-term moving average is 200-days). This is an indication that a stock’s price should increase in the future since the short-term trend has more momentum (i.e., is stronger than) the longer-term trend. A death cross is the opposite: the shorter-term moving average crosses below the longer-term moving average, indicating a loss of positive momentum or a lack of buying support.

The below chart is Apple, Inc.’s ($AAPL) daily share price from July 2012 to March 2014. Each vertical bar, or candlestick, represents the daily range of the share price. The top of the wick, the thin vertical line that stretches through the rectangle, is the maximum daily price and the bottom of the wick is the minimum daily price. The top and bottom of the colored rectangles represent the opening and closing prices of that day. If the box is colored black then your opening price was less than the closing price (the stock traded up during the day’s session). If the box is colored red then your opening price was greater than your closing price (the stock traded down).

As the above chart indicates, in early December 2012 the 50-day moving average crossed below the 200-day moving, representing a death cross, giving traders a sell signal. If you were long AAPL and sold your shares, you protected your embedded gains. If you didn’t own AAPL but proceed to short the stock at ~$550 (i.e., you borrowed AAPL shares from another shareholder and sold them in the market for ~$550/share), you could have covered your short position (i.e., you bought back an equal amount of shares you sold earlier at ~$550/share) anywhere from ~$375 to ~$450 between mid-April 2013 and mid-September 2013. This represents a 18–32% profit (less commissions and borrowing costs) once you returned the shares to the original lender. Additionally, the above chart indicates that a golden cross occurred mid-September 2013. This gave traders a buy signal around $450. Only a few months later, in March 2014, AAPL was trading at ~$530 (an 18% profit less commissions on the purchase).

The above is just one of many examples. It also happens to be one example in which the crossovers indicated profitable trades (can you say cherry picking?). There are plenty of examples of false negatives out there, and I encourage you to explore them on your own. Just remember that stop-losses were invented for a reason.

Do I dare say in conclusion?

As we have seen, moving averages are a simple yet effective tool that every data scientist should have in his or her toolbox. But moving averages alone aren’t enough. An effective forecaster knows how to combine moving averages with other statistical trend indicators (see here, here, and here) in order to produce accurate results.

--

--

J.P. Rinfret

TEM @ Komodo Health — Blogs & Opinions are Mine Only | Data Science & Machine Learning at Flatiron School | Mathematics at Fairfield University