import pandas as pd
import matplotlib.pyplot as plt
import statsmodels as sm
from statsmodels.graphics.tsaplots import plot_acf,plot_pacf
import numpy as np
discfile = r'C:\Users\GitHung Zeng\Documents\WeChat Files\WT13662535567\Files\日线数据\Canadian-dollar_RMB.xlsx'
forecastnum = 5
data = pd.read_excel(discfile, index_col = '时间')
data = data['收']
plt.rcParams['font.sans-serif'] = ['SimHei']
##datetime时间转化
data.index = pd.to_datetime(data.index,format="%Y/%m/%d")
plt.figure(figsize=(8,4))
plt.plot(data)
plt.show()
##ACF,PACF计算
fig = plt.figure(figsize = (12,8))
ax1 = fig.add_subplot(211)
fig = plot_acf(data,lags = 40,ax =ax1)
ax1.xaxis.set_ticks_position('bottom')
fig.tight_layout()
ax2 = fig.add_subplot(212)
fig = plot_pacf(data,lags = 40,ax =ax2)
ax1.xaxis.set_ticks_position('bottom')
fig.tight_layout()
data_diff = data.diff(5)
data_diff = data_diff.dropna()
##ACF,PACF计算
fig = plt.figure(figsize = (12,8))
ax1 = fig.add_subplot(211)
fig = plot_acf(data_diff,lags = 40,ax =ax1)
ax1.xaxis.set_ticks_position('bottom')
fig.tight_layout()
ax2 = fig.add_subplot(212)
fig = plot_pacf(data_diff,lags = 40,ax =ax2)
ax1.xaxis.set_ticks_position('bottom')
fig.tight_layout()
arima11 = sm.tsa.arima_model.ARMA(data_diff,order=(6,4))
model_results=arima11.fit()
predict = model_results.predict()
forecast = model_results.forecast(5)
plt.figure(figsize=(12,8))
plt.plot(data_diff.iloc[data_diff.size-1000:data_diff.size])
plt.plot(predict[data_diff.size-1000:data_diff.size],'r')
plt.show()
forecast[1]