From b594bd2007242117d2907064517ff3251c2fb22b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Mon, 25 Aug 2025 16:07:08 -0300 Subject: [PATCH] minor refactor --- exchange_wrapper.py | 45 +++++++++++++++++++-------------------------- todo.txt | 5 ++--- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/exchange_wrapper.py b/exchange_wrapper.py index 1232e5a..1a36efa 100755 --- a/exchange_wrapper.py +++ b/exchange_wrapper.py @@ -654,16 +654,15 @@ class Broker: :return: 0 if order was succesfully canceled, 1 if not ''' - pair = symbol tries = self.retries//2 while tries>0: try: - while self.get_order(id,pair)["status"]=="open": - self.exchange.cancel_order(id,symbol=pair) + while self.get_order(id,symbol)["status"]=="open": + self.exchange.cancel_order(id,symbol) time.sleep(self.wait_time) return 0 except Exception as e: - if self.get_order(id,pair)["status"]=="canceled": + if self.get_order(id,symbol)["status"]=="canceled": return 0 self.logger.log_this(f"Exception in cancel_order: id {id} - exception: {e}",1) if no_retries: @@ -724,26 +723,25 @@ class Broker: ''' retries = self.retries//2 - pair = symbol while retries>0: try: if self.get_exchange_name()=="gateio" and side=="buy" and not amount_in_base: - new_order = self.exchange.create_market_buy_order_with_cost(pair, size) + new_order = self.exchange.create_market_buy_order_with_cost(symbol, size) else: order_book = self.get_order_book(symbol) if order_book=={}: self.logger.log_this(f"new_simulated_market_order. Order book returned an empty dictionary",1,symbol) return self.empty_order if amount_in_base or side!="buy": - base_amount = self.amount_to_precision(pair,size) + base_amount = self.amount_to_precision(symbol,size) else: avg_price = self.average_price_depth(order_book,size,"sell") base_amount = size/avg_price if avg_price is not None else size/self.get_ticker_price(symbol) price = self.find_minimum_viable_price(order_book,base_amount,side) #Maybe check for slippage here instead of within the trader itself? idk - new_order = self.exchange.create_order(pair,"limit",side,base_amount,price) + new_order = self.exchange.create_order(symbol,"limit",side,base_amount,price) time.sleep(self.wait_time) - return self.get_order(new_order["id"],pair) + return self.get_order(new_order["id"],symbol) except Exception as e: self.logger.log_this(f"new_simulated_market_order exception: {e}",1,symbol) if no_retries: @@ -802,24 +800,23 @@ class Broker: if self.broker_config["simulate_market_orders"]: return self.new_simulated_market_order(symbol,size,side,amount_in_base=amount_in_base) retries = self.retries - pair = symbol while retries>0: try: if side=="buy": to_buy = float(size) if not amount_in_base: - to_buy = float(size)/self.get_top_ask_price(pair) - amount = self.amount_to_precision(pair,to_buy) + to_buy = float(size)/self.get_top_ask_price(symbol) + amount = self.amount_to_precision(symbol,to_buy) else: - amount = self.amount_to_precision(pair,size) #Market sell orders are always nominated in base currency + amount = self.amount_to_precision(symbol,size) #Market sell orders are always nominated in base currency - order_to_send = self.exchange.create_order(pair,"market",side,amount) + order_to_send = self.exchange.create_order(symbol,"market",side,amount) time.sleep(self.wait_time) # Wait a bit more when dealing with Kucoin - return self.get_order(order_to_send["id"],pair) + return self.get_order(order_to_send["id"],symbol) except Exception as e: - self.logger.log_this(f"Exception in new_market_order: {e}",1,pair) + self.logger.log_this(f"Exception in new_market_order: {e}",1,symbol) if no_retries: break time.sleep(self.wait_time) @@ -879,12 +876,11 @@ class Broker: ''' tries = self.retries - pair = symbol while tries>=0: try: - order_to_send = self.exchange.create_order(pair,"limit",side,self.amount_to_precision(pair,size),price) + order_to_send = self.exchange.create_order(symbol,"limit",side,self.amount_to_precision(symbol,size),price) time.sleep(self.wait_time) - return self.get_order(order_to_send["id"],pair) + return self.get_order(order_to_send["id"],symbol) #if order_to_send["amount"] is not None: # Because Kucoin etc etc # return self.get_order(order_to_send["id"],pair) # #self.logger.log_this(f"Error sending order: Null order returned",2,pair) # @@ -892,7 +888,7 @@ class Broker: #retries-=1 except Exception as e: - self.logger.log_this(f"Exception in new_limit_order - Side: {side} - Size: {size} - {self.amount_to_precision(pair,size)} - Exception: {e}",1,symbol) + self.logger.log_this(f"Exception in new_limit_order - Side: {side} - Size: {size} - {self.amount_to_precision(symbol,size)} - Exception: {e}",1,symbol) if self.not_enough_balance_error(e): if tries<=self.retries//2: #Halves the amount of retries if there is a balance error. return 1 @@ -923,10 +919,9 @@ class Broker: if id=="": return self.empty_order tries = self.retries - pair = symbol while tries>0: try: - return self.exchange.fetch_order(id,symbol=pair) + return self.exchange.fetch_order(id,symbol) except Exception as e: self.logger.log_this(f"Exception in get_order: {e}",1,symbol) if no_retries: @@ -944,10 +939,9 @@ class Broker: :return: The market information. ''' tries = self.retries - pair = symbol while tries>0: try: - return self.exchange.market(pair) + return self.exchange.market(symbol) except Exception as e: self.logger.log_this(f"Exception in fetch_market: {e}",1,symbol) if no_retries: @@ -965,10 +959,9 @@ class Broker: :return: The ticker information. ''' tries = self.retries - pair = symbol while tries>0: try: - return self.exchange.fetch_ticker(pair) + return self.exchange.fetch_ticker(symbol) except Exception as e: self.logger.log_this(f"Exception in get_ticker: {e}") if no_retries: diff --git a/todo.txt b/todo.txt index 2ec36ed..f27eeaf 100755 --- a/todo.txt +++ b/todo.txt @@ -8,15 +8,14 @@ Mandatory: 4. Implement api key hashing. 5. Dockerize. 6. Inspect orderbook liquidity prior to changing mode from short to long (big sell market order needs to have liquidity). -7. API endpoint to modify the amount of concurrent safety orders -8. Use create_orders ccxt method to send the batch of safety orders (Binance does not support it in spot trading) +7. Use create_orders ccxt method to send the batch of safety orders (Binance does not support it in spot trading) Would be nice to have: ===================== 0. Trader order: alphabetical; by uptime; by safety orders, by percentage_to_completion. (Although this may be more suitable for the web and mobile apps) 1. Local implementation of amount_to_precision, cost_to_precision and price_to_precision. (Unless the plan is to continue to use CCXT forever) -2. Instead of cancelling and resending the take profit order, you could just edit it (Kucoin only supports editing on high frequency orders) +2. Instead of cancelling and resending the take profit order, edit it (Kucoin only supports editing on high frequency orders) 3. Round-robin trading pairs: Instead of a fixed list of trading pairs, after n closed deals the trader is terminated and a new one spawns, picking the trading pair from a pre-populated list (the trading pairs can be selected by using Yang-Zhang, Parkinson or another volatility indicator) This could be very benefitial, since it limits the long time commitment to a small list of trading pairs, enabling the instance to react to market trends very