Skip to content

Commit be747a8

Browse files
committed
[OrderGroup] try to find matching quantity first
tmp
1 parent 6b5732d commit be747a8

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

octobot_trading/personal_data/orders/groups/balanced_take_profit_and_stop_order_group.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ async def _balance_orders(self, closed_order, ignored_orders):
106106
for order in take_profit_actions[self.CANCEL] + stop_actions[self.CANCEL]:
107107
self.logger.debug(f"Cancelling order to keep balance, order: {order} as {closed_order} is closed")
108108
async with signals.remote_signal_publisher(order.trader.exchange_manager, order.symbol, True):
109-
if order.is_open():
109+
if order.is_open():
110110
# only close open
111111
await signals.cancel_order(order.trader.exchange_manager,
112112
signals.should_emit_trading_signal(order.trader.exchange_manager),
@@ -139,9 +139,12 @@ def _get_balance(self, closed_order, ignored_orders):
139139
self.STOP: _SideBalance()
140140
}
141141
for order in self.get_group_open_orders():
142-
if order is not closed_order \
143-
and (ignored_orders is None or order not in ignored_orders) \
144-
and order not in self.balancing_orders:
142+
if ((not closed_order
143+
or order.order_id is not (closed_order.order_id
144+
if closed_order
145+
else None))
146+
and (ignored_orders is None or order not in ignored_orders)
147+
and order not in self.balancing_orders):
145148
if order_util.is_stop_order(order.order_type):
146149
balance[self.STOP].add_order(order)
147150
else:
@@ -172,16 +175,30 @@ def get_actions_to_balance(self, target_balance):
172175
return actions
173176
to_be_reduced_amount = constants.ZERO
174177
remaining_orders = list(self.orders)
178+
amount_to_reduce = balance - target_balance
179+
180+
# try to find order with the same quantity
181+
matching_quantity_order_index = None
182+
for index, order in enumerate(remaining_orders):
183+
if order.origin_quantity == amount_to_reduce:
184+
matching_quantity_order_index = index
185+
break
186+
175187
while remaining_orders and balance - to_be_reduced_amount > target_balance:
176-
order_quantity = remaining_orders[0].origin_quantity
188+
if matching_quantity_order_index is not None:
189+
order_to_check = matching_quantity_order_index
190+
matching_quantity_order_index = None
191+
else:
192+
order_to_check = 0
193+
order_quantity = remaining_orders[order_to_check].origin_quantity
177194
if balance - to_be_reduced_amount - order_quantity >= target_balance:
178195
# cancel order and keep reducing
179-
actions[BalancedTakeProfitAndStopOrderGroup.CANCEL].append(remaining_orders.pop(0))
196+
actions[BalancedTakeProfitAndStopOrderGroup.CANCEL].append(remaining_orders.pop(order_to_check))
180197
to_be_reduced_amount += order_quantity
181198
else:
182199
# update order and stop reducing
183200
actions[BalancedTakeProfitAndStopOrderGroup.UPDATE].append({
184-
BalancedTakeProfitAndStopOrderGroup.ORDER: remaining_orders.pop(0),
201+
BalancedTakeProfitAndStopOrderGroup.ORDER: remaining_orders.pop(order_to_check),
185202
BalancedTakeProfitAndStopOrderGroup.UPDATED_QUANTITY:
186203
target_balance - (balance - to_be_reduced_amount - order_quantity)
187204
})

0 commit comments

Comments
 (0)