Repository navigation
fix(virtual): keep signal accounts marked, funded and liquidated - #441
Closed
mabaojinbj wants to merge 3 commits into
Closed
mabaojinbj wants to merge 3 commits into
mabaojinbj wants to merge 3 commits into
Conversation
mabaojinbj
force-pushed
the
fix/virtual-account-maintenance
branch
4 times, most recently
from
December 4, 2026 07:22
fbaf053 to
5f4a9e2
Compare
Virtual (signal-mode) positions were marked only when their stored strategy_run_id matched the current runtime run. Every strategy restart (the trading worker restores runtimes on boot) orphaned the still-open positions: mark price, unrealized PnL and equity snapshots froze until a new fill overwrote the row. On the Raspberry Pi deployment strategies 71 and 72 stayed frozen for 28 and 3 days while BTC fell from ~76k/59.6k to ~52k, so the virtual account showed +34k/+569k equity instead of being liquidated. - mark every open virtual position for the strategy, regardless of which run opened it. - add maintenance-margin liquidation for virtual perpetual accounts (configurable via maintenance_margin_rate, default 0.5%), force close at the current mark, and floor the account equity at zero so paper losses can never exceed the initial capital. - accrue settled funding into qd_strategy_funding_fees using public Binance/Gate.io funding history and deduct it from the virtual account's realised PnL; runs from the strategy loop and from a worker sweep so a stopped runtime cannot leave a position unmarked. - wire the sweep into the pending-order worker (virtual ledgers only; the live execution path is untouched). Tests cover the run-scope regression, maintenance-margin bounds, settlement-price selection and the Binance->Gate funding fallback. Co-authored-by: multica-agent <[email protected]>
mabaojinbj
force-pushed
the
fix/virtual-account-maintenance
branch
from
December 4, 2026 07:23
5f4a9e2 to
ced7a24
Compare
- Derive the funding start from the current position cycle by replaying the trade ledger, so a partial reduction no longer resets it and the accrual never falls back to the strategy creation time. - Cap every funding backfill window (default 7 days) so a first sweep or a long gap cannot back-charge months of settlements at today's size. - Gate settlements on 8h clock boundaries instead of holding duration. - Dedupe funding rows per (symbol, minute, side) and only among virtual rows so hedged long/short positions both accrue. - Cancel pending_orders and strategy_order_intents when liquidation cancels virtual orders, matching the normal cancellation path. - Book forgiven loss beyond initial capital as an explicit bad_debt adjustment so trade PnL keeps summing to the account's realized PnL. - Add regression tests for cycle replay, boundary gating, side split, three-table liquidation cancel and bad-debt reconciliation.
|
❌ The last analysis has failed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #440.
What changed
Mark-to-market stopped after every runtime restart.
mark_virtual_positions()selected positions with(strategy_run_id = 0 OR strategy_run_id = current_run). Positions are only re-stamped with a run id by a new fill, so positions opened by an earlier run became invisible to marking once the runtime restarted.mark_virtual_positionsnow marks every open position of the strategy; the run id stays on the row for audit but no longer gates marking.No liquidation. Virtual perpetual accounts had no maintenance-margin check, so paper equity could go arbitrarily negative.
liquidate_virtual_positions()now:maintenance_margin_rate/liquidation_margin_ratefromtrading_config(default 0.5%, capped at half the initial margin),qd_strategy_virtual_orders/qd_strategy_virtual_tradesrows withclose_reason='liquidation'andfill_source='virtual_liquidation',No funding.
accrue_virtual_funding()pulls settled funding rates from public Binance futures history, falling back to Gate.io. It writes oneqd_strategy_funding_feesrow per settlement (external_id = virtual:<venue>:<symbol>:<funding_ms>:<side>, so retries are idempotent) and deducts the amount from the virtual account'srealized_pnl, which is what every signal-mode equity view reads.Sweep independent of the strategy runtime.
sync_virtual_accounts()marks, funds and liquidates every signal strategy with open virtual positions.PendingOrderWorker._maybe_sync_positions()calls it next to the existing live funding sync, so a stopped/restarting strategy runtime no longer leaves a virtual account frozen.The live execution path is untouched: the sweep only reads
qd_strategy_virtual_positionsand writesqd_strategy_virtual_*/qd_strategy_funding_fees.Tests
backend_api_python/tests/test_virtual_account_maintenance.py:strategy_run_id;Shadow replay of the production ledger (hourly Gate.io BTC_USDT marks) is documented in #440: strategy 71 liquidated at 75,741 with 21,633.60 remaining and zero negative observations; a direct mark to the current 52,404 crash price floors equity at 0.0; strategy 72 accrued 4 funding settlements (-6,950.43) before liquidating at 59,131.4.