diff options
-rw-r--r-- | trade_queue.py | 15 |
1 files changed, 13 insertions, 2 deletions
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}." |