from binance.enums import * import yaml from Signals import Signals from indicators import Indicators class Configuration: def __init__(self): # ************** Default values************** # The title of our program. self.application_title = 'BrighterTrades' # The maximum number of candles to store in memory. self.max_data_loaded = 1000 # The default values for the main chart. self.chart_interval = KLINE_INTERVAL_15MINUTE self.trading_pair = 'BTCUSDT' # The name of the file that stores saved_data self.config_FN = 'config.yml' # Call a static method from indicators that fills in a default list of indicators in config. self.indicator_list = Indicators.get_indicator_defaults() # Call a static method from indicators that fills in a default list of indicators in config. self.signals_list = Signals.get_signals_defaults() # list of strategies in config. self.strategies_list = [] # The data that will be saved and loaded from file . self.saved_data = None def new_strategy(self, data): # The strategies_list is modified by reference in strategies the loaded in config_and_states('save'). # Save it to file. self.config_and_states('save') def new_signal(self, data): # Create a new signal. self.saved_data['signals'].append(data) # Save it to file. self.config_and_states('save') def remove(self, what, name): print(f'removing {what}:{name}') for obj in self.saved_data[what]: if obj['name'] == name: self.saved_data[what].remove(obj) break # Save it to file. self.config_and_states('save') def config_and_states(self, cmd): """Loads or saves configurable data to the file set in self.config_FN""" # The data stored and retrieved from file session. self.saved_data = { 'indicator_list': self.indicator_list, 'config': {'chart_interval': self.chart_interval, 'trading_pair': self.trading_pair}, 'signals': self.signals_list, 'strategies': self.strategies_list } def set_loaded_values(): # Sets the values in the saved_data object. self.indicator_list = self.saved_data['indicator_list'] self.chart_interval = self.saved_data['config']['chart_interval'] self.trading_pair = self.saved_data['config']['trading_pair'] self.signals_list = self.saved_data['signals'] self.strategies_list = self.saved_data['strategies'] def load_configuration(filepath): """load file data""" with open(filepath, "r") as file_descriptor: data = yaml.safe_load(file_descriptor) return data def save_configuration(filepath, data): """Saves file data""" with open(filepath, "w") as file_descriptor: yaml.dump(data, file_descriptor) if cmd == 'load': # If load_configuration() finds a file it overwrites # the saved_data object otherwise it creates a new file # with the defaults contained in saved_data> try: # If file exist load the values. self.saved_data = load_configuration(self.config_FN) set_loaded_values() except IOError: # If file doesn't exist create a file and save the default values. save_configuration(self.config_FN, self.saved_data) elif cmd == 'save': try: # Write saved_data to the file. save_configuration(self.config_FN, self.saved_data) except IOError: raise ValueError("save_configuration(): Couldn't save the file.") else: raise ValueError('save_configuration(): Invalid command received.')