import pandas as pd
from WindPy import *
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from scipy.optimize import leastsq
w.start()
data_df_QTJY=w.wsd("300359.SZ", "close", "2018-12-01", "2019-02-28", "Fill=Previous;PriceAdj=F")
data_df_CYBZ=w.wsd("399006.SZ", "close", "2018-12-01", "2019-02-28", "Fill=Previous;PriceAdj=F")
data_df_QTJY=pd.DataFrame(data_df_QTJY.Data,columns=data_df_QTJY.Times,index=data_df_QTJY.Fields)
data_df_QTJY=data_df_QTJY.T
data_df_CYBZ=pd.DataFrame(data_df_CYBZ.Data,columns=data_df_CYBZ.Times,index=data_df_CYBZ.Fields)
data_df_CYBZ=data_df_CYBZ.T
data_df_QTJY['return']=np.log(data_df_QTJY['CLOSE']/data_df_QTJY['CLOSE'].shift(1))
data_df_QTJY['index_return']=np.log(data_df_CYBZ['CLOSE']/data_df_CYBZ['CLOSE'].shift(1))
data_df_QTJY=data_df_QTJY.dropna()
xi=np.array(data_df_QTJY['index_return'])
yi=np.array(data_df_QTJY['return'])
xi
def func(p,x):
k,b=p
return k*x+b
def error(p,x,y):
return func(p,x)-y
p0=[0.5,0.03]
Para=leastsq(error,p0,args=(xi,yi))
Para
k,b=Para[0]
print("k=",k,"b=",b)
plt.figure(figsize=(8,6))
plt.scatter(xi,yi,color="green",label="样本数据",linewidth=2)
x=np.linspace(-0.1,0.1,100)
y=k*x+b
plt.plot(x,y,color="red",label="拟合直线",linewidth=2)
plt.legend()
plt.show()
def get_ar(k):
data_df_QTJY=w.wsd("300359.SZ", "close", "2019-04-01", "2019-04-15", "PriceAdj=F")
data_df_CYBZ=w.wsd("399006.SZ", "close", "2019-04-01", "2019-04-15", "PriceAdj=F")
data_df_QTJY=pd.DataFrame(data_df_QTJY.Data,columns=data_df_QTJY.Times,index=data_df_QTJY.Fields)
data_df_QTJY=data_df_QTJY.T
data_df_CYBZ=pd.DataFrame(data_df_CYBZ.Data,columns=data_df_CYBZ.Times,index=data_df_CYBZ.Fields)
data_df_CYBZ=data_df_CYBZ.T
data_df_QTJY['return']=np.log(data_df_QTJY['CLOSE']/data_df_QTJY['CLOSE'].shift(1))
data_df_QTJY['index_return']=np.log(data_df_CYBZ['CLOSE']/data_df_CYBZ['CLOSE'].shift(1))
data_df_QTJY=data_df_QTJY.dropna()
data_df_QTJY['moni_return']=(data_df_QTJY['index_return']-0.03/365)*k
data_df_QTJY['ar']=data_df_QTJY['return']-data_df_QTJY['moni_return']
return data_df_QTJY
k=0.9595205659434853
df=get_ar(k=k)
def get_ar(k):
data_df_QTJY=w.wsd("300359.SZ", "close", "2019-04-01", "2019-04-15", "PriceAdj=F")
data_df_CYBZ=w.wsd("399006.SZ", "close", "2019-04-01", "2019-04-15", "PriceAdj=F")
data_df_QTJY=pd.DataFrame(data_df_QTJY.Data,columns=data_df_QTJY.Times,index=data_df_QTJY.Fields)
data_df_QTJY=data_df_QTJY.T
data_df_CYBZ=pd.DataFrame(data_df_CYBZ.Data,columns=data_df_CYBZ.Times,index=data_df_CYBZ.Fields)
data_df_CYBZ=data_df_CYBZ.T
data_df_QTJY['return']=np.log(data_df_QTJY['CLOSE']/data_df_QTJY['CLOSE'].shift(1))
data_df_QTJY['index_return']=np.log(data_df_CYBZ['CLOSE']/data_df_CYBZ['CLOSE'].shift(1))
data_df_QTJY=data_df_QTJY.dropna()
data_df_QTJY['moni_return']=(data_df_QTJY['index_return']-0.03/365)*k
data_df_QTJY['ar']=data_df_QTJY['return']-data_df_QTJY['moni_return']
return data_df_QTJY
k=0.9595205659434853
df=get_ar(k=k)
from scipy import stats
result=stats.ttest_1samp(df['ar'],0)
car=[0.0986 ,0.0933 ,0.0512 ,0.0344 ,0.1287 ,0.0957 ,0.0837 ,0.1155 ,0.0916]
print('car显著性检验',stats.ttest_1samp(car,1))
print('单样本检验结果',result)
result2=stats.ttest_ind(df['return'],df['moni_return'],equal_var=False)
print('双样本检验结果',result2)
result3=stats.levene(df['return'],df['moni_return'])
print('原假设方差不同',result3)
x=[1,2,3,4,5,6,7,8,9]
plt.plot(x,df['return'],color='blue',label='actual return')
plt.plot(x,df['moni_return'],color='black',label='semulated_return')
plt.plot(x,np.zeros(9),color='red')
plt.legend()
plt.show()
x=[1,2,3,4,5,6,7,8,9]
plt.plot(x,df['ar'],color='blue',label='ar')
car=[0.0986 ,0.0933 ,0.0512 ,0.0344 ,0.1287 ,0.0957 ,0.0837 ,0.1155 ,0.0916]
plt.plot(x,car,color='black',label='car')
plt.plot(x,np.zeros(9),color='red')
plt.legend()
plt.show()