def run_func(**kwargs): from rqalpha.utils.functools import clear_all_cached_functions from rqalpha.utils.config import parse_config from rqalpha import main config = kwargs.get('config', kwargs.get('__config__', None)) user_funcs = { k: kwargs[k] for k in [ 'init', 'handle_bar', 'handle_tick', 'open_auction', 'before_trading', 'after_trading' ] if k in kwargs } if config is None: config = {} else: assert isinstance(config, dict) try: del config["base"]["strategy_file"] except: pass config = parse_config(config, user_funcs=user_funcs) clear_all_cached_functions() return main.run(config, user_funcs=user_funcs)
def run_func(**kwargs): from rqalpha.utils.functools import clear_all_cached_functions from rqalpha.utils.config import parse_config from rqalpha import main """获取函数config参数, 包含回测主要信息""" config = kwargs.get('config', kwargs.get('__config__', None)) """将回测信息转换为dict格式user_funcs = {"init": init, "handle_bar": handle_bar, ......}""" user_funcs = { k: kwargs[k] for k in [ 'init', 'handle_bar', 'handle_tick', 'open_auction', 'before_trading', 'after_trading' ] if k in kwargs } if config is None: config = {} else: assert isinstance(config, dict) try: """非run_file模式删除strategy_file参数""" del config["base"]["strategy_file"] except: pass config = parse_config(config, user_funcs=user_funcs) """清除函数缓冲""" clear_all_cached_functions() """直接运行主函数""" return main.run(config, user_funcs=user_funcs)
def run_func(**kwargs): """ 传入约定函数和策略配置运行回测。约定函数详见 API 手册约定函数部分,可用的配置项详见参数配置部分。 :Keyword Arguments: * **config** (dict) -- 策略配置字典 * **init** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`], Any]) -- 策略初始化函数,会在策略开始运行时被调用,仅会执行一次 * **before_trading** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`], Any]) -- 盘前函数,会在每日盘前被调用一次 * **open_auction** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`, dict[str, :class:`~rqalpha.model.bar.BarObject`]], Any]) -- 集合竞价函数,会在每日盘前集合竞价阶段被调用一次 * **handle_bar** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`, dict[str, :class:`~rqalpha.model.bar.BarObject`]], Any]) -- k 线处理函数,会在盘中 k 线发生更新时被调用,适用于日/分钟级别回测 * **handle_tick** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`, :class:`~rqalpha.model.tick.TickObject`], Any]) -- 快照数据处理函数,会在每个 tick 到达时被调用,适用于 tick 回测 * **after_trading** (Callable[[:class:`~rqalpha.core.strategy_context.StrategyContext`], Any]) -- 盘后函数,会在每日交易结束后被调用一次 :return: dict :example: .. code-block:: python config = { "base": { ... } } def init(context): ... def handle_bar(context, bar_dict): ... run_func(config=config, init=init, handle_bar=handle_bar) """ from rqalpha.utils.functools import clear_all_cached_functions from rqalpha.utils.config import parse_config from rqalpha import main config = kwargs.get('config', kwargs.get('__config__', None)) user_funcs = { k: kwargs[k] for k in [ 'init', 'handle_bar', 'handle_tick', 'open_auction', 'before_trading', 'after_trading' ] if k in kwargs } if config is None: config = {} else: assert isinstance(config, dict) try: del config["base"]["strategy_file"] except: pass config = parse_config(config, user_funcs=user_funcs) clear_all_cached_functions() return main.run(config, user_funcs=user_funcs)
def run_ipython_cell(line, cell=None): from rqalpha.cmds.run import run from rqalpha.utils.functools import clear_all_cached_functions clear_all_cached_functions() args = line.split() args.extend(["--source-code", cell if cell is not None else ""]) try: # It raise exception every time run.main(args, standalone_mode=True) except SystemExit as e: pass
def run_code(code, config=None): from rqalpha.utils.config import parse_config from rqalpha.utils.functools import clear_all_cached_functions from rqalpha import main if config is None: config = {} else: assert isinstance(config, dict) try: del config["base"]["strategy_file"] except: pass config = parse_config(config, source_code=code) clear_all_cached_functions() return main.run(config, source_code=code)
def run_file(strategy_file_path, config=None): from rqalpha.utils.config import parse_config from rqalpha.utils.functools import clear_all_cached_functions from rqalpha import main if config is None: config = {"base": {"strategy_file": strategy_file_path}} else: assert isinstance(config, dict) if "base" in config: config["base"]["strategy_file"] = strategy_file_path else: config["base"] = {"strategy_file": strategy_file_path} config = parse_config(config) clear_all_cached_functions() return main.run(config)
def run_code(code, config=None): # type: (str, Optional[dict]) -> dict """ 传入字符串形式的策略代码以运行回测。 :param code: 策略代码字符串 :param config: 策略配置项字典,默认为空,此处传入的配置项优先级高于策略内 :code:`__config__` 中的配置项 :example: .. code-block:: python config = { "base": { ... } } CODE = \"\"\" def init(context): ... def handle_bar(context, bar_dict): ... \"\"\" run_code(CODE, config=config) """ from rqalpha.utils.config import parse_config from rqalpha.utils.functools import clear_all_cached_functions from rqalpha import main if config is None: config = {} else: assert isinstance(config, dict) try: del config["base"]["strategy_file"] except: pass config = parse_config(config, source_code=code) clear_all_cached_functions() return main.run(config, source_code=code)
def run_file(strategy_file_path, config=None): # type: (str, Optional[dict]) -> dict """ 传入策略文件路径运行回测。 :param strategy_file_path: 策略文件路径 :param config: 策略配置项字典,默认为空,此处传入的配置项优先级高于策略内 :code:`__config__` 中的配置项 :example: .. code-block:: python config = { "base": { ... } } run_file("strategy.py", config=config) """ from rqalpha.utils.config import parse_config from rqalpha.utils.functools import clear_all_cached_functions from rqalpha import main if config is None: config = {"base": {"strategy_file": strategy_file_path}} else: assert isinstance(config, dict) if "base" in config: config["base"]["strategy_file"] = strategy_file_path else: config["base"] = {"strategy_file": strategy_file_path} config = parse_config(config) clear_all_cached_functions() return main.run(config)