1616# 详细的授权流程,请联系 public@ricequant.com 获取。
1717import datetime
1818from collections import defaultdict
19- import math
2019from rqalpha .const import MATCHING_TYPE , ORDER_TYPE , POSITION_EFFECT , SIDE
2120from rqalpha .environment import Environment
2221from rqalpha .core .events import EVENT , Event
2524from rqalpha .model .tick import TickObject
2625from rqalpha .portfolio .account import Account
2726from rqalpha .utils import is_valid_price
28- from rqalpha .interface import AbstractPriceBoard
27+ from rqalpha .utils . price_limits import reaches_limit
2928from typing import Dict
3029from rqalpha .utils .i18n import gettext as _
3130from .slippage import SlippageDecider
3231
3332
34- LIMIT_PRICE_VALID_THRESHOLD = 1e-7
35-
36-
37- def _price_reaches_limit (order_book_id : str , side : SIDE , deal_price : float , price_board : AbstractPriceBoard ):
38- if side == SIDE .BUY :
39- limit_price = price_board .get_limit_up (order_book_id )
40- return deal_price >= limit_price or math .isclose (deal_price , limit_price , abs_tol = LIMIT_PRICE_VALID_THRESHOLD )
41- elif side == SIDE .SELL :
42- limit_price = price_board .get_limit_down (order_book_id )
43- return deal_price <= limit_price or math .isclose (deal_price , limit_price , abs_tol = LIMIT_PRICE_VALID_THRESHOLD )
44- else :
45- raise ValueError (f"Unsupport side: { side } " )
46-
47-
4833class AbstractMatcher :
4934 def match (self , account , order , open_auction ):
5035 # type: (Account, Order, bool) -> None
@@ -125,6 +110,7 @@ def match(self, account, order, open_auction):
125110 raise NotImplementedError
126111 order_book_id = order .order_book_id
127112 instrument = self ._env .get_instrument (order_book_id )
113+ tick_size = self ._env .data_proxy .get_tick_size (order_book_id )
128114
129115 deal_price = self ._get_deal_price (order , open_auction )
130116
@@ -154,11 +140,11 @@ def match(self, account, order, open_auction):
154140 return
155141 # 是否限制涨跌停不成交
156142 if self ._price_limit :
157- if _price_reaches_limit (order_book_id , order .side , deal_price , price_board ):
143+ if reaches_limit (order_book_id , deal_price , order .side , price_board , tick_size ):
158144 return
159145 else :
160146 if self ._price_limit :
161- if _price_reaches_limit (order_book_id , order .side , deal_price , price_board ):
147+ if reaches_limit (order_book_id , deal_price , order .side , price_board , tick_size ):
162148 reason = _ (
163149 "Order Cancelled: current bar [{order_book_id}] reach the {limit_up_or_down} price."
164150 ).format (order_book_id = order .order_book_id , limit_up_or_down = "limit_up" if order .side == SIDE .BUY else "limit_down" )
@@ -330,6 +316,7 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
330316 # 标的信息
331317 order_book_id = order .order_book_id
332318 instrument = self ._env .get_instrument (order_book_id )
319+ tick_size = self ._env .data_proxy .get_tick_size (order_book_id )
333320
334321 # 获取tick数据
335322 _cur_tick = self ._cur_tick .get (order_book_id )
@@ -375,7 +362,7 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
375362 return
376363 # 是否限制涨跌停不成交
377364 if self ._price_limit :
378- if _price_reaches_limit (order_book_id , order .side , deal_price , price_board ):
365+ if reaches_limit (order_book_id , deal_price , order .side , price_board , tick_size ):
379366 return
380367 if self ._liquidity_limit :
381368 if order .side == SIDE .BUY and price_board .get_a1 (order_book_id ) == 0 :
@@ -384,7 +371,7 @@ def match(self, account, order, open_auction): # type: (Account, Order, bool) -
384371 return
385372 else :
386373 if self ._price_limit :
387- if _price_reaches_limit (order_book_id , order .side , deal_price , price_board ):
374+ if reaches_limit (order_book_id , deal_price , order .side , price_board , tick_size ):
388375 reason = _ (
389376 "Order Cancelled: current tick [{order_book_id}] reach the {limit_up_or_down} price."
390377 ).format (order_book_id = order .order_book_id , limit_up_or_down = "limit_up" if order .side == SIDE .BUY else "limit_down" )
0 commit comments