Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
user_system_log.warn(
_(u"Order Creation Failed: close amount {amount} is larger than position "
u"quantity {quantity}").format(amount=amount, quantity=buy_quantity)
)
return []
buy_old_quantity = buy_old_quantity
if amount > buy_old_quantity:
if buy_old_quantity != 0:
orders.append(Order.__from_create__(
order_book_id,
buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE
))
orders.append(Order.__from_create__(
order_book_id,
amount - buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE_TODAY
))
else:
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
POSITION_EFFECT.CLOSE
))
else:
orders.append(Order.__from_create__(
sell_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE
))
# 剩下还有仓位,则创建一个 POSITION_EFFECT.CLOSE_TODAY 的平今单
orders.append(Order.__from_create__(
order_book_id,
amount - sell_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE_TODAY
))
else:
# 创建 POSITION_EFFECT.CLOSE 的平仓单
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
POSITION_EFFECT.CLOSE
))
else:
if env.portfolio:
position = env.portfolio.positions[order_book_id]
buy_quantity, buy_old_quantity = position.buy_quantity, position.buy_old_quantity
else:
position = env.booking.get_position(order_book_id, POSITION_DIRECTION.LONG)
buy_quantity, buy_old_quantity = position.quantity, position.old_quantity
if amount > buy_quantity:
user_system_log.warn(
else:
position = env.booking.get_position(order_book_id, POSITION_DIRECTION.SHORT)
sell_quantity, sell_old_quantity = position.quantity, position.old_quantity
# 如果平仓量大于持仓量,则 Warning 并 取消订单创建
if amount > sell_quantity:
user_system_log.warn(
_(u"Order Creation Failed: close amount {amount} is larger than position "
u"quantity {quantity}").format(amount=amount, quantity=sell_quantity)
)
return []
sell_old_quantity = sell_old_quantity
if amount > sell_old_quantity:
if sell_old_quantity != 0:
# 如果有昨仓,则创建一个 POSITION_EFFECT.CLOSE 的平仓单
orders.append(Order.__from_create__(
order_book_id,
sell_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE
))
# 剩下还有仓位,则创建一个 POSITION_EFFECT.CLOSE_TODAY 的平今单
orders.append(Order.__from_create__(
order_book_id,
amount - sell_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE_TODAY
))
else:
# 创建 POSITION_EFFECT.CLOSE 的平仓单
def send_order(order_book_id, amount, side, position_effect, style):
env = Environment.get_instance()
order = Order.__from_create__(order_book_id, amount, side, style, position_effect)
env.broker.submit_order(order)
return order
order_book_id,
amount - buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE_TODAY
))
else:
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
POSITION_EFFECT.CLOSE
))
else:
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
position_effect
))
if not is_valid_price(price):
user_system_log.warn(
_(u"Order Creation Failed: [{order_book_id}] No market data").format(order_book_id=order_book_id))
return []
if len(orders) > 1:
user_system_log.warn(_(
"Order was separated, original order: {original_order_repr}, new orders: [{new_orders_repr}]").format(
original_order_repr="Order(order_book_id={}, quantity={}, side={}, position_effect={})".format(
orders.append(Order.__from_create__(
order_book_id,
buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE
))
orders.append(Order.__from_create__(
order_book_id,
amount - buy_old_quantity,
side,
style,
POSITION_EFFECT.CLOSE_TODAY
))
else:
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
POSITION_EFFECT.CLOSE
))
else:
orders.append(Order.__from_create__(
order_book_id,
amount,
side,
style,
position_effect
))
if not is_valid_price(price):