From 6b91d392cc5f792df5d0efb5f0bad62b44e63a8b Mon Sep 17 00:00:00 2001 From: NeuroByte79 Date: Fri, 2 Oct 2026 16:12:11 +0530 Subject: [PATCH 1/2] fix: correct EMA window handling --- financial/exponential_moving_average.py | 32 ++++++++++++++++++------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/financial/exponential_moving_average.py b/financial/exponential_moving_average.py index b56eb2712415..e50a65a75d7d 100644 --- a/financial/exponential_moving_average.py +++ b/financial/exponential_moving_average.py @@ -1,11 +1,13 @@ """ Calculate the exponential moving average (EMA) on the series of stock prices. -Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing -https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential --moving-average-ema + +Wikipedia Reference: [https://en.wikipedia.org/wiki/Exponential_smoothing](https://en.wikipedia.org/wiki/Exponential_smoothing) + +[https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema](https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema) Exponential moving average is used in finance to analyze changes stock prices. -EMA is used in conjunction with Simple moving average (SMA), EMA reacts to the + +EMA is used in conjunction with Simple Moving Average (SMA), EMA reacts to the changes in the value quicker than SMA, which is one of the advantages of using EMA. """ @@ -17,9 +19,13 @@ def exponential_moving_average( ) -> Iterator[float]: """ Yields exponential moving averages of the given stock prices. + >>> tuple(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3)) (2, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625) + >>> tuple(exponential_moving_average(iter([10.0, 20.0, 30.0]), 1)) + (10.0, 20.0, 30.0) + :param stock_prices: A stream of stock prices :param window_size: The number of stock prices that will trigger a new calculation of the exponential average (window_size > 0) @@ -30,9 +36,13 @@ def exponential_moving_average( st = alpha * xt + (1 - alpha) * st_prev Where, + st : Exponential moving average at timestamp t + xt : stock price in from the stock prices at timestamp t + st_prev : Exponential moving average at timestamp t-1 + alpha : 2/(1 + window_size) - smoothing factor Exponential moving average (EMA) is a rule of thumb technique for @@ -49,14 +59,19 @@ def exponential_moving_average( moving_average = 0.0 for i, stock_price in enumerate(stock_prices): - if i <= window_size: + if i < window_size: # Assigning simple moving average till the window_size for the first time # is reached - moving_average = (moving_average + stock_price) * 0.5 if i else stock_price + moving_average = ( + (moving_average + stock_price) * 0.5 if i else stock_price + ) else: # Calculating exponential moving average based on current timestamp data # point and previous exponential average value - moving_average = (alpha * stock_price) + ((1 - alpha) * moving_average) + moving_average = (alpha * stock_price) + ( + (1 - alpha) * moving_average + ) + yield moving_average @@ -68,6 +83,7 @@ def exponential_moving_average( stock_prices = [2.0, 5, 3, 8.2, 6, 9, 10] window_size = 3 result = tuple(exponential_moving_average(iter(stock_prices), window_size)) + print(f"{stock_prices = }") print(f"{window_size = }") - print(f"{result = }") + print(f"{result = }") \ No newline at end of file From 5cdad0e6b1d08ed63ecef5820a7e0f5559172ae7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 2 Oct 2026 10:54:23 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- financial/exponential_moving_average.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/financial/exponential_moving_average.py b/financial/exponential_moving_average.py index e50a65a75d7d..1a7b4a9655ec 100644 --- a/financial/exponential_moving_average.py +++ b/financial/exponential_moving_average.py @@ -62,15 +62,11 @@ def exponential_moving_average( if i < window_size: # Assigning simple moving average till the window_size for the first time # is reached - moving_average = ( - (moving_average + stock_price) * 0.5 if i else stock_price - ) + moving_average = (moving_average + stock_price) * 0.5 if i else stock_price else: # Calculating exponential moving average based on current timestamp data # point and previous exponential average value - moving_average = (alpha * stock_price) + ( - (1 - alpha) * moving_average - ) + moving_average = (alpha * stock_price) + ((1 - alpha) * moving_average) yield moving_average @@ -86,4 +82,4 @@ def exponential_moving_average( print(f"{stock_prices = }") print(f"{window_size = }") - print(f"{result = }") \ No newline at end of file + print(f"{result = }")