mql5/Include/Utils/Config.mqh

372 lines
13 KiB
MQL5
Raw Permalink Normal View History

//+------------------------------------------------------------------+
//| Config.mqh - Professional Configuration Management |
//| Centralized configuration for Revolutionary AI EA |
//+------------------------------------------------------------------+
#ifndef CONFIG_MQH
#define CONFIG_MQH
#include "JAson.mqh"
// Configuration structure for easy management
struct EAConfig {
// Trading settings
bool auto_trading;
double risk_per_trade;
double max_daily_loss;
int min_confidence;
string server_url;
// Timing settings
int analysis_interval;
int risk_check_interval;
int min_time_between_trades;
// Risk management
double max_drawdown;
double max_position_size;
int max_open_positions;
double emergency_stop_level;
// Display settings
bool enable_dashboard;
int dashboard_x_pos;
int dashboard_y_pos;
// Advanced settings
bool enable_logging;
bool enable_alerts;
bool enable_email_notifications;
string log_level;
};
class ConfigManager
{
private:
EAConfig m_config;
string m_config_file;
bool m_loaded;
public:
ConfigManager(void);
~ConfigManager(void);
// Core methods
bool LoadConfig(string config_file = "revolutionary_ai_config.json");
bool SaveConfig(void);
bool LoadDefaults(void);
// Getters
EAConfig GetConfig(void) { return m_config; }
bool IsAutoTradingEnabled(void) { return m_config.auto_trading; }
double GetRiskPerTrade(void) { return m_config.risk_per_trade; }
double GetMaxDailyLoss(void) { return m_config.max_daily_loss; }
int GetMinConfidence(void) { return m_config.min_confidence; }
string GetServerURL(void) { return m_config.server_url; }
int GetAnalysisInterval(void) { return m_config.analysis_interval; }
// Setters
void SetAutoTrading(bool enabled) { m_config.auto_trading = enabled; }
void SetRiskPerTrade(double risk) { m_config.risk_per_trade = risk; }
void SetMaxDailyLoss(double loss) { m_config.max_daily_loss = loss; }
void SetMinConfidence(int confidence) { m_config.min_confidence = confidence; }
// Validation
bool ValidateConfig(void);
string GetValidationErrors(void);
// Utilities
void PrintConfig(void);
string ConfigToJson(void);
bool JsonToConfig(string json_string);
};
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
ConfigManager::ConfigManager(void)
{
m_config_file = "";
m_loaded = false;
LoadDefaults();
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
ConfigManager::~ConfigManager(void)
{
// Auto-save on exit
if (m_loaded && m_config_file != "") {
SaveConfig();
}
}
//+------------------------------------------------------------------+
//| Load configuration from file |
//+------------------------------------------------------------------+
bool ConfigManager::LoadConfig(string config_file = "revolutionary_ai_config.json")
{
m_config_file = config_file;
// Try to load from file
int file_handle = FileOpen(config_file, FILE_READ | FILE_TXT);
if (file_handle == INVALID_HANDLE) {
Print("📁 Config file not found, creating default: ", config_file);
LoadDefaults();
SaveConfig();
return true;
}
// Read file content
string json_content = "";
while (!FileIsEnding(file_handle)) {
json_content += FileReadString(file_handle) + "\n";
}
FileClose(file_handle);
if (json_content == "") {
Print("❌ Config file is empty, using defaults");
LoadDefaults();
return true;
}
// Parse JSON
if (JsonToConfig(json_content)) {
if (ValidateConfig()) {
m_loaded = true;
Print("✅ Configuration loaded successfully from ", config_file);
return true;
} else {
Print("❌ Configuration validation failed: ", GetValidationErrors());
LoadDefaults();
return false;
}
} else {
Print("❌ Failed to parse configuration file, using defaults");
LoadDefaults();
return false;
}
}
//+------------------------------------------------------------------+
//| Save configuration to file |
//+------------------------------------------------------------------+
bool ConfigManager::SaveConfig(void)
{
if (m_config_file == "") {
m_config_file = "revolutionary_ai_config.json";
}
string json_content = ConfigToJson();
int file_handle = FileOpen(m_config_file, FILE_WRITE | FILE_TXT);
if (file_handle == INVALID_HANDLE) {
Print("❌ Failed to create config file: ", m_config_file);
return false;
}
FileWriteString(file_handle, json_content);
FileClose(file_handle);
Print("✅ Configuration saved to ", m_config_file);
return true;
}
//+------------------------------------------------------------------+
//| Load default configuration |
//+------------------------------------------------------------------+
bool ConfigManager::LoadDefaults(void)
{
// Trading defaults
m_config.auto_trading = true;
m_config.risk_per_trade = 2.0;
m_config.max_daily_loss = 5.0;
m_config.min_confidence = 75;
m_config.server_url = "http://localhost:8000";
// Timing defaults
m_config.analysis_interval = 300; // 5 minutes
m_config.risk_check_interval = 60; // 1 minute
m_config.min_time_between_trades = 900; // 15 minutes
// Risk management defaults
m_config.max_drawdown = 15.0;
m_config.max_position_size = 10.0;
m_config.max_open_positions = 3;
m_config.emergency_stop_level = 20.0;
// Display defaults
m_config.enable_dashboard = true;
m_config.dashboard_x_pos = 20;
m_config.dashboard_y_pos = 50;
// Advanced defaults
m_config.enable_logging = true;
m_config.enable_alerts = true;
m_config.enable_email_notifications = false;
m_config.log_level = "INFO";
Print("📋 Default configuration loaded");
return true;
}
//+------------------------------------------------------------------+
//| Validate configuration values |
//+------------------------------------------------------------------+
bool ConfigManager::ValidateConfig(void)
{
// Risk validation
if (m_config.risk_per_trade <= 0 || m_config.risk_per_trade > 20) {
return false;
}
if (m_config.max_daily_loss <= 0 || m_config.max_daily_loss > 50) {
return false;
}
if (m_config.min_confidence < 50 || m_config.min_confidence > 100) {
return false;
}
// Timing validation
if (m_config.analysis_interval < 60 || m_config.analysis_interval > 3600) {
return false;
}
if (m_config.risk_check_interval < 30 || m_config.risk_check_interval > 300) {
return false;
}
// Position validation
if (m_config.max_open_positions < 1 || m_config.max_open_positions > 10) {
return false;
}
return true;
}
//+------------------------------------------------------------------+
//| Get validation error messages |
//+------------------------------------------------------------------+
string ConfigManager::GetValidationErrors(void)
{
string errors = "";
if (m_config.risk_per_trade <= 0 || m_config.risk_per_trade > 20) {
errors += "Risk per trade must be between 0.1% and 20%; ";
}
if (m_config.max_daily_loss <= 0 || m_config.max_daily_loss > 50) {
errors += "Max daily loss must be between 0.1% and 50%; ";
}
if (m_config.min_confidence < 50 || m_config.min_confidence > 100) {
errors += "Min confidence must be between 50% and 100%; ";
}
if (m_config.analysis_interval < 60 || m_config.analysis_interval > 3600) {
errors += "Analysis interval must be between 60 and 3600 seconds; ";
}
if (m_config.max_open_positions < 1 || m_config.max_open_positions > 10) {
errors += "Max open positions must be between 1 and 10; ";
}
return errors;
}
//+------------------------------------------------------------------+
//| Print current configuration |
//+------------------------------------------------------------------+
void ConfigManager::PrintConfig(void)
{
Print("═══ REVOLUTIONARY AI CONFIGURATION ═══");
Print("Auto Trading: ", m_config.auto_trading ? "ENABLED" : "DISABLED");
Print("Risk Per Trade: ", DoubleToString(m_config.risk_per_trade, 2), "%");
Print("Max Daily Loss: ", DoubleToString(m_config.max_daily_loss, 2), "%");
Print("Min Confidence: ", m_config.min_confidence, "%");
Print("Server URL: ", m_config.server_url);
Print("Analysis Interval: ", m_config.analysis_interval, " seconds");
Print("Max Open Positions: ", m_config.max_open_positions);
Print("Dashboard: ", m_config.enable_dashboard ? "ENABLED" : "DISABLED");
Print("═══════════════════════════════════════");
}
//+------------------------------------------------------------------+
//| Convert configuration to JSON string |
//+------------------------------------------------------------------+
string ConfigManager::ConfigToJson(void)
{
string json = "{\n";
json += " \"trading\": {\n";
json += " \"auto_trading\": " + (m_config.auto_trading ? "true" : "false") + ",\n";
json += " \"risk_per_trade\": " + DoubleToString(m_config.risk_per_trade, 2) + ",\n";
json += " \"max_daily_loss\": " + DoubleToString(m_config.max_daily_loss, 2) + ",\n";
json += " \"min_confidence\": " + IntegerToString(m_config.min_confidence) + ",\n";
json += " \"server_url\": \"" + m_config.server_url + "\"\n";
json += " },\n";
json += " \"timing\": {\n";
json += " \"analysis_interval\": " + IntegerToString(m_config.analysis_interval) + ",\n";
json += " \"risk_check_interval\": " + IntegerToString(m_config.risk_check_interval) + ",\n";
json += " \"min_time_between_trades\": " + IntegerToString(m_config.min_time_between_trades) + "\n";
json += " },\n";
json += " \"risk_management\": {\n";
json += " \"max_drawdown\": " + DoubleToString(m_config.max_drawdown, 2) + ",\n";
json += " \"max_position_size\": " + DoubleToString(m_config.max_position_size, 2) + ",\n";
json += " \"max_open_positions\": " + IntegerToString(m_config.max_open_positions) + ",\n";
json += " \"emergency_stop_level\": " + DoubleToString(m_config.emergency_stop_level, 2) + "\n";
json += " },\n";
json += " \"display\": {\n";
json += " \"enable_dashboard\": " + (m_config.enable_dashboard ? "true" : "false") + ",\n";
json += " \"dashboard_x_pos\": " + IntegerToString(m_config.dashboard_x_pos) + ",\n";
json += " \"dashboard_y_pos\": " + IntegerToString(m_config.dashboard_y_pos) + "\n";
json += " },\n";
json += " \"advanced\": {\n";
json += " \"enable_logging\": " + (m_config.enable_logging ? "true" : "false") + ",\n";
json += " \"enable_alerts\": " + (m_config.enable_alerts ? "true" : "false") + ",\n";
json += " \"enable_email_notifications\": " + (m_config.enable_email_notifications ? "true" : "false") + ",\n";
json += " \"log_level\": \"" + m_config.log_level + "\"\n";
json += " }\n";
json += "}";
return json;
}
//+------------------------------------------------------------------+
//| Parse JSON string to configuration |
//+------------------------------------------------------------------+
bool ConfigManager::JsonToConfig(string json_string)
{
// Simplified JSON parsing for essential settings
// In a production system, you'd use a proper JSON parser
// Extract key values using string operations
if (StringFind(json_string, "\"auto_trading\": true") >= 0)
m_config.auto_trading = true;
else if (StringFind(json_string, "\"auto_trading\": false") >= 0)
m_config.auto_trading = false;
// Extract numeric values (simplified)
int pos = StringFind(json_string, "\"risk_per_trade\": ");
if (pos >= 0) {
string risk_str = StringSubstr(json_string, pos + 19, 10);
m_config.risk_per_trade = StringToDouble(risk_str);
}
pos = StringFind(json_string, "\"max_daily_loss\": ");
if (pos >= 0) {
string loss_str = StringSubstr(json_string, pos + 19, 10);
m_config.max_daily_loss = StringToDouble(loss_str);
}
pos = StringFind(json_string, "\"min_confidence\": ");
if (pos >= 0) {
string conf_str = StringSubstr(json_string, pos + 19, 10);
m_config.min_confidence = (int)StringToInteger(conf_str);
}
return true;
}
#endif // CONFIG_MQH