Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if self._price_limit:
if order.side == SIDE.BUY and deal_price >= price_board.get_limit_up(order_book_id):
reason = _(
"Order Cancelled: current bar [{order_book_id}] reach the limit_up price."
).format(order_book_id=order.order_book_id)
order.mark_rejected(reason)
continue
if order.side == SIDE.SELL and deal_price <= price_board.get_limit_down(order_book_id):
reason = _(
"Order Cancelled: current bar [{order_book_id}] reach the limit_down price."
).format(order_book_id=order.order_book_id)
order.mark_rejected(reason)
continue
if self._liquidity_limit:
if order.side == SIDE.BUY and price_board.get_a1(order_book_id) == 0:
reason = _(
"Order Cancelled: [{order_book_id}] has no liquidity."
).format(order_book_id=order.order_book_id)
order.mark_rejected(reason)
continue
if order.side == SIDE.SELL and price_board.get_b1(order_book_id) == 0:
reason = _(
"Order Cancelled: [{order_book_id}] has no liquidity."
).format(order_book_id=order.order_book_id)
order.mark_rejected(reason)
continue
if self._volume_limit:
bar = self._env.bar_dict[order_book_id]
volume_limit = round(bar.volume * self._volume_percent) - self._turnover[order.order_book_id]
round_lot = instrument.round_lot
volume_limit = (volume_limit // round_lot) * round_lot
def _(txt):
return gettext(txt) if use_chinese_fonts else txt
def _get_day_bar_dt(self, date):
if self._env.config.base.market == MARKET.CN:
return date.replace(hour=15, minute=0)
elif self._env.config.base.market == MARKET.HK:
return date.replace(hour=16, minute=0)
else:
raise NotImplementedError(_("Unsupported market {}".format(self._env.config.base.market)))
if bar.isnan:
"""
只有未上市/已退市,对应的bar才为NaN
"""
instrument = env.get_instrument(order_book_id)
if trading_date < instrument.listed_date.date():
order._mark_rejected(_(u"Order Rejected: {order_book_id} is not listed!").format(
order_book_id=order_book_id,
))
elif trading_date > instrument.de_listed_date.date():
order._mark_rejected(_(u"Order Rejected: {order_book_id} has been delisted!").format(
order_book_id=order_book_id,
))
else:
order._mark_rejected(_(u"Order Rejected: {order_book_id} is not trading!").format(
order_book_id=order_book_id,
))
return False
elif not bar.is_trading:
"""
如果bar.is_trading为False,还需要判断是否为停盘,如果不是停牌,则说明交易量为0.
"""
if bar.suspended:
order._mark_rejected(_(u"Order Rejected: {order_book_id} is suspended!").format(
order_book_id=order_book_id,
))
return False
return True
def _are_valid_query_entities(self, func_name, entities):
from sqlalchemy.orm.attributes import InstrumentedAttribute
for e in entities:
if not isinstance(e, InstrumentedAttribute):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, should be entity like "
u"Fundamentals.balance_sheet.total_equity, got {} (type: {})").format(
func_name, self.arg_name, e, type(e)
))
def parse_run_type(rt_str):
assert isinstance(rt_str, six.string_types)
mapping = {
"b": RUN_TYPE.BACKTEST,
"p": RUN_TYPE.PAPER_TRADING,
"r": RUN_TYPE.LIVE_TRADING,
}
try:
return mapping[rt_str]
except KeyError:
raise RuntimeError(_(u"unknown run type: {}").format(rt_str))
def raise_not_valid_future_error(self, func_name, arg_name, value):
raise RQInvalidArgument(
_(u"function {}: invalid {} argument, expect a valid future instrument/order_book_id/symbol, "
u"got {} (type: {})").format(
func_name, self._arg_name, value, type(value)
))
return
config = env.config
start_date = config.base.start_date
end_date = config.base.end_date
if instrument.listed_date.date() > start_date:
raise patch_user_exc(ValueError(
_(u"benchmark {benchmark} has not been listed on {start_date}").format(benchmark=bechmark_order_book_id,
start_date=start_date)))
if instrument.de_listed_date.date() < end_date:
if config.base.run_type == RUN_TYPE.BACKTEST:
msg = _(u"benchmark {benchmark} has been de_listed on {end_date}").format(
benchmark=bechmark_order_book_id,
end_date=end_date)
else:
msg = _(u"the target {benchmark} will be delisted in the short term. "
u"please choose a sustainable target.").format(
benchmark=bechmark_order_book_id)
raise patch_user_exc(ValueError(msg))