Skip to content

fix(backtests): prevent instant deals from blocking aggregation - #30

Merged
de-don merged 1 commit into
mainfrom
fix/aggregation-instant-deal-slot
Jun 15, 2026
Merged

fix(backtests): prevent instant deals from blocking aggregation#30
de-don merged 1 commit into
mainfrom
fix/aggregation-instant-deal-slot

Conversation

@de-don

@de-don de-don commented Jun 15, 2026

Copy link
Copy Markdown
Owner

Проблема

В агрегации бектестов график P&L портфеля «не доходил до конца» — обрывался на месяцы раньше реального конца периода, а суммы занижались. Зависело от лимита одновременных позиций, хотя при числе бектестов меньше лимита блокировки быть не должно.

Причина

В aggregateBacktestsMetrics на каждой временной точке сначала закрываются сделки (end === timePoint), потом открываются (start === timePoint). Сделка с start === end (мгновенный стоп-лосс — buy и SL в одну минуту/свечу, duration: null) добавляется в activeDeals уже после фазы закрытия этой точки. Её end больше никогда не встретится → она навсегда висит как открытая, занимая слот одновременности и блокируя все последующие сделки.

Подтверждено на реальных данных (XRPUSDT + SOLUSDT): у SOL две такие сделки → при низком лимите график обрывался на ~2025-08, intrinsic concurrency раздувался с 1 до 3. P&L этих сделок при этом терялся.

Фикс

Мгновенные сделки (end <= timePoint) финализируются сразу: учитываются в P&L и win/loss, но не кладутся в activeDeals и не блокируются лимитом.

Проверки

  • Регресс-тест на мгновенные сделки.
  • Тесты 80/80, typecheck, lint, build — чисто.

🤖 Generated with Claude Code

Instant deals (start === end), produced by a stop-loss triggering in the
same candle it opened (duration: null), were added to activeDeals during
the open pass after the close pass for that time point had already run.
Their end time point was never revisited, so they stayed "active" forever,
permanently consuming a concurrency slot and blocking every later deal.

Effect: with a low concurrency limit the aggregated P&L chart truncated to
the date of the first instant deal (months early), totals were understated,
and intrinsic concurrency was inflated. Their net was also silently dropped.

Finalize instant deals immediately: count their P&L and win/loss, but never
add them to activeDeals and never subject them to the concurrency limit.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@de-don
de-don merged commit 5fd385f into main Jun 15, 2026
5 checks passed
@de-don

de-don commented Jun 15, 2026

Copy link
Copy Markdown
Owner Author

🎉 This PR is included in version 2.7.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant