From 77e43f493cac074dee26b175d927f4e43c66984c Mon Sep 17 00:00:00 2001 From: Phaneendra-25 Date: Sun, 4 Oct 2026 22:12:11 +0530 Subject: [PATCH 1/3] fix: handle EMA window size of one --- financial/exponential_moving_average.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/financial/exponential_moving_average.py b/financial/exponential_moving_average.py index b56eb2712415..63d2b932a044 100644 --- a/financial/exponential_moving_average.py +++ b/financial/exponential_moving_average.py @@ -19,6 +19,8 @@ def exponential_moving_average( 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, 20, 30]), 1)) + (10, 20, 30) :param stock_prices: A stream of stock prices :param window_size: The number of stock prices that will trigger a new calculation @@ -49,10 +51,14 @@ def exponential_moving_average( moving_average = 0.0 for i, stock_price in enumerate(stock_prices): - if i <= window_size: + if i == 0: + moving_average = stock_price + elif window_size == 1: + moving_average = stock_price + elif 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 else: # Calculating exponential moving average based on current timestamp data # point and previous exponential average value From c7ac912f7b6aa1448ab1935bbe1942ae59c4a5f5 Mon Sep 17 00:00:00 2001 From: Phaneendra-25 Date: Sun, 4 Oct 2026 22:17:36 +0530 Subject: [PATCH 2/3] fix: satisfy ruff check --- financial/exponential_moving_average.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/financial/exponential_moving_average.py b/financial/exponential_moving_average.py index 63d2b932a044..4d0f8b2b1425 100644 --- a/financial/exponential_moving_average.py +++ b/financial/exponential_moving_average.py @@ -51,9 +51,7 @@ def exponential_moving_average( moving_average = 0.0 for i, stock_price in enumerate(stock_prices): - if i == 0: - moving_average = stock_price - elif window_size == 1: + if i == 0 or window_size == 1: moving_average = stock_price elif i <= window_size: # Assigning simple moving average till the window_size for the first time From 511cd56ea0da801635a88e758df342a31c2d8375 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 4 Oct 2026 15:54:37 -1000 Subject: [PATCH 3/3] Update exponential_moving_average.py --- financial/exponential_moving_average.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/financial/exponential_moving_average.py b/financial/exponential_moving_average.py index 4d0f8b2b1425..2eb29a43a1b9 100644 --- a/financial/exponential_moving_average.py +++ b/financial/exponential_moving_average.py @@ -19,8 +19,8 @@ def exponential_moving_average( 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, 20, 30]), 1)) - (10, 20, 30) + >>> tuple(exponential_moving_average(iter([10.1, 20.02, 30.003]), 1)) + (10.1, 20.02, 30.003) :param stock_prices: A stream of stock prices :param window_size: The number of stock prices that will trigger a new calculation @@ -71,7 +71,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)) + result = tuple(exponential_moving_average(stock_prices, window_size)) print(f"{stock_prices = }") print(f"{window_size = }") print(f"{result = }")