summaryrefslogtreecommitdiff
path: root/trade_queue.py
diff options
context:
space:
mode:
authoruvok2025-04-14 21:00:26 +0200
committeruvok2025-04-14 21:00:26 +0200
commitb0baff2798408f5cfa74ec8f675d55ee3437df32 (patch)
tree38f5fdabd299cd9c5509fbdeed4a3ea72d9a74e0 /trade_queue.py
parent869b35a8c3c6b11941087d008ad3eb42da257a12 (diff)
decimal-or-float, conversion
Diffstat (limited to 'trade_queue.py')
-rw-r--r--trade_queue.py15
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))