Exemplo n.º 1
0
    def __QA_backtest_prepare(self):
        """
        这是模型内部的 初始化,主要是初始化一些账户和市场资产
        写成了私有函数
        @yutiansut
        2017/7/20
        """
        if len(str(self.strategy_start_date))==10:
            self.strategy_start_time=str(self.strategy_start_date)+' 15:00:00'
        elif len(str(self.strategy_start_date))==19:
            self.strategy_start_time=str(self.strategy_start_date)
            self.strategy_start_date=str(self.strategy_start_date)[0:10]
        else:
            QA_util_log_info('Wrong start date format')

        if len(str(self.strategy_end_date))==10:
            self.strategy_end_time=str(self.strategy_end_date)+' 15:00:00'
        elif len(str(self.strategy_end_date))==19:
            self.strategy_end_time=str(self.strategy_end_date)
            self.strategy_end_date=str(self.strategy_end_date)[0:10]
        else:
            QA_util_log_info('Wrong end date format')
        # 重新初始账户资产
        self.market = QA_Market(self.commission_fee_coeff)
        self.setting.QA_setting_init()
        self.account.init()
        self.start_real_date = QA_util_get_real_date(
            self.strategy_start_date, self.trade_list, 1)
        self.start_real_time=str(self.start_real_date)+' '+self.strategy_start_time.split(' ')[1]
        self.start_real_id = self.trade_list.index(self.start_real_date)
        self.end_real_date = QA_util_get_real_date(
            self.strategy_end_date, self.trade_list, -1)
        self.end_real_id = self.trade_list.index(self.end_real_date)
        self.end_real_time=str(self.end_real_date)+' '+self.strategy_end_time.split(' ')[1]
        # 重新初始化账户的cookie
        self.account.account_cookie = str(random.random())
        # 初始化股票池的市场数据
        if self.benchmark_type in ['I','index']:
            self.benchmark_data = QA_fetch_index_day_adv(
                self.benchmark_code, self.start_real_date, self.end_real_date)
        elif self.benchmark_type in ['S','stock']:
            self.benchmark_data = QA_fetch_stock_day_adv(
                self.benchmark_code, self.start_real_date, self.end_real_date)
        if self.backtest_type in ['day', 'd', '0x00']:
            self.market_data = QA_fetch_stocklist_day_adv(
                self.strategy_stock_list, self.trade_list[self.start_real_id - int(
                    self.strategy_gap+1)], self.trade_list[self.end_real_id]).to_qfq()

        elif self.backtest_type in ['1min', '5min', '15min', '30min', '60min']:
            self.market_data = QA_fetch_stocklist_min_adv(
                self.strategy_stock_list, QA_util_time_gap(self.start_real_time,self.strategy_gap+1,'<',self.backtest_type),
                QA_util_time_gap(self.end_real_time,1,'>',self.backtest_type), self.backtest_type).to_qfq()

        elif self.backtest_type in ['index_day']:
            self.market_data = QA_fetch_index_day_adv(self.strategy_stock_list, self.trade_list[self.start_real_id - int(
                self.strategy_gap+1)], self.end_real_date)

        elif self.backtest_type in ['index_1min', 'index_5min', 'index_15min', 'index_30min', 'index_60min']:
            self.market_data = QA_fetch_index_min_adv(
                self.strategy_stock_list, QA_util_time_gap(self.start_real_time,self.strategy_gap+1,'<',self.backtest_type.split('_')[1]),  QA_util_time_gap(self.end_real_time,1,'>',self.backtest_type.split('_')[1]), self.backtest_type.split('_')[1])
Exemplo n.º 2
0
    def __QA_backtest_prepare(self):
        """
        这是模型内部的 初始化,主要是初始化一些账户和市场资产
        写成了私有函数
        @yutiansut
        2017/7/20
        """

        self.strategy_stock_list = np.unique(
            self.strategy_stock_list).tolist()  # 保证不会重复
        if len(str(self.strategy_start_date)) == 10:
            self.strategy_start_time = str(
                self.strategy_start_date) + ' 15:00:00'
        elif len(str(self.strategy_start_date)) == 19:
            self.strategy_start_time = str(self.strategy_start_date)
            self.strategy_start_date = str(self.strategy_start_date)[0:10]
        else:
            self.__QA_backtest_log_info(self, 'Wrong start date format')

        if len(str(self.strategy_end_date)) == 10:
            self.strategy_end_time = str(self.strategy_end_date) + ' 15:00:00'
        elif len(str(self.strategy_end_date)) == 19:
            self.strategy_end_time = str(self.strategy_end_date)
            self.strategy_end_date = str(self.strategy_end_date)[0:10]
        else:
            self.__QA_backtest_log_info(self, 'Wrong end date format')
        # 重新初始账户资产
        self.market = QA_Market(self.commission_fee_coeff)
        self.setting.QA_setting_init()
        self.account.init()
        self.account_d_value.append(self.account.init_assest)
        self.start_real_date = QA_util_get_real_date(self.strategy_start_date,
                                                     self.trade_list, 1)
        self.start_real_time = str(
            self.start_real_date) + ' ' + self.strategy_start_time.split(
                ' ')[1]
        self.start_real_id = self.trade_list.index(self.start_real_date)
        self.end_real_date = QA_util_get_real_date(self.strategy_end_date,
                                                   self.trade_list, -1)
        self.end_real_id = self.trade_list.index(self.end_real_date)
        self.end_real_time = str(self.end_real_date) + \
            ' ' + self.strategy_end_time.split(' ')[1]
        # 重新初始化账户的cookie
        self.account.account_cookie = str(random.random())
        # 初始化股票池的市场数据
        if self.benchmark_type in ['I', 'index']:
            self.benchmark_data = QA_fetch_index_day_adv(
                self.benchmark_code, self.trade_list[self.start_real_id - 1],
                self.end_real_date)
        elif self.benchmark_type in ['S', 'stock']:
            self.benchmark_data = QA_fetch_stock_day_adv(
                self.benchmark_code, self.trade_list[self.start_real_id - 1],
                self.end_real_date)
        if self.backtest_type in ['day', 'd', '0x00']:
            self.market_data = QA_fetch_stocklist_day_adv(
                self.strategy_stock_list,
                self.trade_list[self.start_real_id -
                                int(self.strategy_gap + 1)],
                self.trade_list[self.end_real_id]).to_qfq()

        elif self.backtest_type in ['1min', '5min', '15min', '30min', '60min']:
            self.market_data = QA_fetch_stocklist_min_adv(
                self.strategy_stock_list,
                QA_util_time_gap(self.start_real_time, self.strategy_gap + 1,
                                 '<', self.backtest_type),
                QA_util_time_gap(self.end_real_time, 1, '>',
                                 self.backtest_type),
                self.backtest_type).to_qfq()

        elif self.backtest_type in ['index_day']:
            self.market_data = QA_fetch_index_day_adv(
                self.strategy_stock_list,
                self.trade_list[self.start_real_id -
                                int(self.strategy_gap + 1)],
                self.end_real_date)

        elif self.backtest_type in [
                'index_1min', 'index_5min', 'index_15min', 'index_30min',
                'index_60min'
        ]:
            self.market_data = QA_fetch_index_min_adv(
                self.strategy_stock_list,
                QA_util_time_gap(self.start_real_time, self.strategy_gap + 1,
                                 '<',
                                 self.backtest_type.split('_')[1]),
                QA_util_time_gap(self.end_real_time, 1, '>',
                                 self.backtest_type.split('_')[1]),
                self.backtest_type.split('_')[1])
        self.market_data_dict = dict(
            zip(list(self.market_data.code), self.market_data.splits()))
        self.market_data_hashable = self.market_data.dicts
        self.dirs = '.{}QUANTAXIS_RESULT{}{}{}{}{}'.format(
            os.sep, os.sep, self.topic_name, os.sep, self.stratey_version,
            os.sep)
        os.makedirs(self.dirs, exist_ok=True)