financial.streaming_moving_average ================================== .. py:module:: financial.streaming_moving_average .. autoapi-nested-parse:: This module contains a class that calculates the moving average in real-time. Reference --------- https://en.wikipedia.org/wiki/Moving_average Attributes ---------- .. autoapisummary:: financial.streaming_moving_average.moving_average_calculator Classes ------- .. autoapisummary:: financial.streaming_moving_average.StreamingMovingAverage Module Contents --------------- .. py:class:: StreamingMovingAverage(window_size: int) Streaming Moving Average calculator that updates the average in real-time. Attributes ---------- window_size : int 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 .. py:method:: update(new_value: float) -> None Update the moving average with a new data point. Parameters ---------- new_value : float The new data point to update the moving average. Raises ------ TypeError If the type of new value is not float. .. py:attribute:: _mov_avg :type: float :value: 0.0 .. py:property:: mov_avg :type: float Return the current moving average value. .. py:attribute:: window :type: collections.deque[float] .. py:attribute:: window_size .. py:data:: moving_average_calculator