@@ -147,8 +147,8 @@ def update_portfolio_from_filled_order(self, order):
147
147
:param order: the order to be taken into account
148
148
"""
149
149
# stop losses and take profits aren't using available portfolio
150
- # restoring available portfolio when order type is stop loss or take profit
151
- if not _should_update_available (order ):
150
+ # sync available portfolio funds when order type is stop loss or take profit
151
+ if _should_reduce_available_assets_on_fill (order ):
152
152
self .update_portfolio_available_from_order (order )
153
153
154
154
self .update_portfolio_data_from_order (order )
@@ -285,6 +285,21 @@ def log_portfolio_update_from_withdrawal(self, amount, currency):
285
285
f" | { constants .CURRENT_PORTFOLIO_STRING } { self .portfolio } " )
286
286
287
287
288
+ def _should_reduce_available_assets_on_fill (order ):
289
+ """
290
+ Check if the order fill should trigger an update on the portfolio available funds.
291
+ It's the case for orders that are not counted in available funds (stop losses). As they
292
+ are filled, the available funds have to be reduced to keep sync with total funds.
293
+
294
+ This is required to keep uniformity with regular order that are already reducing
295
+ part of available funds. As we are here on the fill context of a stop-like order,
296
+ available funds now have to be updated.
297
+ :param order: The order to check
298
+ :return: True if the order should update available portfolio
299
+ """
300
+ return not order .is_counted_in_available_funds ()
301
+
302
+
288
303
def _should_update_available (order ):
289
304
"""
290
305
Check if the order has impact on availability
0 commit comments