Skip to content

positionSelection

---
config:
  theme: dark
---
sequenceDiagram
    box Liquidation Engine (LE)
    participant Main as LE_Main
    participant PM as LE_PositionManager
    participant DHC as LE_DeltaHedgeChecker
    end
    participant RE as Risk Engine

    Note over Main,DHC: Position Selection Logic

    Main->>PM: Initialize position selection with positions and market data
    PM->>DHC: Check delta exposure
    DHC->>DHC: Calculate portfolio deltas
    DHC->>RE: Check IMR impact of potential delta hedge
    RE-->>DHC: Return projected IMR after hedge

    alt Delta Hedge Case
        Note over DHC: Conditions for delta hedge:<br/>1. Delta exceeds min/max threshold<br/>2. Perp-only account<br/>3. Delta hedge restores IMR health
        DHC-->>PM: Delta hedge/liquidation required
        PM->>Main: Return delta hedge/liquidation decision
    else Option Trade Case
        DHC-->>PM: Delta within acceptable range

        PM->>PM: Identify vertical spreads and synthetics
        Note over PM: If nested vertical spreads exist,<br/>group by closest strikes
        PM->>PM: Identify peripheral options

        PM->>RE: Get isolated margin requirements
        RE-->>PM: Return per-position margins

        PM->>PM: Rank positions by margin requirement
        Note over PM: Synthetics are ranked last in execution flow<br/>regardless of margin requirement

        PM->>Main: Return highest ranked position for liquidation
        Note over Main: If position suggested for liquidation is a spread or a synthetic,<br/>then the liquidation engine will treat it as two separate processes,<br/>giving priority to close the short leg first
    end