def run(config, source_code=None): # [Deprecated] from rqalpha.utils.config import parse_config from rqalpha import main config = parse_config(config, source_code=source_code) return main.run(config, source_code=source_code)
def run(**kwargs): """ Start to run a strategy """ 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)
def run_func(**kwargs): from rqalpha.utils import dummy_func from rqalpha.utils.py2 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 = { 'init': kwargs.get('init', dummy_func), 'handle_bar': kwargs.get('handle_bar', dummy_func), 'handle_tick': kwargs.get('handle_tick', dummy_func), 'before_trading': kwargs.get('before_trading', dummy_func), 'after_trading': kwargs.get('after_trading', dummy_func) } 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 quotation_server(redis_url): """ [sys_stock_realtime] quotation service, download market data into redis Multiple RQAlpha instance can use single market data service. """ import redis import time import json from .utils import get_realtime_quotes redis_client = redis.from_url(redis_url) from rqalpha.data.data_proxy import DataProxy config = parse_config({}, verify_config=False) data_source = BaseDataSource(config.base.data_bundle_path) data_proxy = DataProxy(data_source) order_book_id_list = sorted(ins.order_book_id for ins in data_proxy.all_instruments("CS")) def record_market_data(total_df): for order_book_id, item in total_df.iterrows(): redis_client[order_book_id] = json.dumps(item.to_dict()) while True: try: total_df = get_realtime_quotes(order_book_id_list, include_limit=True) except Exception as e: system_log.exception("get_realtime_quotes fail. {}", e) continue system_log.info("Fetching snapshots, size {}", len(total_df)) record_market_data(total_df) time.sleep(1)
def run_code(code, config=None): from rqalpha.utils.config import parse_config from rqalpha.utils.py2 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.py2 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(**kwargs): """ Start to run a strategy """ 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: sys.exit(1)
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_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)
def atsmain(strategy_path): config_info = parse_config({}, strategy_path) main.run(config_info)
def atsmain(strategyPath): configInfo = parse_config({}, strategyPath) main.run(configInfo)