Skip to content

fix: drawdown metrics do not match portfolio snapshot data#408

Merged
MDUYN merged 2 commits intodevfrom
squad/407-fix-drawdown-metrics
Apr 4, 2026
Merged

fix: drawdown metrics do not match portfolio snapshot data#408
MDUYN merged 2 commits intodevfrom
squad/407-fix-drawdown-metrics

Conversation

@MDUYN
Copy link
Copy Markdown
Collaborator

@MDUYN MDUYN commented Mar 29, 2026

Summary

Fixes #407 — drawdown metrics were inconsistent with the total_value series in portfolio_snapshots.

Changes

Bug 1: get_max_daily_drawdown computed peak-to-trough instead of worst single-day decline

  • The function was resampling to daily frequency then computing peak-to-trough drawdown — identical logic to get_max_drawdown
  • Fix: Rewritten to use pct_change() on the daily series and return the worst negative single-day return
  • Only negative returns are considered (all-positive returns correctly yield 0)

Bug 2: get_max_drawdown_duration counted snapshots instead of calendar days

  • The function incremented a counter per snapshot, but snapshots may not be daily
  • Fix: Now tracks drawdown entry/exit timestamps and computes elapsed calendar days

Bug 3: get_equity_curve didn't sort by timestamp

  • Snapshots were iterated in whatever order they were passed, causing incorrect peak tracking
  • Fix: Output is now sorted by created_at

Tests

  • 15 new tests across 4 test classes added to tests/services/metrics/test_drawdowns.py
  • All 18 tests pass (3 existing + 15 new)
Class Tests Coverage
TestGetMaxDailyDrawdown 6 Worst decline, all-positive, flat, single snapshot, intraday resampling
TestGetMaxDrawdownDuration 6 Daily/weekly snapshots, no drawdown, multiple periods, extends to end
TestDrawdownConsistency 3 Unsorted snapshots, manual computation match, timestamp alignment

MDUYN added 2 commits March 29, 2026 23:16
- Fix get_max_daily_drawdown to compute worst single-day decline instead
  of peak-to-trough drawdown (was identical to max_drawdown)
- Fix get_max_drawdown_duration to count calendar days using timestamps
  instead of counting snapshot entries
- Sort equity curve by created_at in get_equity_curve to ensure
  chronological order for all downstream metrics
- Add 15 new tests for daily drawdown, duration, and consistency
@MDUYN MDUYN merged commit 1554ab6 into dev Apr 4, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant