-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathconfig_manager.py
More file actions
265 lines (218 loc) · 8.27 KB
/
config_manager.py
File metadata and controls
265 lines (218 loc) · 8.27 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import json
import os
import sys
from typing import Dict, Any, Optional
class ConfigManager:
"""
Configuration manager for the Expert Advisor trading bot
Handles loading, validation, and access to configuration settings
"""
def __init__(self, config_file: str = "config.json"):
"""
Initialize configuration manager
Args:
config_file (str): Path to configuration file
"""
self.config_file = config_file
self.config = {}
self.load_config()
def load_config(self) -> bool:
"""
Load configuration from JSON file
Returns:
bool: True if successful, False otherwise
"""
try:
if not os.path.exists(self.config_file):
print(f"Configuration file {self.config_file} not found")
self.create_default_config()
return False
with open(self.config_file, 'r', encoding='utf-8') as f:
self.config = json.load(f)
if self.validate_config():
print(f"Configuration loaded successfully from {self.config_file}")
return True
else:
print("Configuration validation failed")
return False
except json.JSONDecodeError as e:
print(f"Error parsing configuration file: {e}")
return False
except Exception as e:
print(f"Error loading configuration: {e}")
return False
def validate_config(self) -> bool:
"""
Validate configuration settings
Returns:
bool: True if valid, False otherwise
"""
required_sections = ['trading', 'analysis', 'symbols', 'logging', 'mt5']
for section in required_sections:
if section not in self.config:
print(f"Missing required configuration section: {section}")
return False
# Validate trading settings
trading = self.config['trading']
if trading['risk_amount'] <= 0:
print("Risk amount must be positive")
return False
if trading['max_daily_loss'] <= 0:
print("Max daily loss must be positive")
return False
if trading['min_risk_reward'] < 1.0:
print("Minimum risk reward must be at least 1.0")
return False
# Validate symbols
if not self.config['symbols']:
print("At least one symbol must be configured")
return False
return True
def get(self, key_path: str, default: Any = None) -> Any:
"""
Get configuration value using dot notation
Args:
key_path (str): Configuration key path (e.g., 'trading.risk_amount')
default (Any): Default value if key not found
Returns:
Any: Configuration value or default
"""
try:
keys = key_path.split('.')
value = self.config
for key in keys:
value = value[key]
return value
except (KeyError, TypeError):
return default
def set(self, key_path: str, value: Any) -> bool:
"""
Set configuration value using dot notation
Args:
key_path (str): Configuration key path
value (Any): Value to set
Returns:
bool: True if successful, False otherwise
"""
try:
keys = key_path.split('.')
config_ref = self.config
# Navigate to parent of target key
for key in keys[:-1]:
if key not in config_ref:
config_ref[key] = {}
config_ref = config_ref[key]
# Set the value
config_ref[keys[-1]] = value
return True
except Exception as e:
print(f"Error setting configuration value: {e}")
return False
def save_config(self) -> bool:
"""
Save current configuration to file
Returns:
bool: True if successful, False otherwise
"""
try:
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(self.config, f, indent=2, ensure_ascii=False)
print(f"Configuration saved to {self.config_file}")
return True
except Exception as e:
print(f"Error saving configuration: {e}")
return False
def create_default_config(self):
"""
Create default configuration file
"""
default_config = {
"trading": {
"risk_amount": 50.0,
"max_daily_loss": 200.0,
"min_risk_reward": 2.0,
"max_spread_pips": 3.0,
"max_risk_percent": 5.0,
"magic_number": 234567
},
"analysis": {
"lookback_period": 20,
"macd_fast": 12,
"macd_slow": 26,
"macd_signal": 9,
"support_resistance_touches": 2,
"candle_body_threshold": 1.5
},
"symbols": [
{
"name": "EURUSD",
"enabled": True,
"max_spread": 0.0003,
"timeframe": "M1"
},
{
"name": "GBPUSD",
"enabled": True,
"max_spread": 0.0004,
"timeframe": "M1"
}
],
"logging": {
"enabled": True,
"level": "INFO",
"log_trades": True,
"log_analysis": False
},
"mt5": {
"terminal_paths": [
"C:\\Program Files\\MetaTrader 5\\terminal64.exe",
"C:\\Program Files (x86)\\MetaTrader 5\\terminal64.exe"
],
"connection_timeout": 60,
"retry_attempts": 3
}
}
try:
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(default_config, f, indent=2, ensure_ascii=False)
self.config = default_config
print(f"Default configuration created: {self.config_file}")
except Exception as e:
print(f"Error creating default configuration: {e}")
def get_trading_config(self) -> Dict[str, Any]:
"""Get trading configuration section"""
return self.config.get('trading', {})
def get_analysis_config(self) -> Dict[str, Any]:
"""Get analysis configuration section"""
return self.config.get('analysis', {})
def get_enabled_symbols(self) -> list:
"""Get list of enabled trading symbols"""
symbols = self.config.get('symbols', [])
return [s for s in symbols if s.get('enabled', False)]
def get_mt5_config(self) -> Dict[str, Any]:
"""Get MT5 configuration section"""
return self.config.get('mt5', {})
def is_logging_enabled(self) -> bool:
"""Check if logging is enabled"""
return self.config.get('logging', {}).get('enabled', True)
def print_config(self):
"""Print current configuration in readable format"""
print("\nCurrent Configuration:")
print("=" * 50)
print(json.dumps(self.config, indent=2))
def main():
"""Test configuration manager"""
config = ConfigManager()
print("Configuration Manager Test")
print("=" * 30)
# Test getting values
print(f"Risk amount: {config.get('trading.risk_amount')}")
print(f"MACD fast period: {config.get('analysis.macd_fast')}")
print(f"Enabled symbols: {[s['name'] for s in config.get_enabled_symbols()]}")
# Test setting values
config.set('trading.risk_amount', 75.0)
print(f"Updated risk amount: {config.get('trading.risk_amount')}")
# Print full configuration
config.print_config()
if __name__ == "__main__":
main()