diff --git a/main.py b/main.py index 1713837..62a406f 100644 --- a/main.py +++ b/main.py @@ -1385,7 +1385,7 @@ def unwrapped_switch_to_long(base,quote,calculate_profits): x.pause = True if x.switch_to_long(ignore_old_long=ignore_old_long)==1: return jsonify({"Error": "Error in switch_to_long()"}) - if x.start_bot()==1: + if x.start_trader()==1: x.quit = True return jsonify({"Error": "Error switching to long mode (wAPI)"}) return jsonify({"Success": "Pair switched to long mode"}) @@ -1429,7 +1429,7 @@ def unwrapped_switch_to_short(base,quote): #Enabling autoswitch x.config_dict["autoswitch"] = True - if x.start_bot()==1: + if x.start_trader()==1: x.quit = True return jsonify({"Error": "Error switching to short mode (wAPI)"}) return jsonify({"Success": "Pair switched to short mode"}) diff --git a/trader.py b/trader.py index 9c9ea2c..b30ddc4 100755 --- a/trader.py +++ b/trader.py @@ -6,7 +6,7 @@ import os class trader: def __init__(self, broker, config_dict: dict, is_import: bool = False): self.pause = True #Signals the trader to not process order info when an API call manhandles the trader - #True by default, once the trader is started the start_bot method toggles it + #True by default, once the trader is started the start_trader method toggles it self.quit = False #If true, it doesn't restart the bot when profit is reached. self.restart = False @@ -83,9 +83,9 @@ class trader: return None # An alternative would be to set up a variable like self.is_initalized to false and finish the initialization here. - # Then, in the main loop, check if self.is_initalized is false. If it is, run start_bot. + # Then, in the main loop, check if self.is_initalized is false. If it is, run start_trader. - start_result = self.start_bot() + start_result = self.start_trader() if start_result==0: return None #Everything is OK elif start_result==1: #If initialization fails @@ -115,7 +115,7 @@ class trader: return 0 - def start_bot(self) -> int: + def start_trader(self) -> int: ''' Initializes the trader. ''' @@ -133,7 +133,7 @@ class trader: self.market = new_market_data self.pause = True - self.status_dict["pause_reason"] = "start_bot" + self.status_dict["pause_reason"] = "start_trader" if self.is_short: self.broker.logger.log_this("Calculating optimal order size...",2,self.pair) @@ -182,7 +182,7 @@ class trader: else: #Check order size - self.status_dict["pause_reason"] = "start_bot - checking order size" + self.status_dict["pause_reason"] = "start_trader - checking order size" self.broker.logger.log_this("Checking for order size",2,self.pair) minimum_order_size_allowed = self.broker.get_min_quote_size(self.pair) if minimum_order_size_allowed is not None and minimum_order_size_allowed>self.config_dict["order_size"]: @@ -198,17 +198,17 @@ class trader: #check slippage if self.check_slippage: self.broker.logger.log_this("Checking slippage...",2,self.pair) - self.status_dict["pause_reason"] = "start_bot - checking slippage" + self.status_dict["pause_reason"] = "start_trader - 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["pause_reason"] = "start_trader - after slippage" self.status_dict["order_size"] = self.config_dict["order_size"] #Sending initial order - self.status_dict["pause_reason"] = "start_bot - sending first order" + self.status_dict["pause_reason"] = "start_trader - sending first order" self.broker.logger.log_this("Sending first order...",2,self.pair) action = "sell" if self.is_short else "buy" first_order = self.broker.new_market_order(self.pair,self.config_dict["order_size"],action) @@ -219,7 +219,7 @@ class trader: tries = self.broker.get_retries()*2 #This is really necessary, don't change it. Don't. DON'T. #Wait until the first order gets filled - self.status_dict["pause_reason"] = "start_bot - waiting for the first order to get filled" + self.status_dict["pause_reason"] = "start_trader - waiting for the first order to get filled" while True: #Wait a bit longer, to catch a bug: #Sometimes the amount of base taken into account by the trader is lower than the amount bought, @@ -249,7 +249,7 @@ class trader: return 2 #Save the order - self.status_dict["pause_reason"] = "start_bot - saving the order in deal_order_history" + self.status_dict["pause_reason"] = "start_trader - saving the order in deal_order_history" self.status_dict["deal_order_history"].append(returned_order) # Reset the fee count and sum fees from the first order @@ -269,7 +269,7 @@ class trader: return 1 # Send the take profit order - self.status_dict["pause_reason"] = "start_bot - sending tp order" + self.status_dict["pause_reason"] = "start_trader - sending tp order" self.broker.logger.log_this("Sending take profit order...",2,self.pair) if self.send_new_tp_order()==0: self.broker.logger.log_this("Take profit order sent",2,self.pair) @@ -283,7 +283,7 @@ class trader: self.safety_price_table = self.calculate_safety_prices(self.start_price,self.config_dict["no_of_safety_orders"],self.config_dict["safety_order_deviance"]) # Send the first safety order - self.status_dict["pause_reason"] = "start_bot - sending safety order" + self.status_dict["pause_reason"] = "start_trader - sending safety order" self.broker.logger.log_this("Sending safety order...",2,self.pair) if self.send_new_safety_order(self.status_dict["order_size"])==0: self.broker.logger.log_this("Safety order sent",2,self.pair) @@ -293,7 +293,7 @@ class trader: return 1 # Send cleanup order (if cleanup) - self.status_dict["pause_reason"] = "start_bot - doing cleanup (if needed)" + self.status_dict["pause_reason"] = "start_trader - doing cleanup (if needed)" if self.config_dict["cleanup"] and not self.is_short: #Short traders do not need cleanup. self.do_cleanup() @@ -738,7 +738,7 @@ class trader: #Disabling autoswitch #self.config_dict["autoswitch"] = False - #Done. Ready for start_bot + #Done. Ready for start_trader return 0 @@ -788,7 +788,7 @@ class trader: ''' self.pause = True #To stop the main thread to iterate through this bot's orders (just in case) - self.status_dict["pause_reason"] = "take_profit_routine - order handling" #start_bot will set this flag to False again once it starts + self.status_dict["pause_reason"] = "take_profit_routine - order handling" #start_trader will set this flag to False again once it starts #Let's do some type checking first if self.tp_order is None: @@ -923,7 +923,7 @@ class trader: 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) + self.broker.logger.log_this(f"Slippage threshold exceeded, waiting for cooldown and restarting trader",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. @@ -931,39 +931,39 @@ class trader: 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) + self.broker.logger.log_this(f"Orderbook depth not sufficient, waiting for cooldown and restarting trader",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" - restart_bot = self.start_bot() - self.status_dict["pause_reason"] = "take_profit_routine - restart_bot call - start_bot() called" + self.status_dict["pause_reason"] = "take_profit_routine - restart_trader call" + restart_trader = self.start_trader() + self.status_dict["pause_reason"] = "take_profit_routine - restart_trader call - start_trader() called" #retries = self.broker.get_retries() - if restart_bot==0: + if restart_trader==0: return 0 - elif restart_bot==1: + elif restart_trader==1: self.pause = False self.write_status_file(is_backup=True) self.restart = True - self.broker.logger.log_this("Error in trader, start_bot returned 1. Trader will be restarted",1,self.pair) + self.broker.logger.log_this("Error in trader, start_trader returned 1. Trader will be restarted",1,self.pair) return 1 - elif restart_bot==2: + elif restart_trader==2: self.pause = False self.write_status_file(is_backup=True) self.restart = True - self.broker.logger.log_this("Error in trader, start_bot returned 2 (Initial order never got filled). Trader will be restarted",1,self.pair) + self.broker.logger.log_this("Error in trader, start_trader returned 2 (Initial order never got filled). Trader will be restarted",1,self.pair) return 2 - elif restart_bot==3: + elif restart_trader==3: self.pause = False self.write_status_file(is_backup=True) self.restart = True - self.broker.logger.log_this("Error in trader, start_bot returned 3 (Slippage exceeded). Trader will be restarted",1,self.pair) + self.broker.logger.log_this("Error in trader, start_trader returned 3 (Slippage exceeded). Trader will be restarted",1,self.pair) return 3 else: - self.broker.logger.log_this(f"Error restarting trader, trader will be removed. Error code {restart_bot}",0,self.pair) + self.broker.logger.log_this(f"Error restarting trader, trader will be removed. Error code {restart_trader}",0,self.pair) self.write_status_file(is_backup=True) self.quit = True return 1