Esempio n. 1
0
    def init_fixture(self):
        from rqalpha.data.base_data_source import BaseDataSource

        super(BaseDataSourceFixture, self).init_fixture()
        default_bundle_path = os.path.abspath(os.path.expanduser('~/.rqalpha/bundle'))

        for key, table in six.iteritems(self.bcolz_data):
            table_relative_path = "{}.bcolz".format(key)
            if table is None:
                os.symlink(
                    os.path.join(default_bundle_path, table_relative_path),
                    os.path.join(self.temp_dir.name, table_relative_path)
                )
            else:
                table.rootdir = os.path.join(self.temp_dir.name, "{}.bcolz".format(key))
                table.flush()

        for key, obj in six.iteritems(self.pk_data):
            pickle_raletive_path = "{}.pk".format(key)
            if obj is None:
                os.symlink(
                    os.path.join(default_bundle_path, pickle_raletive_path),
                    os.path.join(self.temp_dir.name, pickle_raletive_path)
                )
            else:
                with open(os.path.join(self.temp_dir.name, "{}.pk".format(key)), "wb+") as out:
                    pickle.dump(obj, out, protocol=2)

        for file in ("share_transformation.json", "future_info.json"):
            os.symlink(os.path.join(default_bundle_path, file), os.path.join(self.temp_dir.name, file))

        # TODO: use mocked bcolz file
        self.base_data_source = BaseDataSource(self.temp_dir.name, {})
Esempio n. 2
0
 def choose_stock_iterate(self):
     bd = BaseDataSource(
         os.path.join(os.path.expanduser('~'), '.rqalpha', 'bundle'))
     all_instruments = bd.get_all_instruments()
     all_instruments = pd.DataFrame(
         [item.__dict__ for item in all_instruments])
     all_instruments = all_instruments[all_instruments['exchange'].apply(
         lambda x: x in ('XSHE', 'XSHG'))]
     all_instruments = all_instruments[all_instruments['type'] == 'CS']
     all_instruments = all_instruments[all_instruments['board_type'].apply(
         lambda x: x in ('MainBoard', 'SMEBoard', 'GEM'))]
     all_instruments = all_instruments[all_instruments['status'] ==
                                       'Active']
     for stock_id in all_instruments['order_book_id'].values[0:3]:
         print('choose stock:{}'.format(stock_id))
         yield stock_id
Esempio n. 3
0
    def init_fixture(self):
        from rqalpha.data.base_data_source import BaseDataSource

        super(BaseDataSourceFixture, self).init_fixture()
        default_bundle_path = os.path.abspath(
            os.path.expanduser('~/.rqalpha/bundle'))
        self.base_data_source = BaseDataSource(default_bundle_path, {})
Esempio n. 4
0
def history(stock_code, market=None, bundle_path='~/.rqalpha/bundle'):
    d = BaseDataSource(os.path.expanduser(bundle_path))

    instruments = d._instruments.get_all_instruments()

    stock_map = {i.order_book_id: i for i in instruments}
    if not market:
        market = easyutils.get_stock_type(stock_code)
    if market == 'sh':
        stock_code += '.XSHG'
    else:
        stock_code += '.XSHE'
    raw = d._all_day_bars_of(stock_map[stock_code])
    df = pd.DataFrame.from_dict(raw)
    df.set_index('datetime', inplace=True)
    return df
    def __init__(self,
                 start=start_default,
                 end=end_default,
                 path=RQALPHA_BUNDLE_PATH):
        super(RqalphaAStockTradingCalendar, self).__init__()
        self._data_source = BaseDataSource(path)
        _all_days = self._data_source.get_trading_calendar()
        _all_days = _all_days[_all_days.slice_indexer(start, end)]
        # `DatetimeIndex`s of standard opens/closes for each day.
        self._opens = days_at_time(_all_days, self.open_time, self.tz,
                                   self.open_offset)
        self._closes = days_at_time(_all_days, self.close_time, self.tz,
                                    self.close_offset)

        # In pandas 0.16.1 _opens and _closes will lose their timezone
        # information. This looks like it has been resolved in 0.17.1.
        # http://pandas.pydata.org/pandas-docs/stable/whatsnew.html#datetime-with-tz  # noqa
        self.schedule = pd.DataFrame(
            index=_all_days,
            columns=['market_open', 'market_close'],
            data={
                'market_open': self._opens,
                'market_close': self._closes,
            },
            dtype='datetime64[ns]',
        )

        # Simple cache to avoid recalculating the same minute -> session in
        # "next" mode. Analysis of current zipline code paths show that
        # `minute_to_session_label` is often called consecutively with the same
        # inputs.
        self._minute_to_session_label_cache = LRU(1)

        self.market_opens_nanos = self.schedule.market_open.values. \
            astype(np.int64)

        self.market_closes_nanos = self.schedule.market_close.values. \
            astype(np.int64)

        self._trading_minutes_nanos = self.all_minutes.values. \
            astype(np.int64)

        self.first_trading_session = _all_days[0]
        self.last_trading_session = _all_days[-1]
Esempio n. 6
0
  def __init__(self, path = '~/'):
    data_path = os.path.join(path, ".rqalpha")
    data_bundle_path = os.path.join(os.path.expanduser(data_path), "bundle")
    if not os.path.isdir(data_bundle_path ):
      print("not exist this file", data_bundle_path)
      exit(-1)
    data_source = BaseDataSource(data_bundle_path)
    self.data_source_ = data_source

    Instru = InstrumentMixin(data_source._instruments._instruments)
    self.sym_inst_ = Instru
    self.insts_ = data_source._instruments._instruments
Esempio n. 7
0
def quotation_server(redis_url):
    """
    [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.environment import Environment
    from rqalpha.data.data_proxy import DataProxy
    # 通过 parse_config 函数 获取 默认 data_bundle 的配置
    # 由于 parse_config 会进行配置检查,如果没有设置 account 会提示报错,因此随意设置一个股票初始资金,来保证正确获取默认参数配置
    config = parse_config({
        'base': {
            'accounts': {
                'stock': 10000
            }
        }
    })

    Environment(config)
    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())

    retry_cnt = 0
    while True:
        try:
            total_df = get_realtime_quotes(order_book_id_list, include_limit=True)
        except (OSError, IOError) as e:
            system_log.exception("get_realtime_quotes socket error. retry {} {}", retry_cnt, e)
            time.sleep(retry_cnt * 2)
            retry_cnt += 1
            continue
        system_log.info("Fetching snapshots, size {}", len(total_df))
        record_market_data(total_df)
        time.sleep(1)
        retry_cnt = 0
Esempio n. 8
0
    def __init__(self, bundle_path="~/.rqalpha/bundle"):
        try:
            import rqalpha
        except ImportError:
            print("-" * 50)
            print("Run `pip install rqalpha` to install rqalpha first")
            print("-" * 50)
            raise

        # # FIXME
        # import warnings
        # warnings.simplefilter(action="ignore", category=FutureWarning)

        from rqalpha.data.base_data_source import BaseDataSource
        from rqalpha.data.data_proxy import DataProxy

        self.data_proxy = DataProxy(
            BaseDataSource(os.path.expanduser(bundle_path)))
Esempio n. 9
0
    def __init__(self, bundle_path="~/.rqalpha/bundle"):
        try:
            import rqalpha
        except ImportError:
            print("-" * 50)
            print("Run `pip install rqalpha` to install rqalpha first")
            print("-" * 50)
            raise

        # # FIXME
        # import warnings
        # warnings.simplefilter(action="ignore", category=FutureWarning)

        from rqalpha.data.base_data_source import BaseDataSource
        from rqalpha.data.data_proxy import DataProxy
        from rqalpha.core.bar_dict_price_board import BarDictPriceBoard
        from rqalpha.environment import Environment
        environment = Environment(None)
        self.data_proxy = DataProxy(BaseDataSource(os.path.expanduser(bundle_path)), BarDictPriceBoard())
Esempio n. 10
0
    def __init__(self, data_bundle_path=None):
        default_bundle_path = os.path.abspath(os.path.expanduser('~/.rqalpha'))
        if data_bundle_path is None:
            data_bundle_path = default_bundle_path
        else:
            data_bundle_path = os.path.abspath(
                os.path.join(data_bundle_path, '.'))

        data_bundle_path = data_bundle_path + '/bundle'

        self._data_bundle_path = data_bundle_path

        # basic_system_log.debug('rqalpha data bundle path: ' + data_bundle_path)
        if not os.path.exists(data_bundle_path):
            self.update(skip_last_date_check=True)

        from rqalpha.data.base_data_source import BaseDataSource
        data_source = BaseDataSource(data_bundle_path)
        super(DataSource, self).__init__(data_source)

        self._last_date_date = None
        self.get_data_last_date()
Esempio n. 11
0
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)
Esempio n. 12
0
path = '../../../data/'
path_market = os.path.join(path,'trade_market')
if not os.path.exists(path_market):
      os.makedirs(path_market)
      
scu = SCU(path=path)
stocks = scu.stock_codes_remove_no_stock_basic()
stocks = scu.add_allstock_xshg_xshe(stocks)

#rqa.update_bundle()
rqalpha_path = r"~/.rqalpha"
data_bundle_path = os.path.join(os.path.expanduser(rqalpha_path), "bundle")
if not os.path.isdir(data_bundle_path):
  print("not exist this file", data_bundle_path)
  exit(-1)
data_source = BaseDataSource(data_bundle_path)

Instru = InstrumentMixin(data_source._instruments._instruments)
# for stock_code in stocks:
#   print('procesing...',stock_code)
#   stock_data = pd.DataFrame(data_source._all_day_bars_of(Instru.instruments(stock_code)))
#   stock_data.datetime = stock_data.datetime.apply(lambda x: datetime.strptime(str(x)[:-6],'%Y%m%d'))
#   stock_data.to_csv(os.path.join(path_market,stock_code[:-5]+'.csv'),index=False)

date_time = date(2019, 9, 5)

bars = data_source.history_bars(instrument = Instru.instruments('603032.XSHG'), bar_count = 100,
                                frequency = '1d', fields = 'close', dt = date_time,
                                skip_suspended=False, adjust_orig = date_time)
bar_series = pd.Series(bars)
bar_pct = bar_series.pct_change()
from rqalpha.data.instrument_mixin import InstrumentMixin
#from rqalpha.data.instrument_store import instrument_store

path = '../../../data/'
path_market = os.path.join(path, 'trade_market')
if not os.path.exists(path_market):
    os.makedirs(path_market)

scu = SCU(path=path)
stocks = scu.stock_codes_remove_no_stock_basic()
stocks = scu.add_allstock_xshg_xshe(stocks)

#rqa.update_bundle()
rqalpha_path = "~/.rqalpha"
data_bundle_path = os.path.join(os.path.expanduser(rqalpha_path), "bundle")
data = BaseDataSource(data_bundle_path)

Instru = InstrumentMixin(data._instruments._instruments)
for stock_code in stocks:
    print('procesing...', stock_code)
    stock_data = pd.DataFrame(
        data._all_day_bars_of(Instru.instruments(stock_code)))
    stock_data.datetime = stock_data.datetime.apply(
        lambda x: datetime.strptime(str(x)[:-6], '%Y%m%d'))
    stock_data.to_csv(os.path.join(path_market, stock_code[:-5] + '.csv'),
                      index=False)

sec_name = []
sec_index = []

for k, v in Instru._sym_id_map.items():
Esempio n. 14
0
def run(config, source_code=None, user_funcs=None):
    env = Environment(config)
    persist_helper = None
    init_succeed = False
    mod_handler = ModHandler()

    try:
        # avoid register handlers everytime
        # when running in ipython
        set_loggers(config)
        basic_system_log.debug("\n" + pformat(config.convert_to_dict()))

        env.set_strategy_loader(
            init_strategy_loader(env, source_code, user_funcs, config))
        env.set_global_vars(GlobalVars())
        mod_handler.set_env(env)
        mod_handler.start_up()

        try:
            future_info = config.base.future_info
        except AttributeError:
            pass
        else:
            deep_update(future_info, future_info_cn.CN_FUTURE_INFO)

        if not env.data_source:
            env.set_data_source(BaseDataSource(config.base.data_bundle_path))

        if env.price_board is None:
            from .core.bar_dict_price_board import BarDictPriceBoard
            env.price_board = BarDictPriceBoard()

        env.set_data_proxy(DataProxy(env.data_source, env.price_board))

        Scheduler.set_trading_dates_(env.data_source.get_trading_calendar())
        scheduler = Scheduler(config.base.frequency)
        mod_scheduler._scheduler = scheduler

        env._universe = StrategyUniverse()

        _adjust_start_date(env.config, env.data_proxy)

        # FIXME
        start_dt = datetime.datetime.combine(config.base.start_date,
                                             datetime.datetime.min.time())
        env.calendar_dt = start_dt
        env.trading_dt = start_dt

        broker = env.broker
        assert broker is not None
        env.portfolio = broker.get_portfolio()
        if env.benchmark_provider:
            env.benchmark_portfolio = BenchmarkPortfolio(
                env.benchmark_provider, env.portfolio.units)

        event_source = env.event_source
        assert event_source is not None

        bar_dict = BarMap(env.data_proxy, config.base.frequency)
        env.set_bar_dict(bar_dict)

        ctx = ExecutionContext(const.EXECUTION_PHASE.GLOBAL)
        ctx._push()

        env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT))

        scope = create_base_scope(config.base.run_type == RUN_TYPE.BACKTEST)
        scope.update({"g": env.global_vars})

        apis = api_helper.get_apis()
        scope.update(apis)

        scope = env.strategy_loader.load(scope)

        if env.config.extra.enable_profiler:
            enable_profiler(env, scope)

        ucontext = StrategyContext()
        scheduler.set_user_context(ucontext)

        from .core.executor import Executor
        executor = Executor(env)

        persist_helper = init_persist_helper(env, scheduler, ucontext,
                                             executor, config)

        if persist_helper:
            should_resume = persist_helper.should_resume()
            should_run_init = persist_helper.should_run_init()
        else:
            should_resume = False
            should_run_init = True

        user_strategy = Strategy(env.event_bus, scope, ucontext,
                                 should_run_init)
        env.user_strategy = user_strategy

        if (should_resume and not should_run_init) or not should_resume:
            with run_with_user_log_disabled(disabled=should_resume):
                user_strategy.init()

        if config.extra.context_vars:
            for k, v in six.iteritems(config.extra.context_vars):
                if isinstance(v, RqAttrDict):
                    v = v.__dict__
                setattr(ucontext, k, v)

        if persist_helper:
            env.event_bus.publish_event(Event(EVENT.BEFORE_SYSTEM_RESTORED))
            env.event_bus.publish_event(Event(EVENT.DO_RESTORE))
            env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_RESTORED))

        init_succeed = True

        if should_resume and should_run_init:
            user_strategy.init()

        executor.run(bar_dict)

        if env.profile_deco:
            output_profile_result(env)
    except CustomException as e:
        if init_succeed and persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_CRASH:
            persist_helper.persist()

        code = _exception_handler(e)
        mod_handler.tear_down(code, e)
    except Exception as e:
        if init_succeed and persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_CRASH:
            persist_helper.persist()

        exc_type, exc_val, exc_tb = sys.exc_info()
        user_exc = create_custom_exception(exc_type, exc_val, exc_tb,
                                           config.base.strategy_file)

        code = _exception_handler(user_exc)
        mod_handler.tear_down(code, user_exc)
    else:
        if persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_NORMAL_EXIT:
            persist_helper.persist()
        result = mod_handler.tear_down(const.EXIT_CODE.EXIT_SUCCESS)
        system_log.debug(_(u"strategy run successfully, normal exit"))
        return result
Esempio n. 15
0
def run(config, source_code=None, user_funcs=None):
    env = Environment(config)
    persist_helper = None
    init_succeed = False
    mod_handler = ModHandler()

    try:
        # avoid register handlers everytime
        # when running in ipython
        set_loggers(config)
        basic_system_log.debug("\n" + pformat(config.convert_to_dict()))

        if source_code is not None:
            env.set_strategy_loader(SourceCodeStrategyLoader(source_code))
        elif user_funcs is not None:
            env.set_strategy_loader(UserFuncStrategyLoader(user_funcs))
        else:
            env.set_strategy_loader(FileStrategyLoader(config.base.strategy_file))
        env.set_global_vars(GlobalVars())
        mod_handler.set_env(env)
        mod_handler.start_up()

        if not env.data_source:
            env.set_data_source(BaseDataSource(config.base.data_bundle_path))
        env.set_data_proxy(DataProxy(env.data_source))

        Scheduler.set_trading_dates_(env.data_source.get_trading_calendar())
        scheduler = Scheduler(config.base.frequency)
        mod_scheduler._scheduler = scheduler

        env._universe = StrategyUniverse()

        _adjust_start_date(env.config, env.data_proxy)

        _validate_benchmark(env.config, env.data_proxy)

        # FIXME
        start_dt = datetime.datetime.combine(config.base.start_date, datetime.datetime.min.time())
        env.calendar_dt = start_dt
        env.trading_dt = start_dt

        broker = env.broker
        assert broker is not None
        env.portfolio = broker.get_portfolio()
        env.benchmark_portfolio = create_benchmark_portfolio(env)

        event_source = env.event_source
        assert event_source is not None

        bar_dict = BarMap(env.data_proxy, config.base.frequency)
        env.set_bar_dict(bar_dict)

        if env.price_board is None:
            from .core.bar_dict_price_board import BarDictPriceBoard
            env.price_board = BarDictPriceBoard()

        ctx = ExecutionContext(const.EXECUTION_PHASE.GLOBAL)
        ctx._push()

        env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT))

        scope = create_base_scope()
        scope.update({
            "g": env.global_vars
        })

        apis = api_helper.get_apis()
        scope.update(apis)

        scope = env.strategy_loader.load(scope)

        if env.config.extra.enable_profiler:
            enable_profiler(env, scope)

        ucontext = StrategyContext()
        user_strategy = Strategy(env.event_bus, scope, ucontext)
        scheduler.set_user_context(ucontext)

        if not config.extra.force_run_init_when_pt_resume:
            with run_with_user_log_disabled(disabled=config.base.resume_mode):
                user_strategy.init()

        if config.extra.context_vars:
            for k, v in six.iteritems(config.extra.context_vars):
                setattr(ucontext, k, v)

        if config.base.persist:
            persist_provider = env.persist_provider
            persist_helper = PersistHelper(persist_provider, env.event_bus, config.base.persist_mode)
            persist_helper.register('core', CoreObjectsPersistProxy(scheduler))
            persist_helper.register('user_context', ucontext)
            persist_helper.register('global_vars', env.global_vars)
            persist_helper.register('universe', env._universe)
            if isinstance(event_source, Persistable):
                persist_helper.register('event_source', event_source)
            persist_helper.register('portfolio', env.portfolio)
            if env.benchmark_portfolio:
                persist_helper.register('benchmark_portfolio', env.benchmark_portfolio)
            for name, module in six.iteritems(env.mod_dict):
                if isinstance(module, Persistable):
                    persist_helper.register('mod_{}'.format(name), module)
            # broker will restore open orders from account
            if isinstance(broker, Persistable):
                persist_helper.register('broker', broker)

            persist_helper.restore()
            env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_RESTORED))

        init_succeed = True

        # When force_run_init_when_pt_resume is active,
        # we should run `init` after restore persist data
        if config.extra.force_run_init_when_pt_resume:
            assert config.base.resume_mode == True
            with run_with_user_log_disabled(disabled=False):
                user_strategy.init()

        from .core.executor import Executor
        Executor(env).run(bar_dict)

        if env.profile_deco:
            output_profile_result(env)
    except CustomException as e:
        if init_succeed and env.config.base.persist and persist_helper:
            persist_helper.persist()

        code = _exception_handler(e)
        mod_handler.tear_down(code, e)
    except Exception as e:
        if init_succeed and env.config.base.persist and persist_helper:
            persist_helper.persist()

        exc_type, exc_val, exc_tb = sys.exc_info()
        user_exc = create_custom_exception(exc_type, exc_val, exc_tb, config.base.strategy_file)

        code = _exception_handler(user_exc)
        mod_handler.tear_down(code, user_exc)
    else:
        result = mod_handler.tear_down(const.EXIT_CODE.EXIT_SUCCESS)
        system_log.debug(_(u"strategy run successfully, normal exit"))
        return result
Esempio n. 16
0
def run(config, source_code=None, user_funcs=None):
    env = Environment(config)
    persist_helper = None
    init_succeed = False
    mod_handler = ModHandler()

    try:
        # avoid register handlers everytime
        # when running in ipython
        set_loggers(config)
        init_rqdatac(getattr(config.base, 'rqdatac_uri', None))
        system_log.debug("\n" + pformat(config.convert_to_dict()))

        env.set_strategy_loader(
            init_strategy_loader(env, source_code, user_funcs, config))
        mod_handler.set_env(env)
        mod_handler.start_up()

        if not env.data_source:
            env.set_data_source(
                BaseDataSource(config.base.data_bundle_path,
                               getattr(config.base, "future_info", {})))
        if env.price_board is None:
            from rqalpha.data.bar_dict_price_board import BarDictPriceBoard
            env.price_board = BarDictPriceBoard()
        env.set_data_proxy(DataProxy(env.data_source, env.price_board))

        _adjust_start_date(env.config, env.data_proxy)

        ctx = ExecutionContext(const.EXECUTION_PHASE.GLOBAL)
        ctx._push()

        # FIXME
        start_dt = datetime.datetime.combine(config.base.start_date,
                                             datetime.datetime.min.time())
        env.calendar_dt = start_dt
        env.trading_dt = start_dt

        assert env.broker is not None
        assert env.event_source is not None
        if env.portfolio is None:
            from rqalpha.portfolio import Portfolio
            env.set_portfolio(
                Portfolio(config.base.accounts, config.base.init_positions))

        env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_INIT))

        scope = create_base_scope()
        scope.update({"g": env.global_vars})
        scope.update(get_strategy_apis())
        scope = env.strategy_loader.load(scope)

        if config.extra.enable_profiler:
            enable_profiler(env, scope)

        ucontext = StrategyContext()
        executor = Executor(env)

        persist_helper = init_persist_helper(env, ucontext, executor, config)
        user_strategy = Strategy(env.event_bus, scope, ucontext)
        env.user_strategy = user_strategy

        env.event_bus.publish_event(Event(EVENT.BEFORE_STRATEGY_RUN))
        if persist_helper:
            with LogCapture(user_log) as log_capture:
                user_strategy.init()
        else:
            user_strategy.init()

        if config.extra.context_vars:
            for k, v in config.extra.context_vars.items():
                if isinstance(v, RqAttrDict):
                    v = v.__dict__
                setattr(ucontext, k, v)

        if persist_helper:
            env.event_bus.publish_event(Event(EVENT.BEFORE_SYSTEM_RESTORED))
            restored_obj_state = persist_helper.restore(None)
            check_key = ["global_vars", "user_context", "executor", "universe"]
            kept_current_init_data = not any(
                v for k, v in restored_obj_state.items() if k in check_key)
            system_log.debug(
                "restored_obj_state: {}".format(restored_obj_state))
            system_log.debug(
                "kept_current_init_data: {}".format(kept_current_init_data))
            if kept_current_init_data:
                # 未能恢复init相关数据 保留当前策略初始化变量(展示当前策略初始化日志)
                log_capture.replay()
            else:
                user_system_log.info(_('system restored'))
            env.event_bus.publish_event(Event(EVENT.POST_SYSTEM_RESTORED))

        init_succeed = True

        bar_dict = BarMap(env.data_proxy, config.base.frequency)
        executor.run(bar_dict)
        env.event_bus.publish_event(Event(EVENT.POST_STRATEGY_RUN))

        if env.profile_deco:
            output_profile_result(env)
        release_print(scope)
    except CustomException as e:
        if init_succeed and persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_CRASH:
            persist_helper.persist()

        code = _exception_handler(e)
        mod_handler.tear_down(code, e)
    except Exception as e:
        system_log.error(traceback.format_exc())

        if init_succeed and persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_CRASH:
            persist_helper.persist()

        exc_type, exc_val, exc_tb = sys.exc_info()
        user_exc = create_custom_exception(exc_type, exc_val, exc_tb,
                                           config.base.strategy_file)

        code = _exception_handler(user_exc)
        mod_handler.tear_down(code, user_exc)
    else:
        if persist_helper and env.config.base.persist_mode == const.PERSIST_MODE.ON_NORMAL_EXIT:
            persist_helper.persist()
        result = mod_handler.tear_down(const.EXIT_CODE.EXIT_SUCCESS)
        system_log.debug(_(u"strategy run successfully, normal exit"))
        return result
Esempio n. 17
0
        break

    shutil.rmtree(data_bundle_path, ignore_errors=True)
    os.makedirs(data_bundle_path)
    tar = tarfile.open(tmp, 'r:bz2')
    tar.extractall(data_bundle_path)
    tar.close()
    os.remove(tmp)
    six.print_(
        _(u"Data bundle download successfully in {bundle_path}").format(
            bundle_path=data_bundle_path))


import platform

data_k_bar = BaseDataSource(
    'C:\\Users\\Yue/.rqalpha\\bundle') if platform.system(
    ) != 'Linux' else BaseDataSource('/home/ubuntu/.rqalpha/bundle')


def run(config, source_code=None, user_funcs=None):
    env = Environment(config)
    persist_helper = None
    init_succeed = False
    mod_handler = ModHandler()  #初始化模块处理器,包含list和dict,存储模块名和配置信息

    try:
        # avoid register handlers everytime
        # when running in ipython
        set_loggers(config)
        basic_system_log.debug("\n" + pformat(config.convert_to_dict()))
Esempio n. 18
0
 def get_ex_cum_factor(self, order_book_id):
     return BaseDataSource.get_ex_cum_factor(self, order_book_id)
Esempio n. 19
0
 def __init__(self, end_date, bar_count=20):
     data_source = BaseDataSource('/home/daiab/.rqalpha/bundle')
     self._data_proxy = DataProxy(data_source)
     self.fields = ["open", "close", "high", "low", "volume"]
     self.end_date = end_date
     self.bar_count = bar_count