From 08439f4e5ff863cba53c21fc5bfd285a52b56e89 Mon Sep 17 00:00:00 2001 From: uvok Date: Sat, 19 Apr 2025 15:37:47 +0200 Subject: Don't use remain-amount in remove-coins result is incorrect. --- trade_queue.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) (limited to 'trade_queue.py') diff --git a/trade_queue.py b/trade_queue.py index 81a2a64..c3a4b9d 100644 --- a/trade_queue.py +++ b/trade_queue.py @@ -63,7 +63,10 @@ class FIFOQueue: self._cache_valid = False logger.info(f"Added trade: {trade}.") - def remove_coins(self, amount: float | Decimal) -> List[Trade]: + # The remove_coins method needs updating, it currently operates on the first trade in the queue, disregarding whether it's buy or sell + def remove_coins( + self, amount: float | Decimal, before_ts: str | None = None + ) -> List[Trade]: """ Remove a specified amount of coins from the queue, returning the trades used to buy. This can be used to calculate profit/loss. @@ -74,7 +77,15 @@ class FIFOQueue: amount = Decimal(amount) - if amount > self.get_remaining_amount(): + available = sum( + ( + trade.amount + for trade in self.__queue + if trade.amount > 0 and trade.timestamp < (before_ts or "9999-99-99") + ), + Decimal(0), + ) + if amount > available: logger.error(f"Insufficient assets to process sale of {amount}.") raise ValueError( f"Insufficient assets in queue to process sale of {amount}." -- cgit v1.2.3