@@ -106,7 +106,7 @@ async def _balance_orders(self, closed_order, ignored_orders):
106
106
for order in take_profit_actions [self .CANCEL ] + stop_actions [self .CANCEL ]:
107
107
self .logger .debug (f"Cancelling order to keep balance, order: { order } as { closed_order } is closed" )
108
108
async with signals .remote_signal_publisher (order .trader .exchange_manager , order .symbol , True ):
109
- if order .is_open ():
109
+ if order .is_open ():
110
110
# only close open
111
111
await signals .cancel_order (order .trader .exchange_manager ,
112
112
signals .should_emit_trading_signal (order .trader .exchange_manager ),
@@ -139,9 +139,12 @@ def _get_balance(self, closed_order, ignored_orders):
139
139
self .STOP : _SideBalance ()
140
140
}
141
141
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 ):
145
148
if order_util .is_stop_order (order .order_type ):
146
149
balance [self .STOP ].add_order (order )
147
150
else :
@@ -172,16 +175,30 @@ def get_actions_to_balance(self, target_balance):
172
175
return actions
173
176
to_be_reduced_amount = constants .ZERO
174
177
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
+
175
187
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
177
194
if balance - to_be_reduced_amount - order_quantity >= target_balance :
178
195
# 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 ))
180
197
to_be_reduced_amount += order_quantity
181
198
else :
182
199
# update order and stop reducing
183
200
actions [BalancedTakeProfitAndStopOrderGroup .UPDATE ].append ({
184
- BalancedTakeProfitAndStopOrderGroup .ORDER : remaining_orders .pop (0 ),
201
+ BalancedTakeProfitAndStopOrderGroup .ORDER : remaining_orders .pop (order_to_check ),
185
202
BalancedTakeProfitAndStopOrderGroup .UPDATED_QUANTITY :
186
203
target_balance - (balance - to_be_reduced_amount - order_quantity )
187
204
})
0 commit comments