Lesson 1 of 15
Simple & Exponential Moving Averages
Simple & Exponential Moving Averages
Moving averages smooth out price data to identify trends. They are the foundation of most technical indicators.
Simple Moving Average (SMA)
The SMA over a window of size w at time t is the arithmetic mean of the last w prices:
SMA[t] = (prices[t] + prices[t-1] + ... + prices[t-w+1]) / w
The first w - 1 values are None because there is not enough data yet.
Exponential Moving Average (EMA)
The EMA applies an exponentially decaying weight to past prices using a smoothing factor alpha:
EMA[0] = prices[0]
EMA[t] = alpha * prices[t] + (1 - alpha) * EMA[t-1]
Higher alpha gives more weight to recent prices.
Task
Implement:
sma(prices, window)— returns a list where the firstwindow - 1elements areNoneand subsequent elements are the rolling mean.ema(prices, alpha)— returns the exponential moving average starting fromprices[0].
Python runtime loading...
Loading...
Click "Run" to execute your code.