From 82686a04ab56bfccca0124047299c8f18885aff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20S=C3=A1nchez?= Date: Thu, 31 Oct 2024 09:52:14 -0300 Subject: [PATCH] new trader config option --- changelog.txt | 1 + configs/trader_config.json.example | 1 + todo.txt | 1 - trader.py | 49 ++++++++++++++++-------------- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/changelog.txt b/changelog.txt index c2ecf7b..f48377b 100755 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,6 @@ 2024.10.31: . Added an option to set the wait time before sending a new safety order. +. Added a per-trader option to enable or disable slippage checks. 2024.10.30: . Changes trying to catch the wrong base amount bug. diff --git a/configs/trader_config.json.example b/configs/trader_config.json.example index eb0b8b3..5680550 100755 --- a/configs/trader_config.json.example +++ b/configs/trader_config.json.example @@ -7,6 +7,7 @@ "safety_order_scale": 0.0105, #Size multiplier of every subsequent safety order. (check strategy documentation for more details) "write_logs": false, #Write logs to file. "calculate_fees": true, #Take into account the fees when calculating profits. + "check_slippage": true, #Slippage checks before sending orders. "cleanup": true, #Execute the cleanup routine every trader (re)start. "telegram": true, #Send Telegram notifications. "tp_mode": 3, #Take profit mode. (check strategy documentation for more details) diff --git a/todo.txt b/todo.txt index a3e2539..e549abc 100755 --- a/todo.txt +++ b/todo.txt @@ -5,7 +5,6 @@ Mandatory: 2. Instead of giving a list of order_ids to each trader, give a list of the open orders and that's it (for easier future development, partial order fills for example) 3. Deploying script, both for testnet and for mainnet. 4. Maintain local orderbooks for each trading pair. -5. Global option for slippage checks. Would be nice to have: diff --git a/trader.py b/trader.py index eb9232e..08796de 100755 --- a/trader.py +++ b/trader.py @@ -24,6 +24,9 @@ class trader: self.max_short_safety_orders = 45 if "max_short_safety_orders" in config_dict: self.max_short_safety_orders = config_dict["max_short_safety_orders"] + self.check_slippage = True + if "check_slippage" in self.config_dict: + self.check_slippage = self.config_dict["check_slippage"] self.start_time = int(time.time()) self.total_amount_of_quote=0 self.total_amount_of_base=1 @@ -192,12 +195,13 @@ class trader: return 1 #check slippage - self.broker.logger.log_this("Checking slippage...",2,self.pair) - self.status_dict["pause_reason"] = "start_bot - checking slippage" - if self.check_orderbook_depth(self.broker.get_slippage_default_threshold(),self.config_dict["order_size"]): - #Slippage threshold exceeded - self.broker.logger.log_this("Slippage threshold exceeded",1,self.pair) - return 3 + if self.check_slippage: + self.broker.logger.log_this("Checking slippage...",2,self.pair) + self.status_dict["pause_reason"] = "start_bot - checking slippage" + if self.check_orderbook_depth(self.broker.get_slippage_default_threshold(),self.config_dict["order_size"]): + #Slippage threshold exceeded + self.broker.logger.log_this("Slippage threshold exceeded",1,self.pair) + return 3 self.status_dict["pause_reason"] = "start_bot - after slippage" self.status_dict["order_size"] = self.config_dict["order_size"] @@ -914,22 +918,23 @@ class trader: #self.safety_order_index = 0 self.config_dict = self.reload_config_dict() - self.broker.logger.log_this("Checking slippage...",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) - 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) - time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) - #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. - #This could also be the default behavior. - self.pause = False - self.restart = True - return 1 - elif self.check_orderbook_depth(self.broker.get_slippage_default_threshold(),self.config_dict["order_size"],filled_order["price"]): - self.broker.logger.log_this(f"Orderbook depth not sufficient, waiting for cooldown and restarting pair",1,self.pair) - time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) - self.pause = False - self.restart = True - return 1 + if self.check_slippage: + self.broker.logger.log_this("Checking slippage...",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) + 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) + time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) + #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. + #This could also be the default behavior. + self.pause = False + self.restart = True + return 1 + elif self.check_orderbook_depth(self.broker.get_slippage_default_threshold(),self.config_dict["order_size"],filled_order["price"]): + self.broker.logger.log_this(f"Orderbook depth not sufficient, waiting for cooldown and restarting pair",1,self.pair) + time.sleep(self.broker.get_wait_time()*self.broker.get_cooldown_multiplier()) + self.pause = False + self.restart = True + return 1 #Restarting the trader self.status_dict["pause_reason"] = "take_profit_routine - restart_bot call"