报错:'NoneType' object is not subscriptable
由zzsy80创建,最终由zzsy80 被浏览 45 用户
克隆了原来的 【研究】隐马尔科夫模型(HMM)的择时应用 只是在原来的基础上调整了取数的周期,策略代码如下:
from hmmlearn.hmm import GaussianHMM import datetime import numpy as np import pandas as pd import seaborn as sns import matplotlib.pyplot as plt from matplotlib import cm from matplotlib import pyplot
读取沪深300历史数据
df = DataSource('bar1d_CN_STOCK_A').read(instruments=['000300.HIX'],start_date='2022-01-01', end_date='2022-04-22', fields=['open', 'high', 'low', 'close','volume'])
数据准备
close = df['close'] high = df['high'][5:] low = df['low'][5:] volume = df['volume'][5:] money = df['volume'][5:] datelist = df['date'][5:]
计算当日对数收益率,五日对数收益率,当日对数高低价差
logreturn = (np.log(np.array(close[1:]))-np.log(np.array(close[:-1])))[4:] logreturn5 = np.log(np.array(close[5:]))-np.log(np.array(close[:-5])) diffreturn = (np.log(np.array(high))-np.log(np.array(low))) closeidx = close[5:]
训练并完成预测
X = np.column_stack([logreturn,diffreturn,logreturn5]) hmm = GaussianHMM(n_components = 6, covariance_type='diag',n_iter = 5000).fit(X) latent_states_sequence = hmm.predict(X)
绘制市场状态序列
sns.set_style('white') plt.figure(figsize = (15, 8)) for i in range(hmm.n_components): state = (latent_states_sequence == i) plt.plot(datelist[state],closeidx[state],'.',label = 'latent state %d'%i,lw = 1) plt.legend() plt.grid(1)
报错信息如下:
TypeError Traceback (most recent call last) <ipython-input-5-52711e1b3fcd> in <module> 10 df = DataSource('bar1d_CN_STOCK_A').read(instruments=['000300.HIX'],start_date='2022-01-01', end_date='2022-04-22', fields=['open', 'high', 'low', 'close','volume']) 11 # 数据准备 ---> 12 close = df['close'] 13 high = df['high'][5:] 14 low = df['low'][5:]
TypeError: 'NoneType' object is not subscriptable
请问如何解决?