考虑到针对股票市场的复杂性,我们尝试在预测模型中加入garch项来增强预测效果。
第一步:使用tushare获取数据:
pip install tushare
```javascript
#获取使用接口
def get_token():
ts.set_token("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
pro=ts.pro_api()
return pro
##获取数据列表
def get_data_list(cursor,sql,conn):
cursor.execute(sql)
res=cursor.fetchall()
conn.commit()
ts_codes_list=list(res)
ts_codes_list=[",".join(list(x)) for x in ts_codes_list]
return ts_codes_list
##获取数据
def get_data(ts_codes_list,pro):
daily=pd.DataFrame(columns=["ts_code","trade_date","open","close","high","low","volume"]) ##获取相应的列信息
for i in range(0,len(ts_codes_list),100):
j=i+100
if(j>=len(ts_codes_list)):
j=len(ts_codes_list)
name=",".join(ts_codes_list[i:j])
part= pro.daily(ts_code=name, trade_date=get_date())[["ts_code","trade_date","open","close","high","low","volume"]]
daily=pd.concat([daily,part],ignore_index=True)
daily["trade_date"]=daily["trade_date"].apply(get_date_format)
return daily
第二步:计算GARCH项
#需要的包
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import math
import statsmodels.api as sm
from scipy.optimize import minimize
def load_return(file):
Stock = file
Stock['date'] = pd.to_datetime