Spaces:
Runtime error
Runtime error
import pandas as pd | |
def calculate_ema(prices, period=20): | |
""" | |
Calculates the Exponential Moving Average (EMA) for the given period. | |
Parameters: | |
- prices (pd.Series): A pandas Series containing the stock's closing prices. | |
- period (int): The period over which to calculate the EMA. Defaults to 20. | |
Returns: | |
- pd.Series: A pandas Series containing the EMA values. | |
""" | |
ema = prices.ewm(span=period, adjust=False).mean() | |
return ema | |
if __name__ == "__main__": | |
# Example usage | |
# Create a sample pandas Series of closing prices | |
data = {'Close': [22, 24, 23, 25, 26, 28, 27, 29, 30, 32, 31, 33]} | |
prices = pd.Series(data['Close']) | |
# Calculate the 20-period EMA | |
ema20 = calculate_ema(prices, 20) | |
print("20-period EMA:") | |
print(ema20) | |
# Calculate the 50-period EMA | |
ema50 = calculate_ema(prices, 50) | |
print("\n50-period EMA:") | |
print(ema50) | |