forked from EdwardBetts/FincLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
118 lines (94 loc) · 3.47 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import configparser
import os.path
import datetime as dt
import pandas as pd
"""
Class : Config
Author: Peter Lee
Date created: 25 Jan 2016
The module loads and saves to the configuration file (default 'config.ini').
Usage
-----
Load all config settings
>>> from config import config
Reset to default settings
>>> config.reset_to_defaults()
Save new settings
>>> config.save()
Reload settings
>>> config.load()
Notes
-----
Check the documentation: https://docs.python.org/3/library/configparser.html
and make sure do not overwrite default methods while creating new ones.
"""
class Configuration(configparser.ConfigParser):
"""
Class - the configuration settings.
"""
def __init__(self, config_folder='', config_file='config.ini'):
"""
Initialises the configparser object.
Parameters
----------
config_folder : string, default ''
Folder to the config file.
config_file : string, default 'config.ini'
Full path to the config file or simply the filename if located in the home folder of the app.
"""
configparser.ConfigParser.__init__(self)
self.config_file = os.path.join(config_folder, config_file)
self.load() # Load settings
# Parse dates
self.remote_timezone = self['general']['remote_timezone']
self.local_timezone = self['general']['local_timezone']
start_date = self['data']['start_date'] + ' 00:00:00'
self.dt_start_date = pd.Timestamp(start_date, tz=self.remote_timezone)
# Simply get the last business day on or before the current day
end_date = self['data']['end_date']
if end_date.upper() == "NONE":
self.dt_end_date = pd.Timestamp(dt.datetime.now(), tz=self.local_timezone)
self.dt_end_date = self.dt_end_date.tz_convert(self.remote_timezone)
else:
end_date = self['data']['end_date'] + ' 18:00:00'
self.dt_end_date = pd.Timestamp(end_date, tz=self.remote_timezone)
self.dt_end_date -= 3 * pd.tseries.offsets.BDay()
def load(self):
"""
Load settings from the config file, default 'config.ini'.
"""
try:
self.read(self.config_file)
except IOError as e:
print("Error:", e)
raise e
def save(self):
"""
Save settings to the config file, default "config.ini"
"""
with open(self.config_file, 'w') as configfile:
self.write(configfile)
def reset_to_defaults(self):
"""
Reset settings to defaults.
"""
if not self.has_section("notes"):
self.add_section("notes")
self.set("notes", "author", "Peter Lee")
self.set("notes", "email", "mr.peter.lee@hotmail.com")
if not self.has_section("components"):
self.add_section("components")
self.set("components", "strategy", "MovingAverageCrossover")
self.set("components", "data_handler", "HistoricDataHandler")
self.set("components", "execution_handler", "SimulatedExecutionHandler")
config = Configuration()
if __name__ == '__main__':
print("debugging...")
# config = Configuration()
# config.reset_to_defaults()
print(config.sections())
print(config["notes"]["Author"])
# config.save()
print("UTC", config.remote_timezone)
print("Start date: ", config.dt_start_date)
print("End date: ", config.dt_end_date)