Spaces:
Running
Running
Update indicators/ema.py
Browse files- indicators/ema.py +20 -17
indicators/ema.py
CHANGED
@@ -1,31 +1,34 @@
|
|
1 |
import pandas as pd
|
2 |
|
3 |
-
def calculate_ema(prices,
|
4 |
"""
|
5 |
-
Calculates the Exponential Moving
|
6 |
-
|
7 |
Parameters:
|
8 |
- prices (pd.Series): A pandas Series containing the stock's closing prices.
|
9 |
-
-
|
10 |
-
|
11 |
Returns:
|
12 |
-
- pd.Series: A pandas Series containing the EMA values.
|
13 |
"""
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
16 |
|
17 |
if __name__ == "__main__":
|
18 |
# Example usage
|
19 |
-
# Create a sample pandas Series of closing prices
|
20 |
data = {'Close': [22, 24, 23, 25, 26, 28, 27, 29, 30, 32, 31, 33]}
|
21 |
prices = pd.Series(data['Close'])
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
1 |
import pandas as pd
|
2 |
|
3 |
+
def calculate_ema(prices, periods):
|
4 |
"""
|
5 |
+
Calculates the Exponential Moving Averages (EMA) for the given periods.
|
6 |
+
|
7 |
Parameters:
|
8 |
- prices (pd.Series): A pandas Series containing the stock's closing prices.
|
9 |
+
- periods (list of int): A list of integers representing the periods over which to calculate the EMAs.
|
10 |
+
|
11 |
Returns:
|
12 |
+
- dict of pd.Series: A dictionary where each key is the period, and the value is a pandas Series containing the EMA values for that period.
|
13 |
"""
|
14 |
+
emas = {}
|
15 |
+
for period in periods:
|
16 |
+
ema = prices.ewm(span=period, adjust=False).mean()
|
17 |
+
emas[period] = ema
|
18 |
+
return emas
|
19 |
|
20 |
if __name__ == "__main__":
|
21 |
# Example usage
|
|
|
22 |
data = {'Close': [22, 24, 23, 25, 26, 28, 27, 29, 30, 32, 31, 33]}
|
23 |
prices = pd.Series(data['Close'])
|
24 |
|
25 |
+
# User-defined EMA periods
|
26 |
+
periods = [20, 50] # Example: 20-day and 50-day EMAs
|
27 |
+
|
28 |
+
# Calculate EMAs for specified periods
|
29 |
+
ema_values = calculate_ema(prices, periods)
|
30 |
|
31 |
+
for period, ema in ema_values.items():
|
32 |
+
print(f"{period}-period EMA:")
|
33 |
+
print(ema)
|
34 |
+
print()
|