日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

用python對(duì)股票期貨做時(shí)序分析

 禁忌石 2022-05-16 發(fā)布于浙江

選擇期貨CFFEX.IF1808,截止到當(dāng)日的1000條收盤價(jià)格走勢(shì):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# encoding: utf-8
import talib
from talib.abstract import SMA
import numpy as np
import pandas as pd
import math
import datetime
from collections import deque
from gm.api import *  #掘金
import matplotlib.pyplot as plt
import matplotlib as mpl
import mpl_finance as mpf
import matplotlib.dates as mpd
import seaborn as sns
import statsmodels.tsa.stattools as ts
import statsmodels.api as sm
from statsmodels.tsa.arima_model import ARMA
from scipy import  stats
from statsmodels.graphics.api import qqplot
set_token('****************************') #自行填寫(xiě)自己的token
1
2
3
4
5
6
7
now=datetime.datetime.now().date()
last_day=get_previous_trading_date(exchange='SHSE',date=now)
index_futures=get_continuous_contracts(csymbol='CFFEX.IF',start_date=last_day,end_date=last_day)
#print index_futures
strike_info=history_n(symbol='CFFEX.IF1808',frequency='60s',end_time='2018-07-01',fields='symbol,close,frequency,cum_volume',count=1000,df=True)
strike_info.dropna()
price=np.array(strike_info['close'])

一個(gè)時(shí)間序列,他可能是有趨勢(shì)的,是不平穩(wěn)的,所以如果不平穩(wěn)需要做差分。

ADF檢測(cè)結(jié)果:

95%置信區(qū)間,p=0.0076,99%置信區(qū)間下,p=-3.5。對(duì)模型做一階差分,希望得到一個(gè)平穩(wěn)的時(shí)間序列

一階差分后,模型基本平穩(wěn):

1
2
3
p=ts.adfuller(strike_info['close'])[0]
#print p
price_log=strike_info['close'].diff()

AR(p)模型,PACF會(huì)在lag=p時(shí)截尾,也就是,PACF圖中的值落入寬帶區(qū)域中。

MA(q)模型,ACF會(huì)在lag=q時(shí)截尾,同理,ACF圖中的值落入寬帶區(qū)域中。

用ACF(自相關(guān)系數(shù))或者PACF(偏自相關(guān)系數(shù))觀察模型:

1
2
3
4
5
6
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()

優(yōu)先選擇PACF圖,因?yàn)镻ACF大約在lag=1時(shí)截尾,即PACF的值落入寬帶區(qū)域中

選擇AR(P=1)的模型進(jìn)行自回歸擬合,得到擬合效果:

1
2
3
4
5
6
7
8
9
arma_mod80 = sm.tsa.ARMA(strike_info['close'],(1,0)).fit()
print(arma_mod80.aic,arma_mod80.bic,arma_mod80.hqic)
resid = arma_mod80.resid
print(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()

 

檢驗(yàn):計(jì)算得到序列的殘差,基本為白噪音

1
2
3
4
5
6
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()

 

用自回歸擬合的模型進(jìn)行預(yù)測(cè),結(jié)果如下:

1
2
3
4
5
6
7
8
9
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()

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多