Numpy Moving Average
The moving average is a popular and widely used indicator in statistics and data analysis. It smooths out fluctuations in data to show the underlying trend. With numpy, calculating moving averages efficiently on numeric data becomes trivial.
In this comprehensive guide, we will cover the ins and outs of using numpy for moving average calculation.
What is a Moving Average
A moving average is calculated by taking the average of a subset of data points from a larger dataset. The subset of data moves across the dataset with each new calculation, hence the name "moving average".
For example, a 5day moving average of a stock price would be calculated by taking the average of the closing prices over the past 5 days. On the next day, the average would move forward by one day, dropping the oldest price and including the newest price.
Moving averages smooth out daytoday volatility to reveal gradual changes in trends. They act as low pass filters to remove high frequency components.
The two most common types of moving averages are:
 Simple Moving Average (SMA): Average of the last n data points with equal weighting on each point.
 Exponential Moving Average (EMA): Average with exponential decay weighting that places higher importance on recent values.
In trading and technical analysis, moving averages form the building blocks of many analysis techniques and trading strategies.
Now that we know what a moving average is, let‘s look at how to calculate it with Numpy.
Moving Average with Numpy
Numpy provides efficient operations for manipulating numeric data stored in numpy arrays. With numpy arrays and simple mathematical operations, we can easily compute moving averages.
Here are a few ways to calculate the moving average with numpy:
1. Manual Computation
We can manually calculate the moving average by looping through the array and taking the averages over slices:
import numpy as np
data = [1, 2, 3, 4, 5, 6]
window_size = 3
arr = np.array(data)
for i in range(len(arr)  window_size + 1):
window = arr[i : i + window_size]
avg = np.mean(window)
print(avg)
This prints:
2.0
3.0
4.0
5.0
While simple, manual computation like this is slow and inefficient. Numpy provides better methods.
2. np.convolve()
The np.convolve() function computes the convolution between two arrays. We can use it to calculate a moving window average by convolving the input data with an averaging window consisting of ones:
a = [1, 2, 3, 4, 5]
np.convolve(a, np.ones(window_size)/window_size, mode=‘valid‘)
This returns:
array([2., 3., 4.])
By changing the size of the ones window, we can modify the moving average period.
3. Pandas Rolling
Another option is using the Pandas rolling method:
import pandas as pd
data = [1, 2, 3, 4, 5]
df = pd.Series(data)
df.rolling(window_size).mean()
This computes the moving averages, aligning the result to the original data.
Pandas has optimized routines for rolling window computations and is ideal for working with tabular or time series data.
4. Exponential Moving Average
An exponential moving average applies weighting factors which decrease exponentially. More recent values get higher weightage.
The formula for EMA with a 2 period EMA as example:
EMA(current) = (Price(current) * k) + (EMA(prev) * (1k))
Where,
k = 2 / (N+1)
N = number of periods
Implementing this in Numpy:
import numpy as np
def ema(data, period=2, smooth=2):
c = np.convolve(2 / (period + 1) * np.ones(period), data)[period  1:period + 1]
return c
data = [1, 2, 3, 4, 5]
ema(data) # calculates 2 period EMA
Here smooth
is the number of times to apply smoothing. Higher smoothness means more bias towards recent data.
So in summary, Numpy provides several avenues to efficiently calculate moving averages to reveal meaningful trends in data for analysis and forecasting. The np.convolve and Pandas rolling approaches offer simple yet powerful alternatives ideal for most use cases.
Now let‘s go through a detailed realworld demonstration.
Real World Example with Stock Market Data
To demonstrate a practical example of using Numpy to analyze moving averages, we will work with historical stock market data.
Our objectives are to:
 Import price history data
 Calculate and plot various moving averages
 Visually analyze trends
This will exercise many of the Numpy concepts we have covered so far.
Step 1) Get the Data
We will use the yfinance library to download Apple‘s share price data from the Yahoo Finance API.
import yfinance as yf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
apple = yf.Ticker("AAPL")
# get historical prices
apple_df = apple.history(period="5y")
# store Close price
apple_prices = apple_df[[‘Close‘]]
This retrieves 5 years of AAPL‘s previous closing prices. We isolate just the Close column for simplicity.
Step 2) Compute Moving Averages
Next we calculate 50 and 200 day Simple Moving Averages:
sma_50 = apple_prices.rolling(50).mean()
sma_200 = apple_prices.rolling(200).mean()
We also compute the 20 day Exponential Moving Average:
ema_20 = apple_prices.ewm(span=20).mean()
Step 3) Visualize the Data
Now we can plot and visualize these moving averages alongside the original closing prices:
apple_prices.plot(label=‘AAPL Close‘, legend=True)
sma_50.plot(label=‘SMA 50‘)
sma_200.plot(label=‘SMA 200‘)
ema_20.plot(label=‘EMA 20‘)
plt.title(‘Moving Averages of Apple‘)
plt.ylabel(‘Price ($)‘)
From this chart, we can analyze and observe a few things:
 Times when shorter term EMA crosses above or below the longer term SMAs may indicate trend reversals
 Price tended to honor the 200 period SMA as key support and resistance
 The 50 day SMA formed short term swing highs and lows
 etc…
Identifying such patterns and relationships between moving averages and price movements assists in crafting profitable trading systems and strategies.
And there we have it – a complete workflow leveraging Numpy, Pandas and Matplotlib to fetch, process, analyze and visualize moving averages on stock market price data!
The same principles apply to calculating moving averages on any other sequential data from science, engineering or otherwise. Numpy enables efficient computations to uncover meaningful statistics and trends.
Common Moving Average Analysis Techniques
Now that we have a solid understanding of utilizing Numpy for moving averages, let explore some common techniques used when analyzing moving averages:
1. Crossovers
Crossovers occur when a shorter period moving average crosses a longer period moving average.
A golden cross is when a faster SMA crosses above a slower SMA, signaling the potential start of an uptrend.
A death cross is the opposite, with the faster SMA crossing below the slower SMA, signaling the potential start of a downtrend.
Detecting and reacting to such crossovers form the basis of many popular trading strategies.
For example, the 50 day / 200 day golden cross system buys when the 50 day SMA moves above 200 day SMA.
2. Support and Resistance Levels
Prices repeatedly bounce off key moving average levels which define support and resistance zones.
These act as "floors" and "ceilings" corresponding to peaks and troughs of trader activity. Determining these zones with MAs assists with forecasting future price levels.
In a healthily bullish uptrend for instance, former resistance levels start functioning as support regions where buyers affirm control and drive continuation upwards.
3. Moving Average Ribbons
Plotting bands made of multiple moving averages provides dynamic envelopes around price which quantify volatility and trends.
Narrowing bands reflect decreasing volatility which often precedes breakouts.
Widening bands evidence surging volatility common after sharp directional movements when new trading ranges are established.
Combining bands with other indicators furnishes actionable inputs to mechanical, rulesbased trading systems
So in summary, utilizing moving averages judiciously within wellconstructed, statistically robust frameworks unlocks immense value in identifying opportunities in financial markets.
Leveraging NumPy powers the heavy number crunching involved in efficiently computing and analyzing moving averages on big datasets.
Challenges with Moving Averages
However, moving averages do come with some drawbacks to consider:

Lagging indicator: Moving averages always lag behind current prices which delays signal generation. The longer the period, the larger the lag which limits responsiveness.

Whipsaws: In choppy nontrending conditions, moving averages tend to whipsaw with repeated crossover fakeouts. This frustrates efforts to pinpoint reliable trade entry and exit points.

Tuning complexity: Numerous technical parameters arounddurations, thresholds, combinations etc require extensive optimization and machine learning techniques to build predictive capabilities.

False signals: Despite statistical Edge, no indicator provides guaranteed accurate signals. Even tested systems suffer periods of underperformance resulting in behavioral fatigue.
So traditional moving averages do have limitations. More advanced techniques attempt to overcome these issues to an extent.
Advanced Moving Average Methodologies
Given the above drawbacks, developers and practitioners often tweak moving averages in an attempt to improve performance. Some popular enhanced approaches include:
Adaptive Moving Averages
Instead of fixed lookback periods, these systems employ dynamic windows tuned to market volatility or other heuristic metrics. By adapting the underlying sample size, lag is reduced during transitional periods.
MESA Adaptive Moving Average (MAMA)
Developed by John Ehlers, MAMA attempts to minimizes lag and its related problems. MAMA adapts to price action in an effort to stay responsive.
Gaussian Moving Averages
Applies gaussian smoothing instead of linear smoothing to retain higher sensitivity to recent prices. Requires more math but delivers faster signals.
Zerolag EMA
Uses a special cumulative EMA formulation to align signal timing with current price activity to eliminate lag.
So in summary, active research continues into moving average theory seeking to enhance responsiveness and prediction accuracy.
But the basics we have covered – simple, exponential and analysis techniques still remain highly popular and widely utilized due to their simplicity and reasonable effectiveness.
Conclusion
And there we have it – a comprehensive guide to calculating, visualizing and analyzing moving averages using Numpy and associated scientific Python analysis libraries.
We covered the essential theory, various methodologies, real world demonstrative example and popular analysis techniques leveraging moving averages.
Here are some key takeaways:
 Moving averages smooth noisy data to uncover underlying trends and facilitate forecasting.
 Numpy provides efficient computational routines for calculating moving averages on numeric data
 Analyzing relationship of moving averages to prices identifies trading opportunities
 Common techniques involve detecting crossovers, supports, resistances and bands
 Advanced adaptive moving averages aim to improve predictive capabilities
Overall, mastering moving averages powered by the vast capabilities of Numpy opens up immense potential for gaining valuable insights into financial, scientific and data analytics domains.
The skills to wield essential statistical tools like moving averages for modeling realworld data analysis problems forms an integral part of every data scientist and numeric programmer‘s education.
So grab some data, fire up Numpy, and start honing the craft of moving average analytics today!