summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test_trade.py2
-rw-r--r--trade_queue.py22
2 files changed, 19 insertions, 5 deletions
diff --git a/test_trade.py b/test_trade.py
index 81185b0..e6bb445 100644
--- a/test_trade.py
+++ b/test_trade.py
@@ -52,7 +52,7 @@ class TestTrade(unittest.TestCase):
"""
Test the __repr__ method for correct string representation.
"""
- expected_repr = "Trade(amount=10, price_per_coin=10.00, total_cost=100.00, date=2025-04-14)"
+ expected_repr = "Trade(amount=10.00, price_per_coin=10.00, total_cost=100.00, date=2025-04-14)"
self.assertEqual(repr(self.trade), expected_repr)
diff --git a/trade_queue.py b/trade_queue.py
index 08e0d06..9cae560 100644
--- a/trade_queue.py
+++ b/trade_queue.py
@@ -1,9 +1,12 @@
+import logging
from collections import deque
from decimal import Decimal
from typing import Deque, List
-
from trade import Trade
+# Set up a dedicated logger for FIFOQueue
+logger = logging.getLogger(__name__)
+logger.setLevel(logging.DEBUG)
class FIFOQueue:
"""
@@ -15,28 +18,34 @@ class FIFOQueue:
self.queue: Deque[Trade] = deque()
self._cached_total: Decimal = Decimal(0)
self._cache_valid: bool = True
+ logger.info("FIFOQueue initialized with empty queue.")
- def add(self, amount: float|Decimal, total_cost: float|Decimal, date: str) -> None:
+ def add(self, amount: float | Decimal, total_cost: float | Decimal, date: str) -> None:
"""
Add a trade to the queue.
"""
trade = Trade(amount, total_cost, date)
self.queue.append(trade)
self._cache_valid = False
+ logger.info(f"Added trade: {trade}.")
- def remove_coins(self, amount: float|Decimal) -> List[Trade]:
+ def remove_coins(self, amount: float | Decimal) -> 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.
"""
if amount <= 0:
+ logger.error("Attempted to remove non-positive amount.")
raise ValueError("The amount to remove must be positive.")
amount = Decimal(amount)
if amount > self.get_remaining_amount():
+ logger.error(f"Insufficient assets to process sale of {amount}.")
raise ValueError(f"Insufficient assets in queue to process sale of {amount}.")
+ logger.debug(f"Removing {amount:.2f} coins from the queue.")
+ logger.info("Cache invalidated before removing coins.")
self._cache_valid = False
remaining: Decimal = amount
@@ -44,16 +53,19 @@ class FIFOQueue:
while remaining > 0:
trade = self.queue[0]
+ logger.debug(f"Processing trade: {trade}")
if trade.amount > remaining:
ppc = trade.price_per_coin
trade.remove_coins(remaining)
entries.append(Trade(remaining, remaining * ppc, trade.date))
+ logger.info(f"Partial removal from trade: {remaining:.2f} coins.")
break
else:
remaining -= trade.amount
entries.append(trade)
self.queue.popleft()
+ logger.info(f"Removed full trade: {trade}. Remaining coins to remove: {remaining}")
return entries
@@ -61,9 +73,11 @@ class FIFOQueue:
"""
Calculate the total remaining amount in the queue.
"""
-
if not self._cache_valid:
+ logger.debug("Cache invalid, recalculating remaining amount.")
self._cached_total = sum((trade.amount for trade in self.queue), Decimal(0))
self._cache_valid = True
+ logger.info(f"Cache recalculated: {self._cached_total:.2f}")
+ logger.debug(f"Returning cached remaining amount: {self._cached_total:.2f}")
return self._cached_total