[增强] 增加实时CCI指标计算

This commit is contained in:
msincenselee 2020-03-25 16:40:38 +08:00
parent 48f78cce8f
commit 4b33a59aa8

View File

@ -30,6 +30,7 @@ from vnpy.trader.object import BarData, TickData
from vnpy.trader.constant import Interval, Color
from vnpy.trader.utility import round_to, get_trading_date, get_underlying_symbol
def get_cta_bar_type(bar_name: str):
"""根据名称返回K线类型和K线周期"""
@ -50,6 +51,7 @@ def get_cta_bar_type(bar_name: str):
return CtaDayBar, int(interval)
raise Exception(u'{}参数错误'.format(bar_name))
def get_cta_bar_class(bar_type: str):
"""根据类型名获取对象"""
assert isinstance(bar_type, str)
@ -553,6 +555,8 @@ class CtaLineBar(object):
# K 线的CCI计算数据
self.line_cci = []
self.cur_cci = None
self._rt_cci = None
# 卡尔曼过滤器
self.kf = None
@ -2961,11 +2965,35 @@ class CtaLineBar(object):
cur_cci = ta.CCI(high=self.high_array[-2 * self.para_cci_len:], low=self.low_array[-2 * self.para_cci_len:],
close=self.close_array[-2 * self.para_cci_len:], timeperiod=self.para_cci_len)[-1]
cur_cci = round(float(cur_cci), 3)
self.cur_cci = round(float(cur_cci), 3)
if len(self.line_cci) > self.max_hold_bars:
del self.line_cci[0]
self.line_cci.append(cur_cci)
self.line_cci.append(self.cur_cci)
def rt_count_cci(self):
"""实时计算CCI值"""
if self.para_cci_len <= 0:
return
# 1、lineBar满足长度才执行计算
if len(self.line_bar) < self.para_cci_len + 2:
self.write_log(u'数据未充分,当前Bar数据数量{0}计算CCI需要{1}'.
format(len(self.line_bar), self.para_cci_len + 2))
return
self._rt_cci = ta.CCI(high=np.append(self.high_array[-2 * self.para_cci_len:], [self.line_bar[-1].high_price]),
low=np.append(self.low_array[-2 * self.para_cci_len:], [self.line_bar[-1].low_price]),
close=np.append(self.close_array[-2 * self.para_cci_len:],
[self.line_bar[-1].close_price]),
timeperiod=self.para_cci_len)[-1]
@property
def rt_cci(self):
self.check_rt_funcs(self.rt_count_cci)
if self._rt_cci is None:
return self.cur_cci
return self._rt_cci
def __count_kf(self):
"""计算卡尔曼过滤器均线"""
@ -3514,12 +3542,10 @@ class CtaLineBar(object):
return
# 收盘价 = 结算bar + 最后一个未结束得close
close_list = self.close_array[-data_len:]
close_list.append(self.line_bar[-1].close_price)
# close_list = [x.close_price for x in self.lineBar[-data_len:]]
close_array = np.append(self.close_array[-data_len:], [self.line_bar[-1].close_price])
# 计算最后得动态RSI值
last_rsi = ta.RSI(self.close_array[-2 * self.para_skd_fast_len:], self.para_skd_fast_len)[-1]
last_rsi = ta.RSI(close_array[-2 * self.para_skd_fast_len:], self.para_skd_fast_len)[-1]
# 所有RSI值长度不足计算标准
if len(self.line_skd_rsi) < self.para_skd_slow_len: