Skip to content

Commit

Permalink
Merge a897585 into d889589
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Mar 28, 2024
2 parents d889589 + a897585 commit e86a99e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 54 deletions.
8 changes: 3 additions & 5 deletions rqalpha/apis/api_base.py
Expand Up @@ -170,11 +170,9 @@ def submit_order(id_or_ins, amount, side, price=None, position_effect=None):
style = cal_style(price, None)
market_price = env.get_last_price(order_book_id)
if not is_valid_price(market_price):
user_system_log.warn(
_(u"Order Creation Failed: [{order_book_id}] No market data").format(
order_book_id=order_book_id
)
)
reason = _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
return

amount = int(amount)
Expand Down
33 changes: 18 additions & 15 deletions rqalpha/mod/rqalpha_mod_sys_accounts/api/api_future.py
Expand Up @@ -35,6 +35,7 @@
from rqalpha.utils.logger import user_system_log
from rqalpha.utils.i18n import gettext as _
from rqalpha.utils.arg_checker import apply_rules, verify_that
from rqalpha.core.events import Event, EVENT


__all__ = []
Expand All @@ -47,9 +48,11 @@ def _submit_order(id_or_ins, amount, side, position_effect, style):

amount = int(amount)
if amount == 0:
user_system_log.warn(_(
u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}"
).format(order_book_id=order_book_id))
reason = _(u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}").format(
order_book_id=order_book_id
)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
return None
if isinstance(style, LimitOrder) and np.isnan(style.get_limit_price()):
raise RQInvalidArgument(_(u"Limit order price should not be nan."))
Expand All @@ -62,34 +65,34 @@ def _submit_order(id_or_ins, amount, side, position_effect, style):

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)
)
reason = _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
return

env = Environment.get_instance()

orders = []
if position_effect in (POSITION_EFFECT.CLOSE_TODAY, POSITION_EFFECT.CLOSE):
direction = POSITION_DIRECTION.LONG if side == SIDE.SELL else POSITION_DIRECTION.SHORT
position = env.portfolio.get_position(order_book_id, direction) # type: Position
if position_effect == POSITION_EFFECT.CLOSE_TODAY:
if amount > position.today_closable:
user_system_log.warning(_(
reason = _(
"Order Creation Failed: "
"close today amount {amount} is larger than today closable quantity {quantity}"
).format(amount=amount, quantity=position.today_closable))
"close today amount {amount} is larger than today closable quantity {quantity}").format(
amount=amount, quantity=position.today_closable)
user_system_log.warning(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
return []
orders.append(Order.__from_create__(
order_book_id, amount, side, style, POSITION_EFFECT.CLOSE_TODAY
))
else:
quantity, old_quantity = position.quantity, position.old_quantity
if amount > quantity:
user_system_log.warn(_(
u"Order Creation Failed: close amount {amount} is larger than position quantity {quantity}").format(
amount=amount, quantity=quantity
))
reason = _(u"Order Creation Failed: close amount {amount} is larger than position quantity {quantity}").format(
amount=amount, quantity=quantity)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
return []
if amount > old_quantity:
if old_quantity != 0:
Expand Down
39 changes: 20 additions & 19 deletions rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py
Expand Up @@ -34,6 +34,7 @@
INSTRUMENT_TYPE, ORDER_TYPE, POSITION_DIRECTION,
POSITION_EFFECT, SIDE)
from rqalpha.core.execution_context import ExecutionContext
from rqalpha.core.events import Event, EVENT
from rqalpha.environment import Environment
from rqalpha.mod.rqalpha_mod_sys_risk.validators.cash_validator import \
is_cash_enough
Expand Down Expand Up @@ -100,18 +101,19 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a
raise RQInvalidArgument(_(u"Limit order price should not be nan."))
price = env.data_proxy.get_last_price(ins.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=ins.order_book_id))
reason = _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=ins.order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None, reason=reason))
return

if (side == SIDE.BUY and current_quantity != -amount) or (side == SIDE.SELL and current_quantity != abs(amount)):
# 在融券回测中,需要用买单作为平空,对于此种情况下出现的碎股,亦允许一次性申报卖出
amount = _round_order_quantity(ins, amount)

if amount == 0:
user_system_log.warn(_(
u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}"
).format(order_book_id=ins.order_book_id))
reason = _(u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}").format(order_book_id=ins.order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None, reason=reason))
return
order = Order.__from_create__(ins.order_book_id, abs(amount), side, style, position_effect)
if side == SIDE.BUY and auto_switch_order_value:
Expand All @@ -138,9 +140,9 @@ def _order_value(account, position, ins, cash_amount, style):
else:
price = env.data_proxy.get_last_price(ins.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=ins.order_book_id)
)
reason = _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=ins.order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None, reason=reason))
return

amount = int(Decimal(cash_amount) / Decimal(price))
Expand All @@ -155,9 +157,9 @@ def _order_value(account, position, ins, cash_amount, style):
break
amount -= round_lot
else:
user_system_log.warn(_(
u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}"
).format(order_book_id=ins.order_book_id))
reason = _(u"Order Creation Failed: 0 order quantity, order_book_id={order_book_id}").format(order_book_id=ins.order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None, reason=reason))
return

if amount < 0:
Expand Down Expand Up @@ -342,9 +344,9 @@ def order_target_portfolio(
order_book_id = ins.order_book_id
last_price = env.data_proxy.get_last_price(order_book_id)
if not is_valid_price(last_price):
user_system_log.warn(
_(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
)
reason = _(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None, reason=reason))
continue

price_or_style = price_or_styles.get(ins.order_book_id)
Expand Down Expand Up @@ -380,12 +382,11 @@ def order_target_portfolio(
open_price = _get_order_style_price(order_book_id, open_style)
close_price = _get_order_style_price(order_book_id, close_style)
if not (is_valid_price(close_price) and is_valid_price(open_price)):
user_system_log.warn(_(
"Adjust position of {id_or_ins} Failed: "
"Invalid close/open price {close_price}/{open_price}").format(
id_or_ins=order_book_id, close_price=close_price, open_price=open_price
)
reason = _("Adjust position of {id_or_ins} Failed: Invalid close/open price {close_price}/{open_price}").format(
id_or_ins=order_book_id, close_price=close_price, open_price=open_price
)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, order=None, reason=reason))
continue

delta_quantity = (account_value * target_percent / close_price) - current_quantities.get(order_book_id, 0)
Expand Down
14 changes: 10 additions & 4 deletions rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py
Expand Up @@ -22,6 +22,8 @@
from rqalpha.utils.logger import user_system_log
from rqalpha.model.order import Order
from rqalpha.portfolio.account import Account
from rqalpha.core.events import Event, EVENT
from rqalpha.environment import Environment

from rqalpha.utils.i18n import gettext as _

Expand All @@ -38,21 +40,25 @@ def can_submit_order(self, order, account=None):
return True
position = account.get_position(order.order_book_id, order.position_direction) # type: AbstractPosition
if order.position_effect == POSITION_EFFECT.CLOSE_TODAY and order.quantity > position.today_closable:
user_system_log.warn(_(
reason = _(
"Order Creation Failed: not enough today position {order_book_id} to close, target"
" quantity is {quantity}, closable today quantity is {closable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
closable=position.today_closable,
))
)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order._order_book_id, order=order, reason=reason))
return False
if order.position_effect == POSITION_EFFECT.CLOSE and order.quantity > position.closable:
user_system_log.warn(_(
reason = _(
"Order Creation Failed: not enough position {order_book_id} to close or exercise, target"
" sell quantity is {quantity}, closable quantity is {closable}").format(
order_book_id=order.order_book_id,
quantity=order.quantity,
closable=position.closable,
))
)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order.order_book_id, order=order, reason=reason))
return False
return True
10 changes: 5 additions & 5 deletions rqalpha/mod/rqalpha_mod_sys_risk/validators/cash_validator.py
Expand Up @@ -18,6 +18,7 @@
from rqalpha.interface import AbstractFrontendValidator
from rqalpha.const import POSITION_EFFECT
from rqalpha.utils.logger import user_system_log
from rqalpha.core.events import Event, EVENT

from rqalpha.utils.i18n import gettext as _

Expand All @@ -29,14 +30,13 @@ def is_cash_enough(env, order, cash, warn=False):
if cost_money <= cash:
return True
if warn:
user_system_log.warn(
_("Order Creation Failed: not enough money to buy {order_book_id}, needs {cost_money:.2f},"
reason = _("Order Creation Failed: not enough money to buy {order_book_id}, needs {cost_money:.2f},"
" cash {cash:.2f}").format(
order_book_id=order.order_book_id,
cost_money=cost_money,
cash=cash,
)
)
cash=cash)
user_system_log.warn(reason)
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order.order_book_id, order=order, reason=reason))
return False


Expand Down
Expand Up @@ -20,6 +20,8 @@
from rqalpha.utils.logger import user_system_log
from rqalpha.utils.i18n import gettext as _
from rqalpha.const import INSTRUMENT_TYPE
from rqalpha.core.events import Event, EVENT
from rqalpha.environment import Environment


class IsTradingValidator(AbstractFrontendValidator):
Expand All @@ -29,16 +31,19 @@ def __init__(self, env):
def can_submit_order(self, order, account=None):
instrument = self._env.data_proxy.instrument(order.order_book_id)
if instrument.type != INSTRUMENT_TYPE.INDX and not instrument.listing_at(self._env.trading_dt):
user_system_log.warn(_(u"Order Creation Failed: {order_book_id} is not listing!").format(
order_book_id=order.order_book_id,
))
reason = _(u"Order Creation Failed: {order_book_id} is not listing!").format(
order_book_id=order.order_book_id)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order._order_book_id, order=order, reason=reason))
return False

if instrument.type == 'CS' and self._env.data_proxy.is_suspended(order.order_book_id, self._env.trading_dt):
user_system_log.warn(_(u"Order Creation Failed: security {order_book_id} is suspended on {date}").format(
reason = _(u"Order Creation Failed: security {order_book_id} is suspended on {date}").format(
order_book_id=order.order_book_id,
date=self._env.trading_dt.date()
))
)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order._order_book_id, order=order, reason=reason))
return False

return True
Expand Down
Expand Up @@ -16,6 +16,8 @@
from rqalpha.interface import AbstractFrontendValidator
from rqalpha.const import ORDER_TYPE, POSITION_EFFECT
from rqalpha.utils.logger import user_system_log
from rqalpha.core.events import Event, EVENT
from rqalpha.environment import Environment

from rqalpha.utils.i18n import gettext as _

Expand All @@ -40,6 +42,7 @@ def can_submit_order(self, order, account=None):
limit_up=limit_up
)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order.order_book_id, order=order, reason=reason))
return False

limit_down = round(self._env.price_board.get_limit_down(order.order_book_id), 4)
Expand All @@ -53,6 +56,7 @@ def can_submit_order(self, order, account=None):
limit_down=limit_down
)
user_system_log.warn(reason)
Environment.get_instance().event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order._order_book_id, order=order, reason=reason))
return False

return True
Expand Down
7 changes: 7 additions & 0 deletions rqalpha/model/order.py
Expand Up @@ -25,6 +25,7 @@
from rqalpha.utils.repr import property_repr, properties
from rqalpha.utils.logger import user_system_log
from rqalpha.environment import Environment
from rqalpha.core.events import Event, EVENT


class Order(object):
Expand Down Expand Up @@ -325,13 +326,19 @@ def mark_rejected(self, reject_reason):
self._message = reject_reason
self._status = ORDER_STATUS.REJECTED
user_system_log.warn(reject_reason)
Environment.get_instance().event_bus.publish_event(
Event(EVENT.ORDER_CREATION_REJECT, order_book_id=self.order_book_id, order=self, reason=reject_reason)
)

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)
Environment.get_instance().event_bus.publish_event(
Event(EVENT.ORDER_CREATION_REJECT, order_book_id=self.order_book_id, order=self, reason=cancelled_reason)
)

def set_frozen_price(self, value):
self._frozen_price = value
Expand Down
1 change: 0 additions & 1 deletion rqalpha/portfolio/account.py
Expand Up @@ -101,7 +101,6 @@ def register_event(self):
EVENT.TRADE, lambda e: self.apply_trade(e.trade, e.order) if e.account == self else None
)
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)

Expand Down

0 comments on commit e86a99e

Please sign in to comment.