config handler initial implementation

This commit is contained in:
Nicolás Sánchez 2025-02-26 10:38:45 -03:00
parent 72c8773263
commit 3d9f28d6a0
3 changed files with 20 additions and 48 deletions

View File

@ -33,7 +33,7 @@ class ConfigHandler:
"force_restart_if_retries_exhausted": False,
"check_old_long_price": False #switch_to_short should flip this to True unless stated
}
self.config_file_path = f"configs/{pair.split('/')[0]}{pair.split('/')[1]}.json"
if config_dict is None:
self.config_dictionary = self.default_config_dictionary.copy()
else:
@ -213,16 +213,20 @@ class ConfigHandler:
return 0
def save_to_file(self, file_path: str):
def save_to_file(self, file_path = None):
if file_path is None:
file_path = self.config_file_path
try:
with open(file_path, "w") as f:
json.dump(self.config_dictionary, f)
f.write(json.dumps(self.config_dictionary, indent=4))
return 0
except Exception as e:
self.broker.logger.log_this(f"Error saving config to file: {file_path}: {e}",1,self.get_pair())
return 1
def load_from_file(self, file_path: str):
def load_from_file(self, file_path = None):
if file_path is None:
file_path = self.config_file_path
try:
with open(file_path, "r") as f:
self.set_config(json.load(f))

View File

@ -13,6 +13,7 @@ Mandatory:
* Status object (instead of a status dictionary).
8. Once #7 is implemented, if a config file does not exist, don't generate it, just use the default. If a config file exists, it overrides the defaults.
9. Implement the ability to add safety orders to a short trader depending on the amount of free funds available, not just any number of orders.
10. API documentation.
Would be nice to have:

View File

@ -57,11 +57,6 @@ class trader:
"short_price_exceeds_old_long": False,
"speol_notified": False
}
#What is this? Why?
if self.config.get_programmed_stop() and int(self.config.get_programmed_stop_time())<int(time.time()):
self.config_dict.pop("stop_time",None)
#Write config file changes
self.broker.rewrite_config_file()
if self.is_short:
#Check if there is an old_long file. If so, load it.
@ -192,9 +187,7 @@ class trader:
self.broker.logger.log_this(f"Order size: {self.broker.amount_to_precision(self.pair,order_size)}. Amount of safety orders: {no_of_safety_orders}",2,self.pair)
#Write the changes to the config file
with open(f"configs/{self.base}{self.quote}.json","w") as g:
g.write(json.dumps(self.config_dict, indent=4))
self.config.save_to_file()
else:
#Check order size
self.status_dict["pause_reason"] = "start_trader - checking order size"
@ -319,22 +312,6 @@ class trader:
return 0
def reload_config_dict(self) -> dict:
'''
Reloads the config dictionary from disk
:return: dict
'''
config_filename = f"configs/{self.base}{self.quote}.json"
with open(config_filename,"r") as y:
config_dict = json.load(y)
if self.config.get_autoswitch(): # In what context was this useful?
config_dict["autoswitch"] = True #
return config_dict
def update_status(self, write_to_disk: bool) -> int:
'''
Updates the status dictionary
@ -378,9 +355,7 @@ class trader:
self.status_dict["start_time"]=self.start_time
self.status_dict["deal_start_time"]=self.deal_start_time
self.status_dict["stop_when_profit"]=self.stop_when_profit
self.status_dict["autoswitch"]=False
if "autoswitch" in self.config_dict:
self.status_dict["autoswitch"]=self.config.get_autoswitch()
self.status_dict["autoswitch"]=self.config.get_autoswitch()
except Exception as e:
self.broker.logger.log_this(f"Can't update status dictionary. Exception: {e}",1,self.pair)
@ -895,9 +870,9 @@ class trader:
self.quit = True
return 1
# Clear variables and reload config_dict (Only reloading the config file is needed.)
# Clear variables and reload the config dictionary (Only reloading the config file is needed.)
self.status_dict["pause_reason"] = "take_profit_routine - var cleanup"
self.config_dict = self.reload_config_dict()
self.config.load_from_file()
if self.check_slippage:
self.broker.logger.log_this("Checking slippage...",2,self.pair)
@ -1197,7 +1172,7 @@ class trader:
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 "autoswitch" in self.config_dict and self.config.get_autoswitch() and "old_long" in self.status_dict:
if self.is_short and self.config.get_autoswitch() and "old_long" in self.status_dict:
#If selling the base currency left at the current market price plus the quote already received turns out to be more than the old long deal target,
# it means that we already are in profit territory, switch back to long.
#A more conservative approach would be old_target = self.status_dict["old_long"]["quote_spent"], just breaking even.
@ -1262,7 +1237,7 @@ class trader:
else:
tp_level = self.config.get_tp_level()-0.005
elif self.config.get_tp_mode()==2:
if ["tp_table"] in self.config_dict:
if self.config.get_tp_table()!=[]:
if len(self.config.get_tp_table())>=order_index:
tp_level = self.config.get_tp_table()[order_index] #Custom percentage table
tp_level = self.config.get_tp_table()[-1]
@ -1420,16 +1395,11 @@ class trader:
Generates a table of safety order's prices
'''
safety_price_table = [start_price]
if self.config.get_dynamic_so_deviance():# and no_of_safety_orders>=30:
#if self.config.get_dynamic_so_deviance() and not self.is_short:
if self.config.get_dynamic_so_deviance():
#bias should be a real number between -1 and 1 (1>n>-1, NOT 1=>n>=-1)
#If bias -> 1, more space between the first orders, if -> -1, more space between the last orders, if 0, no change..
if "bias" in self.config_dict:
deviance_factor = safety_order_deviance*self.clip_value(self.config.get_bias(),-.99,.99)
so_deviance_table = self.linear_space(safety_order_deviance+deviance_factor,safety_order_deviance-deviance_factor,no_of_safety_orders)
else:
#Old way of calculating deviance
so_deviance_table = self.linear_space(safety_order_deviance-self.config.get_dsd_range(),safety_order_deviance+self.config.get_dsd_range(),no_of_safety_orders)
deviance_factor = safety_order_deviance*self.clip_value(self.config.get_bias(),-.99,.99)
so_deviance_table = self.linear_space(safety_order_deviance+deviance_factor,safety_order_deviance-deviance_factor,no_of_safety_orders)
so_deviance_table.extend([so_deviance_table[-1]]*2) #This extra entries are needed in the next for loop
else:
so_deviance_table = [safety_order_deviance]*(no_of_safety_orders+2)
@ -1546,11 +1516,8 @@ class trader:
self.broker.logger.log_this(f"Exception while writing new old_long file: {e}",1,self.pair)
#Write the new config file
try:
with open(f"configs/{self.base}{self.quote}.json","w") as c:
c.write(json.dumps(self.config_dict, indent=4))
except Exception as e:
self.broker.logger.log_this(f"Exception while writing new trader config file: {e}",1,self.pair)
if self.config.save_to_file(f"configs/{self.base}{self.quote}.json")==1:
self.broker.logger.log_this(f"Error while writing the new trader config file",1,self.pair)
#Undoing changes
self.quote_currency_undo_changes(new_quote,old_quote,True)
return 1