# `DeltaCalc.PortfolioMargin`
[🔗](https://github.com/ZenHive/delta_calc/blob/v0.3.0/lib/delta_calc/portfolio_margin.ex#L1)

Portfolio-margin calculations over a caller-supplied position list.

This module nets offsetting long and short quantities before computing maintenance
margin, liquidation price, and margin usage. It performs no I/O and assumes all
positions in a call belong to the same risk unit.

## API Functions
| Function | Arity | Description | Param Kinds |
| --- | --- | --- | --- |
| `margin_usage` | 1 | Calculate used and available margin under portfolio-margin netting. | `account: value` |
| `portfolio_liquidation_price` | 1 | Estimate liquidation price for the netted portfolio book. | `account: value` |
| `combined_maintenance_margin` | 1 | Calculate maintenance margin after netting offsetting portfolio positions. | `account: value` |

# `account`

```elixir
@type account() :: %{
  :positions =&gt; [position()],
  optional(:equity) =&gt; DeltaCalc.Decimal.input()
}
```

Portfolio-margin account input.

# `position`

```elixir
@type position() :: %{
  side: side(),
  quantity: DeltaCalc.Decimal.input(),
  mark_price: DeltaCalc.Decimal.input(),
  mmr: DeltaCalc.Decimal.input()
}
```

Portfolio-margin position input.

# `side`

```elixir
@type side() :: :long | :short
```

Position side used for net exposure.

# `usage`

```elixir
@type usage() :: %{used: Decimal.t(), available: Decimal.t(), usage_pct: Decimal.t()}
```

Margin usage under the portfolio model.

# `combined_maintenance_margin`

```elixir
@spec combined_maintenance_margin(account()) :: Decimal.t()
```

Return net quantity x mark price x max MMR across the portfolio.

# `margin_usage`

```elixir
@spec margin_usage(account()) :: usage()
```

Return used maintenance margin, available equity, and usage percentage.

# `portfolio_liquidation_price`

```elixir
@spec portfolio_liquidation_price(account()) :: Decimal.t() | nil
```

Return the price where equity equals net maintenance margin, or nil for a flat book.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
