Example #1
0
    def __init__(self, account, benchmark_code='000300', benchmark_type=MARKET_TYPE.INDEX_CN, if_fq=True, market_data=None):
        """
        account: QA_Account类/QA_PortfolioView类
        benchmark_code: [str]对照参数代码
        benchmark_type: [QA.PARAM]对照参数的市场
        if_fq: [Bool]原account是否使用复权数据
        if_fq选项是@尧提出的,关于回测的时候成交价格问题(如果按不复权撮合 应该按不复权价格计算assets)
        """
        self.account = account
        self.benchmark_code = benchmark_code  # 默认沪深300
        self.benchmark_type = benchmark_type

        self.fetch = {MARKET_TYPE.STOCK_CN: QA_fetch_stock_day_adv,
                      MARKET_TYPE.INDEX_CN: QA_fetch_index_day_adv}
        if self.account.market_type == MARKET_TYPE.STOCK_CN:
            self.market_data = QA_fetch_stock_day_adv(
                self.account.code, self.account.start_date, self.account.end_date)
        elif self.account.market_type == MARKET_TYPE.FUTURE_CN:
            self.market_data = market_data
        self.if_fq = if_fq

        if self.market_value is not None:
            self._assets = (self.market_value.sum(
                axis=1) + self.account.daily_cash.set_index('date').cash).fillna(method='pad')
        else:
            self._assets = self.account.daily_cash.set_index(
                'date').cash.fillna(method='pad')

        self.time_gap = QA_util_get_trade_gap(
            self.account.start_date, self.account.end_date)
        self.init_cash = self.account.init_cash
        self.init_assets = self.account.init_assets
Example #2
0
    def __init__(self,
                 account,
                 benchmark_code='000300',
                 benchmark_type=MARKET_TYPE.INDEX_CN,
                 if_fq=True):
        """
        if_qf选项是@尧提出的,关于回测的时候成交价格问题(如果按不复权撮合 应该按不复权价格计算assets)
        """
        self.account = account
        self.benchmark_code = benchmark_code  # 默认沪深300
        self.benchmark_type = benchmark_type

        self.fetch = {
            MARKET_TYPE.STOCK_CN: QA_fetch_stock_day_adv,
            MARKET_TYPE.INDEX_CN: QA_fetch_index_day_adv
        }
        self.market_data = QA_fetch_stock_day_adv(self.account.code,
                                                  self.account.start_date,
                                                  self.account.end_date)
        self.if_fq = if_fq

        self._assets = (self.market_value.sum(axis=1) +
                        self.account.daily_cash.set_index('date').cash).fillna(
                            method='pad')

        self.time_gap = QA_util_get_trade_gap(self.account.start_date,
                                              self.account.end_date)
        self.init_cash = self.account.init_cash
        self.init_assets = self.account.init_assets
Example #3
0
    def __init__(self,
                 account,
                 benchmark_code='000300',
                 benchmark_type=MARKET_TYPE.INDEX_CN,
                 if_fq=True,
                 market_data=None,
                 auto_reload=False):
        """
        account: QA_Account类/QA_PortfolioView类
        benchmark_code: [str]对照参数代码
        benchmark_type: [QA.PARAM]对照参数的市场
        if_fq: [Bool]原account是否使用复权数据
        if_fq选项是@尧提出的,关于回测的时候成交价格问题(如果按不复权撮合 应该按不复权价格计算assets)
        """
        self.account = account
        self.benchmark_code = benchmark_code  # 默认沪深300
        self.benchmark_type = benchmark_type
        self.client = DATABASE.risk

        self.client.create_index([("account_cookie", ASCENDING),
                                  ("user_cookie", ASCENDING),
                                  ("portfolio_cookie", ASCENDING)],
                                 unique=True)
        if auto_reload:
            pass
        else:
            self.fetch = {
                MARKET_TYPE.STOCK_CN: QA_fetch_stock_day_adv,
                MARKET_TYPE.INDEX_CN: QA_fetch_index_day_adv
            }
            if market_data == None:
                if self.account.market_type == MARKET_TYPE.STOCK_CN:
                    self.market_data = QA_fetch_stock_day_adv(
                        self.account.code, self.account.start_date,
                        self.account.end_date)
                elif self.account.market_type == MARKET_TYPE.FUTURE_CN:
                    self.market_data = QA_fetch_future_day_adv(
                        self.account.code, self.account.start_date,
                        self.account.end_date)
            else:
                self.market_data = market_data
            self.if_fq = if_fq
            if self.account.market_type == MARKET_TYPE.FUTURE_CN:
                self.if_fq = False  # 如果是期货, 默认设为FALSE

            if self.market_value is not None:
                self._assets = (
                    self.market_value.sum(axis=1) +
                    self.account.daily_cash.set_index('date').cash).fillna(
                        method='pad')
            else:
                self._assets = self.account.daily_cash.set_index(
                    'date').cash.fillna(method='pad')

            self.time_gap = QA_util_get_trade_gap(self.account.start_date,
                                                  self.account.end_date)
            self.init_cash = self.account.init_cash
            self.init_assets = self.account.init_assets
Example #4
0
    def __init__(self, account, benchmark_code='000300', benchmark_type=MARKET_TYPE.INDEX_CN):
        self.account = account
        self.benchmark_code = benchmark_code  # 默认沪深300
        self.benchmark_type = benchmark_type

        self.fetch = {MARKET_TYPE.STOCK_CN: QA_fetch_stock_day_adv,
                      MARKET_TYPE.INDEX_CN: QA_fetch_index_day_adv}
        self.market_data = QA_fetch_stock_day_adv(
            self.account.code, self.account.start_date, self.account.end_date)
        self._assets = ((self.market_data.to_qfq().pivot('close') * self.account.daily_hold).sum(
            axis=1) + self.account.daily_cash.set_index('date').cash).fillna(method='pad')

        self.time_gap = QA_util_get_trade_gap(
            self.account.start_date, self.account.end_date)
        self.init_assets = self.account.init_assets