summaryrefslogtreecommitdiff
path: root/trade_queue.py
diff options
context:
space:
mode:
Diffstat (limited to 'trade_queue.py')
-rw-r--r--trade_queue.py32
1 files changed, 31 insertions, 1 deletions
diff --git a/trade_queue.py b/trade_queue.py
index 6cad1f5..73640bf 100644
--- a/trade_queue.py
+++ b/trade_queue.py
@@ -2,7 +2,7 @@ import logging
from collections import deque
from copy import deepcopy
from decimal import Decimal
-from typing import Deque, List
+from typing import Callable, Deque, List
from trade import Trade
@@ -99,3 +99,33 @@ class FIFOQueue:
logger.debug(f"Returning cached remaining amount: {self._cached_total:.2f}")
return self._cached_total
+
+ def remove(self, predicate: Callable[[Trade], bool]) -> Trade:
+ """
+ Remove a trade from the queue based on a given predicate.
+
+ Args:
+ predicate (Callable[[Trade], bool]): A function that returns True for the trade to remove.
+
+ Returns:
+ Trade: The removed trade.
+
+ Raises:
+ ValueError: If no trade matches the predicate or multiple trades are found.
+ """
+ # Use filter to find matching trades
+ matching_trades = list(filter(predicate, self.__queue))
+
+ if len(matching_trades) == 0:
+ logger.error("No matching trade found for removal.")
+ raise ValueError("No trade matches the given predicate.")
+ elif len(matching_trades) > 1:
+ logger.error("Multiple matching trades found for removal.")
+ raise ValueError("Multiple trades match the given predicate. Please refine your criteria.")
+
+ # Locate the exact match in the original queue
+ trade_to_remove = matching_trades[0]
+ self.__queue.remove(trade_to_remove)
+ self._cache_valid = False
+ logger.info(f"Removed trade: {trade_to_remove}.")
+ return trade_to_remove