diff options
author | uvok | 2025-04-17 11:46:37 +0200 |
---|---|---|
committer | uvok | 2025-04-17 11:46:37 +0200 |
commit | 3f46426718bf34d8ae753d1eeb23f3a0865b38c8 (patch) | |
tree | 0cdf016e378a62f0c4008fdd8a72c122a4701dfc /trade_queue.py | |
parent | 2ae046643e5d02489b50a0e0d9d2955bb9c71657 (diff) |
Add trade removal
Diffstat (limited to 'trade_queue.py')
-rw-r--r-- | trade_queue.py | 32 |
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 |