Skip to content

Commit

Permalink
Merge pull request #638 from mar-heaven/counterparty_offer
Browse files Browse the repository at this point in the history
Counterparty offer
  • Loading branch information
Cuizi7 committed Jun 16, 2021
2 parents 5ce13b9 + 1275bdd commit 2afd22c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 41 deletions.
28 changes: 0 additions & 28 deletions rqalpha/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,34 +239,6 @@ def get_b1(self, order_book_id):
"""
raise NotImplementedError

def get_ask_vols(self, order_book_id):
# type: (str) -> list
"""
获取合约的多档卖方申报量
"""
raise NotImplementedError

def get_bid_vols(self, order_book_id):
# type: (str) -> list
"""
获取合约的多档买方申报量
"""
raise NotImplementedError

def get_ask_prices(self, order_book_id):
# type: (str) -> list
"""
获取合约的多档卖方申报价
"""
raise NotImplementedError

def get_bid_prices(self, order_book_id):
# type: (str) -> list
"""
获取合约的多档买方申报价
"""
raise NotImplementedError


class AbstractDataSource(object):
"""
Expand Down
22 changes: 9 additions & 13 deletions rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ def __init__(self, env, mod_config):
self._b_volume = {}
self._a_price = {}
self._b_price = {}
self._env.event_bus.add_listener(EVENT.TICK, self._pre_tick)

def match(self, account, order, open_auction):
# type: (Account, Order, bool) -> None
Expand All @@ -260,7 +261,6 @@ def match(self, account, order, open_auction):
按照该tick,a1,b1进行成交,剩余订单直接撤单
"""
order_book_id = order.order_book_id
self._pre_match(order_book_id)

self._pop_volume_and_price(order)
if order.side == SIDE.BUY:
Expand Down Expand Up @@ -318,16 +318,12 @@ def match(self, account, order, open_auction):
self._b_volume[order.order_book_id][0] -= min(amount, fill)

if order.type == ORDER_TYPE.MARKET and order.unfilled_quantity != 0:
reason = _(
u"Order Cancelled: market order {order_book_id} volume {order_volume} is"
u" larger than {volume_percent_limit} percent of current bar volume, fill {filled_volume} actually"
).format(
reason = _("Order Cancelled: market order {order_book_id} fill {filled_volume} actually").format(
order_book_id=order.order_book_id,
order_volume=order.quantity,
filled_volume=order.filled_quantity,
volume_percent_limit=self._volume_percent * 100.0
)
order.mark_cancelled(reason)
return
if order.unfilled_quantity != 0:
self.match(account, order, open_auction)

Expand All @@ -344,13 +340,13 @@ def _pop_volume_and_price(self, order):
except IndexError:
return

def _pre_match(self, order_book_id):
if self._a_volume.get(order_book_id) is None:
self._a_volume[order_book_id] = self._env.price_board.get_ask_vols(order_book_id)
self._b_volume[order_book_id] = self._env.price_board.get_bid_vols(order_book_id)
def _pre_tick(self, event):
order_book_id = event.tick.order_book_id
self._a_volume[order_book_id] = event.tick.ask_vols
self._b_volume[order_book_id] = event.tick.bid_vols

self._a_price[order_book_id] = self._env.price_board.get_ask_prices(order_book_id)
self._b_price[order_book_id] = self._env.price_board.get_bid_prices(order_book_id)
self._a_price[order_book_id] = event.tick.asks
self._b_price[order_book_id] = event.tick.bids

def update(self):
pass
6 changes: 6 additions & 0 deletions rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def _account_order_from_state(order_state):
self._open_auction_orders = [_account_order_from_state(v) for v in value.get("open_auction_orders", [])]

def submit_order(self, order):
self._check_subscribe(order)
if order.position_effect == POSITION_EFFECT.MATCH:
raise TypeError(_("unsupported position_effect {}").format(order.position_effect))
account = self._env.get_account(order.order_book_id)
Expand Down Expand Up @@ -170,3 +171,8 @@ def _match(self, order_book_id=None):
for account, order in final_orders:
if order.status == ORDER_STATUS.REJECTED or order.status == ORDER_STATUS.CANCELLED:
self._env.event_bus.publish_event(Event(EVENT.ORDER_UNSOLICITED_UPDATE, account=account, order=order))

def _check_subscribe(self, order):
if self._env.config.base.frequency == "tick" and order.order_book_id not in self._env.get_universe():
raise RuntimeError(_("{order_book_id} should be subscribed when frequency is tick.").format(
order_book_id=order.order_book_id))
Binary file modified rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.mo
Binary file not shown.
10 changes: 10 additions & 0 deletions rqalpha/utils/translations/zh_Hans_CN/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,12 @@ msgstr ""
"{order_book_id} 下单量 {order_volume} 超过当前 Bar 成交量的 "
"{volume_percent_limit}%,实际成交 {filled_volume}"

#: rqalpha/mod/rqalpha_mod_sys_simulation/matcher.py:226
msgid ""
"Order Cancelled: market order {order_book_id} fill {filled_volume} actually"
msgstr ""
"订单取消:标的 {order_book_id} 实际成交 {filled_volume}"

#: rqalpha/mod/rqalpha_mod_sys_simulation/mod.py:42
msgid "invalid margin multiplier value: value range is (0, +∞]"
msgstr "无效的 保证金乘数 设置: 其值范围为 (0, +∞]"
Expand Down Expand Up @@ -506,6 +512,10 @@ msgstr "订单 {order_id} 被手动取消。"
msgid "Order Rejected: {order_book_id} can not match. Market close."
msgstr "订单被拒单: {order_book_id} 当天交易结束,订单无法成交。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/simulation_broker.py:177
msgid "{order_book_id} should be subscribed when frequency is tick."
msgstr "tick回测下单失败,请使用 subscribe 订阅合约 {order_book_id}。"

#: rqalpha/mod/rqalpha_mod_sys_simulation/simulation_event_source.py:44
#: rqalpha/mod/rqalpha_mod_sys_simulation/simulation_event_source.py:72
msgid "Unsupported market {}"
Expand Down

0 comments on commit 2afd22c

Please sign in to comment.