CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Machine Learning for Finance in Python

Afficher le cours

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.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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])
Modifier et exécuter le code