Skip to content

Commit b15917e

Browse files
Guillaume De Saint MartinGuillaumeDSM
Guillaume De Saint Martin
authored andcommitted
[Portfolio] make available funds sync on stop order fill clearer
1 parent 0ebc7dc commit b15917e

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

octobot_trading/personal_data/portfolios/portfolio.pxd

+1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ cdef class Portfolio:
5656
cdef void _reset_all_portfolio_available(self)
5757
cdef object _reset_currency_portfolio_available(self, str currency_to_reset, object reset_quantity)
5858

59+
cdef bint _should_reduce_available_assets_on_fill(order_class.Order order)
5960
cdef bint _should_update_available(order_class.Order order)
6061
cdef tuple _parse_raw_currency_balance(dict raw_currency_balance)

octobot_trading/personal_data/portfolios/portfolio.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def update_portfolio_from_filled_order(self, order):
147147
:param order: the order to be taken into account
148148
"""
149149
# 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):
152152
self.update_portfolio_available_from_order(order)
153153

154154
self.update_portfolio_data_from_order(order)
@@ -285,6 +285,21 @@ def log_portfolio_update_from_withdrawal(self, amount, currency):
285285
f" | {constants.CURRENT_PORTFOLIO_STRING} {self.portfolio}")
286286

287287

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+
288303
def _should_update_available(order):
289304
"""
290305
Check if the order has impact on availability

0 commit comments

Comments
 (0)