Changes in slippage detection

This commit is contained in:
Nicolás Sánchez 2024-10-29 11:24:01 -03:00
parent 7e08ede786
commit 452bddbe3e
4 changed files with 62 additions and 22 deletions

View File

@ -1,3 +1,6 @@
2024.10.29:
. New functions to get top ask and top bid prices.
2024.10.28: 2024.10.28:
. Docstrings and comments improvements. . Docstrings and comments improvements.
. New endpoint: /edit_cooldown_multiplier. . New endpoint: /edit_cooldown_multiplier.

View File

@ -337,6 +337,50 @@ class broker:
return self.last_price return self.last_price
def get_top_ask_price(self,symbol):
'''
Returns the top ask price for the given symbol
Parameters:
symbol: the symbol to get the top ask price for
Returns:
the top ask price for the given symbol
'''
orderbook = self.get_order_book(symbol)
if orderbook=={}:
self.logger.log_this("Can't fetch orderbook (from get_top_ask_price)",1,symbol)
return self.get_ticker_price(symbol)
try:
return orderbook["asks"][0][0]
except Exception as e:
self.logger.log_this(f"Exception getting top ask price: {e}",1,symbol)
return self.get_ticker_price(symbol)
def get_top_bid_price(self,symbol):
'''
Returns the top bid price for the given symbol
Parameters:
symbol: the symbol to get the top bid price for
Returns:
the top bid price for the given symbol
'''
orderbook = self.get_order_book(symbol)
if orderbook=={}:
self.logger.log_this("Can't fetch orderbook (from get_top_bid_price)",1,symbol)
return self.get_ticker_price(symbol)
try:
return orderbook["bids"][0][0]
except Exception as e:
self.logger.log_this(f"Exception getting top mid price: {e}",1,symbol)
return self.get_ticker_price(symbol)
def get_mid_price(self,symbol): def get_mid_price(self,symbol):
''' '''
Retrieves the orderbook and returns the average price [(top bid + top ask)/2] Retrieves the orderbook and returns the average price [(top bid + top ask)/2]

View File

@ -22,7 +22,7 @@ In case the permissions of the certificate changes, reset them this way:
# ll /etc/letsencrypt/ # ll /etc/letsencrypt/
''' '''
version = "2024.10.28" version = "2024.10.29"
''' '''
Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors Color definitions. If you want to change them, check the reference at https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

View File

@ -890,7 +890,7 @@ class trader:
self.stop_when_profit = True self.stop_when_profit = True
self.status_dict["pause_reason"] = "take_profit_routine - if stop_when_profit" self.status_dict["pause_reason"] = "take_profit_routine - if stop_when_profit"
if self.stop_when_profit: #Signal to stop when profit if self.stop_when_profit: #Signal to stop when trade is closed
self.broker.logger.log_this("Pair shutting down. So long and thanks for all the fish",0,self.pair) self.broker.logger.log_this("Pair shutting down. So long and thanks for all the fish",0,self.pair)
self.quit = True self.quit = True
return 1 return 1
@ -906,23 +906,16 @@ class trader:
#self.safety_order_index = 0 #self.safety_order_index = 0
self.config_dict = self.reload_config_dict() self.config_dict = self.reload_config_dict()
self.status_dict["pause_reason"] = "take_profit_routine - Cooldown" self.broker.logger.log_this("Checking slippage...",2,self.pair)
self.broker.logger.log_this(f"Cooldown period...",2,self.pair) price_to_compare = self.broker.get_top_bid_price(self.pair) if self.is_short else self.broker.get_top_ask_price(self.pair)
time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) if abs(filled_order["price"]-price_to_compare)/filled_order["price"]>self.broker.get_slippage_default_threshold():
self.broker.logger.log_this(f"Slippage threshold exceeded, waiting for cooldown and restarting pair",1,self.pair)
try: time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier())
percentage_difference = abs(filled_order['price']-self.broker.get_mid_price(self.pair))/filled_order["price"] #The trader is restarted by the instance instead of by itself to allow a couple of more seconds for the price to return to normal.
if percentage_difference>self.broker.get_slippage_default_threshold(): #This could also be the default behavior.
#self.broker.logger.log_this(f"Slippage threshold exceeded, {self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()}s cooldown.",1,self.pair) self.pause = False
self.broker.logger.log_this(f"Slippage threshold exceeded, waiting for cooldown and restarting pair",1,self.pair) self.restart = True
time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) return 1
self.pause = False
self.restart = True
#self.write_status_file(True)
return 1
except Exception as e:
self.broker.logger.log_this(f"Exception while calculating percentage difference: {e}",1,self.pair)
self.broker.logger.log_this(f"Resuming...",2,self.pair)
#Restarting the trader #Restarting the trader
self.status_dict["pause_reason"] = "take_profit_routine - restart_bot call" self.status_dict["pause_reason"] = "take_profit_routine - restart_bot call"
@ -1083,7 +1076,7 @@ class trader:
return 0 return 0
def check_slippage(self, threshold: float, order_size: float) -> bool: def check_slippage(self, threshold: float, order_size: float, old_price: float = 0) -> bool:
''' '''
Checks that the slippage is between range. Returns True if threshold is exceeded, False otherwise. Checks that the slippage is between range. Returns True if threshold is exceeded, False otherwise.
''' '''
@ -1093,9 +1086,9 @@ class trader:
return False return False
suma = 0 suma = 0
try: try:
mid_price = (order_book["asks"][0][0]+order_book["bids"][0][0])/2 mid_price = old_price if old_price!=0 else (order_book["asks"][0][0]+order_book["bids"][0][0])/2
amount = (order_size/mid_price)*2 #Extra margin to avoid surprises. amount = (order_size/mid_price)*2 #Extra margin to avoid surprises.
first_price = order_book["asks"][0][0] first_price = order_book["asks"][0][0] #This could be first_price = old_price if old_price!=0 else order_book["asks"][0][0]
for x in order_book["asks"]: for x in order_book["asks"]:
suma += x[1] suma += x[1]
if suma>=amount: if suma>=amount: