Moving Average

Moving Averages are implemented in finquant.moving_average.

The module provides functions to compute and visualise:

  • Simple Moving Averages,

  • Exponential Moving Averages,

  • a band of Moving Averages (simple or exponential), and

  • Bollinger Bands.

finquant.moving_average.compute_ma(data, fun, spans, plot=True)

Computes a band of moving averages (sma or ema, depends on the input argument fun) for a number of different time windows. If plot is True, it also computes and sets markers for buy/sell signals based on crossovers of the Moving Averages with the shortest/longest spans.

Parameters:
  • data (SERIES_OR_DATAFRAME) – A series/dataframe of daily stock prices (if DataFrame, only one column is expected)

  • fun (Callable[[Union[Series, DataFrame], int], Series]) – Function that computes a moving average, e.g. `sma` (simple) or `ema` (exponential).

  • spans (List[int]) – List of integers, time windows to compute the Moving Average on.

  • plot (bool) – boolean, whether to plot the moving averages and buy/sell signals based on crossovers of shortest and longest moving average, or not, default: True

Return type:

DataFrame

Returns:

Moving averages of given data.

finquant.moving_average.ema(data, span=100)

Computes and returns the exponential moving average.

Note: the moving average is computed on all columns.

Parameters:
  • data (DataFrame) – A dataframe of daily stock prices

  • span (int) – Number of days/values over which the average is computed, default: 100

Return type:

DataFrame

Returns:

Exponential moving average

finquant.moving_average.ema_std(data, span=100)

Computes and returns the standard deviation of the exponential moving average.

Parameters:
  • data (DataFrame) – A dataframe of daily stock prices

  • span (int) – Number of days/values over which the average is computed, default: 100

Return type:

DataFrame

Returns:

Standard deviation of exponential moving average

finquant.moving_average.plot_bollinger_band(data, fun, span=100)

Computes and visualises a Bolling Band.

Parameters:
  • data (DataFrame) – A dataframe of daily stock prices

  • fun (Callable[[DataFrame, int], DataFrame]) – function that computes a moving average, e.g. sma (simple) or ema (exponential).

  • span (int) – Number of days/values over which the average is computed, default: 100

Return type:

None

finquant.moving_average.sma(data, span=100)

Computes and returns the simple moving average.

Note: the moving average is computed on all columns.

Parameters:
  • data (DataFrame) – A dataframe of daily stock prices

  • span (int) – Number of days/values over which the average is computed, default: 100

Return type:

DataFrame

Returns:

Simple moving average

finquant.moving_average.sma_std(data, span=100)

Computes and returns the standard deviation of the simple moving average.

Parameters:
  • data (DataFrame) – A dataframe of daily stock prices

  • span (int) – Number of days/values over which the average is computed, default: 100

Return type:

DataFrame

Returns:

Standard deviation of simple moving average