Skip to content

Virtual (signal) accounts stop marking to market after runtime restart; no liquidation, no funding #440

Done

Description

@mabaojinbj

Summary

Virtual (signal-mode) accounts on our deployment silently stop marking to market after the strategy runtime restarts, never liquidate, and never pay funding. On v5.6.3 (and still on main) an open virtual perpetual position can drift for weeks while showing a frozen unrealized_pnl/equity snapshot.

Production evidence (v5.6.3, 2026-12-04)

Two 100x signal strategies (BTC/USDT swap) were opened and then left open:

strategy opened entry stored mark stored equity real BTC price real unrealized
71 2026-11-06 00:01 76,088.13 75,900.80 (frozen 11-07 00:10) 34,305.63 ~52,400 about -3,111%
72 2026-12-01 00:00 59,616.49 60,458.10 (frozen 12-01 05:01) 568,598.87 ~52,400 about -1,210%
  • qd_strategy_virtual_positions.updated_at stops at the exact timestamp the previous strategy_runs row is stopped; every later run updates only its health state.
  • qd_strategy_equity_snapshots keeps being written every ~5 minutes with the same frozen equity.
  • qd_strategy_funding_fees has zero rows for either strategy although they have been holding >23,000,000 USDT notional for weeks.
  • There is no maintenance-margin check anywhere in the virtual ledger, so the account can go arbitrarily negative on paper and is never force-closed.

Root cause

mark_virtual_positions() and match_virtual_limit_orders() filter by strategy_run_id:

SELECT ... FROM qd_strategy_virtual_positions
WHERE strategy_id = %s
  AND (%s = 0 OR strategy_run_id = %s)

trading_executor always passes its current run id. Positions opened under an earlier run keep the old strategy_run_id (it is only rewritten by a new fill), so after any runtime restart they are invisible to mark-to-market. The runtimes here restart daily/on worker restart, which is exactly when both strategies froze.

Proposed fix (PR follows)

  1. Mark every open virtual position of the strategy, independent of run id.
  2. Add a maintenance-margin check for virtual perpetual accounts (maintenance_margin_rate, default 0.5%, capped below the initial margin): force close at the current mark, write a close_* trade with close_reason='liquidation', and floor the account equity at zero.
  3. Accrue settled funding from public Binance/Gate.io funding history into qd_strategy_funding_fees and deduct it from the virtual account's realised PnL.
  4. Run the mark/funding/liquidation sweep from PendingOrderWorker as well, so a stopped or restarting strategy runtime cannot leave a position unmarked. The sweep only touches qd_strategy_virtual_* / qd_strategy_funding_fees; the live execution path is unchanged.

Verification

Shadow replay against a copy of the production ledger, hourly BTC_USDT marks from Gate.io:

  • 71 replayed from 2026-11-06: equity moved with price every hour (46,812.88 at 76,095.9 → liquidated on 2026-11-06 04:00 at 75,741, remaining equity 21,633.60, zero negative observations).
  • 71 direct mark to the current crash price (52,404): liquidated, final equity 0.0.
  • 72 replayed from 2026-12-01: equity followed price to 863,064 → liquidated 2026-12-02 21:00 at 59,131.4, final equity 13,811.47, plus 4 funding settlements (-6,950.43) deducted before liquidation.
  • 71 funding-only over the full 28-day window: 84 settlements, -14,617.71 total (avg -174.02, range -486.56..+221.10), deducted from equity.

Live strategies 70/86/92 (execution_mode=live) do not enter any of the new code paths.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions