-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConfigManager.cpp
More file actions
201 lines (174 loc) · 8.97 KB
/
ConfigManager.cpp
File metadata and controls
201 lines (174 loc) · 8.97 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
// Copyright (c) 2025 Chek Wei Tan
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
#include "ConfigManager.hpp"
#include <fstream>
#include <sstream>
#include <stdexcept>
#include "Enum.hpp"
#include "OnChartLogging.hpp"
#include "CombinationGenerator.hpp"
using json = nlohmann::json;
namespace
{
void ParseMainSettings(const json &root, StrategyOptimizerConfig &outConfig)
{
outConfig.OpenResultsFolder = root.value("openResultsFolder", true);
}
void ParseReplayConfig(const json &root, StrategyOptimizerConfig &outConfig, SCStudyInterfaceRef sc)
{
if (!root.contains("replayConfig"))
throw std::runtime_error("Missing required section: 'replayConfig'");
const auto &replayParams = root["replayConfig"];
if (!replayParams.contains("replaySpeed"))
throw std::runtime_error("Missing required field in 'replayConfig': 'replaySpeed'");
outConfig.ReplayConfig.ReplaySpeed = replayParams["replaySpeed"].get<float>();
if (!replayParams.contains("startDate"))
throw std::runtime_error("Missing required field in 'replayConfig': 'startDate'");
outConfig.ReplayConfig.StartDate = replayParams["startDate"].get<std::string>().c_str();
if (!replayParams.contains("startTime"))
throw std::runtime_error("Missing required field in 'replayConfig': 'startTime'");
outConfig.ReplayConfig.StartTime = replayParams["startTime"].get<std::string>().c_str();
if (!replayParams.contains("replayMode"))
throw std::runtime_error("Missing required field in 'replayConfig': 'replayMode'");
outConfig.ReplayConfig.ReplayMode = replayParams["replayMode"].get<int>();
if (!replayParams.contains("chartsToReplay"))
throw std::runtime_error("Missing required field in 'replayConfig': 'chartsToReplay'");
outConfig.ReplayConfig.ChartsToReplay = replayParams["chartsToReplay"].get<int>();
if (!replayParams.contains("clearExistingTradeSimulationDataForSymbolAndTradeAccount"))
throw std::runtime_error("Missing required field in 'replayConfig': 'clearExistingTradeSimulationDataForSymbolAndTradeAccount'");
outConfig.ReplayConfig.ClearExistingTradeSimulationDataForSymbolAndTradeAccount = replayParams["clearExistingTradeSimulationDataForSymbolAndTradeAccount"].get<int>();
if (!replayParams.contains("skipEmptyPeriods"))
throw std::runtime_error("Missing required field in 'replayConfig': 'skipEmptyPeriods'");
outConfig.ReplayConfig.SkipEmptyPeriods = replayParams["skipEmptyPeriods"].get<int>();
SCDateTimeMS DateValue = sc.DateStringToSCDateTime(outConfig.ReplayConfig.StartDate);
SCDateTimeMS TimeValue = sc.TimeStringToSCDateTime(outConfig.ReplayConfig.StartTime);
outConfig.ReplayConfig.StartDateTime = DateValue + TimeValue;
}
void ParseLogConfig(const json &root, StrategyOptimizerConfig &outConfig)
{
if (root.contains("logConfig"))
{
const auto &logParams = root["logConfig"];
outConfig.LogConfig.EnableLog = logParams.value("enableLog", true);
outConfig.LogConfig.EnableShowLogOnChart = logParams.value("enableShowLogOnChart", true);
outConfig.LogConfig.MaxLogLines = logParams.value("maxLogLines", 20);
}
else // Defaults if section is missing
{
outConfig.LogConfig.EnableLog = true;
outConfig.LogConfig.EnableShowLogOnChart = true;
outConfig.LogConfig.MaxLogLines = 20;
}
}
void ParseParamConfigs(const json &root, StrategyOptimizerConfig &outConfig)
{
if (!root.contains("paramConfigs"))
throw std::runtime_error("Missing required section: 'paramConfigs'");
const auto &inputs = root["paramConfigs"];
if (inputs.is_array())
{
for (const auto &input : inputs)
{
if (input.contains("increment") && input["increment"].get<double>() == 0)
{
continue;
}
InputType type = InputType::INT; // Default to INT
if (input.contains("type"))
{
std::string typeStr = input["type"].get<std::string>();
if (typeStr == "float")
{
type = InputType::FLOAT;
}
else if (typeStr == "bool")
{
type = InputType::BOOL;
}
else if (typeStr == "int")
{
type = InputType::INT;
}
else
{
continue; // Skip unsupported types
}
if (!input.contains("index") || !input.contains("min") || !input.contains("max") || !input.contains("increment"))
{
throw std::runtime_error("A 'paramConfigs' entry is missing a required field (index, min, max, or increment).");
}
}
outConfig.ParamConfigs.push_back({input["index"].get<int>(), input["min"].get<double>(), input["max"].get<double>(), input["increment"].get<double>(), type});
}
}
else
{
throw std::runtime_error("'paramConfigs' must be an array.");
}
}
}
namespace ConfigLoader
{
bool LoadConfig(SCStudyInterfaceRef sc, const std::string &filePath, StrategyOptimizerConfig &outConfig)
{
SCString logMessage;
logMessage.Format("INFO: Attempting to load configuration from: %s", filePath.c_str());
OnChartLogging::AddLog(sc, logMessage);
std::ifstream configFile(filePath);
if (!configFile.is_open())
{
logMessage.Format("ERROR: Could not open configuration file at: %s", filePath.c_str());
OnChartLogging::AddLog(sc, logMessage);
return false;
}
try
{
json root;
configFile >> root;
configFile.close();
OnChartLogging::AddLog(sc, "INFO: Config file parsed successfully. Loading settings...");
ParseMainSettings(root, outConfig);
unsigned int studyId = sc.Input[StudyInputs::TargetStudyRef].GetStudyID();
n_ACSIL::s_CustomStudyInformation customStudyInfo;
sc.GetCustomStudyInformation(sc.ChartNumber, studyId, customStudyInfo);
logMessage.Format("INFO: - Study: %s (%s)", customStudyInfo.DLLFileName.GetChars(), customStudyInfo.StudyOriginalName.GetChars());
OnChartLogging::AddLog(sc, logMessage);
logMessage.Format("INFO: - Open Results Folder: %s", outConfig.OpenResultsFolder ? "true" : "false");
OnChartLogging::AddLog(sc, logMessage);
ParseReplayConfig(root, outConfig, sc);
OnChartLogging::AddLog(sc, "INFO: Replay Config Loaded:");
logMessage.Format("INFO: - StartDateTime: %s", sc.DateTimeToString(outConfig.ReplayConfig.StartDateTime, FLAG_DT_COMPLETE_DATETIME_MS).GetChars());
OnChartLogging::AddLog(sc, logMessage);
logMessage.Format("INFO: - Replay Speed: %.1f", outConfig.ReplayConfig.ReplaySpeed);
OnChartLogging::AddLog(sc, logMessage);
ParseLogConfig(root, outConfig);
OnChartLogging::AddLog(sc, "INFO: Log Config Loaded:");
logMessage.Format("INFO: - Enable Log: %s", outConfig.LogConfig.EnableLog ? "true" : "false");
OnChartLogging::AddLog(sc, logMessage);
logMessage.Format("INFO: - Show Log on Chart: %s", outConfig.LogConfig.EnableShowLogOnChart ? "true" : "false");
OnChartLogging::AddLog(sc, logMessage);
logMessage.Format("INFO: - Max Log Lines: %d", outConfig.LogConfig.MaxLogLines);
OnChartLogging::AddLog(sc, logMessage);
ParseParamConfigs(root, outConfig);
logMessage.Format("INFO: Loaded %d parameter configurations for optimization.", (int)outConfig.ParamConfigs.size());
OnChartLogging::AddLog(sc, logMessage);
OnChartLogging::AddLog(sc, "INFO: Configuration loading complete.");
return true;
}
catch (const json::exception &e)
{
SCString error_msg;
error_msg.Format("ERROR: Failed to parse JSON from '%s'. Details: %s", filePath.c_str(), e.what());
OnChartLogging::AddLog(sc, error_msg);
OnChartLogging::AddLog(sc, "ERROR: Please check the JSON structure, syntax, and ensure all required fields are present.");
return false;
}
catch (const std::exception &e)
{
SCString error_msg;
error_msg.Format("ERROR: An unexpected error occurred while loading config from '%s': %s", filePath.c_str(), e.what());
OnChartLogging::AddLog(sc, error_msg);
return false;
}
}
}