Skip to content

fix: backtest equity curve bugs#41

Merged
WrBug merged 1 commit intomainfrom
ai_fix/n_39
Mar 30, 2026
Merged

fix: backtest equity curve bugs#41
WrBug merged 1 commit intomainfrom
ai_fix/n_39

Conversation

@WrBug
Copy link
Copy Markdown
Owner

@WrBug WrBug commented Mar 28, 2026

Fixes #39

Changes

Backend (BacktestExecutionService.kt)

  1. SELL logic fix (core bug): Position quantity is now correctly reduced after selling. Previously position.quantity was never decremented, causing already-sold positions to be settled again at backtest end, inflating final balance and creating 224+ duplicate SETTLEMENT records.

  2. settleRemainingPositions fix: profitLoss now correctly computes settlementValue - cost instead of settlementValue.negate(). For closed settlements at avgPrice, this now correctly yields ~0 PnL.

  3. Max drawdown fix: Uses current trade's balance instead of the previous iteration's runningBalance, preventing maxDrawdown from exceeding initial balance.

Frontend (BacktestChart.tsx)

  • Added clarifying comment that the chart displays cash balance, not total equity. A true equity curve would require a new equityAfter field in trade records (schema change).

Testing

  • ✅ Backend compiles successfully
  • ✅ Frontend compiles successfully (tsc --noEmit)

- Fix SELL logic: correctly reduce position.quantity after selling (core bug)
  Previously positions were never decremented, causing sold positions to be
  settled again at backtest end, inflating final balance.
- Fix settleRemainingPositions: profitLoss now correctly computes
  settlementValue - cost (was incorrectly using settlementValue.negate())
- Fix max drawdown calculation: use current balance instead of previous
  iteration's runningBalance
- Frontend: add comment noting chart shows cash balance, not total equity
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.

[Bug] / Backtest 资金曲线异常:图表使用现金余额且 SELL 未减仓,导致资金先跌到 0 再在结算时突然拉升

1 participant