Moving Average
Calculate a Moving Average (MA) of a given price series: the simple, exponential, double exponential, triangular, weighted or Hull moving average.
A Moving Average smooths out price data by averaging the price over a specified number of periods. The variants differ in how they weight the prices within the window: equally (simple), with exponentially decaying weights (exponential and double exponential), with weights that peak in the middle of the window (triangular), with linearly increasing weights (weighted) or with a combination of weighted averages that reduces the lag (Hull).
The formula is as follows, per method:
\[\text{"simple":}\;\; \text{SMA} = (\text{Sum of Prices}) / (\text{Number of Prices})\] \[\text{"exponential":}\;\; \text{the first EMA is SMA(Close, Window)},\;\; \text{on the Window-th period},\;\; \text{and EMA} = (\text{Close} - \text{Previous EMA}) \cdot (2 / (1 + \text{Window})) + \text{Previous EMA}\] \[\text{"double\_exponential":}\;\; \text{DEMA} = 2 \cdot \operatorname{EMA}(\text{Close},\; \text{Window}) - \operatorname{EMA}(\operatorname{EMA}(\text{Close},\; \text{Window}),\; \text{Window})\] \[\text{"triangular":}\;\; \text{TMA} = \operatorname{SMA}(\operatorname{SMA}(\text{Close},\; \text{Sub-window Length}\; 1),\; \text{Sub-window Length}\; 2),\;\; \text{with both sub-windows} (\text{Window} + 1) / 2 \;\text{for an odd window and Window} / 2 \;\text{and Window} / 2 + 1 \;\text{for an even window} (\text{TA-Lib's TRIMA convention})\] \[\text{"weighted":}\;\; \text{WMA} = (\text{Sum of} (\text{Price} \cdot \text{Weight})) / (\text{Sum of Weights}),\;\; \text{with weights 1 to Window from the oldest to the latest price}\] \[\text{"hull":}\;\; \text{HMA} = \operatorname{WMA}(2 \cdot \operatorname{WMA}(\text{Close},\; \text{Window} / 2) - \operatorname{WMA}(\text{Close},\; \text{Window}),\; \sqrt{\text{Window}})\]Seeding the EMA with a simple average, as TA-Lib and StockCharts do, leaves the first Window - 1 periods of the price history empty. The DEMA, TRIX, MACD, Percentage Price Oscillator, Force Index, Elder Ray Index, Keltner Channels and Chaikin and McClellan Oscillators use the same EMA and start once their own EMAs are seeded. The Kaufman Adaptive Moving Average, which adapts its smoothing to the efficiency of the price move, is get_kaufman_adaptive_moving_average.
Also known as: MA, SMA, simple moving average, EMA, exponential moving average, DEMA, double exponential moving average, TMA, TRIMA, triangular moving average, WMA, linearly weighted moving average, HMA, Hull moving average.
Moving Average in Python or via the MCP
Install the Finance Toolkit with:
pip install financetoolkit -U
Then call get_moving_average as shown below.
from financetoolkit import Toolkit
toolkit = Toolkit(
tickers=["^GSPC"],
api_key="FINANCIAL_MODELING_PREP_KEY",
start_date="2021-01-01",
end_date="2025-12-31",
)
toolkit.technicals.get_moving_average(window=50).tail(5)
Or, after setting up the Finance Toolkit MCP, ask:
How did the 50-day simple moving average of the S&P 500 move from 24 to 31 December 2025?
Which returns:
| Date | ^GSPC | Benchmark |
|---|---|---|
| 2025-12-24 | 6784.99 | 669.712 |
| 2025-12-26 | 6790.16 | 670.25 |
| 2025-12-29 | 6795.7 | 670.828 |
| 2025-12-30 | 6800.34 | 671.316 |
| 2025-12-31 | 6802.55 | 671.566 |
Parameters
get_moving_average accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
period | str | The time period to consider for historical data. Can be “intraday”, “daily”, “weekly”, “monthly”, “quarterly” or “yearly”. Defaults to “daily”. | "weekly" |
close_column | str | The column name for closing prices in the historical data. Defaults to “Adj Close”. | "Close" |
window | int | Number of periods to consider for the moving average. The number of periods (time intervals) over which to calculate the MA. Defaults to 14. | 20 |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the rounding of the Toolkit (4 unless set otherwise). | 2 |
growth | bool | Whether to calculate the growth of the MA. Defaults to False. | True |
lag | int | list[int] | The lag to use for the growth calculation. Defaults to 1. | 4 |
standardize | bool | Whether to standardize (Z-Score) the result. When combined with growth=True, standardizes the growth values instead of the raw values. Defaults to False. | True |
method | str | The moving average: “simple”, “exponential”, “double_exponential”, “triangular”, “weighted” or “hull”. Defaults to “simple”. | "simple" |