Margin Calculations
SPAN-like portfolio margin calculations and stress testing
Diffusal uses a SPAN-like portfolio margin system that calculates margin requirements by stress testing positions across multiple market scenarios. This approach captures the worst-case loss across plausible market movements.
For Black-Scholes pricing used in these calculations, see Black-Scholes Model.
Stress Testing
4 Corner Scenarios
The margin engine tests each portfolio against four extreme market scenarios combining ±30% spot moves with +50%/−30% IV changes. These scenarios capture the most adverse combinations of price and volatility movements for option portfolios.
See Margin System: Stress Scenarios for the complete scenario table and rationale.
Stressed Price Calculation
For each scenario, option prices are recalculated using Black-Scholes with stressed parameters:
The stressed option price is then computed:
Scenario Loss
For each scenario, the portfolio loss is calculated by comparing current mark prices to stressed prices:
Where:
- n = number of positions
- P_mark = current Black-Scholes price
- P_stressed = price under stress scenario
- size = position size (positive for long, negative for short)
Maximum Stress Loss
The stress loss component of margin is the maximum loss across all four scenarios:
Margin Requirements
Initial Margin (IM)
Initial margin is the collateral required to open or maintain positions:
Components:
-
Stress Loss - Maximum loss across 4 corner scenarios
-
Adverse PnL Buffer - Additional buffer when positions are underwater:
- Notional Buffer - Percentage of total notional exposure:
Maintenance Margin (MM)
Maintenance margin is the minimum collateral to avoid liquidation:
A position becomes liquidatable when equity falls below maintenance margin.
Unrealized PnL
Unrealized profit/loss across all positions:
Where:
- P_mark = current Black-Scholes price
- P_entry = price at which position was opened
- size = signed position size
For long positions (size > 0): profit when mark > entry For short positions (size < 0): profit when mark < entry
Total Notional
Total notional exposure of the portfolio:
Where:
- size = position size
- S = current spot price of the underlying
Equity and Health
Equity
Health Check
A user is healthy when:
A user is liquidatable when:
Maximum Withdrawal
Constants Reference
All margin constants are defined in Constants.sol:
| Constant | Value | Description |
|---|---|---|
STRESS_SPOT_DOWN | 0.70 (70%) | Spot down scenario multiplier |
STRESS_SPOT_UP | 1.30 (130%) | Spot up scenario multiplier |
STRESS_IV_DOWN | 0.70 (70%) | IV down scenario multiplier |
STRESS_IV_UP | 1.50 (150%) | IV up scenario multiplier |
MAINTENANCE_MARGIN_RATE | 0.80 (80%) | MM as percentage of IM |
INITIAL_MARGIN_RATE | 0.15 (15%) | Notional buffer rate |
ADVERSE_PNL_MULTIPLIER | 1.05 (105%) | Buffer on unrealized losses |
Contract Interface
MarginEngine Functions
calculateInitialMargin(positions, quoterAddress)— Calculate IM for an array of positions (returns WAD)calculateMargin(positions, quoterAddress)— Get complete margin metrics: unrealizedPnL (WAD, can be negative), stressLoss (WAD), initialMargin (WAD), maintenanceMargin (WAD)
CollateralVault Functions
getMarginInfo(user)— Returns complete MarginInfo struct containing: deposit (USDC), unrealizedPnL, equity (deposit + unrealizedPnL), initialMargin, maintenanceMargin, maxWithdraw, and isHealthy (whether equity >= MM)
Implementation
The margin calculations are implemented in Solidity:
| Contract/Library | Purpose |
|---|---|
| MarginEngine | Core SPAN-like stress testing library |
| DiffusalCollateralVault | Margin enforcement on deposits/withdrawals |
| DiffusalLiquidationEngine | Liquidation when MM is breached |
Related
- Margin System — Protocol-level margin system with worked examples
- Black-Scholes Model — Option pricing formulas used in stress calculations
- Solidity Math — Precision handling and numeric conversions
- Liquidation — What happens when equity falls below MM