Repository navigation
Expand file tree
/
Copy pathreport_replay.cpp
More file actions
418 lines (379 loc) · 18.2 KB
/
Copy pathreport_replay.cpp
File metadata and controls
418 lines (379 loc) · 18.2 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "report_replay.hpp"
#include "engine_internal.hpp"
#include "native_execution_consumer.hpp"
#include "report_value_invalid.hpp"
#include "report_sink_writer.hpp"
#include "selected_report_view.hpp"
#include <pineforge/version.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iterator>
#include <map>
#include <mutex>
#include <stdexcept>
#include <sys/stat.h>
#include <utility>
namespace pineforge {
namespace report {
namespace {
constexpr std::uint64_t safe_integer = UINT64_C(9007199254740991);
[[noreturn]] void invalid(const char* text) {
throw coded<std::logic_error>(RunFailureCode::engine_invariant, {}, text);
}
std::string integer(std::uint64_t value) {
if (value > safe_integer) invalid("replay index exceeds the exact JSON integer bound");
return std::to_string(value);
}
std::string real(double value, const char* field) {
return report_sink::f64(finite_value(value, field));
}
const char* basis_name(EquityBasis basis) {
switch (basis) {
case EquityBasis::kernel_open: return "kernel_open";
case EquityBasis::range_end: return "range_end";
case EquityBasis::terminal_quote: return "terminal_quote";
case EquityBasis::anchor: return "anchor";
}
invalid("unknown replay equity basis");
}
bool same_bits(double a, double b) { return std::memcmp(&a, &b, sizeof a) == 0; }
struct ReplayRegistry {
std::mutex mutex;
std::map<const void*, std::unique_ptr<ReportReplay>> owners;
};
ReplayRegistry& replay_registry() {
// ReportSinkState constructors establish this lifetime before their
// engine finishes construction, including engines with static storage.
// Empty OFF registries have no nodes and allocate no heap storage.
static ReplayRegistry registry;
return registry;
}
// Fixed descriptors belong to the companion only. Primary E/T are untouched.
const char* descriptors =
"[{\"type\":\"S\",\"fields\":[\"initial_capital\",\"pointvalue\",\"net_profit_sum\"],"
"\"types\":[\"f64\",\"f64\",\"f64\"],\"units\":[\"account_ccy\",\"quote_ccy\",\"account_ccy\"]},"
"{\"type\":\"Q\",\"fields\":[\"point_index\",\"time_ms\",\"settlement_count\",\"net_profit_sum\","
"\"equity_basis\",\"equity_term\"],\"types\":[\"i64\",\"i64\",\"i64?\",\"f64?\",\"str\",\"f64\"],"
"\"units\":[\"\",\"unix_ms\",\"\",\"account_ccy\",\"\",\"account_ccy\"]},"
"{\"type\":\"R\",\"fields\":[\"n\",\"settlement_index\",\"parent_id\",\"slice_index\","
"\"slice_count\",\"recut_fields\",\"child_index\",\"transaction_qty\",\"whole_qty\","
"\"whole_pnl\",\"whole_commission\",\"whole_max_runup\",\"whole_max_drawdown\","
"\"whole_exit_fee\",\"transaction_fee\"],"
"\"types\":[\"i64\",\"i64?\",\"str?\",\"i64?\",\"i64?\",\"str[]?\",\"i64?\","
"\"f64?\",\"f64?\",\"f64?\",\"f64?\",\"f64?\",\"f64?\",\"f64?\",\"f64?\"],"
"\"units\":[\"\",\"\",\"\",\"\",\"\",\"\",\"\",\"qty\",\"qty\",\"account_ccy\","
"\"account_ccy\",\"account_ccy\",\"account_ccy\",\"account_ccy\",\"account_ccy\"]}]";
}
void initialize_replay_registry() { (void)replay_registry(); }
ReportReplay* find_replay(const void* key) noexcept {
auto& registry = replay_registry();
const std::lock_guard<std::mutex> lock(registry.mutex);
const auto found = registry.owners.find(key);
return found == registry.owners.end() ? nullptr : found->second.get();
}
void erase_replay(const void* key) noexcept {
auto& registry = replay_registry();
const std::lock_guard<std::mutex> lock(registry.mutex);
registry.owners.erase(key);
}
void install_replay(const void* key, std::unique_ptr<ReportReplay> replay) {
auto& registry = replay_registry();
const std::lock_guard<std::mutex> lock(registry.mutex);
registry.owners.insert_or_assign(key, std::move(replay));
}
void copy_replay(const void* from, const void* to) {
auto& registry = replay_registry();
const std::lock_guard<std::mutex> lock(registry.mutex);
const auto found = registry.owners.find(from);
if (found != registry.owners.end())
registry.owners.emplace(to, found->second->configuration_copy());
}
void move_replay(const void* from, const void* to) noexcept {
if (from == to) return;
auto& registry = replay_registry();
const std::lock_guard<std::mutex> lock(registry.mutex);
auto node = registry.owners.extract(from);
registry.owners.erase(to);
if (!node.empty()) {
node.key() = to;
registry.owners.insert(std::move(node)); // node transfer allocates nothing
}
}
ReportReplay::ReportReplay(std::string path, std::string run_id)
: path_(std::move(path)), run_id_(std::move(run_id)) {}
std::unique_ptr<ReportReplay> ReportReplay::configuration_copy() const {
return std::make_unique<ReportReplay>(path_, run_id_);
}
void ReportReplay::begin(const std::string& scratch_dir, double capital, double pointvalue,
bool selected, std::int64_t start, std::int64_t end) {
writer_.reset();
trades_.clear();
points_.clear();
directory_ = scratch_dir;
trades_.set_directory(directory_);
points_.set_directory(directory_);
trade_base_ = 0;
settlements_ = q_count_ = r_count_ = 0;
capital_ = capital;
pointvalue_ = pointvalue;
selected_ = selected;
last_selected_ = complete_ = false;
start_ = start;
end_ = end;
if (selected_) points_.push_back({start_, 0, 0.0, 0.0, EquityBasis::anchor});
const pf_report_sink_v1 sink{sizeof(pf_report_sink_v1), 1,
path_.c_str(), run_id_.c_str(), directory_.c_str()};
writer_ = std::make_unique<report_sink::LineWriter>(sink);
writer_->begin_companion(
"{\"schema\":\"pf-report-replay\",\"version\":1,\"run_id\":"
+ report_sink::escaped_string(run_id_) + ",\"engine\":"
+ report_sink::escaped_string(PINEFORGE_VERSION_STRING) + ",\"records\":"
+ descriptors + "}");
}
void ReportReplay::close(std::size_t global) {
if (complete_ || global != trade_base_ + trades_.size() || settlements_ == safe_integer)
invalid("replay close cursor is outside the resident window");
ReplayTrade row;
row.settlement = settlements_;
trades_.push_back(row);
++settlements_;
}
ReplayTrade ReportReplay::trade(std::size_t global) const {
if (global < trade_base_) invalid("replay row has already been retired");
return trades_.at(global - trade_base_);
}
void ReportReplay::replace(std::size_t global, const ReplayTrade& value) {
if (global < trade_base_) invalid("replay replacement precedes the resident window");
trades_.set(global - trade_base_, value);
}
void ReportReplay::resize_trades(std::size_t global_end) {
if (global_end < trade_base_) invalid("replay resize precedes the resident window");
trades_.resize(global_end - trade_base_);
}
void ReportReplay::retire(std::size_t global_end) {
if (global_end < trade_base_ || global_end - trade_base_ > trades_.size()
|| global_end > r_count_)
invalid("replay retirement precedes successful R emission");
trades_.erase(trades_.begin(), trades_.begin() + (global_end - trade_base_));
if (trades_.empty()) trades_.clear();
trade_base_ = global_end;
}
void ReportReplay::point(const pf_equity_point_t& value, double net) {
last_selected_ = !selected_ || (value.time_ms >= start_ && value.time_ms < end_);
if (!last_selected_) return;
points_.push_back({value.time_ms, settlements_, net, value.open_profit,
EquityBasis::kernel_open});
}
void ReportReplay::amend_point(std::int64_t time, EquityBasis basis, double term, double net) {
if (complete_ || !last_selected_) return;
if (points_.empty()) invalid("replay presentation lost its held point");
auto row = points_.at(points_.size() - 1);
row.time = time;
row.basis = basis;
row.term = term;
row.net = net;
row.settlements = settlements_;
points_.set(points_.size() - 1, row);
}
void ReportReplay::write_q(const pf_equity_point_t& point) {
if (!writer_ || complete_ || points_.empty())
invalid("replay Q has no recorded operands");
const auto row = points_.at(0);
if (row.time < -static_cast<std::int64_t>(safe_integer)
|| row.time > static_cast<std::int64_t>(safe_integer))
invalid("replay time exceeds the exact JSON integer bound");
const double expected = row.basis == EquityBasis::anchor ? capital_
: row.basis == EquityBasis::terminal_quote ? capital_ + row.term
: (capital_ + row.net) + row.term;
if (row.time != point.time_ms || !same_bits(expected, point.equity))
throw coded<std::logic_error>(RunFailureCode::report_shape_unsupported,
{{"shape", "replay_presenter"}},
"exact replay unavailable: presenter did not retain its equity operands");
std::string line = "[\"Q\"," + integer(q_count_) + "," + std::to_string(row.time) + ",";
if (row.basis == EquityBasis::anchor) line += "null,null,";
else line += integer(row.settlements) + "," + real(row.net, "equity_curve.net_profit_sum") + ",";
line += report_sink::escaped_string(basis_name(row.basis)) + ","
+ real(row.term, "equity_curve.equity_term") + "]";
writer_->companion_record(line);
(void)integer(q_count_ + 1);
++q_count_;
points_.erase(points_.begin(), points_.begin() + 1);
if (points_.empty()) points_.clear();
}
void ReportReplay::write_r(std::size_t global, bool terminal) {
if (!writer_ || complete_ || global != r_count_)
invalid("replay R does not match its global T ordinal");
const ReplayTrade row = terminal ? ReplayTrade{} : trade(global);
std::string line = "[\"R\"," + integer(r_count_ + 1) + ","
+ (terminal ? "null" : integer(row.settlement));
if (!row.marked) {
for (int i = 0; i < 13; ++i) line += ",null";
} else {
line += "," + report_sink::escaped_string(std::to_string(row.parent))
+ "," + integer(row.slice) + "," + integer(row.slices) + ","
+ (row.fifo ? "[\"qty\",\"commission\",\"pnl\",\"pnl_pct\",\"max_runup\",\"max_drawdown\"]" : "[]")
+ "," + integer(row.child);
const double values[] = {row.transaction_qty, row.whole_qty, row.whole_pnl,
row.whole_commission, row.whole_runup, row.whole_drawdown};
const char* fields[] = {"trades.recut.transaction_qty", "trades.recut.whole_qty",
"trades.recut.whole_pnl", "trades.recut.whole_commission",
"trades.recut.whole_max_runup", "trades.recut.whole_max_drawdown"};
for (std::size_t i = 0; i < 6; ++i) line += "," + real(values[i], fields[i]);
line += row.fees ? "," + real(row.whole_exit_fee, "trades.recut.whole_exit_fee")
+ "," + real(row.transaction_fee, "trades.recut.transaction_fee") : ",null,null";
}
line += "]";
writer_->companion_record(line);
++r_count_;
}
void ReportReplay::finish(double net, std::string_view base_sha256) {
if (complete_) return;
if (!writer_ || !points_.empty()) invalid("replay companion has unconsumed equity points");
writer_->companion_record("[\"S\"," + real(capital_, "replay.initial_capital") + ","
+ real(pointvalue_, "replay.pointvalue") + "," + real(net, "replay.net_profit_sum") + "]");
writer_->finish_companion("{\"S\":1,\"Q\":" + integer(q_count_)
+ ",\"R\":" + integer(r_count_) + "}", base_sha256);
complete_ = true;
trades_.clear();
points_.clear();
}
void ReportReplay::release() noexcept {
trades_.clear();
points_.clear();
if (writer_) {
if (complete_) writer_->release();
else writer_.reset();
}
}
} // namespace report
int BacktestEngine::set_report_replay_v1(const char* path, const char* run_id) {
const int status = report_sink_setter_status();
if (status != 0) return status;
if (trades_.base() != 0) return -3;
if (!path) {
report::erase_replay(&report_sink_state_);
pending_equity_report_mode_ = pending_equity_report_mode();
return 0;
}
// Bound new configuration allocations before copying either C string.
constexpr std::size_t max_text = 64 * 1024;
if (!*path || ::strnlen(path, max_text + 1) > max_text
|| (run_id && ::strnlen(run_id, max_text + 1) > max_text)
|| (report_sink_state_.enabled && path == report_sink_state_.path)) return -1;
auto replay = std::make_unique<report::ReportReplay>(path, run_id ? run_id : "");
report::install_replay(&report_sink_state_, std::move(replay));
pending_equity_report_mode_ = static_cast<ReportMode>(
static_cast<std::uint32_t>(pending_equity_report_mode_) | report_replay_flag);
return 0;
}
double BacktestEngine::report_pointvalue() const noexcept {
const auto* replay = report_replay_state();
return replay && replay->begun() ? replay->pointvalue() : syminfo_.pointvalue;
}
void BacktestEngine::begin_report_replay() {
auto& sink = report_sink_state_;
auto* replay = report_replay_state();
if (!replay) return;
struct stat primary{}, companion{};
if (sink.enabled && sink.fd < 0
&& (sink.path == replay->path()
|| (::stat(sink.path.c_str(), &primary) == 0
&& ::stat(replay->path().c_str(), &companion) == 0
&& primary.st_dev == companion.st_dev && primary.st_ino == companion.st_ino)))
throw coded<std::logic_error>(RunFailureCode::engine_invariant, {},
"primary and replay sinks must be separate files");
if (pending_equity_report_mode() == ReportMode::SUMMARY_ONLY)
throw coded<std::logic_error>(RunFailureCode::report_shape_unsupported,
{{"shape", "replay_summary"}}, "exact replay requires recorded equity points");
const auto* consumer = execution_consumer_slot_.ptr
? &as_native_consumer(*execution_consumer_slot_.ptr) : nullptr;
const auto* spec = consumer ? consumer->view().spec : nullptr;
if (spec && spec->report_policy == NativeReportPolicy::HostRecorded)
throw coded<std::logic_error>(RunFailureCode::report_shape_unsupported,
{{"shape", "replay_host_curve"}}, "exact replay requires recorded equity operands");
pf_selected_window_config_v1 window{};
const bool selected = NativeExecutionConsumer::selected_window_admitted(*this, &window);
replay->begin(sink.enabled ? sink.scratch_dir : trades_.scratch_directory(),
initial_capital_, syminfo_.pointvalue, selected, window.start_ms, window.end_ms);
}
void BacktestEngine::record_report_replay_close(std::size_t global) {
if (auto* replay = report_replay_state()) replay->close(global);
}
void BacktestEngine::record_report_replay_point(const pf_equity_point_t& point) {
if (auto* replay = report_replay_state()) replay->point(point, net_profit_sum_);
}
void BacktestEngine::amend_report_replay_point(
std::int64_t time, report::EquityBasis basis, double term) const {
if (auto* replay = report_replay_state())
replay->amend_point(time, basis, term, net_profit_sum_);
}
void BacktestEngine::write_report_replay_equity(const pf_equity_point_t& point) const {
if (auto* replay = report_replay_state()) replay->write_q(point);
}
void BacktestEngine::write_report_replay_trade(std::size_t global, bool terminal) const {
if (auto* replay = report_replay_state()) replay->write_r(global, terminal);
}
void BacktestEngine::retire_report_replay_trades(std::size_t global_end) {
if (auto* replay = report_replay_state()) replay->retire(global_end);
}
void BacktestEngine::finish_report_replay(const ReportC* out) const {
auto& sink = report_sink_state_;
auto* owner = report_replay_state();
if (!owner) return;
auto& replay = *owner;
const auto* consumer = execution_consumer_slot_.ptr
? &as_native_consumer(*execution_consumer_slot_.ptr) : nullptr;
if (replay.complete() || last_run_status_ != 0 || !last_error_.empty()
|| !consumer || consumer->state_kind() != NativeLifecycleKind::Completed) return;
if (sink.enabled) {
if (!sink.receipt.complete) return;
if (replay.r_count() != static_cast<std::uint64_t>(sink.receipt.count_t)
|| replay.q_count() != static_cast<std::uint64_t>(sink.receipt.count_e))
throw coded<std::logic_error>(RunFailureCode::engine_invariant, {},
"replay coverage differs from the primary receipt");
} else {
if (out->trades_len != out->total_trades)
throw coded<std::logic_error>(RunFailureCode::report_shape_unsupported,
{{"shape", "replay_presenter"}}, "exact replay requires every presented row");
for (std::int64_t i = 0; i < out->equity_curve_len; ++i)
replay.write_q(out->equity_curve[i]);
for (int i = 0; i < out->trades_len; ++i)
replay.write_r(static_cast<std::size_t>(i), out->trades[i].open_at_end != 0);
}
replay.finish(net_profit_sum_,
sink.enabled ? std::string_view(sink.receipt.sha256_hex) : std::string_view{});
}
report::ReportReplay* BacktestEngine::find_report_replay() const noexcept {
return report::find_replay(&report_sink_state_);
}
BacktestEngine::ReportSinkState::ReportSinkState(ReportSinkState&& other) noexcept
: ReportSinkState() { *this = std::move(other); }
BacktestEngine::ReportSinkState&
BacktestEngine::ReportSinkState::operator=(ReportSinkState&& other) noexcept {
if (this == &other) return *this;
report::move_replay(&other, this);
enabled = other.enabled;
trade_history_streamable = other.trade_history_streamable;
path = std::move(other.path);
run_id = std::move(other.run_id);
scratch_dir = std::move(other.scratch_dir);
fd = other.fd;
fd_device = other.fd_device;
fd_inode = other.fd_inode;
fd_identity_valid = other.fd_identity_valid;
active_calls = other.active_calls;
stream_calls = other.stream_calls;
writer = std::move(other.writer);
equity_scratch = std::move(other.equity_scratch);
equity_tail_stored = other.equity_tail_stored;
receipt = other.receipt;
summary = other.summary;
std::copy(std::begin(other.equity_prefix), std::end(other.equity_prefix), equity_prefix);
output_base = other.output_base;
selected_equity_stream = std::move(other.selected_equity_stream);
std::copy(std::begin(other.selected_released_extremes), std::end(other.selected_released_extremes),
selected_released_extremes);
return *this;
}
} // namespace pineforge