From 3f46426718bf34d8ae753d1eeb23f3a0865b38c8 Mon Sep 17 00:00:00 2001 From: uvok Date: Thu, 17 Apr 2025 11:46:37 +0200 Subject: Add trade removal --- trade_queue.py | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'trade_queue.py') 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 -- cgit v1.2.3