config handler initial implementation
This commit is contained in:
parent
72c8773263
commit
3d9f28d6a0
|
|
@ -33,7 +33,7 @@ class ConfigHandler:
|
||||||
"force_restart_if_retries_exhausted": False,
|
"force_restart_if_retries_exhausted": False,
|
||||||
"check_old_long_price": False #switch_to_short should flip this to True unless stated
|
"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:
|
if config_dict is None:
|
||||||
self.config_dictionary = self.default_config_dictionary.copy()
|
self.config_dictionary = self.default_config_dictionary.copy()
|
||||||
else:
|
else:
|
||||||
|
|
@ -213,16 +213,20 @@ class ConfigHandler:
|
||||||
return 0
|
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:
|
try:
|
||||||
with open(file_path, "w") as f:
|
with open(file_path, "w") as f:
|
||||||
json.dump(self.config_dictionary, f)
|
f.write(json.dumps(self.config_dictionary, indent=4))
|
||||||
return 0
|
return 0
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.broker.logger.log_this(f"Error saving config to file: {file_path}: {e}",1,self.get_pair())
|
self.broker.logger.log_this(f"Error saving config to file: {file_path}: {e}",1,self.get_pair())
|
||||||
return 1
|
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:
|
try:
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
self.set_config(json.load(f))
|
self.set_config(json.load(f))
|
||||||
|
|
|
||||||
1
todo.txt
1
todo.txt
|
|
@ -13,6 +13,7 @@ Mandatory:
|
||||||
* Status object (instead of a status dictionary).
|
* 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.
|
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.
|
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:
|
Would be nice to have:
|
||||||
|
|
|
||||||
55
trader.py
55
trader.py
|
|
@ -57,11 +57,6 @@ class trader:
|
||||||
"short_price_exceeds_old_long": False,
|
"short_price_exceeds_old_long": False,
|
||||||
"speol_notified": 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:
|
if self.is_short:
|
||||||
#Check if there is an old_long file. If so, load it.
|
#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)
|
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
|
#Write the changes to the config file
|
||||||
with open(f"configs/{self.base}{self.quote}.json","w") as g:
|
self.config.save_to_file()
|
||||||
g.write(json.dumps(self.config_dict, indent=4))
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
#Check order size
|
#Check order size
|
||||||
self.status_dict["pause_reason"] = "start_trader - checking order size"
|
self.status_dict["pause_reason"] = "start_trader - checking order size"
|
||||||
|
|
@ -319,22 +312,6 @@ class trader:
|
||||||
return 0
|
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:
|
def update_status(self, write_to_disk: bool) -> int:
|
||||||
'''
|
'''
|
||||||
Updates the status dictionary
|
Updates the status dictionary
|
||||||
|
|
@ -378,9 +355,7 @@ class trader:
|
||||||
self.status_dict["start_time"]=self.start_time
|
self.status_dict["start_time"]=self.start_time
|
||||||
self.status_dict["deal_start_time"]=self.deal_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["stop_when_profit"]=self.stop_when_profit
|
||||||
self.status_dict["autoswitch"]=False
|
self.status_dict["autoswitch"]=self.config.get_autoswitch()
|
||||||
if "autoswitch" in self.config_dict:
|
|
||||||
self.status_dict["autoswitch"]=self.config.get_autoswitch()
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.broker.logger.log_this(f"Can't update status dictionary. Exception: {e}",1,self.pair)
|
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
|
self.quit = True
|
||||||
return 1
|
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.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:
|
if self.check_slippage:
|
||||||
self.broker.logger.log_this("Checking slippage...",2,self.pair)
|
self.broker.logger.log_this("Checking slippage...",2,self.pair)
|
||||||
|
|
@ -1197,7 +1172,7 @@ class trader:
|
||||||
|
|
||||||
self.status_dict["pause_reason"] = "check for autoswitch"
|
self.status_dict["pause_reason"] = "check for autoswitch"
|
||||||
#If it's a short bot that used to be long AND autoswitch is enabled
|
#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,
|
#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.
|
# 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.
|
#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:
|
else:
|
||||||
tp_level = self.config.get_tp_level()-0.005
|
tp_level = self.config.get_tp_level()-0.005
|
||||||
elif self.config.get_tp_mode()==2:
|
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:
|
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()[order_index] #Custom percentage table
|
||||||
tp_level = self.config.get_tp_table()[-1]
|
tp_level = self.config.get_tp_table()[-1]
|
||||||
|
|
@ -1420,16 +1395,11 @@ class trader:
|
||||||
Generates a table of safety order's prices
|
Generates a table of safety order's prices
|
||||||
'''
|
'''
|
||||||
safety_price_table = [start_price]
|
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():
|
||||||
#if self.config.get_dynamic_so_deviance() and not self.is_short:
|
|
||||||
#bias should be a real number between -1 and 1 (1>n>-1, NOT 1=>n>=-1)
|
#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 -> 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)
|
||||||
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 = 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)
|
|
||||||
so_deviance_table.extend([so_deviance_table[-1]]*2) #This extra entries are needed in the next for loop
|
so_deviance_table.extend([so_deviance_table[-1]]*2) #This extra entries are needed in the next for loop
|
||||||
else:
|
else:
|
||||||
so_deviance_table = [safety_order_deviance]*(no_of_safety_orders+2)
|
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)
|
self.broker.logger.log_this(f"Exception while writing new old_long file: {e}",1,self.pair)
|
||||||
|
|
||||||
#Write the new config file
|
#Write the new config file
|
||||||
try:
|
if self.config.save_to_file(f"configs/{self.base}{self.quote}.json")==1:
|
||||||
with open(f"configs/{self.base}{self.quote}.json","w") as c:
|
self.broker.logger.log_this(f"Error while writing the new trader config file",1,self.pair)
|
||||||
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)
|
|
||||||
#Undoing changes
|
#Undoing changes
|
||||||
self.quote_currency_undo_changes(new_quote,old_quote,True)
|
self.quote_currency_undo_changes(new_quote,old_quote,True)
|
||||||
return 1
|
return 1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue