Skip to content

Commit

Permalink
[OrderGroup] try to find matching quantity first
Browse files Browse the repository at this point in the history
tmp
  • Loading branch information
techfreaque committed Mar 9, 2023
1 parent 6b5732d commit be747a8
Showing 1 changed file with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ async def _balance_orders(self, closed_order, ignored_orders):
for order in take_profit_actions[self.CANCEL] + stop_actions[self.CANCEL]:
self.logger.debug(f"Cancelling order to keep balance, order: {order} as {closed_order} is closed")
async with signals.remote_signal_publisher(order.trader.exchange_manager, order.symbol, True):
if order.is_open():
if order.is_open():
# only close open
await signals.cancel_order(order.trader.exchange_manager,
signals.should_emit_trading_signal(order.trader.exchange_manager),
Expand Down Expand Up @@ -139,9 +139,12 @@ def _get_balance(self, closed_order, ignored_orders):
self.STOP: _SideBalance()
}
for order in self.get_group_open_orders():
if order is not closed_order \
and (ignored_orders is None or order not in ignored_orders) \
and order not in self.balancing_orders:
if ((not closed_order
or order.order_id is not (closed_order.order_id
if closed_order
else None))
and (ignored_orders is None or order not in ignored_orders)
and order not in self.balancing_orders):
if order_util.is_stop_order(order.order_type):
balance[self.STOP].add_order(order)
else:
Expand Down Expand Up @@ -172,16 +175,30 @@ def get_actions_to_balance(self, target_balance):
return actions
to_be_reduced_amount = constants.ZERO
remaining_orders = list(self.orders)
amount_to_reduce = balance - target_balance

# try to find order with the same quantity
matching_quantity_order_index = None
for index, order in enumerate(remaining_orders):
if order.origin_quantity == amount_to_reduce:
matching_quantity_order_index = index
break

while remaining_orders and balance - to_be_reduced_amount > target_balance:
order_quantity = remaining_orders[0].origin_quantity
if matching_quantity_order_index is not None:
order_to_check = matching_quantity_order_index
matching_quantity_order_index = None
else:
order_to_check = 0
order_quantity = remaining_orders[order_to_check].origin_quantity
if balance - to_be_reduced_amount - order_quantity >= target_balance:
# cancel order and keep reducing
actions[BalancedTakeProfitAndStopOrderGroup.CANCEL].append(remaining_orders.pop(0))
actions[BalancedTakeProfitAndStopOrderGroup.CANCEL].append(remaining_orders.pop(order_to_check))
to_be_reduced_amount += order_quantity
else:
# update order and stop reducing
actions[BalancedTakeProfitAndStopOrderGroup.UPDATE].append({
BalancedTakeProfitAndStopOrderGroup.ORDER: remaining_orders.pop(0),
BalancedTakeProfitAndStopOrderGroup.ORDER: remaining_orders.pop(order_to_check),
BalancedTakeProfitAndStopOrderGroup.UPDATED_QUANTITY:
target_balance - (balance - to_be_reduced_amount - order_quantity)
})
Expand Down

0 comments on commit be747a8

Please sign in to comment.