How to use the rqalpha.environment.Environment.get_instance 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 / api / api_future.py View on Github external
if env.config.base.run_type != RUN_TYPE.BACKTEST:
        if "88" in order_book_id:
            raise RQInvalidArgument(_(u"Main Future contracts[88] are not supported in paper trading."))
        if "99" in order_book_id:
            raise RQInvalidArgument(_(u"Index Future contracts[99] are not supported in paper trading."))

    price = env.get_last_price(order_book_id)
    if not is_valid_price(price):
        user_system_log.warn(
            _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
        )
        return

    amount = int(amount)

    env = Environment.get_instance()

    orders = []
    if position_effect == POSITION_EFFECT.CLOSE:
        if side == SIDE.BUY:
            if env.portfolio:
                position = env.portfolio.positions[order_book_id]
                sell_quantity, sell_old_quantity = position.sell_quantity, position.sell_old_quantity
            else:
                position = env.booking.get_position(order_book_id, POSITION_DIRECTION.SHORT)
                sell_quantity, sell_old_quantity = position.quantity, position.old_quantity

            # 如果平仓量大于持仓量,则 Warning 并 取消订单创建
            if amount > sell_quantity:
                user_system_log.warn(
                    _(u"Order Creation Failed: close amount {amount} is larger than position "
                      u"quantity {quantity}").format(amount=amount, quantity=sell_quantity)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / account_model / future_account.py View on Github external
def margin_of(order_book_id, quantity, price):
    env = Environment.get_instance()
    margin_multiplier = env.config.base.margin_multiplier
    instrument = env.get_instrument(order_book_id)
    return quantity * instrument.contract_multiplier * price * instrument.margin_rate * margin_multiplier
github ricequant / rqalpha / rqalpha / model / benchmark_portfolio.py View on Github external
def annualized_returns(self):
        # fixme: do not rely on env
        if self.unit_net_value <= 0:
            return -1

        env = Environment.get_instance()
        date_count = float(env.data_proxy.count_trading_dates(env.config.base.start_date, env.trading_dt.date()))
        return self.unit_net_value ** (DAYS_CNT.TRADING_DAYS_A_YEAR / date_count) - 1
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_funcat / mod.py View on Github external
def __init__(self):
                from rqalpha.api import (
                    all_instruments,
                    instruments,
                )

                self.set_current_date = set_current_date
                self.all_instruments = all_instruments
                self.instruments = instruments
                self.rqalpha_env = Environment.get_instance()

                self.rqalpha_env.event_bus.add_listener(EVENT.PRE_BEFORE_TRADING, self._pre_before_trading)
                self.rqalpha_env.event_bus.add_listener(EVENT.PRE_BAR, self._pre_handle_bar)

                self.fetch_data_by_api = True
github ricequant / rqalpha / rqalpha / model / bar.py View on Github external
def mavg(self, intervals, frequency='1d'):
        if frequency == 'day':
            frequency = '1d'
        if frequency == 'minute':
            frequency = '1m'

        # copy form history
        env = Environment.get_instance()
        dt = env.calendar_dt

        if (env.config.base.frequency == '1m' and frequency == '1d') or ExecutionContext.phase() == EXECUTION_PHASE.BEFORE_TRADING:
            # 在分钟回测获取日线数据, 应该推前一天
            dt = env.data_proxy.get_previous_trading_date(env.calendar_dt.date())
        bars = env.data_proxy.fast_history(self._instrument.order_book_id, intervals, frequency, 'close', dt)
        return bars.mean()
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_stock_realtime / direct_data_source.py View on Github external
def __init__(self, path):
        super(DirectDataSource, self).__init__(path)
        self._env = Environment.get_instance()
github ricequant / rqalpha / rqalpha / core / tick_price_board.py View on Github external
def __init__(self):
        self._env = Environment.get_instance()
        self._env.event_bus.prepend_listener(EVENT.TICK, self._on_tick)
        self._tick_board = {}
        self._settlement_lock = False
        if ACCOUNT_TYPE.FUTURE in self._env.config.base.account_list:
            self._env.event_bus.add_listener(EVENT.PRE_SETTLEMENT, self._lock_settlement)
            self._env.event_bus.add_listener(EVENT.POST_BEFORE_TRADING, self._unlock_settlement)
github ricequant / rqalpha / rqalpha / model / bar.py View on Github external
def __contains__(self, o):
        return o in Environment.get_instance().get_universe()