summaryrefslogtreecommitdiff
path: root/trade_queue.py
diff options
context:
space:
mode:
authoruvok2025-04-19 15:37:47 +0200
committeruvok2025-04-19 15:37:47 +0200
commit08439f4e5ff863cba53c21fc5bfd285a52b56e89 (patch)
treed7449ab2c52656307a4b680e54f6ac0ead26b7f4 /trade_queue.py
parentdb3ca3d76d4b3e66dfa58bed0f0e5816c3d65b17 (diff)
Don't use remain-amount in remove-coins
result is incorrect.
Diffstat (limited to 'trade_queue.py')
-rw-r--r--trade_queue.py15
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}."