# Math behind the demand spread

Demand-driven component to spread as input takes several variables:&#x20;

* Liquidity depth in the liquidity pools&#x20;
* amount of collateral deposited by the swap takers&#x20;
* amount of notional exposure on each leg (pay-fixed or receive-fixed)

As well as configuration params from the[ risk oracle](/ipor-derivatives/automated-market-maker/risk-oracle.md):

* Max Collateral Factor
* Max Leverage

The demand part is calculated in a couple of steps:&#x20;

1. first, the depth of the pools is assessed by adding the liquidity available in the pools and the absolute value of the difference between collateral on both legs.&#x20;

$$
lpDepth = LPCollateral - |Collateral\_{payFix} - Collatereal\_{recFix}|
$$

2. Next, with help from the risk oracle the notional depth can be calculated. It is necessary later on to compile. MaxLeverage and MaxLPCollateralFactorPerLeg are fetched from the risk oracle

$$
notionalDepth = lpDepth \cdot MaxLeverage \cdot MaxLPCollateralFactorPerLeg
$$

3. As the swaps get open their notional value is added and forms Time Weighted Notional (TWN) per leg. This value is kept separately per each asset and tenor.&#x20;

$$
TWN\_{t,tenor,leg} = TWN\_{t, tenor,leg} + N\_{swap}
$$

4. As the time passes the effect of a swap on the TWN is decaying to 0 at maturity. This value is calculated on the fly and used to measure the dynamic exposure of the pool to a given leg. &#x20;

$$
TWN\_{t, tenor,leg} = \frac{TWN\_{t-1, tenor, leg} \cdot (tenor\[s] - \Delta T)}{tenor\[s]}
$$

5. In the next step the values of each tenor-specific TWN are added

$$
TWN\_{t, leg} = \sum\_{tenor}TWN\_{t,leg}
$$

6. Next it has to be determined, which leg is overweight and which one is underweight. This can be done by subtracting the TWN on each leg&#x20;

$$
overweight\_{payFixed} = TWN\_{payFixed} - TWN\_{receiveFixed}
$$

$$
overweight\_{receiveFixed} = TWN\_{receiveFixed} - TWN\_{payFixed}
$$

7. Once it can be determined which leg is overweight, the spread can be applied to IT depending on the size of the difference. Formula for both legs is identical.

$$
ratio\_{payFixed} = \begin{cases}\
\frac{overweight\_{payFixed}}{notionalDepth} & overweight\_payFixed > 0 \\
0 & overweight\_{payFixced} \le  0
\end{cases}
$$

8. Finaly the demand spread can be calculated with help of simple step function. Linear step function presented below is subject to configuration and the final values of slope and base are to be set by the DAO.

$$
demandSpread\_{leg} = \begin{cases}\
0.005 \cdot ratio & ratio < 0.1 \\
0.01 \cdot ratio + 0.005 & ratio < 0.2 \\
0.015 \cdot ratio + 0.005 & ratio < 0.3 \\
0.02 \cdot ratio + 0.015 & ratio < 0.4 \\
0.05 \cdot ratio + 0.03 & ratio < 0.5 \\
0.33(3) \cdot ratio + 0.15 & ratio < 0.8 \\
0.5 \cdot ratio + 0.2 & ratio < 1 \\
\end{cases}
$$

9. Now that the demand spread for leg is calculated same operations is repeated only this time  notional of the new position that is to be opened is added to the "overweight" value. Afterwards simple average is calculated and final spread is returned. The last step is necessary in order to account for the slippage caused by the position and it removes the incentive of splitting position into multiple smaller swaps to potentially get a better rate&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.ipor.io/ipor-derivatives/automated-market-maker/spread/math-behind-the-demand-spread.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
