financial.streaming_moving_average

This module contains a class that calculates the moving average in real-time.

Reference

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

Attributes

moving_average_calculator

Classes

StreamingMovingAverage

Streaming Moving Average calculator that updates

Module Contents

class financial.streaming_moving_average.StreamingMovingAverage(window_size: int)

Streaming Moving Average calculator that updates the average in real-time.

Attributes

window_sizeint

The size of the moving average window.

Methods

update(new_value: float) -> None

Update the moving average with a new data point.

mov_avg() -> float

Return the current moving average value.

Raises

ValueError

If the window size is less than 1.

Examples

>>> mov_avg_calculator = StreamingMovingAverage(3)
>>> mov_avg_calculator.update(10.)
>>> mov_avg_calculator.update(12.)
>>> mov_avg_calculator.update(2.)
>>> mov_avg_calculator.update(1.)
>>> mov_avg_calculator.mov_avg
5.0
>>> mov_avg_calculator = StreamingMovingAverage(5)
>>> mov_avg_calculator.update(2.)
>>> mov_avg_calculator.update(3.)
>>> mov_avg_calculator.mov_avg
2.5
update(new_value: float) → None

Update the moving average with a new data point.

Parameters

new_valuefloat

The new data point to update the moving average.

Raises

TypeError

If the type of new value is not float.

_mov_avg: float = 0.0
property mov_avg: float

Return the current moving average value.

window: collections.deque[float]
window_size
financial.streaming_moving_average.moving_average_calculator