netflypsb's picture
Update indicators/ema.py
fe8332e verified
raw
history blame
No virus
1.13 kB
import pandas as pd
def calculate_ema(prices, periods):
"""
Calculates the Exponential Moving Averages (EMA) for the given periods.
Parameters:
- prices (pd.Series): A pandas Series containing the stock's closing prices.
- periods (list of int): A list of integers representing the periods over which to calculate the EMAs.
Returns:
- 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.
"""
emas = {}
for period in periods:
ema = prices.ewm(span=period, adjust=False).mean()
emas[period] = ema
return emas
if __name__ == "__main__":
# Example usage
data = {'Close': [22, 24, 23, 25, 26, 28, 27, 29, 30, 32, 31, 33]}
prices = pd.Series(data['Close'])
# User-defined EMA periods
periods = [20, 50] # Example: 20-day and 50-day EMAs
# Calculate EMAs for specified periods
ema_values = calculate_ema(prices, periods)
for period, ema in ema_values.items():
print(f"{period}-period EMA:")
print(ema)
print()