Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions financial/exponential_moving_average.py
Original file line number Diff line number Diff line change
@@ -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.
"""

Expand All @@ -17,9 +19,13 @@
) -> 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)
Expand All @@ -30,9 +36,13 @@
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
Expand All @@ -49,14 +59,19 @@
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


Expand All @@ -68,6 +83,7 @@
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 = }")

Check failure on line 89 in financial/exponential_moving_average.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W292)

financial/exponential_moving_average.py:89:26: W292 No newline at end of file help: Add trailing newline

Check failure on line 89 in financial/exponential_moving_average.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (W292)

financial/exponential_moving_average.py:89:26: W292 No newline at end of file help: Add trailing newline
Loading