forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexponential_moving_average.py
More file actions
Latest commit
73 lines (56 loc) · 2.59 KB
/
Copy pathexponential_moving_average.py
File metadata and controls
73 lines (56 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
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
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.
"""
fromcollections.abcimportIterator
defexponential_moving_average(
stock_prices: Iterator[float], window_size: int
) ->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)
: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)
:return: Yields a sequence of exponential moving averages
Formula:
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
smoothing time series data using an exponential window function.
"""
ifwindow_size<=0:
raiseValueError("window_size must be > 0")
# Calculating smoothing factor
alpha=2/ (1+window_size)
# Exponential average at timestamp t
moving_average=0.0
fori, stock_priceinenumerate(stock_prices):
ifi<=window_size:
# Assigning simple moving average till the window_size for the first time
# is reached
moving_average= (moving_average+stock_price) *0.5ifielsestock_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)
yieldmoving_average
if__name__=="__main__":
importdoctest
doctest.testmod()
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=}")