Skip to content

Adds exponential moving average algorithm #10273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 12, 2023
Merged
Changes from 6 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
72 changes: 72 additions & 0 deletions financial/exponential_moving_average.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
Calculates exponential moving average (EMA) on the series of numbers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active tense

Suggested change
Calculates exponential moving average (EMA) on the series of numbers
Calculate the exponential moving average (EMA) on the series of numbers.

Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
Reference: https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential-moving-average-ema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines of Python code should be max 88 characters so let's not overdo it.

Suggested change
Reference: 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
changes in the value quicker than SMA, which is one of the advantages of using EMA.
"""

from collections.abc import Iterator


def exponential_moving_average(
series_generator: Iterator[float], window_size: int
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just plain series is fine.

Suggested change
series_generator: Iterator[float], window_size: int
series: Iterator[float], window_size: int

) -> Iterator[float]:
"""
Returns the generator which generates exponential moving average of the given
series generator
>>> list(exponential_moving_average((ele for ele in [2, 5, 3, 8.2, 6, 9, 10]), 3))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just easier to read...

Suggested change
>>> list(exponential_moving_average((ele for ele in [2, 5, 3, 8.2, 6, 9, 10]), 3))
>>> list(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3))

[2.0, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625]

:param series_generator: Generator which generates numbers
:param window_size: Window size for calculating average (window_size > 0)
:return: Returns generator of which returns exponentially averaged numbers

Formula:

st = alpha * xt + (1 - alpha) * st_prev
alpha = 2/(1 + window_size) - smoothing factor

Exponential moving average (EMA) is a rule of thumb technique for
smoothing time series data using the exponential window function.
"""

if window_size <= 0:
raise ValueError("window_size must be > 0")

# Calculating smoothing factor
alpha = 2 / (1 + window_size)

# Defining timestamp t
t = 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t = 0
timestamp = 0

Or maybe even better, ticks.


# Exponential average at timestamp t
st = 0.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
st = 0.0
exponential_average = 0.0


for xt in series_generator:
if t <= window_size:
# Assigning simple moving average till the window_size for the first time
# is reached
st = float(xt) if t == 0 else (st + xt) * 0.5
else:
# Calculating exponential moving average based on current timestamp data
# point and previous exponential average value
st = (alpha * xt) + ((1 - alpha) * st)
t += 1
yield st


if __name__ == "__main__":
import doctest

doctest.testmod()

test_series = [2, 5, 3, 8.2, 6, 9, 10]
test_generator = (ele for ele in test_series)
test_window_size = 3
result = exponential_moving_average(test_generator, test_window_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test_generator = (ele for ele in test_series)
test_window_size = 3
result = exponential_moving_average(test_generator, test_window_size)
test_window_size = 3
result = exponential_moving_average(series=iter(test_series), window_size=test_window_size)

print("Test series: ", test_series)
print("Window size: ", test_window_size)
print("Result: ", list(result))