Skip to content

Commit

Permalink
pr update
Browse files Browse the repository at this point in the history
  • Loading branch information
Lin-Dongzhao committed Mar 29, 2024
1 parent 8c67bed commit 70a2b6f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion rqalpha/apis/api_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def submit_order(id_or_ins, amount, side, price=None, position_effect=None):
if not is_valid_price(market_price):
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, reason=reason))
return

amount = int(amount)
Expand Down
10 changes: 5 additions & 5 deletions rqalpha/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from itertools import chain

import rqalpha
from rqalpha.core.events import EventBus
from rqalpha.core.events import EventBus, Event, EVENT
from rqalpha.const import INSTRUMENT_TYPE
from rqalpha.utils.logger import system_log, user_log, user_system_log
from rqalpha.core.global_var import GlobalVars
Expand Down Expand Up @@ -114,9 +114,7 @@ def _get_frontend_validators(self, instrument_type):
return chain(self._frontend_validators.get(instrument_type, []), self._default_frontend_validators)

def submit_order(self, order):
instrument_type = self.data_proxy.instrument(order.order_book_id).type
account = self.portfolio.get_account(order.order_book_id)
if all(v.can_submit_order(order, account) for v in self._get_frontend_validators(instrument_type)):
if self.can_submit_order(order):
self.broker.submit_order(order)
return order

Expand Down Expand Up @@ -184,6 +182,8 @@ def can_submit_order(self, order):
instrument_type = self.data_proxy.instrument(order.order_book_id).type
account = self.portfolio.get_account(order.order_book_id)
for v in self._get_frontend_validators(instrument_type):
if not v.can_submit_order(order, account):
result = v.can_submit_order(order, account)
if not (result is True):
self.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order.order_book_id, reason=result))
return False
return True
1 change: 1 addition & 0 deletions rqalpha/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ class AbstractFrontendValidator(with_metaclass(abc.ABCMeta)):
def can_submit_order(self, order, account=None):
"""
判断是否可以下单
:return: 可以下单返回 `True`, 不可下单返回原因
"""
raise NotImplementedError

Expand Down
30 changes: 24 additions & 6 deletions rqalpha/mod/rqalpha_mod_sys_accounts/api/api_stock.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a
if not is_valid_price(price):
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, reason=reason))
return

if (side == SIDE.BUY and current_quantity != -amount) or (side == SIDE.SELL and current_quantity != abs(amount)):
Expand All @@ -113,7 +113,7 @@ def _submit_order(ins, amount, side, position_effect, style, current_quantity, a
if amount == 0:
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, 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 Down Expand Up @@ -142,7 +142,7 @@ def _order_value(account, position, ins, cash_amount, style):
if not is_valid_price(price):
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, reason=reason))
return

amount = int(Decimal(cash_amount) / Decimal(price))
Expand All @@ -159,7 +159,7 @@ def _order_value(account, position, ins, cash_amount, style):
else:
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, reason=reason))
return

if amount < 0:
Expand Down Expand Up @@ -192,26 +192,44 @@ def stock_order_percent(id_or_ins, percent, price_or_style=None, price=None, sty

@order_target_value.register(INST_TYPE_IN_STOCK_ACCOUNT)
def stock_order_target_value(id_or_ins, cash_amount, price_or_style=None, price=None, style=None):
env = Environment.get_instance()
account, position, ins = _get_account_position_ins(id_or_ins)
open_style, close_style = calc_open_close_style(price, style, price_or_style)
if cash_amount == 0:
return _submit_order(
ins, position.closable, SIDE.SELL, POSITION_EFFECT.CLOSE, close_style, position.quantity, False
)
_delta = cash_amount - position.market_value
price = env.data_proxy.get_last_price(ins.order_book_id)
if not is_valid_price(price):
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, reason=reason))
amount = int(Decimal(_delta) / Decimal(price))
if _round_order_quantity(ins, amount) == 0:
return
_style = open_style if _delta > 0 else close_style
return _order_value(account, position, ins, _delta, _style)


@order_target_percent.register(INST_TYPE_IN_STOCK_ACCOUNT)
def stock_order_target_percent(id_or_ins, percent, price_or_style=None, price=None, style=None):
env = Environment.get_instance()
account, position, ins = _get_account_position_ins(id_or_ins)
open_style, close_style = calc_open_close_style(price, style, price_or_style)
if percent == 0:
return _submit_order(
ins, position.closable, SIDE.SELL, POSITION_EFFECT.CLOSE, close_style, position.quantity, False
)
_delta = account.total_value * percent - position.market_value
price = env.data_proxy.get_last_price(ins.order_book_id)
if not is_valid_price(price):
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, reason=reason))
amount = int(Decimal(_delta) / Decimal(price))
if _round_order_quantity(ins, amount) == 0:
return
_style = open_style if _delta > 0 else close_style
return _order_value(account, position, ins, _delta, _style)

Expand Down Expand Up @@ -346,7 +364,7 @@ def order_target_portfolio(
if not is_valid_price(last_price):
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=ins.order_book_id, reason=reason))
continue

price_or_style = price_or_styles.get(ins.order_book_id)
Expand Down Expand Up @@ -386,7 +404,7 @@ def order_target_portfolio(
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))
env.event_bus.publish_event(Event(EVENT.ORDER_CREATION_REJECT, order_book_id=order_book_id, reason=reason))
continue

delta_quantity = (account_value * target_percent / close_price) - current_quantities.get(order_book_id, 0)
Expand Down
8 changes: 2 additions & 6 deletions rqalpha/mod/rqalpha_mod_sys_accounts/position_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
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 @@ -48,8 +46,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))
return False
return reason
if order.position_effect == POSITION_EFFECT.CLOSE and order.quantity > position.closable:
reason = _(
"Order Creation Failed: not enough position {order_book_id} to close or exercise, target"
Expand All @@ -59,6 +56,5 @@ 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))
return False
return reason
return True
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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 @@ -36,7 +35,7 @@ def is_cash_enough(env, order, cash, warn=False):
cost_money=cost_money,
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 reason
return False


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
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 @@ -34,17 +32,15 @@ def can_submit_order(self, order, account=None):
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
return reason

if instrument.type == 'CS' and self._env.data_proxy.is_suspended(order.order_book_id, self._env.trading_dt):
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 reason

return True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
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 @@ -42,8 +40,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
return reason

limit_down = round(self._env.price_board.get_limit_down(order.order_book_id), 4)
if order.price < limit_down:
Expand All @@ -56,8 +53,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 reason

return True

Expand Down
7 changes: 0 additions & 7 deletions rqalpha/model/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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 @@ -326,19 +325,13 @@ 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

0 comments on commit 70a2b6f

Please sign in to comment.