minor refactor

This commit is contained in:
Nicolás Sánchez 2025-08-25 16:07:08 -03:00
parent 58fcff8618
commit b594bd2007
2 changed files with 21 additions and 29 deletions

View File

@ -654,16 +654,15 @@ class Broker:
:return: 0 if order was succesfully canceled, 1 if not :return: 0 if order was succesfully canceled, 1 if not
''' '''
pair = symbol
tries = self.retries//2 tries = self.retries//2
while tries>0: while tries>0:
try: try:
while self.get_order(id,pair)["status"]=="open": while self.get_order(id,symbol)["status"]=="open":
self.exchange.cancel_order(id,symbol=pair) self.exchange.cancel_order(id,symbol)
time.sleep(self.wait_time) time.sleep(self.wait_time)
return 0 return 0
except Exception as e: except Exception as e:
if self.get_order(id,pair)["status"]=="canceled": if self.get_order(id,symbol)["status"]=="canceled":
return 0 return 0
self.logger.log_this(f"Exception in cancel_order: id {id} - exception: {e}",1) self.logger.log_this(f"Exception in cancel_order: id {id} - exception: {e}",1)
if no_retries: if no_retries:
@ -724,26 +723,25 @@ class Broker:
''' '''
retries = self.retries//2 retries = self.retries//2
pair = symbol
while retries>0: while retries>0:
try: try:
if self.get_exchange_name()=="gateio" and side=="buy" and not amount_in_base: 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: else:
order_book = self.get_order_book(symbol) order_book = self.get_order_book(symbol)
if order_book=={}: if order_book=={}:
self.logger.log_this(f"new_simulated_market_order. Order book returned an empty dictionary",1,symbol) self.logger.log_this(f"new_simulated_market_order. Order book returned an empty dictionary",1,symbol)
return self.empty_order return self.empty_order
if amount_in_base or side!="buy": if amount_in_base or side!="buy":
base_amount = self.amount_to_precision(pair,size) base_amount = self.amount_to_precision(symbol,size)
else: else:
avg_price = self.average_price_depth(order_book,size,"sell") 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) 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) price = self.find_minimum_viable_price(order_book,base_amount,side)
#Maybe check for slippage here instead of within the trader itself? idk #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) 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: except Exception as e:
self.logger.log_this(f"new_simulated_market_order exception: {e}",1,symbol) self.logger.log_this(f"new_simulated_market_order exception: {e}",1,symbol)
if no_retries: if no_retries:
@ -802,24 +800,23 @@ class Broker:
if self.broker_config["simulate_market_orders"]: if self.broker_config["simulate_market_orders"]:
return self.new_simulated_market_order(symbol,size,side,amount_in_base=amount_in_base) return self.new_simulated_market_order(symbol,size,side,amount_in_base=amount_in_base)
retries = self.retries retries = self.retries
pair = symbol
while retries>0: while retries>0:
try: try:
if side=="buy": if side=="buy":
to_buy = float(size) to_buy = float(size)
if not amount_in_base: if not amount_in_base:
to_buy = float(size)/self.get_top_ask_price(pair) to_buy = float(size)/self.get_top_ask_price(symbol)
amount = self.amount_to_precision(pair,to_buy) amount = self.amount_to_precision(symbol,to_buy)
else: 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) time.sleep(self.wait_time)
# Wait a bit more when dealing with Kucoin # 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: 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: if no_retries:
break break
time.sleep(self.wait_time) time.sleep(self.wait_time)
@ -879,12 +876,11 @@ class Broker:
''' '''
tries = self.retries tries = self.retries
pair = symbol
while tries>=0: while tries>=0:
try: 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) 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 #if order_to_send["amount"] is not None: # Because Kucoin etc etc
# return self.get_order(order_to_send["id"],pair) # # return self.get_order(order_to_send["id"],pair) #
#self.logger.log_this(f"Error sending order: Null order returned",2,pair) # #self.logger.log_this(f"Error sending order: Null order returned",2,pair) #
@ -892,7 +888,7 @@ class Broker:
#retries-=1 #retries-=1
except Exception as e: 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 self.not_enough_balance_error(e):
if tries<=self.retries//2: #Halves the amount of retries if there is a balance error. if tries<=self.retries//2: #Halves the amount of retries if there is a balance error.
return 1 return 1
@ -923,10 +919,9 @@ class Broker:
if id=="": if id=="":
return self.empty_order return self.empty_order
tries = self.retries tries = self.retries
pair = symbol
while tries>0: while tries>0:
try: try:
return self.exchange.fetch_order(id,symbol=pair) return self.exchange.fetch_order(id,symbol)
except Exception as e: except Exception as e:
self.logger.log_this(f"Exception in get_order: {e}",1,symbol) self.logger.log_this(f"Exception in get_order: {e}",1,symbol)
if no_retries: if no_retries:
@ -944,10 +939,9 @@ class Broker:
:return: The market information. :return: The market information.
''' '''
tries = self.retries tries = self.retries
pair = symbol
while tries>0: while tries>0:
try: try:
return self.exchange.market(pair) return self.exchange.market(symbol)
except Exception as e: except Exception as e:
self.logger.log_this(f"Exception in fetch_market: {e}",1,symbol) self.logger.log_this(f"Exception in fetch_market: {e}",1,symbol)
if no_retries: if no_retries:
@ -965,10 +959,9 @@ class Broker:
:return: The ticker information. :return: The ticker information.
''' '''
tries = self.retries tries = self.retries
pair = symbol
while tries>0: while tries>0:
try: try:
return self.exchange.fetch_ticker(pair) return self.exchange.fetch_ticker(symbol)
except Exception as e: except Exception as e:
self.logger.log_this(f"Exception in get_ticker: {e}") self.logger.log_this(f"Exception in get_ticker: {e}")
if no_retries: if no_retries:

View File

@ -8,15 +8,14 @@ Mandatory:
4. Implement api key hashing. 4. Implement api key hashing.
5. Dockerize. 5. Dockerize.
6. Inspect orderbook liquidity prior to changing mode from short to long (big sell market order needs to have liquidity). 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 7. Use create_orders ccxt method to send the batch of safety orders (Binance does not support it in spot trading)
8. 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: 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) 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) 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 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) 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 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