From 8ad157d17d173072e6007241e5ee603e83b73203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Mon, 26 May 2025 19:01:34 -0300 Subject: [PATCH] closed orders methods --- exchange_wrapper.py | 78 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-) diff --git a/exchange_wrapper.py b/exchange_wrapper.py index f16cb47..39d2633 100755 --- a/exchange_wrapper.py +++ b/exchange_wrapper.py @@ -540,10 +540,10 @@ class Broker: def fetch_open_orders(self,pairs=None) -> list: ''' - Returns a list of IDs of all open orders on the exchange + Returns a list of all open orders on the exchange :param pairs: list of pairs to get opened orders - :return: list of IDs of all open orders + :return: list of all open orders ''' if pairs is None: @@ -553,11 +553,26 @@ class Broker: if self.get_exchange_name()=="binance": return self.get_opened_orders_binance(pairs) return self.get_opened_orders() - #else: - # orders = self.get_opened_orders() - #if orders!=[]: - # id_list.extend(x["id"] for x in orders) - #return id_list + except Exception as e: + self.logger.log_this(f"Exception in fetch_open_orders: {e}",2) + return [] + + + def fetch_closed_orders(self,pairs=None) -> list: + ''' + Returns a list of all closed orders on the exchange + + :param pairs: list of pairs to get opened orders + :return: list of all open orders + ''' + + if pairs is None: + pairs = [] + try: + #id_list = [] + if self.get_exchange_name()=="binance": + return self.get_closed_orders_binance(pairs) + return self.get_closed_orders() except Exception as e: self.logger.log_this(f"Exception in fetch_open_orders: {e}",2) return [] @@ -565,7 +580,7 @@ class Broker: def get_opened_orders(self,no_retries=False): #It should return a list of all opened orders ''' - Returns a list of all the orders on the exchange + Returns a list of all the open orders on the exchange :param pairs: list of pairs :return: list of all the open orders on the exchange @@ -584,6 +599,27 @@ class Broker: return [] + def get_closed_orders(self,no_retries=False): #It should return a list of all opened orders + ''' + Returns a list of all the open orders on the exchange + + :param pairs: list of pairs + :return: list of all the open orders on the exchange + ''' + + retries = self.retries + while retries>0: + try: + return self.exchange.fetch_closed_orders() + except Exception as e: + self.logger.log_this(f"Exception in get_closed_orders: {e}",1) + if no_retries: + break + time.sleep(self.wait_time) + retries-=1 + return [] + + def get_opened_orders_binance(self,pairs): ''' Returns a list of all the open orders on the exchange @@ -605,6 +641,27 @@ class Broker: return [] + def get_closed_orders_binance(self,pairs): + ''' + Returns a list of all the closed orders on the exchange + + :param pairs: list of pairs + :return: list of all the closed orders on the exchange + ''' + + try: + if "unified_order_query" in self.broker_config and self.broker_config["unified_order_query"] is True: + return self.exchange.fetch_closed_orders() + result = [] + for pair in pairs: + a = self.exchange.fetch_closed_orders(pair) + result.extend(iter(a)) + return result + except Exception as e: + self.logger.log_this(f"Exception in get_closed_orders_binance: {e}",1) + return [] + + def cancel_order(self,id,symbol,no_retries=False): ''' Receives an order id and cancels the corresponding order @@ -1102,4 +1159,9 @@ class Logger: return 0 + +class Order: + def __init__(self, order: dict = {}): + pass + \ No newline at end of file