Margin System
Portfolio margin calculation using SPAN-like stress testing
Diffusal uses a portfolio margin system inspired by CME SPAN (Standard Portfolio Analysis of Risk). Unlike isolated margin where each position is collateralized independently, portfolio margin allows netting of positions and calculates collateral requirements based on worst-case portfolio loss under stress scenarios.
Cross-Margin Architecture
Users do not collateralize individual positions. Instead, all positions share a single collateral pool with deposited USDC and multiple option positions across different series.
This enables capital efficiency: a long call can offset some of the risk from a short put, reducing total margin requirements.
Portfolio Equity Calculation
The core health metric is Portfolio Equity:
Unrealized PnL
For each position, unrealized PnL is calculated against current mark price:
For long positions (size > 0):
- Positive if option value increased (mark > entry)
- Negative if option value decreased
For short positions (size < 0):
- Positive if option value decreased (obligation worth less)
- Negative if option value increased (obligation worth more)
Example
| Position | Entry Premium | Current Mark | Size | PnL |
|---|---|---|---|---|
| Long ETH Call | $150 | $180 | +10 | +$300 |
| Short ETH Put | $120 | $140 | -5 | -$100 |
| Total | +$200 |
If deposited collateral is $5,000:
Stress Testing (SPAN-like)
The margin system uses stress testing to determine required collateral. We test the corners of the risk space to find worst-case portfolio loss.
Stress Scenarios
We apply shocks to both spot price and implied volatility:
| Parameter | Down Shock | Up Shock |
|---|---|---|
| Spot Price | -30% | +30% |
| Implied Volatility | -30% | +50% |
This creates 4 corner scenarios:
| Scenario | Spot Shock | IV Shock | Primary Risk For |
|---|---|---|---|
| 1 | -30% | +50% | Short calls (down + high vol) |
| 2 | -30% | -30% | Long calls (down + vol crush) |
| 3 | +30% | +50% | Short puts (up + high vol) |
| 4 | +30% | -30% | Long puts (up + vol crush) |
Why These Values?
- ±30% spot: Captures significant market moves (e.g., crypto flash crashes, major news events)
- +50% IV: Volatility spikes are common during market stress (VIX can double in hours)
- -30% IV: Vol crush after events resolve (post-earnings, post-FOMC)
Stress Loss Calculation
For each scenario:
- Recalculate all option premiums with stressed spot and IV using Black-Scholes
- Compute portfolio value change vs current mark
- Track the maximum loss across all scenarios
Required Margin Calculation
Initial Margin (IM)
Where:
- Stress Loss: Maximum loss across stress scenarios
- Adverse PnL Buffer: (covers current losses with 5% buffer)
- Notional Buffer: (15% of total position notional)
Maintenance Margin (MM)
Health States
| State | Condition | Action |
|---|---|---|
| Healthy | Equity ≥ MM | Normal trading |
| Liquidatable | Equity < MM | Position can be liquidated |
Worked Example
Consider a portfolio with ETH at $3,000 spot and 50% IV:
Positions
| Position | Strike | Expiry | Entry Premium | Size |
|---|---|---|---|---|
| Long ETH Call | $3,200 | 30 days | $150 | +10 |
| Short ETH Put | $2,800 | 30 days | $120 | -5 |
Deposited Collateral: $2,000
Step 1: Current Premiums (Black-Scholes)
Using Black-Scholes with spot=$3,000, IV=50%, r=5%, T=30/365:
| Option | Current Mark |
|---|---|
| $3,200 Call | $98.76 |
| $2,800 Put | $80.63 |
Step 2: Unrealized PnL
| Position | Entry | Mark | Size | PnL |
|---|---|---|---|---|
| Long Call | $150 | $98.76 | +10 | -$512.40 |
| Short Put | $120 | $80.63 | -5 | +$196.85 |
| Total | -$315.55 |
Step 3: Stress Losses
Testing scenario 1 (Spot -30%, IV +50%):
- Stressed spot: $2,100
- Stressed IV: 75%
| Position | Current Mark | Stressed Mark | Loss |
|---|---|---|---|
| Long Call | $98.76 | $45.20 | -$535.60 |
| Short Put | $80.63 | $142.30 | -$307.85 |
| Total | -$843.45 |
After testing all 4 scenarios, maximum stress loss: $843.45
Step 4: Required Collateral
Step 5: Health Check
Since 1,539.82 (MM), the position is Healthy.
Risk Parameters
| Parameter | Default | Description |
|---|---|---|
INITIAL_MARGIN_RATE | 15% | % of notional for IM buffer |
MAINTENANCE_MARGIN_RATE | 80% | MM as % of IM |
STRESS_SPOT_DOWN | -30% | Spot down shock |
STRESS_SPOT_UP | +30% | Spot up shock |
STRESS_IV_UP | +50% | IV up shock |
STRESS_IV_DOWN | -30% | IV down shock |
ADVERSE_PNL_MULTIPLIER | 1.05 | Buffer on unrealized losses |
Mark Price vs Trade Price
The margin system uses mark price (Black-Scholes theoretical) for all calculations, not the trade price:
| Concept | Source | Used For |
|---|---|---|
| Mark Price | Black-Scholes (oracle inputs) | Margin, PnL, liquidation triggers |
| Trade Price | Market (order book / RFQ) | Actual premium paid/received |
This prevents manipulation: a user cannot avoid liquidation by submitting fake orders at favorable prices.
Entry Premium Recording
When a trade executes, the actual trade price is recorded as entry premium. For example, if a user buys 10 contracts via RFQ at 51.50. Later, if the current mark price (from Black-Scholes) is 50.20 - 13.00.
The user paid above theoretical (the spread), so they start with a small unrealized loss.
Integration with Trading
Pre-Trade Check
Before any trade, the system verifies:
- New position margin: Would the post-trade portfolio be healthy?
- Sufficient collateral: Does user have enough deposited USDC?
The trade execution updates positions for both buyer and seller, then performs a post-trade margin check to ensure both parties remain healthy. If either fails the margin check, the transaction reverts.
Deposit and Withdrawal
- Deposit: Always allowed (increases equity)
- Withdrawal: Only if post-withdrawal Equity ≥ Initial Margin
Gas Optimization
The margin calculation is gas-intensive due to Black-Scholes evaluations. Optimizations include:
| Technique | Benefit |
|---|---|
| Batch oracle reads | Single oracle call for all positions on same pair |
| Cached Greeks | Reuse delta/gamma for nearby strikes |
| Off-chain pre-check | Frontend simulates margin before submission |
| Incremental updates | Only recalculate affected positions on trade |
Target gas cost: ~50,000 gas per margin check on high-throughput EVM chains.
Contract Implementation
The margin system is implemented across several contracts:
| Contract | Role |
|---|---|
| MarginEngine | Core SPAN-like stress testing library |
| DiffusalCollateralVault | Deposit/withdrawal with margin enforcement |
| DiffusalLiquidationEngine | Liquidation when MM is breached |
Related
- Margin Calculations — Mathematical formulas and derivations
- Liquidation — What happens when equity falls below MM
- Black-Scholes Model — Pricing used in stress calculations