Skip to content

Commit 997ead0

Browse files
committed
Fix EMA calculation for window size 1
1 parent b93bcc3 commit 997ead0

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

‎financial/exponential_moving_average.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ def exponential_moving_average(
1919
Yields exponential moving averages of the given stock prices.
2020
>>> tuple(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3))
2121
(2, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625)
22+
>>> tuple(exponential_moving_average(iter([10.0, 20.0, 30.0]), 1))
23+
(10.0, 20.0, 30.0)
2224
25+
2326
:param stock_prices: A stream of stock prices
2427
:param window_size: The number of stock prices that will trigger a new calculation
2528
of the exponential average (window_size > 0)
@@ -49,7 +52,7 @@ def exponential_moving_average(
4952
moving_average = 0.0
5053

5154
for i, stock_price in enumerate(stock_prices):
52-
if i <= window_size:
55+
if i < window_size:
5356
# Assigning simple moving average till the window_size for the first time
5457
# is reached
5558
moving_average = (moving_average + stock_price) * 0.5 if i else stock_price

0 commit comments

Comments
 (0)