Get startedGet started for free

Calculate EWMAs

We will now work towards creating some features to be able to predict our ideal portfolios. We will simply use the price movement as a feature for now. To do this we will create a daily exponentially-weighted moving average (EWMA), then resample that to the monthly timeframe. Finally, we'll shift the monthly moving average of price one month in the future, so we can use it as a feature for predicting future portfolios.

This exercise is part of the course

Machine Learning for Finance in Python

View Course

Exercise instructions

  • Use a span of 30 to calculate the daily exponentially-weighted moving average (ewma_daily).
  • Resample the daily ewma to the month by using the Business Monthly Start frequency (BMS) and the first day of the month (.first()).
  • Shift ewma_monthly by one month forward, so we can use the previous month's EWMA as a feature to predict the next month's ideal portfolio.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Calculate exponentially-weighted moving average of daily returns
ewma_daily = returns_daily.ewm(span=____).mean()

# Resample daily returns to first business day of the month with the first day for that month
ewma_monthly = ewma_daily.resample(____).____

# Shift ewma for the month by 1 month forward so we can use it as a feature for future predictions 
ewma_monthly = ewma_monthly.____.dropna()

print(ewma_monthly.iloc[-1])
Edit and Run Code