Exemplo n.º 1
0
    def set_env(self, environment):
        self._env = environment

        config = environment.config

        for mod_name in config.mod.__dict__:
            mod_config = getattr(config.mod, mod_name)
            if not mod_config.enabled:
                continue
            self._mod_list.append((mod_name, mod_config))

        for idx, (mod_name, user_mod_config) in enumerate(self._mod_list):
            if hasattr(user_mod_config, 'lib'):
                lib_name = user_mod_config.lib
            elif mod_name in SYSTEM_MOD_LIST:
                lib_name = "rqalpha.mod.rqalpha_mod_" + mod_name
            else:
                lib_name = "rqalpha_mod_" + mod_name
            system_log.debug(_(u"loading mod {}").format(lib_name))
            mod_module = import_mod(lib_name)
            if mod_module is None:
                del self._mod_list[idx]
                return
            mod = mod_module.load_mod()

            mod_config = RqAttrDict(
                copy.deepcopy(getattr(mod_module, "__config__", {})))
            mod_config.update(user_mod_config)
            setattr(config.mod, mod_name, mod_config)
            self._mod_list[idx] = (mod_name, mod_config)
            self._mod_dict[mod_name] = mod

        self._mod_list.sort(key=lambda item: getattr(item[1], "priority", 100))
        environment.mod_dict = self._mod_dict
Exemplo n.º 2
0
    def set_env(self, environment):
        self._env = environment

        config = environment.config
        #取出config配置详情,如果启用某模块,则将模块名和配置放置到属性_mod_list中
        for mod_name in config.mod.__dict__:
            mod_config = getattr(config.mod, mod_name)
            if not mod_config.enabled:
                continue
            self._mod_list.append((mod_name, mod_config))#每个模块,0:mod_name,1:mod_config
        # 读取每个mod的模块位置,并导入
        for idx, (mod_name, user_mod_config) in enumerate(self._mod_list):
            if hasattr(user_mod_config, 'lib'):
                lib_name = user_mod_config.lib
            elif mod_name in SYSTEM_MOD_LIST:
                lib_name = "rqalpha.mod.rqalpha_mod_" + mod_name
            else:
                lib_name = "rqalpha_mod_" + mod_name
            system_log.debug(_(u"loading mod {}").format(lib_name))
            mod_module = import_mod(lib_name)
            if mod_module is None:
                del self._mod_list[idx]
                return
            mod = mod_module.load_mod()
            #处理mod配置信息,使用用户配置覆盖默认配置
            mod_config = RqAttrDict(copy.deepcopy(getattr(mod_module, "__config__", {})))
            mod_config.update(user_mod_config)
            setattr(config.mod, mod_name, mod_config)
            self._mod_list[idx] = (mod_name, mod_config)#更新 list 容器
            self._mod_dict[mod_name] = mod#更新 dict 容器

        self._mod_list.sort(key=lambda item: getattr(item[1], "priority", 100))
        environment.mod_dict = self._mod_dict#把mod绑定到环境变量
Exemplo n.º 3
0
def run(**kwargs):
    config_path = kwargs.get('config_path', None)
    if config_path is not None:
        config_path = os.path.abspath(config_path)
        kwargs.pop('config_path')
    if not kwargs.get('base__securities', None):
        kwargs.pop('base__securities', None)

    from rqalpha import main
    source_code = kwargs.get("base__source_code")
    cfg = parse_config(kwargs,
                       config_path=config_path,
                       click_type=True,
                       source_code=source_code)
    source_code = cfg.base.source_code
    results = main.run(cfg, source_code=source_code)

    # store results into ipython when running in ipython
    from rqalpha.utils import is_run_from_ipython
    if results is not None and is_run_from_ipython():
        import IPython
        from rqalpha.utils import RqAttrDict
        ipy = IPython.get_ipython()
        report = results.get("sys_analyser", {})
        ipy.user_global_ns["results"] = results
        ipy.user_global_ns["report"] = RqAttrDict(report)

    if results is None:
        return 1
    def events(self, start_date, end_date, frequency):

        if not self._mod_config.all_day:
            while datetime.now().date() < start_date - timedelta(days=1):
                continue

        mark_time_thread = Thread(target=self.mark_time_period,
                                  args=(start_date,
                                        date.fromtimestamp(2147483647)))
        mark_time_thread.setDaemon(True)
        mark_time_thread.start()
        while True:
            if self._time_period == TimePeriod.BEFORE_TRADING:
                if self._after_trading_processed:
                    self._after_trading_processed = False
                if not self._before_trading_processed:
                    system_log.debug("VNPYEventSource: before trading event")
                    yield Event(EVENT.BEFORE_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now() + timedelta(days=1))
                    self._before_trading_processed = True
                    continue
                else:
                    continue
            elif self._time_period == TimePeriod.TRADING:
                if not self._before_trading_processed:
                    system_log.debug("VNPYEventSource: before trading event")
                    yield Event(EVENT.BEFORE_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now() + timedelta(days=1))
                    self._before_trading_processed = True
                    continue
                else:
                    tick = self._engine.get_tick()
                    calendar_dt = tick['datetime']
                    if calendar_dt.hour > 20:
                        trading_dt = calendar_dt + timedelta(days=1)
                    else:
                        trading_dt = calendar_dt
                    system_log.debug("VNPYEventSource: tick {}", tick)
                    yield Event(EVENT.TICK,
                                calendar_dt=calendar_dt,
                                trading_dt=trading_dt,
                                tick=RqAttrDict(tick))
            elif self._time_period == TimePeriod.AFTER_TRADING:
                if self._before_trading_processed:
                    self._before_trading_processed = False
                if not self._after_trading_processed:
                    system_log.debug("VNPYEventSource: after trading event")
                    yield Event(EVENT.AFTER_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now())
                    self._after_trading_processed = True
                else:
                    continue
Exemplo n.º 5
0
    def init_fixture(self):
        from rqalpha.utils import RqAttrDict
        from rqalpha.environment import Environment

        super(EnvironmentFixture, self).init_fixture()
        self.env = Environment(RqAttrDict(self.env_config))
Exemplo n.º 6
0
def parse_config(config_args,
                 config_path=None,
                 click_type=False,
                 source_code=None,
                 user_funcs=None):
    conf = default_config()
    deep_update(user_config(), conf)
    deep_update(project_config(), conf)
    if config_path is not None:
        deep_update(load_yaml(config_path), conf)

    if 'base__strategy_file' in config_args and config_args[
            'base__strategy_file']:
        # FIXME: ugly, we need this to get code
        conf['base']['strategy_file'] = config_args['base__strategy_file']
    elif ('base' in config_args and 'strategy_file' in config_args['base']
          and config_args['base']['strategy_file']):
        conf['base']['strategy_file'] = config_args['base']['strategy_file']

    if user_funcs is None:
        for k, v in code_config(conf, source_code).items():
            if k in conf['whitelist']:
                deep_update(v, conf[k])

    mod_configs = config_args.pop('mod_configs', [])
    for k, v in mod_configs:
        key = 'mod__{}'.format(k.replace('.', '__'))
        config_args[key] = mod_config_value_parse(v)

    if click_type:
        for k, v in config_args.items():
            # click multiple=True时传入tuple类型 无输入时为tuple()
            if v is None or (v == tuple()):
                continue
            if k == 'base__accounts' and not v:
                continue

            key_path = k.split('__')
            sub_dict = conf
            for p in key_path[:-1]:
                if p not in sub_dict:
                    sub_dict[p] = {}
                sub_dict = sub_dict[p]
            sub_dict[key_path[-1]] = v
    else:
        deep_update(config_args, conf)

    config = RqAttrDict(conf)

    set_locale(config.extra.locale)

    def _to_date(v):
        return pd.Timestamp(v).date()

    config.base.start_date = _to_date(config.base.start_date)
    config.base.end_date = _to_date(config.base.end_date)

    if config.base.data_bundle_path is None:
        config.base.data_bundle_path = os.path.join(
            os.path.expanduser(rqalpha_path), "bundle")

    config.base.run_type = parse_run_type(config.base.run_type)
    config.base.accounts = parse_accounts(config.base.accounts)
    config.base.init_positions = parse_init_positions(
        config.base.init_positions)
    config.base.persist_mode = parse_persist_mode(config.base.persist_mode)
    config.base.future_info = parse_future_info(config.base.future_info)

    if config.extra.context_vars:
        if isinstance(config.extra.context_vars, six.string_types):
            config.extra.context_vars = json.loads(config.extra.context_vars)

    if config.base.frequency == "1d":
        logger.DATETIME_FORMAT = "%Y-%m-%d"

    return config
Exemplo n.º 7
0
def parse_config(config_args,
                 config_path=None,
                 click_type=False,
                 source_code=None,
                 user_funcs=None):
    conf = default_config()
    deep_update(user_config(), conf)
    deep_update(project_config(), conf)

    if 'base__strategy_file' in config_args and config_args[
            'base__strategy_file']:
        # FIXME: ugly, we need this to get code
        conf['base']['strategy_file'] = config_args['base__strategy_file']
    elif ('base' in config_args and 'strategy_file' in config_args['base']
          and config_args['base']['strategy_file']):
        conf['base']['strategy_file'] = config_args['base']['strategy_file']

    if user_funcs is None:
        for k, v in six.iteritems(code_config(conf, source_code)):
            if k in conf['whitelist']:
                deep_update(v, conf[k])

    mod_configs = config_args.pop('mod_configs', [])
    for k, v in mod_configs:
        key = 'mod__{}'.format(k.replace('.', '__'))
        config_args[key] = mod_config_value_parse(v)

    if click_type:
        for k, v in six.iteritems(config_args):
            if v is None:
                continue
            if k == 'base__accounts' and not v:
                continue

            key_path = k.split('__')
            sub_dict = conf
            for p in key_path[:-1]:
                if p not in sub_dict:
                    sub_dict[p] = {}
                sub_dict = sub_dict[p]
            sub_dict[key_path[-1]] = v
    else:
        deep_update(config_args, conf)

    config = RqAttrDict(conf)

    set_locale(config.extra.locale)

    def _to_date(v):
        return pd.Timestamp(v).date()

    config.base.start_date = _to_date(config.base.start_date)
    config.base.end_date = _to_date(config.base.end_date)

    if config.base.data_bundle_path is None:
        config.base.data_bundle_path = os.path.join(
            os.path.expanduser(rqalpha_path), "bundle")

    config.base.run_type = parse_run_type(config.base.run_type)
    config.base.accounts = parse_accounts(config.base.accounts)
    config.base.persist_mode = parse_persist_mode(config.base.persist_mode)

    if config.extra.context_vars:
        if isinstance(config.extra.context_vars, six.string_types):
            config.extra.context_vars = json.loads(
                to_utf8(config.extra.context_vars))

    system_log.level = getattr(logbook, config.extra.log_level.upper(),
                               logbook.NOTSET)
    std_log.level = getattr(logbook, config.extra.log_level.upper(),
                            logbook.NOTSET)
    user_log.level = getattr(logbook, config.extra.log_level.upper(),
                             logbook.NOTSET)
    user_system_log.level = getattr(logbook, config.extra.log_level.upper(),
                                    logbook.NOTSET)

    if config.base.frequency == "1d":
        logger.DATETIME_FORMAT = "%Y-%m-%d"

    system_log.debug("\n" + pformat(config.convert_to_dict()))

    return config
Exemplo n.º 8
0
    def events(self, start_date, end_date, frequency):

        if not self._mod_config.event.all_day:
            while datetime.now().date() < start_date - timedelta(days=1):
                continue

        mark_time_thread = Thread(target=self.mark_time_period,
                                  args=(start_date,
                                        date.fromtimestamp(2147483647)))
        mark_time_thread.setDaemon(True)
        mark_time_thread.start()
        while True:
            if self._time_period == TimePeriod.BEFORE_TRADING:
                if self._after_trading_processed:
                    self._after_trading_processed = False
                if not self._before_trading_processed:
                    system_log.debug("CtpEventSource: before trading event")
                    yield Event(EVENT.BEFORE_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now() + timedelta(days=1))
                    self._before_trading_processed = True
                    continue
                else:
                    continue
            elif self._time_period == TimePeriod.TRADING:
                if not self._before_trading_processed:
                    system_log.debug("CtpEventSource: before trading event")
                    yield Event(EVENT.BEFORE_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now() + timedelta(days=1))
                    self._before_trading_processed = True
                    continue
                else:
                    tick = self._md_gateway.get_tick()
                    dt_str = ''.join(
                        (str(tick.date),
                         str(float(tick.time) /
                             1000))) if tick.time >= 100000000 else '0'.join(
                                 (str(tick.date),
                                  str(float(tick.time) / 1000)))
                    calendar_dt = parse(dt_str.split('.')[0]) + timedelta(
                        milliseconds=100 * int(dt_str.split('.')[1]))
                    if calendar_dt.hour > 20:
                        trading_dt = calendar_dt + timedelta(days=1)
                    else:
                        trading_dt = calendar_dt
                    system_log.debug("CtpEventSource: tick {}", tick)
                    yield Event(EVENT.TICK,
                                calendar_dt=calendar_dt,
                                trading_dt=trading_dt,
                                tick=RqAttrDict(tick))
            elif self._time_period == TimePeriod.AFTER_TRADING:
                if self._before_trading_processed:
                    self._before_trading_processed = False
                if not self._after_trading_processed:
                    system_log.debug("CtpEventSource: after trading event")
                    yield Event(EVENT.AFTER_TRADING,
                                calendar_dt=datetime.now(),
                                trading_dt=datetime.now())
                    self._after_trading_processed = True
                else:
                    continue