一、原理
该策略一般使用20周期内的最高价和最低价来显示市场价格的波动性,当其通道窄时表示市场波动较小,反之通道宽则表示市场波动比较大。
当价格冲破该通道的上轨道时,就是可能的买入信号;反之,冲破下轨时就是可能的卖出信号。
唐奇安通道的各项指标的计算方法为:
上轨 = Max(最高低,n), n日最高价的最大值
下轨 = Min(最低价,n), n日最低价的最小值
二、代码
def tang_channel_strategy(self, stock_dict, period='day', output_dir='.'):
for code, stock_list in stock_dict.items():
high_list, low_list, close_list = [], [], [] # 最高价,最低价,收盘价
for stock in stock_list:
high_list.append(stock.high)
low_list.append(stock.low)
close_list.append(stock.close)
# 计算涨幅
fluct_list = self.fluctuate(close_list)
# 指标
up_line = talib.MAX(np.array(high_list), timeperiod=20)
down_line = talib.MIN(np.array(low_list), timeperiod=20)
# 重新组装数据
rs_list, last_date = [], None
for i in range(1, len(stock_list)):
stock = stock_list[i]
color = 'grey'
if stock.close > self.pe(up_line[i - 1]):
color = 'red'
elif stock.close < self.pe(down_line[i - 1]):
color = 'green'
element = [stock.dt, stock.open, stock.close, stock.high, stock.low, stock.vol,
fluct_list[i], color, self.pe(up_line[i - 1]), self.pe(down_line[i - 1])]
rs_list.append(element)
last_date = stock.dt
# 增加尾部占位,用于页面展示
for i in range(1, 5):
last_date = DateUtil.getAfterAmDate(last_date, '%Y-%m-%d', i)
element = [last_date, '-', '-', '-', '-', '-','-', '-', '-', '-', '-', '-', '-']
rs_list.append(element)
# 转化为json格式输出
ar = AjaxResult()
json_rs = ar.succResult('data', rs_list)
json_file = output_dir + code + '_' + period + '.json'
self.write_file(json_rs, json_file)
self._logger.info('tang channel json file ' + json_file)