原理
import tushare as ts
ts.set_token('你的token')
df=ts.get_k_data('399300', index=True,start='2016-01-01', end='2016-12-31')
df.head()
本接口即将停止更新,请尽快使用Pro版接口:https://tushare.pro/document/2
|
date |
open |
close |
high |
low |
volume |
code |
0 |
2016-01-04 |
3725.86 |
3470.41 |
3726.25 |
3469.01 |
115370674.0 |
sz399300 |
1 |
2016-01-05 |
3382.18 |
3478.78 |
3518.22 |
3377.28 |
162116984.0 |
sz399300 |
2 |
2016-01-06 |
3482.41 |
3539.81 |
3543.74 |
3468.47 |
145966144.0 |
sz399300 |
3 |
2016-01-07 |
3481.15 |
3294.38 |
3481.15 |
3284.74 |
44102641.0 |
sz399300 |
4 |
2016-01-08 |
3371.87 |
3361.56 |
3418.85 |
3237.93 |
185959451.0 |
sz399300 |
import numpy as np
df['r']=(df['close'] - df['close'].shift(1)) / df['close'].shift(1)
df['rtn']=np.log(df['close'] / df['close'].shift(1))
df=df.dropna()
df.head()
|
date |
open |
close |
high |
low |
volume |
code |
r |
rtn |
1 |
2016-01-05 |
3382.18 |
3478.78 |
3518.22 |
3377.28 |
162116984.0 |
sz399300 |
0.002412 |
0.002409 |
2 |
2016-01-06 |
3482.41 |
3539.81 |
3543.74 |
3468.47 |
145966144.0 |
sz399300 |
0.017544 |
0.017391 |
3 |
2016-01-07 |
3481.15 |
3294.38 |
3481.15 |
3284.74 |
44102641.0 |
sz399300 |
-0.069334 |
-0.071855 |
4 |
2016-01-08 |
3371.87 |
3361.56 |
3418.85 |
3237.93 |
185959451.0 |
sz399300 |
0.020392 |
0.020187 |
5 |
2016-01-11 |
3303.12 |
3192.45 |
3342.48 |
3192.45 |
174638387.0 |
sz399300 |
-0.050307 |
-0.051617 |
import matplotlib.pyplot as plt
from statsmodels.graphics.tsaplots import plot_acf
%matplotlib inline
fig = plt.figure(figsize=(10,5))
ax1=fig.add_subplot(111)
fig = plot_acf(df['rtn'],ax=ax1,lags=50)
plt.show()
1阶的自相关系数为负值,2阶自相关系数为正值,如果从股票现象来分析的话,今天股票和昨天股票的涨跌和昨天股票涨跌成负相关,今天和前天成正相关,理论上来说是比较何理的,比如昨天涨了,但是今天动力不足,或者利好出货,很有可能下跌,然后明天再次卷土重来,当然要得出这个结论还需要进行很多验证才行,但是感觉上来说,是成立的。