brighter-trading/Configuration.py

78 lines
2.9 KiB
Python

from binance.enums import *
import yaml
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()
# The data that will be saved and loaded from file .
self.saved_data = None
def set_indicator_list(self, list):
self.indicator_list = list
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}
}
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']
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.')