In the world of Quantitative Finance, the Simple Moving Average (SMA) is the "Hello World" of technical indicators. While basic, it forms the backbone of sophisticated trend-following algorithms and momentum strategies.
The Mathematics
The SMA is the unweighted mean of the previous $n$ data points. If we represent the closing price at time $t$ as $P_t$, the SMA for a period $n$ is calculated as:
Why it matters in Quant
Quant traders use SMA primarily to smooth out price action and identify the underlying trend. By removing the "noise" of daily volatility, we can identify support and resistance levels more clearly. However, the SMA is a lagging indicator—it reacts to price changes rather than predicting them.
Implementation in Python
For industrial-grade backtesting, we typically use the pandas library for vectorized calculations:
import pandas as pd
def calculate_sma(data, window=20):
"""
Calculates the Simple Moving Average
"""
return data['close'].rolling(window=window).mean()
# Example Usage
df['SMA_50'] = calculate_sma(df, window=50)
df['SMA_200'] = calculate_sma(df, window=200)
Strategy: The Golden Cross
One of the most famous quantitative signals is the Golden Cross. This occurs when a short-term SMA (e.g., 50-day) crosses above a long-term SMA (e.g., 200-day). This signal is often used as a bullish indicator to enter long positions.