Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# check amount
if abs(amount) < int(self.data_proxy.instrument(order_book_id).round_lot):
return False, _("Order Rejected: amount must over 100 for {order_book_id} ").format(
order_book_id=order_book_id,
)
# check money is enough
if is_buy and close_price * amount > self.account.portfolio.cash:
return False, _("Order Rejected: no enough money to buy {order_book_id}, needs {cost_money:.2f}, cash {cash:.2f}").format(
order_book_id=order_book_id,
cost_money=cost_money,
cash=portfolio.cash,
)
if order.quantity < 0 and abs(order.quantity) > position.sellable:
return False, _("Order Rejected: no enough stock {order_book_id} to sell, you want to sell {quantity}, sellable {sellable}").format(
order_book_id=order_book_id,
quantity=abs(order.quantity),
sellable=position.sellable,
)
# # TODO check whether is limit up or limit down
# # FIXME need to handle ST 5%
# last_close = self.data_proxy.history(order_book_id, 2, "1d", "close").iloc[-2]
# if is_buy and price >= last_close * 1.1:
# return False, _("Order Rejected: {order_book_id} is limit up.").format(
# order_book_id=order_book_id,
# )
# elif not is_buy and price <= last_close * 0.9:
# return False, _("Order Rejected: {order_book_id} is limit down.").format(
# order_book_id=order_book_id,
# )
def reject_all_open_orders(self):
for order_book_id, order_list in iteritems(self.open_orders):
for order in order_list:
user_log.warn(_("Order Rejected: {order_book_id} can not match, {order_list}").format(
order_book_id=order_book_id,
order_list=order_list,
))
order.mark_rejected(_("market close"))
del order_list[:]
# check whether is trading
if not bar.is_trading:
return False, _("Order Rejected: {order_book_id} is not trading.").format(
order_book_id=order_book_id,
)
# handle limit order
if self.trading_params.frequency == "1d":
if isinstance(order.style, LimitOrder):
limit_price = order.style.get_limit_price(is_buy)
if is_buy and limit_price < bar.close:
return False, _("Order Rejected: price is too low to buy {order_book_id}").format(
order_book_id=order_book_id)
elif not is_buy and limit_price > bar.close:
return False, _("Order Rejected: price is too high to sell {order_book_id}").format(
order_book_id=order_book_id)
else:
raise NotImplementedError
# check amount
if abs(amount) < int(self.data_proxy.instrument(order_book_id).round_lot):
return False, _("Order Rejected: amount must over 100 for {order_book_id} ").format(
order_book_id=order_book_id,
)
# check money is enough
if is_buy and close_price * amount > self.account.portfolio.cash:
return False, _("Order Rejected: no enough money to buy {order_book_id}, needs {cost_money:.2f}, cash {cash:.2f}").format(
order_book_id=order_book_id,
cost_money=cost_money,
cash=portfolio.cash,
price = self.account.slippage_decider.get_trade_price(self.data_proxy, order)
cost_money = price * amount
is_buy = amount > 0
# check whether is trading
if not bar.is_trading:
return False, _("Order Rejected: {order_book_id} is not trading.").format(
order_book_id=order_book_id,
)
# handle limit order
if self.trading_params.frequency == "1d":
if isinstance(order.style, LimitOrder):
limit_price = order.style.get_limit_price(is_buy)
if is_buy and limit_price < bar.close:
return False, _("Order Rejected: price is too low to buy {order_book_id}").format(
order_book_id=order_book_id)
elif not is_buy and limit_price > bar.close:
return False, _("Order Rejected: price is too high to sell {order_book_id}").format(
order_book_id=order_book_id)
else:
raise NotImplementedError
# check amount
if abs(amount) < int(self.data_proxy.instrument(order_book_id).round_lot):
return False, _("Order Rejected: amount must over 100 for {order_book_id} ").format(
order_book_id=order_book_id,
)
# check money is enough
if is_buy and close_price * amount > self.account.portfolio.cash:
return False, _("Order Rejected: no enough money to buy {order_book_id}, needs {cost_money:.2f}, cash {cash:.2f}").format(
# # TODO check whether is limit up or limit down
# # FIXME need to handle ST 5%
# last_close = self.data_proxy.history(order_book_id, 2, "1d", "close").iloc[-2]
# if is_buy and price >= last_close * 1.1:
# return False, _("Order Rejected: {order_book_id} is limit up.").format(
# order_book_id=order_book_id,
# )
# elif not is_buy and price <= last_close * 0.9:
# return False, _("Order Rejected: {order_book_id} is limit down.").format(
# order_book_id=order_book_id,
# )
# TODO check volume is over 25%
# FIXME might have mulitiple order
if amount > bar.volume * 0.25:
return False, _("Order Rejected: {order_book_id} volume is over 25%.").format(
order_book_id=order_book_id,
)
return True, None
portfolio = self.account.portfolio
positions = portfolio.positions
position = positions[order_book_id]
bar = bar_dict[order_book_id]
amount = order.quantity
close_price = bar.close
price = self.account.slippage_decider.get_trade_price(self.data_proxy, order)
cost_money = price * amount
is_buy = amount > 0
# check whether is trading
if not bar.is_trading:
return False, _("Order Rejected: {order_book_id} is not trading.").format(
order_book_id=order_book_id,
)
# handle limit order
if self.trading_params.frequency == "1d":
if isinstance(order.style, LimitOrder):
limit_price = order.style.get_limit_price(is_buy)
if is_buy and limit_price < bar.close:
return False, _("Order Rejected: price is too low to buy {order_book_id}").format(
order_book_id=order_book_id)
elif not is_buy and limit_price > bar.close:
return False, _("Order Rejected: price is too high to sell {order_book_id}").format(
order_book_id=order_book_id)
else:
raise NotImplementedError