-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathResultAnalyzer.cpp
More file actions
179 lines (158 loc) · 5.34 KB
/
ResultAnalyzer.cpp
File metadata and controls
179 lines (158 loc) · 5.34 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
// Copyright (c) 2025 Chek Wei Tan
// Licensed under the MIT License. See LICENSE file in the project root for full license information.
#include "ResultAnalyzer.hpp"
#include "OnChartLogging.hpp"
#include "Enum.hpp"
#include "nlohmann/json.hpp"
#include <fstream>
#include <sstream>
#include <filesystem>
#include <algorithm>
#include <iomanip>
using json = nlohmann::json;
namespace fs = std::filesystem;
void ResultAnalyzer::AnalyzeResults(SCStudyInterfaceRef sc, const std::string &resultsDir, const std::string &reportFileName)
{
SCString msg;
std::vector<CombinationResult> results;
for (const auto &entry : fs::directory_iterator(resultsDir))
{
if (entry.is_regular_file() && entry.path().extension() == ".json")
{
results.push_back(ParseJsonResult(entry.path().string(), sc));
}
}
if (results.empty())
{
OnChartLogging::AddLog(sc, "No JSON files found to analyze.");
return;
}
std::sort(results.begin(), results.end(), [](const CombinationResult &a, const CombinationResult &b)
{ return a.totalProfitLoss > b.totalProfitLoss; });
std::ofstream csvFile(reportFileName);
if (!csvFile.is_open())
{
msg.Format("Failed to create summary report file at: %s", reportFileName.c_str());
OnChartLogging::AddLog(sc, msg);
return;
}
csvFile << CombinationResult::GetCsvHeader();
for (const auto &result : results)
{
csvFile << result.ToCsvRow();
}
msg.Format("Summary report generated at: %s", reportFileName.c_str());
OnChartLogging::AddLog(sc, msg);
}
CombinationResult ResultAnalyzer::ParseJsonResult(const std::string &filePath, SCStudyInterfaceRef sc)
{
CombinationResult result;
result.sourceFile = filePath;
try
{
json j = GetJsonFromFile(filePath, sc);
if (j.contains("customStudyInformation"))
{
const auto &header = j["customStudyInformation"];
if (header.contains("StudyOriginalName"))
result.strategyName = header["StudyOriginalName"];
if (header.contains("DLLFileName"))
result.dllName = header["DLLFileName"];
for (auto it = header.begin(); it != header.end(); ++it)
{
if (it.key() != "StudyOriginalName" && it.key() != "DLLFileName")
{
std::stringstream ss;
ss << it.value();
result.params.push_back({it.key(), ss.str()});
}
}
}
if (j.contains("tradeStatistics") && j["tradeStatistics"].contains("All Trades"))
{
const auto &stats = j["tradeStatistics"]["All Trades"];
if (stats.contains("ClosedTradesProfitLoss"))
result.totalProfitLoss = stats["ClosedTradesProfitLoss"];
if (stats.contains("ProfitFactor"))
result.profitFactor = stats["ProfitFactor"];
if (stats.contains("TotalTrades"))
result.totalTrades = stats["TotalTrades"];
if (stats.contains("PercentProfitable"))
result.winningTradesPercentage = stats["PercentProfitable"];
if (stats.contains("MaximumDrawdown"))
result.maxDrawdown = stats["MaximumDrawdown"];
}
}
catch (const std::exception &e)
{
SCString msg;
msg.Format("Error parsing JSON file %s: %s", filePath.c_str(), e.what());
OnChartLogging::AddLog(sc, msg);
}
catch (...)
{
SCString msg;
msg.Format("An unknown error occurred while parsing JSON file: %s", filePath.c_str());
OnChartLogging::AddLog(sc, msg);
}
return result;
}
json ResultAnalyzer::GetJsonFromFile(const std::string &filePath, SCStudyInterfaceRef sc)
{
std::ifstream ifs(filePath);
if (!ifs.is_open())
{
SCString msg;
msg.Format("Failed to open file: %s", filePath.c_str());
OnChartLogging::AddLog(sc, msg);
return {};
}
try
{
json j;
ifs >> j;
return j;
}
catch (const std::exception &e)
{
SCString msg;
msg.Format("Exception while reading JSON from %s: %s", filePath.c_str(), e.what());
OnChartLogging::AddLog(sc, msg);
return {};
}
catch (...)
{
SCString msg;
msg.Format("An unknown error occurred while reading JSON from file: %s", filePath.c_str());
OnChartLogging::AddLog(sc, msg);
return {};
}
}
std::string CombinationResult::GetCsvHeader()
{
return "Strategy,DLL Name,Parameters,Total P/L,Profit Factor,Total Trades,Win Rate (%),Max Drawdown,Source File\n";
}
std::string CombinationResult::ToCsvRow() const
{
std::stringstream ss;
ss << std::fixed << std::setprecision(2);
ss << "\"" << strategyName << "\","
<< "\"" << dllName << "\",";
std::stringstream params_ss;
for (size_t i = 0; i < params.size(); ++i)
{
params_ss << params[i].first << ": " << params[i].second;
if (i < params.size() - 1)
{
params_ss << " | ";
}
}
ss << "\"" << params_ss.str() << "\","
<< totalProfitLoss << ","
<< profitFactor << ","
<< totalTrades << ","
<< winningTradesPercentage * 100 << ","
<< maxDrawdown << ","
<< "\"" << sourceFile << "\"\n";
return ss.str();
}