netflypsb commited on
Commit
fe8332e
1 Parent(s): 1400cd6

Update indicators/ema.py

Browse files
Files changed (1) hide show
  1. indicators/ema.py +20 -17
indicators/ema.py CHANGED
@@ -1,31 +1,34 @@
1
  import pandas as pd
2
 
3
- def calculate_ema(prices, period=20):
4
  """
5
- Calculates the Exponential Moving Average (EMA) for the given period.
6
-
7
  Parameters:
8
  - prices (pd.Series): A pandas Series containing the stock's closing prices.
9
- - period (int): The period over which to calculate the EMA. Defaults to 20.
10
-
11
  Returns:
12
- - pd.Series: A pandas Series containing the EMA values.
13
  """
14
- ema = prices.ewm(span=period, adjust=False).mean()
15
- return ema
 
 
 
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
- # Calculate the 20-period EMA
24
- ema20 = calculate_ema(prices, 20)
25
- print("20-period EMA:")
26
- print(ema20)
 
27
 
28
- # Calculate the 50-period EMA
29
- ema50 = calculate_ema(prices, 50)
30
- print("\n50-period EMA:")
31
- print(ema50)
 
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()