financial.simple_moving_average

The Simple Moving Average (SMA) is a statistical calculation used to analyze data points by creating a constantly updated average price over a specific time period. In finance, SMA is often used in time series analysis to smooth out price data and identify trends.

Reference: https://en.wikipedia.org/wiki/Moving_average

Attributes

data

Functions

simple_moving_average(→ list[float | None])

Calculate the simple moving average (SMA) for some given time series data.

Module Contents

financial.simple_moving_average.simple_moving_average(data: collections.abc.Sequence[float], window_size: int) list[float | None]

Calculate the simple moving average (SMA) for some given time series data.

Parameters:
  • data – A list of numerical data points.

  • window_size – An integer representing the size of the SMA window.

Returns:

A list of SMA values with the same length as the input data.

Examples: >>> sma = simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 3) >>> [round(value, 2) if value is not None else None for value in sma] [None, None, 12.33, 13.33, 14.0, 14.33, 16.0, 17.0, 18.0, 19.0] >>> simple_moving_average([10, 12, 15], 5) [None, None, None] >>> simple_moving_average([10, 12, 15, 13, 14, 16, 18, 17, 19, 21], 0) Traceback (most recent call last): … ValueError: Window size must be a positive integer

financial.simple_moving_average.data = [10, 12, 15, 13, 14, 16, 18, 17, 19, 21]