Using Python to make time series analysis of stock futures

Source: Internet
Author: User
Tags truncated statsmodels

Cffex. IF1808, 1000 closing price movements by the end of the day:

# encoding:utf-8import talibfrom talib.abstract import smaimport numpy as Npimport pandas as Pdimport mathimport datetime From collections Import Dequefrom gm.api import *  #掘金import Matplotlib.pyplot as Pltimport matplotlib as Mplimport MPL _finance as Mpfimport matplotlib.dates as Mpdimport Seaborn as Snsimport statsmodels.tsa.stattools as Tsimport statsmodels . API as Smfrom Statsmodels.tsa.arima_model import armafrom scipy import  statsfrom statsmodels.graphics.api Import Qqplotset_token (' **************************** ') #自行填写自己的token

  

  

A time series, he may be trending, is not smooth, so if not smooth need to do differential.

ADF test Results:

95% confidence interval, p=0.0076,99% confidence interval, p=-3.5. To make a first order difference for a model, hoping to get a stationary time series

After the first order difference, the model is basically stable:

P=ts.adfuller (strike_info[' close ') [0] #print pprice_log=strike_info[' close '].diff ()

  

AR (p) model, PACF is truncated at lag=p, that is, the values in the PACF graph fall into the broadband area.

MA (q) model, the ACF will intercept at LAG=Q, in the same vein, the values in the ACF graph fall into the broadband area.

The model is observed using ACF (autocorrelation coefficient) or pacf (partial autocorrelation coefficient):

Fig = Plt.figure (figsize= (12,8)) Ax1=fig.add_subplot (211) FIG = SM.GRAPHICS.TSA.PLOT_ACF (strike_info[' Close '],lags= 40,AX=AX1) ax2 = Fig.add_subplot (212) FIG = SM.GRAPHICS.TSA.PLOT_PACF (strike_info[' close '],lags=40,ax=ax2) plt.show ()

  

The PACF chart is preferred because PACF is truncated at approximately lag=1, that is, the value of PACF falls into the broadband area

Select AR (p=1) model for self-regression fitting to get the fitting effect:

Arma_mod80 = Sm.tsa.ARMA (strike_info[' close '), (1,0)). Fit () print (ARMA_MOD80.AIC,ARMA_MOD80.BIC,ARMA_MOD80.HQIC) Resid = Arma_mod80.residprint (Sm.stats.durbin_watson (arma_mod80.resid.values)) print (Stats.normaltest (resid)) FIG = Plt.figure (figsize= (12,8)) ax = fig.add_subplot (111) FIG = Qqplot (resid, line= ' Q ', Ax=ax, Fit=true) plt.show ()

  

Test: Calculate the residual of the sequence, basically white noise

Fig = Plt.figure (figsize= (12,8)) Ax1 = Fig.add_subplot (211) FIG = SM.GRAPHICS.TSA.PLOT_ACF (Resid.values.squeeze (), lags =40, ax=ax1) ax2 = Fig.add_subplot (212) FIG = SM.GRAPHICS.TSA.PLOT_PACF (Resid, lags=40, AX=AX2) plt.show ()

  

The model with autoregressive fitting is used to predict the results as follows:

Fig=plt.figure (figsize= (15,7)) price2=strike_info=history_n (symbol= ' Cffex. IF1808 ', frequency= ' 60s ', end_time= ' 2018-07-01 ', fields= ' Symbol,close,frequency,cum_volume ', count=1000,df=true) [' Close ']price3=strike_info=history_n (symbol= ' Cffex. IF1808 ', frequency= ' 60s ', end_time=now,fields= ' Symbol,close,frequency,cum_volume ', count=1000,df=true) [' Close '] Print Len (price2) fit = arma_mod80.predict (0, 1100) plt.plot (range (1100), fit[:1100],label= ' predict ') plt.plot (Price2, Label= ' price ') plt.legend (loc=4) plt.show ()

  

Using Python to make time series analysis of stock futures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.