How to use the rqalpha.environment.Environment function in rqalpha

To help you get started, we’ve selected a few rqalpha examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / account_model / future_account.py View on Github external
def _frozen_cash_of_order(order):
        order_cost = margin_of(
            order.order_book_id, order.quantity, order.frozen_price
        ) if order.position_effect == POSITION_EFFECT.OPEN else 0
        return order_cost + Environment.get_instance().get_order_transaction_cost(
            DEFAULT_ACCOUNT_TYPE.FUTURE, order
        )
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / account_model / asset_account.py View on Github external
def register_event(self):
        event_bus = Environment.get_instance().event_bus
        event_bus.add_listener(EVENT.TRADE, self._on_trade)
        event_bus.add_listener(EVENT.ORDER_PENDING_NEW, self._on_order_pending_new)
        event_bus.add_listener(EVENT.ORDER_CREATION_REJECT, self._on_order_unsolicited_update)
        event_bus.add_listener(EVENT.ORDER_UNSOLICITED_UPDATE, self._on_order_unsolicited_update)
        event_bus.add_listener(EVENT.ORDER_CANCELLATION_PASS, self._on_order_unsolicited_update)

        event_bus.add_listener(EVENT.PRE_BEFORE_TRADING, self._on_before_trading)
        event_bus.add_listener(EVENT.SETTLEMENT, self._on_settlement)

        event_bus.add_listener(EVENT.BAR, self._update_last_price)
        event_bus.add_listener(EVENT.TICK, self._update_last_price)
github ricequant / rqalpha / rqalpha / main.py View on Github external
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()

        if not env.data_source:
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / position_model / future_position.py View on Github external
def is_de_listed(self):
        """
        判断合约是否过期
        """
        instrument = Environment.get_instance().get_instrument(self.order_book_id)
        current_date = Environment.get_instance().trading_dt
        if instrument.de_listed_date is not None and current_date >= instrument.de_listed_date:
            return True
        return False
github ricequant / rqalpha / rqalpha / model / new_position / future_position.py View on Github external
def margin_rate(self):
        margin_rate = ExecutionContext.get_future_margin_rate(self.order_book_id)
        margin_multiplier = Environment.get_instance().config.base.margin_multiplier
        return margin_rate * margin_multiplier
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_stock_realtime / redis_data_source.py View on Github external
def __init__(self, path, redis_uri):
        super(RedisDataSource, self).__init__(path)
        self._env = Environment.get_instance()
        import redis
        self._redis_client = redis.from_url(redis_uri)
github ricequant / rqalpha / rqalpha / utils / scheduler.py View on Github external
def next_day_(self, event):
        if len(self._registry) == 0:
            return

        self._today = Environment.get_instance().trading_dt.date()
        self._last_minute = 0
        self._current_minute = 0
        if not self._this_week or self._today > self._this_week[-1]:
            self._fill_week()
        if not self._this_month or self._today > self._this_month[-1]:
            self._fill_month()
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / account_model / stock_account.py View on Github external
def _on_settlement(self, event):
        env = Environment.get_instance()
        for position in list(self._positions.values()):
            order_book_id = position.order_book_id
            if position.is_de_listed() and position.quantity != 0:
                try:
                    transform_data = env.data_proxy.get_share_transformation(order_book_id)
                except NotImplementedError:
                    pass
                else:
                    if transform_data is not None:
                        self._pending_transform[order_book_id] = transform_data
                        continue
                if env.config.mod.sys_accounts.cash_return_by_stock_delisted:
                    self._total_cash += position.market_value
                user_system_log.warn(
                    _(u"{order_book_id} is expired, close all positions by system").format(
                        order_book_id=order_book_id)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_benchmark / benchmark_provider.py View on Github external
def __init__(self, order_book_id):
        self._order_book_id = order_book_id
        self._env = Environment.get_instance()
        self._daily_returns = 0
        self._total_returns = 0
        self._env.event_bus.prepend_listener(EVENT.BAR, self._on_bar)
github ricequant / rqalpha / rqalpha / model / new_account / benchmark_account.py View on Github external
def __init__(self, total_cash, positions, backward_trade_set, dividend_receivable=None):
        super(BenchmarkAccount, self).__init__(total_cash, positions, backward_trade_set, dividend_receivable)
        self.benchmark = Environment.get_instance().config.base.benchmark