minor refactor in take_profit_routine

This commit is contained in:
Nicolás Sánchez 2024-11-10 00:29:07 -03:00
parent 2f02459aeb
commit af9a9531fe
2 changed files with 15 additions and 56 deletions

View File

@ -1,5 +1,6 @@
2024.11.09:
. Removed multiplier modifier added in 0.12d, since it was not needed anymore.
. Minor refactor in take_profit_routine
2024.11.08:
. Added boosted take profit level: If the trader is actively closing deals, the expected profit level is raised by x%.

View File

@ -792,15 +792,13 @@ class trader:
#Let's do some type checking first
if self.tp_order is None:
error_string = time.strftime(f"[%Y/%m/%d %H:%M:%S] | {self.pair} | TP order is None")
self.status_dict["pause_reason"] = error_string
self.status_dict["pause_reason"] = time.strftime(f"[%Y/%m/%d %H:%M:%S] | {self.pair} | TP order is None")
self.broker.logger.log_this("Error. Take profit order is None, pair will be restarted",0,self.pair)
self.write_status_file(is_backup=True)
self.restart = True
return 1
if self.so is None:
error_string = time.strftime(f"[%Y/%m/%d %H:%M:%S] | {self.pair} | Safety order is None")
self.status_dict["pause_reason"] = error_string
self.status_dict["pause_reason"] = time.strftime(f"[%Y/%m/%d %H:%M:%S] | {self.pair} | Safety order is None")
self.broker.logger.log_this("Error. Safety order is None",1,self.pair)
self.so = self.broker.get_empty_order()
@ -829,12 +827,12 @@ class trader:
if old_so_order["filled"]>0:
self.broker.logger.log_this(f"Old safety order is partially filled, ID: {old_so_order['id']}",1,self.pair)
self.status_dict["deal_order_history"].append(old_so_order)
#Uncomment the next two lines if you do not want to ignore the partial fill
#Uncomment the next two lines if you do not want to ignore the partial fill.
#Keep them commented out if you want to send a market sell order.
#self.total_amount_of_base = self.total_amount_of_base + old_so_order["filled"]
#self.total_amount_of_quote = self.total_amount_of_quote + old_so_order["cost"]
#Hypothetical market order code below:
#DO NOT UPDATE total_amount_of_base and total_amount_of_quote
#If there is not enough base for an order, we'll accumulate it and let the cleanup routine deal with that.
# floor_amount = self.broker.get_min_base_size(self.pair)
# if floor_amount is not None and old_so_order["filled"]>floor_amount:
@ -878,25 +876,6 @@ class trader:
self.broker.logger.log_this(f"Fill price: {filled_order['price']} {self.quote}",2,self.pair)
self.broker.logger.log_this(f"Safety orders triggered: {self.safety_order_index-1}",2,self.pair)
'''
RENDERED OBSOLETE BY THE NEW SWITCH TO LONG OPTIMIZATION
#Check if it needs to switch the trader mode
self.status_dict["pause_reason"] = "check for autoswitch"
#If it's a short bot that used to be long AND autoswitch is enabled
if self.is_short and self.config_dict["autoswitch"] and "old_long" in self.status_dict and filled_order["average"]>self.status_dict["old_long"]["tp_price"]:
#Checking the price again to prevent being fooled by anomalous spikes
#It could be done above instead of using filled_order["average"], bit this way eases the API load a little bit,
#since every time (except the last one) it won't be bigger anyway.
if float(self.broker.get_ticker_price(self.pair))>self.status_dict["old_long"]["tp_price"]:
#Sell all base (market), report the profits and restart the trader
self.status_dict["pause_reason"] = "automatic_switch"
if self.switch_to_long(from_take_profit=True)==1:
self.write_status_file(is_backup=True)
self.restart = True
self.broker.logger.log_this("Error, switch_to_long returned 1, pair will be restarted",0,self.pair)
return 1
'''
self.status_dict["pause_reason"] = "take_profit_routine - check time limit"
#Checks if there is a time limit for the trader
if "stop_time" in self.config_dict and time.time()>int(self.config_dict["stop_time"]):
@ -908,15 +887,8 @@ class trader:
self.quit = True
return 1
# Clear variables and reload config_dict
#self.pause = False
# Clear variables and reload config_dict (Only reloading the config file is needed.)
self.status_dict["pause_reason"] = "take_profit_routine - var cleanup"
self.fees_paid_in_quote = 0
self.fees_paid_in_base = 0
#self.tp_order = self.broker.get_empty_order()
#self.so = self.broker.get_empty_order()
self.safety_price_table.clear()
#self.safety_order_index = 0
self.config_dict = self.reload_config_dict()
if self.check_slippage:
@ -937,37 +909,23 @@ class trader:
self.restart = True
return 1
#Possible restart errors
restart_errors = {1: "start_trader returned error #1. Trader will be restarted",
2: "start_trader returned error #2: Initial order never got filled. Trader will be restarted",
3: "start_trader returned error #3: Slippage threshold exceeded. Trader will be restarted"}
#Restarting the trader
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_trader==0:
return 0
elif restart_trader==1:
if restart_trader in restart_errors.keys():
self.pause = False
self.write_status_file(is_backup=True)
self.restart = True
self.broker.logger.log_this("Error in trader, start_trader returned 1. Trader will be restarted",1,self.pair)
return 1
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_trader returned 2 (Initial order never got filled). Trader will be restarted",1,self.pair)
return 2
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_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_trader}",0,self.pair)
self.write_status_file(is_backup=True)
self.quit = True
return 1
self.broker.logger.log_this(restart_errors[restart_trader],1,self.pair)
return restart_trader
def sum_filled_amounts(self, order: dict) -> int:
'''