How to use the rqalpha.const.SIDE.BUY 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 / 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 / account_model / future_account.py View on Github external
orders.append(order(
                    order_book_id,
                    min(quantity, sell_old_quantity),
                    SIDE.BUY,
                    POSITION_EFFECT.CLOSE,
                    style
                ))
                quantity -= sell_old_quantity
            if quantity <= 0:
                return orders
            # 平今仓
            if sell_today_quantity > 0:
                orders.append(order(
                    order_book_id,
                    min(quantity, sell_today_quantity),
                    SIDE.BUY,
                    POSITION_EFFECT.CLOSE_TODAY,
                    style
                ))
                quantity -= sell_today_quantity
            if quantity <= 0:
                return orders
            # 开多仓
            orders.append(order(
                order_book_id,
                quantity,
                SIDE.BUY,
                POSITION_EFFECT.OPEN,
                style
            ))
            return orders
        else:
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_booking / api_booking.py View on Github external
def buy_close(id_or_ins, amount, price=None, style=None, close_today=False):
    position_effect = POSITION_EFFECT.CLOSE_TODAY if close_today else POSITION_EFFECT.CLOSE
    return send_order(id_or_ins, amount, SIDE.BUY, position_effect, cal_style(price, style))
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / api / api_future.py View on Github external
:param float price: 下单价格,默认为None,表示 :class:`~MarketOrder`, 此参数主要用于简化 `style` 参数。

    :param style: 下单类型, 默认是市价单。目前支持的订单类型有 :class:`~LimitOrder` 和 :class:`~MarketOrder`
    :type style: `OrderStyle` object

    :return: :class:`~Order` object | None

    :example:

    .. code-block:: python

        #以价格为3500的限价单开仓买入2张上期所AG1607合约:
        buy_open('AG1607', amount=2, price=3500))
    """
    return order(id_or_ins, amount, SIDE.BUY, POSITION_EFFECT.OPEN, cal_style(price, style))
github ricequant / rqalpha / rqalpha / model / trade.py View on Github external
def position_effect(self):
        if self._position_effect is None:
            if self._side == SIDE.BUY:
                return POSITION_EFFECT.OPEN
            else:
                return POSITION_EFFECT.CLOSE
        return self._position_effect
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_booking / mod.py View on Github external
return

        if env.config.base.init_positions:
            raise RuntimeError("RQAlpha receive init positions. rqalpha_mod_sys_booking does not support init_positions")

        self.booking_account = BookingAccount(register_event=True)

        # 昨仓
        trades = []
        position_list = self._get_old_position_list()
        for position_dict in position_list:
            if position_dict["buy_quantity"] != 0:
                trade = self._create_trade(
                    position_dict["order_book_id"],
                    position_dict["buy_quantity"],
                    SIDE.BUY,
                    POSITION_EFFECT.OPEN,
                )
                trades.append(trade)
            if position_dict["sell_quantity"] != 0:
                trade = self._create_trade(
                    position_dict["order_book_id"],
                    position_dict["sell_quantity"],
                    SIDE.SELL,
                    POSITION_EFFECT.OPEN,
                )
                trades.append(trade)

        system_log.info("yesterday positions trades")
        for trade in trades:
            system_log.info("trade: {:9}, qtx {}, side {}", trade.order_book_id, trade.last_quantity, trade.side)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_accounts / account_model / stock_account.py View on Github external
    @staticmethod
    def _frozen_cash_of_order(order):
        order_cost = order.frozen_price * order.quantity if order.side == SIDE.BUY else 0
        return order_cost + Environment.get_instance().get_order_transaction_cost(DEFAULT_ACCOUNT_TYPE.STOCK, order)
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_risk / validators / future_position_validator.py View on Github external
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,
            ))
            return False

        elif order.side == SIDE.SELL and order.quantity > position.closable_buy_quantity:
            user_system_log.warn(_(
                "Order Creation Failed: not enough securities {order_book_id} to sell close, target"
                " sell quantity is {quantity}, buy_closable_quantity {closable}").format(
                order_book_id=order.order_book_id,
                quantity=order.quantity,
                closable=position.closable_buy_quantity,
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_booking / api_booking.py View on Github external
def order_shares(id_or_ins, amount, price=None, style=None):
    order_book_id = assure_order_book_id(id_or_ins)
    env = Environment.get_instance()

    if amount > 0:
        side = SIDE.BUY
        position_effect = POSITION_EFFECT.OPEN
    else:
        amount = abs(amount)
        side = SIDE.SELL
        position_effect = POSITION_EFFECT.CLOSE

    round_lot = int(env.get_instrument(order_book_id).round_lot)
    try:
        amount = int(Decimal(amount) / Decimal(round_lot)) * round_lot
    except ValueError:
        amount = 0

    order = Order.__from_create__(order_book_id, amount, side, style, position_effect)

    if amount == 0:
        # 如果计算出来的下单量为0, 则不生成Order, 直接返回None
github ricequant / rqalpha / rqalpha / mod / rqalpha_mod_sys_simulation / matcher.py View on Github external
else:
                if self._price_limit:
                    if order.side == SIDE.BUY and deal_price >= price_board.get_limit_up(order_book_id):
                        reason = _(
                            "Order Cancelled: current bar [{order_book_id}] reach the limit_up price."
                        ).format(order_book_id=order.order_book_id)
                        order.mark_rejected(reason)
                        continue
                    if order.side == SIDE.SELL and deal_price <= price_board.get_limit_down(order_book_id):
                        reason = _(
                            "Order Cancelled: current bar [{order_book_id}] reach the limit_down price."
                        ).format(order_book_id=order.order_book_id)
                        order.mark_rejected(reason)
                        continue
                if self._liquidity_limit:
                    if order.side == SIDE.BUY and price_board.get_a1(order_book_id) == 0:
                        reason = _(
                            "Order Cancelled: [{order_book_id}] has no liquidity."
                        ).format(order_book_id=order.order_book_id)
                        order.mark_rejected(reason)
                        continue
                    if order.side == SIDE.SELL and price_board.get_b1(order_book_id) == 0:
                        reason = _(
                            "Order Cancelled: [{order_book_id}] has no liquidity."
                        ).format(order_book_id=order.order_book_id)
                        order.mark_rejected(reason)
                        continue

            if self._volume_limit:
                bar = self._env.bar_dict[order_book_id]
                volume_limit = round(bar.volume * self._volume_percent) - self._turnover[order.order_book_id]
                round_lot = instrument.round_lot