Skip to content

Commit ce8ecce

Browse files
authored
Update exponential_moving_average.py
1 parent 68c4bb8 commit ce8ecce

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

Diff for: financial/exponential_moving_average.py

+23-35
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Calculate the exponential moving average (EMA) on the series of numbers.
2+
Calculate the exponential moving average (EMA) on the series of stock prices.
33
Wikipedia Reference: https://en.wikipedia.org/wiki/Exponential_smoothing
44
https://www.investopedia.com/terms/e/ema.asp#toc-what-is-an-exponential
55
-moving-average-ema
@@ -13,30 +13,30 @@
1313

1414

1515
def exponential_moving_average(
16-
series: Iterator[float], window_size: int
16+
stock_prices: Iterator[float], window_size: int
1717
) -> Iterator[float]:
1818
"""
19-
Returns the generator which generates exponential moving average of the given
20-
series
21-
>>> list(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3))
22-
[2.0, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625]
19+
Yields exponential moving averages of the given stock prices.
20+
>>> tuple(exponential_moving_average(iter([2, 5, 3, 8.2, 6, 9, 10]), 3))
21+
(2, 3.5, 3.25, 5.725, 5.8625, 7.43125, 8.715625)
2322
24-
:param series: Generator which generates numbers
25-
:param window_size: Window size for calculating average (window_size > 0)
26-
:return: Returns generator of which returns exponentially averaged numbers
23+
:param stock_prices: A stream of stock prices
24+
:param window_size: The number of stock prices that will trigger a new calculation
25+
of the exponential average (window_size > 0)
26+
:return: Yields a sequence of exponential moving averages
2727
2828
Formula:
2929
3030
st = alpha * xt + (1 - alpha) * st_prev
3131
3232
Where,
3333
st : Exponential moving average at timestamp t
34-
xt : Datapoint in series at timestamp t
34+
xt : stock price in from the stock prices at timestamp t
3535
st_prev : Exponential moving average at timestamp t-1
3636
alpha : 2/(1 + window_size) - smoothing factor
3737
3838
Exponential moving average (EMA) is a rule of thumb technique for
39-
smoothing time series data using the exponential window function.
39+
smoothing time series data using an exponential window function.
4040
"""
4141

4242
if window_size <= 0:
@@ -45,41 +45,29 @@ def exponential_moving_average(
4545
# Calculating smoothing factor
4646
alpha = 2 / (1 + window_size)
4747

48-
# Defining timestamp t
49-
timestamp = 0
50-
5148
# Exponential average at timestamp t
52-
exponential_average = 0.0
49+
moving_average = 0.0
5350

54-
for datapoint in series:
55-
if timestamp <= window_size:
51+
for i, stock_price in enumerate(stock_prices):
52+
if i <= window_size:
5653
# Assigning simple moving average till the window_size for the first time
5754
# is reached
58-
exponential_average = (
59-
float(datapoint)
60-
if timestamp == 0
61-
else (exponential_average + datapoint) * 0.5
62-
)
55+
moving_average = (moving_average + stock_price) * 0.5 if i else stock_price
6356
else:
6457
# Calculating exponential moving average based on current timestamp data
6558
# point and previous exponential average value
66-
exponential_average = (alpha * datapoint) + (
67-
(1 - alpha) * exponential_average
68-
)
69-
timestamp += 1
70-
yield exponential_average
59+
moving_average = (alpha * stock_price) + ((1 - alpha) * moving_average)
60+
yield moving_average
7161

7262

7363
if __name__ == "__main__":
7464
import doctest
7565

7666
doctest.testmod()
7767

78-
test_series = [2, 5, 3, 8.2, 6, 9, 10]
79-
test_window_size = 3
80-
result = exponential_moving_average(
81-
series=iter(test_series), window_size=test_window_size
82-
)
83-
print("Test series: ", test_series)
84-
print("Window size: ", test_window_size)
85-
print("Result: ", list(result))
68+
stock_prices = [2.0, 5, 3, 8.2, 6, 9, 10]
69+
window_size = 3
70+
result = tuple(exponential_moving_average(iter(stock_prices), window_size))
71+
print(f"{stock_prices = }")
72+
print(f"{window_size = }")
73+
print(f"{result = }")

0 commit comments

Comments
 (0)