diff options
Diffstat (limited to 'trade_queue.py')
-rw-r--r-- | trade_queue.py | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/trade_queue.py b/trade_queue.py index 1048b8a..ed9902e 100644 --- a/trade_queue.py +++ b/trade_queue.py @@ -1,4 +1,5 @@ from collections import deque +from decimal import Decimal from typing import Deque, List from trade import Trade @@ -13,14 +14,14 @@ class FIFOQueue: def __init__(self) -> None: self.queue: Deque[Trade] = deque() - def add(self, amount: float, total_cost: float, 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) - def remove(self, amount: float) -> List[Trade]: + def remove(self, amount: float|Decimal) -> List[Trade]: """ Remove a specified amount from the queue, returning the trades used to buy. @@ -28,7 +29,9 @@ class FIFOQueue: if amount <= 0: raise ValueError("The amount to remove must be positive.") - remaining: float = amount + amount = Decimal(amount) + + remaining: Decimal = amount entries: List[Trade] = [] while remaining > 0: @@ -39,7 +42,7 @@ class FIFOQueue: if trade.amount > remaining: trade.remove_coins(remaining) entries.append(Trade(remaining, trade.total_cost, trade.date)) - remaining = 0 + remaining = Decimal(0) else: remaining -= trade.amount entries.append(trade) @@ -47,8 +50,8 @@ class FIFOQueue: return entries - def get_remaining_amount(self) -> float: + def get_remaining_amount(self) -> Decimal: """ Calculate the total remaining amount in the queue. """ - return sum(trade.amount for trade in self.queue) + return sum((trade.amount for trade in self.queue), Decimal(0)) |