第四章 规则策略--通道突破策略

一、原理

该策略一般使用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)

三、结果展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值