So I am trying to forecast stocks using the yfinance api (using the amazon ticker) and I think it is working correctly, the only problem I have is plotting it in matplotlib. Whenever I try and plot the closing price with the predicted mean it does plot it together with the prediction in front of the current price as you would expect instead the two plots are in two different places.
Here is the code:
import yfinance as yf
ticker = yf.Ticker("AMZN")
history = ticker.history(period="max")
import matplotlib.pyplot as plt
import statsmodels.api as sm
model=sm.tsa.statespace.SARIMAX(history['Close'],order=(1, 1, 1),seasonal_order=(1,1,1,12))
results=model.fit()
prediction = results.get_forecast(steps=12)
pred_ci = prediction.conf_int()
ax = history['Close'].plot(label='observed', figsize=(14, 4))
prediction.predicted_mean.plot(ax=ax, label='Forecast')
ax.fill_between(pred_ci.index,
pred_ci.iloc[:, 0],
pred_ci.iloc[:, 1], color='k', alpha=.25)
plt.legend()
plt.show()