1.导入函数
import numpy as np import pandas as pd import matplotlib.pyplot as plt import tushare as ts import math
2. 数据获取
data = ts.get_hist_data(‘000012‘,start=‘2015-06-23‘,end=‘2017-11-16‘)
3.移动平均值
# 滚动窗口的使用data[‘42d‘]= pd.rolling_mean(data[‘close‘],window=42)data[‘252d‘] =pd.rolling_mean(data[‘close‘],window=252)print(data[[‘close‘,‘42d‘,‘252d‘]].tail()) data[[‘close‘,‘42d‘,‘252d‘]].plot(figsize=(8,5))plt.show()
4.移动历史波动
data[‘return‘]=np.log(data[‘close‘]/data[‘close‘].shift(1)) data[‘mov_vol‘] = pd.rolling_std(data[‘return‘],window=252)*math.sqrt(252) data[[‘close‘,‘return‘,‘mov_vol‘]].plot(subplots=True,style=‘b‘,figsize=(8,7)) plt.show()
时间: 2024-11-14 12:01:48