Example #1
0
    def compare(self):
        '''比较同一指标下的六个策略'''
        show_period = 120
        
        #global lt,wan,qian,bai,shi,ge,zbname,tf
        logging.info('开始:', time.asctime())
        t0 = time.time()
        
        series = self.get_series()
        close = np.cumsum(series).astype(float)
        
        fig, axes = plt.subplots(7, 1, sharex=True)
        #ax1, ax2, ax3, ax4 = axes[0], axes[1], axes[2], axes[3]
        
        
        # 布林线
        upperband, middleband, lowerband = ta.BBANDS(close, timeperiod=5, nbdevup=2, nbdevdn=2, matype=0)
        axes[0].plot(close[-show_period:], 'rd-', markersize = 5)
        axes[0].plot(upperband[-show_period:], 'y-')
        axes[0].plot(middleband[-show_period:], 'b-')
        axes[0].plot(lowerband[-show_period:], 'y-')
        title1 = '指标:' + self.wei + '位 ' + self.zbname.get() + ' 同反:' + str(self.tf.get())
        axes[0].set_title(title1, fontproperties="SimHei")
        
        stratename = ['金叉死叉','通道宽度','顺势而为','KDJ极限','随机漫步','正弦变换']
        '''strategies = {
            '金叉死叉': strategy_gold_die,
            '通道宽度': strategy_bandwidth,
            '顺势而为': strategy_ma,
            'KDJ极限': strategy_kdj,
            '随机漫步': strategy_rnd,
            '正弦变换': strategy_ht_sine,
        }'''
        order1, earn1, ma_fast, ma_slow = strategy.strategy_gold_die(series, is_backtest=True)
        order2, earn2, bandwidth, mean = strategy.strategy_bandwidth(series, is_backtest=True)
        order3, earn3, ma = strategy.strategy_ma(series, is_backtest=True)
        order4, earn4, fastk, fastd = strategy.strategy_kdj(series, is_backtest=True)
        order5, earn5, rnd = strategy.strategy_rnd(series, is_backtest=True)
        order6, earn6, sine, leadsine = strategy.strategy_ht_sine(series, is_backtest=True)
        for ax, order, name, c in zip(axes[1:],[order1, order2, order3, order4, order5, order6], stratename, 'bgbgbg'):
            ax.plot(order[-show_period:], c+'-')
            ax.set_ylabel(name, fontproperties="SimHei")
            ax.set_ylim([0, 243])

        t = time.time() - t0
        logging.info('结束', time.asctime())
        logging.info('耗时:{}分{}秒'.format(int(t)//60, int(t)%60))
        
        multi = MultiCursor(fig.canvas, (axes[0], axes[1], axes[2], axes[3], axes[4], axes[5], axes[6]), color='b', lw=2)
        
        plt.show()
Example #2
0
    def _use_strategy(self, strategy_name, is_backtest):
        '''12期均线与20期均线交点作为入场信号'''
        #global lt,wan,qian,bai,shi,ge,zbname,tf,ml_name

        show_period = 120

        w, q, b, s, g = self.wan.get(), self.qian.get(), self.bai.get(
        ), self.shi.get(), self.ge.get()
        name, pre_n = self.zbname.get(), self.tf.get()
        loc = ''.join((w, q, b, s, g))
        series = self.get_series(loc, name, pre_n)
        close = np.cumsum(series).astype(float)

        if is_backtest:
            print('开始:', time.asctime())
            t0 = time.time()

            fig, axes = plt.subplots(4, 1, sharex=True)
            ax1, ax2, ax3, ax4 = axes[0], axes[1], axes[2], axes[3]

            # 布林线
            upperband, middleband, lowerband = ta.BBANDS(close,
                                                         timeperiod=5,
                                                         nbdevup=2,
                                                         nbdevdn=2,
                                                         matype=0)
            axes[0].plot(close[-show_period:], 'rd-', markersize=5)
            axes[0].plot(upperband[-show_period:], 'y-')
            axes[0].plot(middleband[-show_period:], 'b-')
            axes[0].plot(lowerband[-show_period:], 'y-')

            if strategy_name == '金叉死叉':  # <===============================>
                order, earn, ma_fast, ma_slow = strategy.strategy_gold_die(
                    series, is_backtest=True)
                axes[1].plot(ma_fast[-show_period:], 'g-')
                axes[1].plot(ma_slow[-show_period:], 'r-')
            elif strategy_name == '均线差分':
                order, earn, ma_fast, ma_slow = strategy.strategy_mean_diff(
                    series, is_backtest=True)
                axes[1].plot(ma_fast[-show_period:], 'g-')
                axes[1].plot(ma_slow[-show_period:], 'r-')
            elif strategy_name == '通道宽度':  # <===============================>
                order, earn, bandwidth, mean = strategy.strategy_bandwidth(
                    series, is_backtest=True)
                axes[1].plot(bandwidth[-show_period:], 'r-')
                axes[1].plot([0, show_period], [mean, mean], 'k', alpha=.2)
            elif strategy_name == '顺势而为':  # <===============================>
                order, earn, ma = strategy.strategy_ma(series,
                                                       is_backtest=True)
                axes[1].plot(ma[-show_period:], 'r-')
            elif strategy_name == 'KDJ极限':  # <===============================>
                order, earn, fastk, fastd = strategy.strategy_kdj(
                    series, is_backtest=True)
                axes[1].plot(fastk[-show_period:], 'r-')
                axes[1].plot(fastd[-show_period:], 'g-')
                axes[1].plot([0, show_period], [5, 5], 'k', alpha=.2)
                axes[1].plot([0, show_period], [95, 95], 'k', alpha=.2)
            elif strategy_name == 'KDJ绿线':  # <===============================>
                order, earn, fastk, fastd = strategy.strategy_kdj2(
                    series, is_backtest=True)
                axes[1].plot(fastk[-show_period:], 'r-')
                axes[1].plot(fastd[-show_period:], 'g-')
                axes[1].plot([0, show_period], [5, 5], 'k', alpha=.2)
                axes[1].plot([0, show_period], [95, 95], 'k', alpha=.2)
            elif strategy_name == '随机漫步':  # <===============================>
                order, earn, rnd = strategy.strategy_rnd(series,
                                                         is_backtest=True)
                axes[1].plot(rnd[-show_period:], 'ro-', markersize=2)
            elif strategy_name == '正弦变换':  # <===============================>
                order, earn, sine, leadsine = strategy.strategy_ht_sine(
                    series, is_backtest=True)
                axes[1].plot(sine[-show_period:], 'r-')
                axes[1].plot(leadsine[-show_period:], 'g-')
            elif strategy_name == '正弦变换2':  # <===============================>
                order, earn, sine, leadsine = strategy.strategy_ht_sine2(
                    series, is_backtest=True)
                axes[1].plot(sine[-show_period:], 'r-')
                axes[1].plot(leadsine[-show_period:], 'g-')
            elif strategy_name == '机器学习':  # <===============================>
                order, earn, ml = strategy.strategy_ml(series,
                                                       self.ml_name.get(),
                                                       is_backtest=True)
                axes[1].plot(ml[-show_period:], 'ro-', markersize=2)
            elif strategy_name == '三个综合':  # <===============================>
                order, earn, signals = strategy.strategy_three(
                    series, is_backtest=True)
                axes[1].plot(signals[-show_period:], 'ro-', markersize=2)
            elif strategy_name == '五个综合':  # <===============================>
                order, earn, signals = strategy.strategy_five(series,
                                                              is_backtest=True)
                axes[1].plot(signals[-show_period:], 'ro-', markersize=2)
            elif strategy_name == '敬请期待':  # <===============================>
                pass

            axes[2].plot(order[-show_period:], 'r-')
            #axes[3].plot(np.cumsum(earn[-show_period:])-np.cumsum(order[-show_period:]), 'g-')
            axes[3].fill_between(np.arange(show_period),
                                 0,
                                 np.cumsum(earn[-show_period:]) -
                                 np.cumsum(order[-show_period:]),
                                 facecolor="#00FF00",
                                 alpha=.7)

            title1 = '指标:' + loc + '位 ' + name + ' 同反:' + str(pre_n)
            title2 = '策略:' + strategy_name
            if strategy_name == '机器学习':
                title2 += '--' + self.ml_name.get()
            axes[0].set_title(title1, fontproperties="SimHei")
            axes[1].set_title(title2, fontproperties="SimHei")
            axes[2].set_title('投注', fontproperties="SimHei")
            axes[3].set_title('收益', fontproperties="SimHei")

            axes[3].set_xticks(range(120))  # 120个数据
            axes[3].set_xticklabels(self.get_labels(self.lt.last_period),
                                    rotation=35)  # 设置x轴标签(13个)
            axes[3].xaxis.set_major_locator(MultipleLocator(10))  # 主坐标:间隔10

            t = time.time() - t0
            print('结束', time.asctime())
            print('耗时:{}分{}秒'.format(int(t) // 60, int(t) % 60))

            multi = MultiCursor(fig.canvas,
                                (axes[0], axes[1], axes[2], axes[3]),
                                color='b',
                                lw=2)

            plt.show()

        else:
            if strategy_name == '金叉死叉':  # <===============================>
                signal = strategy.strategy_gold_die(series, is_backtest=False)
            elif strategy_name == '均线差分':
                signal = strategy.strategy_mean_diff(series, is_backtest=False)
            elif strategy_name == '通道宽度':  # <===============================>
                signal = strategy.strategy_bandwidth(series, is_backtest=False)
            elif strategy_name == '顺势而为':  # <===============================>
                signal = strategy.strategy_ma(series, is_backtest=False)
            elif strategy_name == 'KDJ极限':  # <===============================>
                signal = strategy.strategy_kdj(series, is_backtest=False)
            elif strategy_name == 'KDJ绿线':  # <===============================>
                signal = strategy.strategy_kdj2(series, is_backtest=False)
            elif strategy_name == '随机漫步':  # <===============================>
                signal = strategy.strategy_rnd(series, is_backtest=False)
            elif strategy_name == '正弦变换':  # <===============================>
                signal = strategy.strategy_ht_sine(series, is_backtest=False)
            elif strategy_name == '正弦变换2':  # <===============================>
                signal = strategy.strategy_ht_sine2(series, is_backtest=False)
            elif strategy_name == '机器学习':  # <===============================>
                signal = strategy.strategy_ml(series,
                                              self.ml_name.get(),
                                              is_backtest=False)
            elif strategy_name == '三个综合':  # <===============================>
                signal = strategy.strategy_three(series, is_backtest=False)
            elif strategy_name == '五个综合':  # <===============================>
                signal = strategy.strategy_five(series, is_backtest=False)
            elif strategy_name == '敬请期待':  # <===============================>
                pass