# Profit & Loss Calculations

## PnL Impact on Margin

### Realized PnL

The margin system incorporates realized PnL when calculating margin requirements for position-closing orders. This affects both resting orders and position reversals.

#### Example - Position Close and then Rest
Given:
- Current Position: Long 2 BTC at $48,000 entry
- Current Mark Price: $50,000
- Sell Order: 3 BTC at $50,000
- Initial Margin Rate: 1%

```
Realized PnL from Close = 2 × ($50,000 - $48,000) = +$4,000
Margin Required for New Position = 1 × $50,000 × 0.01 = $500

Available Margin After Close = Initial Margin + Realized PnL
```

If the initial available margin was $300, the order would still be accepted because the realized PnL covers the initial margin on the resting order.

### Position Reversal Scenarios

Orders that reverse positions are evaluated including the PnL from closing the existing position.

#### Example - Position Reversal
Given:
- Current Position: Long 2 BTC at $45,000 entry
- Current Mark Price: $50,000
- Sell Order: 4 BTC at $50,000 (reverses to short 2 BTC)
- Initial Margin Rate: 1%
- Available Margin: $500

```
Step 1: Calculate Realized PnL
Realized PnL = 2 × ($50,000 - $45,000) = +$10,000

Step 2: Calculate New Position Margin
Short Position Margin = 2 × $50,000 × 0.01 = $1,000

Step 3: Evaluate Margin Requirement
New Available Margin = $500 + $10,000 = $10,500
Required Margin = $1,000

Result: Order Accepted (Available Margin > Required Margin)
```

## Unrealized PnL Treatment

### Unrealized Losses

Unrealized losses directly affect available margin by reducing the amount of balance available for margin.

#### Example - Unrealized Loss Impact
Given:
- Position: Long 2 BTC at $50,000 entry
- Current Mark Price: $48,000
- Initial Margin Rate: 1%
- Account Balance: $2,000

```
Unrealized Loss = 2 × ($48,000 - $50,000) = -$4,000
Position Margin = 2 × $48,000 × 0.01 = $960
Effective Available Margin = $2,000 - $4,000 = -$2,000

Result: Cannot place the position-opening order until more collateral is deposited or the price recovers
```

### Unrealized Gains

Unrealized gains are excluded from margin calculations to prevent a cascade of increased leverage that could create systemic risk.

## Detailed Example Scenario

Let's follow a sequence of trades to understand why this is critical:

### Initial Position
```
Account Balance: $10,000 USD
Initial Margin Rate: 1%
Maximum Position (initially): $10,000 / 0.01 = $1,000,000 notional
Initial Position: Long 20 BTC at $50,000 ($1,000,000 notional)
Initial Margin Used: $10,000
```

### Price Movement
```
Price moves from $50,000 to $60,000

Current Position Value: 20 BTC × $60,000 = $1,200,000
Unrealized Gain: $200,000
```

### If Unrealized Gains Were Included

If the system counted unrealized gains as available margin:
```
New "Available" Margin: $10,000 + $200,000 = $210,000
New Maximum Position: $210,000 / 0.01 = $21,000,000 notional
Additional Position Possible: ~350 BTC at $60,000
```

This would create several problems:
1. The account could open a position 17.5× larger than originally allowed by their actual collateral
2. A price reversion to $50,000 would create a loss of:
   ```
   Loss on Original Position: $0 (back to entry)
   Loss on Additional Position: 350 × ($50,000 - $60,000) = -$3,500,000
   ```
3. This loss far exceeds the actual account balance and unrealized gains

### With Unrealized Gains Excluded

Under the actual system:
```
Available Margin: $10,000 (original balance only)
Maximum Position: Remains at $1,000,000 notional
Additional Position Possible: $0 (fully utilized)
```

Benefits of this approach:
1. Position size remains proportional to actual deposited collateral
2. Market reversals can only affect profits, not create system-wide losses
3. Prevents leverage from scaling exponentially with market movements

### Closing and Opening New Positions

If the trader wants to utilize their gains, they must first realize them:
```
1. Close 5 BTC position at $60,000
   Realized Gain: 5 × ($60,000 - $50,000) = $50,000
   New Account Balance: $60,000

2. Now can open new positions based on actual balance:
   New Maximum Position: $60,000 / 0.01 = $6,000,000 notional
```

## Implications for Exchange Risk Management

This conservative approach provides several systemic benefits:

1. **Predictable Leverage**
    - System-wide leverage remains tied to actual deposited collateral
    - Prevents exponential growth of positions during market rallies

2. **Controlled Risk**
    - Losses remain proportional to actual deposits
    - Prevents cascade of forced liquidations during market reversals

3. **Market Stability**
    - Reduces pro-cyclical leverage in rising markets
    - Minimizes systemic impact of price reversals

4. **Clear Capital Requirements**
    - Traders must realize gains to increase position sizes
    - Creates natural circuit breaker in market dynamics

## Combined Scenarios

### Complex Position Management

When managing positions with both realized and unrealized PnL, the Nekuti Matching Engine:

1. Calculates unrealized losses on remaining positions and subtracts them from available margin
2. Calculates realized PnL from position-closing portions
3. Calculates new position margin requirements

#### Example - Complex Position Management
Given:
- Current Position: Long 3 BTC (2 BTC at $45,000, 1 BTC at $48,000)
- Current Mark Price: $50,000
- New Order: Sell 5 BTC at $50,000
- Initial Margin Rate: 1%
- Account Balance: $2,000

```
Step 1: Calculate Realized PnL from Position Close
PnL from 2 BTC = 2 × ($50,000 - $45,000) = +$10,000
PnL from 1 BTC = 1 × ($50,000 - $48,000) = +$2,000
Total Realized PnL = $12,000

Step 2: Calculate New Short Position Margin
New Position Size = 2 BTC
Required Margin = 2 × $50,000 × 0.01 = $1,000

Step 3: Evaluate Final Margin
Available Margin = $2,000 + $12,000 = $14,000
Required Margin = $1,000

Result: Order Accepted
```
