Skip to content

fix(trading): preserve lifetime equity and clear closing precision tails - #399

Merged
brokermr810 merged 78 commits into
mainfrom
test
Nov 21, 2026
Merged

brokermr810 merged 78 commits into
mainfrom
test

Conversation

@brokermr810

Copy link
Copy Markdown
Collaborator

Root cause

  • Runtime equity was scoped to the latest strategy run, which could reset the opening budget after a restart despite accumulated losses.
  • Split-close floating-point subtraction left tiny residual quantities while position updates retained stale unrealized P&L. Equity metrics and curves summed that stale cache.

Changes

  • Use lifetime strategy-ledger equity across restarts. Fail evaluation on ledger-query failure instead of using initial capital; report effective equity in sizing diagnostics.
  • Use decimal quantity arithmetic and absorb only binary rounding noise during closing fills. Preserve genuine small positions.
  • Write quantity, price and floating P&L together; reject delayed marks when the position quantity or entry cost changed.
  • Calculate live floating P&L and equity curves from current quantity, entry price and mark so historical stale profit cannot inflate equity.
  • Document the distinction between strategy-owned accounting and venue cost basis when manual trades share an account.

Verification

  • 406 targeted backend regression tests passed (fill accounting, split closes, historical tails, small holdings, delayed marks, restart equity, budgets, grid paths and venue reconciliation).
  • Ruff, backend structural checks, documentation validation and git diff checks passed.
  • All new commits have DCO sign-offs.

Deployment and remaining boundaries

  • No schema migration, historical trade rewrite or bulk deletion of residual inventory.
  • Deploy updated backend/runtime images normally. No production database or exchange positions were modified during this work.
  • Existing residual rows are valued using actual quantities. Genuine small positions and venue minimum-order policies remain unchanged.
  • Accuracy still depends on recorded fills, fees and marks. Venue order P&L does not replace the strategy ledger.

Companion PC frontend changes: OpenByteInc/QuantDinger-Vue@7c2f158
Localized strategy/venue P&L explanations; relevant tests, i18n audit and production build passed. Lint completed with no errors and existing warnings.

tianhe added 30 commits November 1, 2026 17:17
Signed-off-by: TIANHE <[email protected]>

# Conflicts:
#	backend_api_python/app/routes/community.py
Signed-off-by: TIANHE <[email protected]>

# Conflicts:
#	backend_api_python/tests/test_safe_exec.py
#	backend_api_python/tests/test_strategy_runtime_process_isolation.py
Signed-off-by: TIANHE <[email protected]>

# Conflicts:
#	backend_api_python/app/professional_report/features/equity.py
#	backend_api_python/app/research/workflows.py
#	backend_api_python/tests/test_professional_report_builder.py
…emails

Persist definitive order failure streaks and portfolio batch outcomes, send deduplicated alerts across opted-in channels, and render transactional emails using localized monochrome templates and configured branding.

Signed-off-by: TIANHE <[email protected]>
Signed-off-by: TIANHE <[email protected]>

# Conflicts:
#	backend_api_python/app/services/strategy_v2/live_execution.py
#	backend_api_python/app/services/trading_executor.py
#	backend_api_python/tests/test_strategy_v2_order_gateway.py
#	docs/trading/LIVE_TRADING_SAFETY.md
#	docs/trading/LIVE_TRADING_SAFETY_CN.md
Use the canonical strategy_name column for automatic-stop and rebalance notifications, honor explicit stop-and-close requests, and exercise the production schema and notification delivery paths in regression tests.

Signed-off-by: TIANHE <[email protected]>
Keep order sizing aligned with the deployment ledger across restarts. Revalue positions atomically with fills and derive live floating PnL from current quantities so stale residual rows cannot inflate equity. Preserve real small holdings and add cross-path regression coverage.

Signed-off-by: TIANHE <[email protected]>
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...nd_api_python/app/services/live_trading/records.py 90.00% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@brokermr810
brokermr810 merged commit 3bedecf into main Nov 21, 2026
15 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.

3 participants