-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStrategyOptimizer.cpp
More file actions
289 lines (244 loc) · 13.1 KB
/
StrategyOptimizer.cpp
File metadata and controls
289 lines (244 loc) · 13.1 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright (c) 2025 Chek Wei Tan
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
#include "../sierrachart.h"
#include "StrategyOptimizer.hpp"
#include "ConfigManager.hpp"
#include "Logging.hpp"
#include "ReplayManager.hpp"
#include "OnChartLogging.hpp"
#include "Enum.hpp"
#include "ResultAnalyzer.hpp"
#include <string>
#include <vector>
#include <utility>
#include <sstream>
#include <filesystem>
#include <algorithm>
#include <windows.h>
void InitializePersistentPointers(SCStudyInterfaceRef sc);
void HandleSetDefaults(SCStudyInterfaceRef sc);
void HandleFullRecalculation(SCStudyInterfaceRef sc);
bool HandleReplayLogic(SCStudyInterfaceRef sc);
void HandleMenuEvents(SCStudyInterfaceRef sc);
void HandleReplayCompletion(SCStudyInterfaceRef sc);
SCDLLName("scsf_StrategyOptimizer");
SCSFExport scsf_StrategyOptimizer(SCStudyInterfaceRef sc)
{
InitializePersistentPointers(sc);
ReplayState &replayState = reinterpret_cast<ReplayState &>(sc.GetPersistentIntFast(PersistentVars::ReplayStateEnum));
int &comboIndex = sc.GetPersistentIntFast(PersistentVars::ComboIndex);
auto *config = reinterpret_cast<StrategyOptimizerConfig *>(sc.GetPersistentPointer(PersistentVars::BacktestConfigPtr));
auto *combinations = reinterpret_cast<std::vector<std::vector<double>> *>(sc.GetPersistentPointer(PersistentVars::CombinationsPtr));
auto *logging = reinterpret_cast<Logging *>(sc.GetPersistentPointer(PersistentVars::LoggingPtr));
if (sc.LastCallToFunction)
{
StrategyOptimizerHelpers::HandleResetEvent(sc, replayState, comboIndex, config, combinations, logging);
return;
}
if (sc.SetDefaults)
{
HandleSetDefaults(sc);
return;
}
if (sc.IsFullRecalculation)
{
HandleFullRecalculation(sc);
}
if (sc.MenuEventID != 0)
{
HandleMenuEvents(sc);
return;
}
if (HandleReplayLogic(sc))
{
return;
}
}
void InitializePersistentPointers(SCStudyInterfaceRef sc)
{
if (sc.GetPersistentPointer(PersistentVars::BacktestConfigPtr) == nullptr)
{
sc.SetPersistentPointer(PersistentVars::BacktestConfigPtr, new StrategyOptimizerConfig());
}
if (sc.GetPersistentPointer(PersistentVars::CombinationsPtr) == nullptr)
{
sc.SetPersistentPointer(PersistentVars::CombinationsPtr, new std::vector<std::vector<double>>());
}
if (sc.GetPersistentPointer(PersistentVars::LoggingPtr) == nullptr)
{
sc.SetPersistentPointer(PersistentVars::LoggingPtr, new Logging(sc));
}
}
void HandleSetDefaults(SCStudyInterfaceRef sc)
{
sc.GraphName = "Strategy Optimizer";
sc.StudyDescription = "Runs automated backtests of a trading study or strategy across all possible parameter combinations within user-defined ranges.";
sc.AutoLoop = 0;
sc.GraphRegion = 0;
sc.MaintainTradeStatisticsAndTradesData = true;
sc.Subgraph[Subgraphs::LogText].Name = "Log";
sc.Subgraph[Subgraphs::LogText].DrawStyle = DRAWSTYLE_HIDDEN;
sc.Subgraph[Subgraphs::LogText].PrimaryColor = RGB(255, 255, 255);
sc.Subgraph[Subgraphs::LogText].LineWidth = 10; // Used for font size
sc.Input[StudyInputs::GenerateConfigButtonNumber].Name = "Generate Config Button Number";
sc.Input[StudyInputs::GenerateConfigButtonNumber].SetInt(6);
sc.Input[StudyInputs::GenerateConfigButtonNumber].SetIntLimits(1, MAX_ACS_CONTROL_BAR_BUTTONS);
sc.Input[StudyInputs::VerifyConfigButtonNumber].Name = "Verify Config Button Number";
sc.Input[StudyInputs::VerifyConfigButtonNumber].SetInt(7);
sc.Input[StudyInputs::VerifyConfigButtonNumber].SetIntLimits(1, MAX_ACS_CONTROL_BAR_BUTTONS);
sc.Input[StudyInputs::StartButtonNumber].Name = "Start Button Number";
sc.Input[StudyInputs::StartButtonNumber].SetInt(8);
sc.Input[StudyInputs::StartButtonNumber].SetIntLimits(1, MAX_ACS_CONTROL_BAR_BUTTONS);
sc.Input[StudyInputs::ResetButtonNumber].Name = "Reset Button Number";
sc.Input[StudyInputs::ResetButtonNumber].SetInt(9);
sc.Input[StudyInputs::ResetButtonNumber].SetIntLimits(1, MAX_ACS_CONTROL_BAR_BUTTONS);
sc.Input[StudyInputs::ConfigFilePath].Name = "Config File Path";
sc.Input[StudyInputs::ConfigFilePath].SetString("C:\\SierraChart\\Data\\StrategyOptimizerConfig.json");
sc.Input[StudyInputs::TargetStudyRef].Name = "Target Study";
sc.Input[StudyInputs::TargetStudyRef].SetDescription("Only study id (first dropdown) required, subgraph index (second dropdown) values not used)");
sc.Input[StudyInputs::TargetStudyRef].SetStudySubgraphValues(0, 0);
OnChartLogging::AddLog(sc, "Strategy Optimizer defaults set.");
}
void HandleFullRecalculation(SCStudyInterfaceRef sc)
{
sc.SetCustomStudyControlBarButtonHoverText(sc.Input[StudyInputs::GenerateConfigButtonNumber].GetInt(), "Generate Strategy Optimizer Configuration");
sc.SetCustomStudyControlBarButtonText(sc.Input[StudyInputs::GenerateConfigButtonNumber].GetInt(), "Generate");
sc.SetCustomStudyControlBarButtonShortCaption(sc.Input[StudyInputs::GenerateConfigButtonNumber].GetInt(), "Generate Config");
sc.SetCustomStudyControlBarButtonColor(sc.Input[StudyInputs::GenerateConfigButtonNumber].GetInt(), RGB(0, 0, 255)); // Blue
sc.SetCustomStudyControlBarButtonHoverText(sc.Input[StudyInputs::StartButtonNumber].GetInt(), "Start Strategy Optimizer");
sc.SetCustomStudyControlBarButtonText(sc.Input[StudyInputs::StartButtonNumber].GetInt(), "Start");
sc.SetCustomStudyControlBarButtonShortCaption(sc.Input[StudyInputs::StartButtonNumber].GetInt(), "Start Strategy Optimizer");
sc.SetCustomStudyControlBarButtonColor(sc.Input[StudyInputs::StartButtonNumber].GetInt(), RGB(0, 255, 0)); // Green
sc.SetCustomStudyControlBarButtonHoverText(sc.Input[StudyInputs::ResetButtonNumber].GetInt(), "Reset / Stop Strategy Optimizer");
sc.SetCustomStudyControlBarButtonText(sc.Input[StudyInputs::ResetButtonNumber].GetInt(), "Reset / Stop");
sc.SetCustomStudyControlBarButtonShortCaption(sc.Input[StudyInputs::ResetButtonNumber].GetInt(), "Reset / Stop Strategy Optimizer");
sc.SetCustomStudyControlBarButtonColor(sc.Input[StudyInputs::ResetButtonNumber].GetInt(), RGB(255, 0, 0)); // Red
sc.SetCustomStudyControlBarButtonHoverText(sc.Input[StudyInputs::VerifyConfigButtonNumber].GetInt(), "Verify Strategy Optimizer Configuration");
sc.SetCustomStudyControlBarButtonText(sc.Input[StudyInputs::VerifyConfigButtonNumber].GetInt(), "Verify");
sc.SetCustomStudyControlBarButtonShortCaption(sc.Input[StudyInputs::VerifyConfigButtonNumber].GetInt(), "Verify Config");
sc.SetCustomStudyControlBarButtonColor(sc.Input[StudyInputs::VerifyConfigButtonNumber].GetInt(), RGB(255, 255, 0)); // Yellow
}
bool HandleReplayLogic(SCStudyInterfaceRef sc)
{
ReplayState &replayState = reinterpret_cast<ReplayState &>(sc.GetPersistentIntFast(PersistentVars::ReplayStateEnum));
if (replayState == ReplayState::WaitingForReplayToStart && sc.IsFullRecalculation == 0)
{
int replayStatus = sc.GetReplayStatusFromChart(sc.ChartNumber);
SCString msg;
msg.Format("Waiting for replay to start. Current status: %d (0=Stopped, 1=Running, 2=Paused)", replayStatus);
OnChartLogging::AddLog(sc, msg);
if (replayStatus == REPLAY_RUNNING)
{
OnChartLogging::AddLog(sc, "Replay has started successfully.");
replayState = ReplayState::ReplayInProgress;
OnChartLogging::AddLog(sc, "State changed: Replay in progress.");
}
else if (replayStatus == REPLAY_STOPPED || replayStatus == REPLAY_PAUSED)
{
OnChartLogging::AddLog(sc, "Attempting to resume replay...");
sc.ResumeChartReplay(sc.ChartNumber);
}
return true;
}
if (replayState == ReplayState::ReplayInProgress && sc.GetReplayHasFinishedStatus())
{
HandleReplayCompletion(sc);
return true;
}
return false;
}
void HandleReplayCompletion(SCStudyInterfaceRef sc)
{
int &comboIndex = sc.GetPersistentIntFast(PersistentVars::ComboIndex);
auto *combinations = reinterpret_cast<std::vector<std::vector<double>> *>(sc.GetPersistentPointer(PersistentVars::CombinationsPtr));
auto *config = reinterpret_cast<StrategyOptimizerConfig *>(sc.GetPersistentPointer(PersistentVars::BacktestConfigPtr));
auto *logging = reinterpret_cast<Logging *>(sc.GetPersistentPointer(PersistentVars::LoggingPtr));
ReplayState &replayState = reinterpret_cast<ReplayState &>(sc.GetPersistentIntFast(PersistentVars::ReplayStateEnum));
SCString msg;
msg.Format("--- Combination %d/%d finished ---", comboIndex + 1, (int)combinations->size());
OnChartLogging::AddLog(sc, msg);
const auto ¤tCombo = (*combinations)[comboIndex];
unsigned int studyID = sc.Input[StudyInputs::TargetStudyRef].GetStudyID();
std::vector<std::pair<std::string, double>> params;
std::stringstream paramStream;
for (size_t i = 0; i < currentCombo.size(); ++i)
{
SCString inputName;
sc.GetStudyInputName(sc.ChartNumber, studyID, config->ParamConfigs[i].Index, inputName);
params.push_back({inputName.GetChars(), currentCombo[i]});
paramStream << "-" << inputName.GetChars() << "-" << currentCombo[i];
}
SCDateTime &backtestStartDateTime = sc.GetPersistentSCDateTimeFast(PersistentVars::BacktestStartDateTime);
std::string startDateTimeString(sc.FormatDateTime(backtestStartDateTime).GetChars());
std::replace(startDateTimeString.begin(), startDateTimeString.end(), '/', '-');
std::replace(startDateTimeString.begin(), startDateTimeString.end(), ':', '-');
std::replace(startDateTimeString.begin(), startDateTimeString.end(), ' ', '_');
n_ACSIL::s_CustomStudyInformation customStudyInfo;
sc.GetCustomStudyInformation(sc.ChartNumber, studyID, customStudyInfo);
std::stringstream reportFileName;
reportFileName << customStudyInfo.DLLFileName
<< "-" << comboIndex;
std::string resultsDir = std::filesystem::path(sc.Input[StudyInputs::ConfigFilePath].GetString()).parent_path().string() + "/results/" + customStudyInfo.DLLFileName.GetChars() + "-" + startDateTimeString + "/";
std::filesystem::create_directories(resultsDir);
std::string reportPath = resultsDir + reportFileName.str() + ".json";
logging->LogMetrics(sc, customStudyInfo.DLLFileName.GetChars(), reportPath, params, studyID);
OnChartLogging::AddLog(sc, "Logged metrics for completed combination.");
replayState = ReplayState::Idle;
comboIndex++;
if (comboIndex < (int)combinations->size())
{
OnChartLogging::AddLog(sc, "Proceeding to next combination.");
sc.StopChartReplay(sc.ChartNumber);
ReplayManager::StartReplayForCombination(sc, *config, *combinations, comboIndex, replayState);
}
else
{
OnChartLogging::AddLog(sc, "--- All combinations finished. Backtesting complete. ---");
ResultAnalyzer::AnalyzeResults(sc, resultsDir, resultsDir + reportFileName.str() + "-summary.csv");
if (config->OpenResultsFolder)
{
ShellExecuteA(NULL, "open", resultsDir.c_str(), NULL, NULL, SW_SHOWNORMAL);
}
}
}
void HandleMenuEvents(SCStudyInterfaceRef sc)
{
SCInputRef Input_Start = sc.Input[StudyInputs::StartButtonNumber];
SCInputRef Input_Reset = sc.Input[StudyInputs::ResetButtonNumber];
SCInputRef Input_VerifyConfig = sc.Input[StudyInputs::VerifyConfigButtonNumber];
SCInputRef Input_GenerateConfig = sc.Input[StudyInputs::GenerateConfigButtonNumber];
SCInputRef Input_ConfigFilePath = sc.Input[StudyInputs::ConfigFilePath];
ReplayState &replayState = reinterpret_cast<ReplayState &>(sc.GetPersistentIntFast(PersistentVars::ReplayStateEnum));
int &comboIndex = sc.GetPersistentIntFast(PersistentVars::ComboIndex);
auto *config = reinterpret_cast<StrategyOptimizerConfig *>(sc.GetPersistentPointer(PersistentVars::BacktestConfigPtr));
auto *combinations = reinterpret_cast<std::vector<std::vector<double>> *>(sc.GetPersistentPointer(PersistentVars::CombinationsPtr));
auto *logging = reinterpret_cast<Logging *>(sc.GetPersistentPointer(PersistentVars::LoggingPtr));
if (sc.MenuEventID == Input_Start.GetInt())
{
if (sc.GlobalTradeSimulationIsOn)
{
StrategyOptimizerHelpers::HandleStartEvent(sc, Input_ConfigFilePath, config, combinations, replayState, comboIndex);
}
else
{
OnChartLogging::AddLog(sc, "Trade Simulation Mode is off.");
}
ReplayManager::ResetButton(sc, Input_Start);
}
else if (sc.MenuEventID == Input_Reset.GetInt())
{
sc.StopChartReplay(sc.ChartNumber);
StrategyOptimizerHelpers::HandleResetEvent(sc, replayState, comboIndex, config, combinations, logging);
ReplayManager::ResetButton(sc, Input_Reset);
}
else if (sc.MenuEventID == Input_VerifyConfig.GetInt())
{
StrategyOptimizerHelpers::HandleVerifyConfigEvent(sc, Input_ConfigFilePath, config, combinations);
ReplayManager::ResetButton(sc, Input_VerifyConfig);
}
else if (sc.MenuEventID == Input_GenerateConfig.GetInt())
{
StrategyOptimizerHelpers::HandleGenerateConfigEvent(sc);
ReplayManager::ResetButton(sc, Input_GenerateConfig);
}
}