Spaces:
Runtime error
Runtime error
File size: 1,132 Bytes
ac432a5 fe8332e ac432a5 fe8332e ac432a5 fe8332e ac432a5 fe8332e ac432a5 fe8332e ac432a5 fe8332e ac432a5 fe8332e |
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 |
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()
|