fix: MaxDD episode reset after done bar, not before

The done bar's return (liquidation loss) must be counted in the current
episode's DD before resetting. Previous code reset BEFORE compounding,
which applied the liquidation return to fresh initial_capital.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
jgrusewski
2026-03-27 13:01:27 +01:00
parent 65cd2614dd
commit f94febd95a

View File

@@ -125,17 +125,19 @@ pub(crate) fn compute_epoch_financials(
let mut max_dd = 0.0_f64;
for (idx, &ret) in returns[dd_start..].iter().enumerate() {
let global_idx = dd_start + idx;
// Reset equity at episode boundaries (capital floor / episode end)
equity *= 1.0 + ret;
if equity > peak {
peak = equity;
}
// Reset equity AFTER processing the done bar's return.
// The done bar's loss is part of this episode's DD — the reset
// applies to the NEXT bar (start of new episode with fresh capital).
if global_idx < trade_stats.done_flags.len()
&& trade_stats.done_flags[global_idx] > 0.5
{
equity = initial_capital;
peak = initial_capital;
}
equity *= 1.0 + ret;
if equity > peak {
peak = equity;
}
let dd = if peak > 1e-10 {
(peak - equity) / peak
} else {