Get startedGet started for free

Applying an MA Model

The bouncing of the stock price between bid and ask induces a negative first order autocorrelation, but no autocorrelations at lags higher than 1. You get the same ACF pattern with an MA(1) model. Therefore, you will fit an MA(1) model to the intraday stock data from the last exercise.

The first step is to compute minute-by-minute returns from the prices in intraday, and plot the autocorrelation function. You should observe that the ACF looks like that for an MA(1) process. Then, fit the data to an MA(1), the same way you did for simulated data.

This exercise is part of the course

Time Series Analysis in Python

View Course

Exercise instructions

  • Import plot_acf and ARIMA modules from statsmodels
  • Compute minute-to-minute returns from prices:
    • Compute returns with the .pct_change() method
    • Use the pandas method .dropna() to drop the first row of returns, which is NaN
  • Plot the ACF function with lags up to 60 minutes
  • Fit the returns data to an MA(1) model and print out the MA(1) parameter

Hands-on interactive exercise

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

# Import plot_acf and ARIMA modules from statsmodels
from statsmodels.graphics.tsaplots import plot_acf
from statsmodels.tsa.arima.model import ARIMA

# Compute returns from prices and drop the NaN
returns = intraday.___
returns = returns.___

# Plot ACF of returns with lags up to 60 minutes
plot_acf(___, ___)
plt.show()

# Fit the data to an MA(1) model
mod = ARIMA(___, order=(0,0,1))
res = mod.fit()
print(res.params[1])
Edit and Run Code