From ec90e7b0d86d7c6b428aefe10a6e0e9bde50d612 Mon Sep 17 00:00:00 2001 From: "lin.dongzhao" <542698096@qq.com> Date: Mon, 18 Mar 2024 17:43:42 +0800 Subject: [PATCH 1/4] develop --- rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py | 3 +++ .../rqalpha_mod_sys_risk/validators/is_trading_validator.py | 1 + rqalpha/portfolio/account.py | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py index 6cea19437..9f8fdf29f 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py @@ -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 @@ -102,6 +103,7 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a 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)) + env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None)) return if (side == SIDE.BUY and current_quantity != -amount) or (side == SIDE.SELL and current_quantity != abs(amount)): @@ -112,6 +114,7 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a 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)) + env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None)) return order = Order.__from_create__(ins.order_book_id, abs(amount), side, style, position_effect) if side == SIDE.BUY and auto_switch_order_value: diff --git a/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py b/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py index 46da9915e..cf0e2c10a 100644 --- a/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py @@ -22,6 +22,7 @@ from rqalpha.const import INSTRUMENT_TYPE + class IsTradingValidator(AbstractFrontendValidator): def __init__(self, env): self._env = env diff --git a/rqalpha/portfolio/account.py b/rqalpha/portfolio/account.py index c5e1d6a11..0e3488b0e 100644 --- a/rqalpha/portfolio/account.py +++ b/rqalpha/portfolio/account.py @@ -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) From 1dae3e466bfe6d2a8e7b8bc592ada60e46f9a4c2 Mon Sep 17 00:00:00 2001 From: "lin.dongzhao" <542698096@qq.com> Date: Fri, 22 Mar 2024 17:21:51 +0800 Subject: [PATCH 2/4] developing --- .../rqalpha_mod_sys_accounts/api/api_stock.py | 6 +++--- .../position_validator.py | 14 ++++++++++---- .../validators/cash_validator.py | 10 +++++----- .../validators/is_trading_validator.py | 16 ++++++++++------ .../validators/price_validator.py | 4 ++++ 5 files changed, 32 insertions(+), 18 deletions(-) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py index 9f8fdf29f..745940994 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py @@ -101,9 +101,9 @@ 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)) - env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None)) + 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)): diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py b/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py index 8af82f932..6ea6cd6aa 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py @@ -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 _ @@ -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 diff --git a/rqalpha/mod/rqalpha_mod_sys_risk/validators/cash_validator.py b/rqalpha/mod/rqalpha_mod_sys_risk/validators/cash_validator.py index 43c2d1503..3090407cd 100644 --- a/rqalpha/mod/rqalpha_mod_sys_risk/validators/cash_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_risk/validators/cash_validator.py @@ -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 _ @@ -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 diff --git a/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py b/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py index cf0e2c10a..80d9e6e86 100644 --- a/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_risk/validators/is_trading_validator.py @@ -20,7 +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): @@ -30,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 diff --git a/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py b/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py index 353a9466f..b540e6e81 100644 --- a/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py @@ -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 _ @@ -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) @@ -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 From 5415a72658f219a2f8d0769022e1c80c61939a08 Mon Sep 17 00:00:00 2001 From: "lin.dongzhao" <542698096@qq.com> Date: Tue, 26 Mar 2024 11:38:55 +0800 Subject: [PATCH 3/4] in development --- rqalpha/apis/api_base.py | 8 +++----- .../rqalpha_mod_sys_accounts/api/api_stock.py | 19 +++++++++---------- .../position_validator.py | 4 ++-- .../validators/price_validator.py | 4 ++-- rqalpha/model/order.py | 7 +++++++ 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/rqalpha/apis/api_base.py b/rqalpha/apis/api_base.py index 7b2aff5a4..1ee9bf271 100644 --- a/rqalpha/apis/api_base.py +++ b/rqalpha/apis/api_base.py @@ -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) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py index 745940994..676616d3d 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py @@ -111,10 +111,9 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a 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)) - env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, order=None)) + 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: @@ -141,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)) @@ -158,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: diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py b/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py index 6ea6cd6aa..c017f66a0 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py @@ -48,7 +48,7 @@ def can_submit_order(self, order, account=None): 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)) + 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: reason = _( @@ -59,6 +59,6 @@ def can_submit_order(self, order, account=None): 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)) + 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 diff --git a/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py b/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py index b540e6e81..112c36bd3 100644 --- a/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py +++ b/rqalpha/mod/rqalpha_mod_sys_risk/validators/price_validator.py @@ -42,7 +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)) + 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) @@ -56,7 +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)) + 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 diff --git a/rqalpha/model/order.py b/rqalpha/model/order.py index dde9b1a04..4bc8645af 100644 --- a/rqalpha/model/order.py +++ b/rqalpha/model/order.py @@ -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): @@ -325,6 +326,9 @@ 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(): @@ -332,6 +336,9 @@ def mark_cancelled(self, cancelled_reason, user_warn=True): 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 From cddf7d32c31417b5dd47731f77b5a191a7cc86aa Mon Sep 17 00:00:00 2001 From: "lin.dongzhao" <542698096@qq.com> Date: Wed, 27 Mar 2024 11:52:01 +0800 Subject: [PATCH 4/4] in development --- .../api/api_future.py | 33 ++++++++++--------- .../rqalpha_mod_sys_accounts/api/api_stock.py | 15 ++++----- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_future.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_future.py index c4e13a4c0..e601cd934 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_future.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_future.py @@ -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__ = [] @@ -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.")) @@ -62,23 +65,23 @@ 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 @@ -86,10 +89,10 @@ def _submit_order(id_or_ins, amount, side, position_effect, style): 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: diff --git a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py index 676616d3d..f50239011 100644 --- a/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py +++ b/rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py @@ -344,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) @@ -382,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)