How to use the rqalpha.utils.logger.user_system_log 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_risk / validators / price_validator.py View on Github external
def can_submit_order(self, order, account=None):
        if order.type != ORDER_TYPE.LIMIT:
            return True

        # FIXME: it may be better to round price in data source
        limit_up = round(self._env.price_board.get_limit_up(order.order_book_id), 4)
        if order.price > limit_up:
            reason = _(
                "Order Creation Failed: limit order price {limit_price} is higher than limit up {limit_up}."
            ).format(
                limit_price=order.price,
                limit_up=limit_up
            )
            user_system_log.warn(reason)
            return False

        limit_down = round(self._env.price_board.get_limit_down(order.order_book_id), 4)
        if order.price < limit_down:
            reason = _(
                "Order Creation Failed: limit order price {limit_price} is lower than limit down {limit_down}."
            ).format(
                limit_price=order.price,
                limit_down=limit_down
            )
            user_system_log.warn(reason)
            return False

        return True
github ricequant / rqalpha / rqalpha / model / order.py View on Github external
def mark_cancelled(self, cancelled_reason, user_warn=True):
        if not self.is_final():
            self._message = cancelled_reason
            self._status = ORDER_STATUS.CANCELLED
            if user_warn:
                user_system_log.warn(cancelled_reason)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / api / api_future.py View on Github external
else:
        orders.append(Order.__from_create__(
            order_book_id,
            amount,
            side,
            style,
            position_effect
        ))

    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 []

    if len(orders) > 1:
        user_system_log.warn(_(
            "Order was separated, original order: {original_order_repr}, new orders: [{new_orders_repr}]").format(
                original_order_repr="Order(order_book_id={}, quantity={}, side={}, position_effect={})".format(
                    order_book_id, amount, side, position_effect
                ), new_orders_repr=", ".join(["Order({}, {}, {}, {})".format(
                    o.order_book_id, o.quantity, o.side, o.position_effect
                ) for o in orders])
            )
        )

    for o in orders:
        if o.type == ORDER_TYPE.MARKET:
            o.set_frozen_price(price)
        reject_validator_type = env.can_submit_order(o)
        if not reject_validator_type:
            env.broker.submit_order(o)
        else:
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_risk / validators / self_trade_validator.py View on Github external
def can_submit_order(self, order, account=None):
        open_orders = [o for o in self._env.get_open_orders(order.order_book_id) if o.side != order.side]
        if len(open_orders) == 0:
            return True
        reason = _("Create order failed, there are active orders leading to the risk of self-trade: [{}...]")
        if order.type == ORDER_TYPE.MARKET:
            user_system_log.warn(reason.format(open_orders[0]))
            return False
        if order.side == SIDE.BUY:
            for open_order in open_orders:
                if order.price >= open_order.price:
                    user_system_log.warn(reason.format(open_order))
                    return False
        else:
            for open_order in open_orders:
                if order.price <= open_order.price:
                    user_system_log.warn(reason.format(open_order))
                    return False
github ricequant / rqalpha / rqalpha / utils / class_helper.py View on Github external
def getter(self):
        user_system_log.warn(_(
            "\"{}\" is deprecated, please use \"{}\" instead, check the document for more information"
        ).format(property_name, instead_property_name))
        return getattr(self, instead_property_name)
    return property(getter)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_risk / validators / future_position_validator.py View on Github external
position = account.positions[order.order_book_id]

        if order.side == SIDE.BUY and order.position_effect == POSITION_EFFECT.CLOSE_TODAY \
                and order.quantity > position.closable_today_sell_quantity:
            user_system_log.warn(_(
                "Order Creation Failed: not enough today position {order_book_id} to buy close, target"
                " quantity is {quantity}, closable today quantity {closable}").format(
                order_book_id=order.order_book_id,
                quantity=order.quantity,
                closable=position.closable_today_sell_quantity,
            ))
            return False

        if order.side == SIDE.SELL and order.position_effect == POSITION_EFFECT.CLOSE_TODAY \
                and order.quantity > position.closable_today_buy_quantity:
            user_system_log.warn(_(
                "Order Creation Failed: not enough today position {order_book_id} to sell close, target"
                " quantity is {quantity}, closable today quantity {closable}").format(
                order_book_id=order.order_book_id,
                quantity=order.quantity,
                closable=position.closable_today_buy_quantity,
            ))
            return False

        if order.side == SIDE.BUY and order.quantity > position.closable_sell_quantity:
            user_system_log.warn(_(
                "Order Creation Failed: not enough securities {order_book_id} to buy close, target"
                " sell quantity is {quantity}, sell_closable_quantity {closable}").format(
                order_book_id=order.order_book_id,
                quantity=order.quantity,
                closable=position.closable_sell_quantity,
            ))
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_risk / validators / self_trade_validator.py View on Github external
def can_submit_order(self, order, account=None):
        open_orders = [o for o in self._env.get_open_orders(order.order_book_id) if o.side != order.side]
        if len(open_orders) == 0:
            return True
        reason = _("Create order failed, there are active orders leading to the risk of self-trade: [{}...]")
        if order.type == ORDER_TYPE.MARKET:
            user_system_log.warn(reason.format(open_orders[0]))
            return False
        if order.side == SIDE.BUY:
            for open_order in open_orders:
                if order.price >= open_order.price:
                    user_system_log.warn(reason.format(open_order))
                    return False
        else:
            for open_order in open_orders:
                if order.price <= open_order.price:
                    user_system_log.warn(reason.format(open_order))
                    return False
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / api / api_future.py View on Github external
@ExecutionContext.enforce_phase(EXECUTION_PHASE.ON_BAR,
                                EXECUTION_PHASE.ON_TICK,
                                EXECUTION_PHASE.SCHEDULED,
                                EXECUTION_PHASE.GLOBAL)
@apply_rules(verify_that('id_or_ins').is_valid_future(),
             verify_that('amount').is_number().is_greater_or_equal_than(0),
             verify_that('side').is_in([SIDE.BUY, SIDE.SELL]),
             verify_that('position_effect').is_in([POSITION_EFFECT.OPEN, POSITION_EFFECT.CLOSE]),
             verify_that('style').is_instance_of((LimitOrder, MarketOrder, type(None))))
def order(id_or_ins, amount, side, position_effect, style):
    if not isinstance(style, OrderStyle):
        raise RuntimeError
    if amount < 0:
        raise RuntimeError
    if amount == 0:
        user_system_log.warn(_(u"Order Creation Failed: Order amount is 0."))
        return None
    if isinstance(style, LimitOrder) and style.get_limit_price() <= 0:
        raise RQInvalidArgument(_(u"Limit order price should be positive"))

    order_book_id = assure_future_order_book_id(id_or_ins)
    env = Environment.get_instance()
    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)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / position_model / stock_position.py View on Github external
def bought_value(self):
        """
        [已弃用]
        """
        user_system_log.warn(_(u"[abandon] {} is no longer valid.").format('stock_position.bought_value'))
        return self._quantity * self._avg_price