# Price Indices

### Oracle Price <a href="#oracle-price" id="oracle-price"></a>

Perpl uses the Oracle Price (Chainlink Spot Oracle) in multiple ways:

1. To ensure that the funding price, a spot index value, is reasonable and within tolerance.
2. To create a synthetic perp price using the [BitMex method](https://www.bitmex.com/app/fairPriceMarking#Calculation-of-Fair-Price-for-Perpetual-Contracts) at every oracle update. This is used to compute unrealized P\&L for positions and collateralization of increasing P\&L when a position is increased or inverted. It is also used to reject the new position if it is under MMR.
3. To ensure that the mark price that is set by the protocol is within a tolerance of the synthetic perp price.
   * Synthetic perp price will be computed in the smart contract as follows: $$P\_{SyntheticPerp1} = P\_{Oracle} \cdot \left(1 + C\_{FundingRate} \cdot \frac{C\_{BlocksUntilNextFunding}}{C\_{BlocksPerFundingInterval}}\right)$$
   * *C*<sub>*FundingRate*</sub> is the value of the last funding rate.
   * *C*<sub>*BlocksUntilNextFunding*</sub> is the number of blocks until the next funding rate event.
   * *C*<sub>*BlocksPerFundingInterval*</sub> is the number of blocks between funding events.

### Mark Price <a href="#oracle-price" id="oracle-price"></a>

Mark price is an unbiased and robust estimate of the fair perp price, and is used for margining, liquidations, triggering TP/SL, and computing unrealized PnL. It's computed off-chain from a mix of inputs and updated periodically OR during significant periods of change.

The mark price is computed periodically as follows:

<p align="center"><span class="math">P_{Mark} = \operatorname{median}(\{P_{SyntheticPerp1}, P_{Book}, P_{SyntheticPerp2}\})</span></p>

Where:

<p align="center"><span class="math">P_{SyntheticPerp1} = P_{Oracle} \cdot \left(1 + C_{FundingRate} \cdot{{C_{BlocksUntilNextFunding}\over{C_{BlocksPerFundingInterval}}}}\right)</span></p>

<p align="center"><span class="math">P_{Book} = \operatorname{median}(\{P_{MinAsk},P_{MaxBid},P_{Last}\})</span></p>

<p align="center"><span class="math">P_{SyntheticPerp2} = \operatorname{MA30}\left( \frac{P_{MinAsk}+P_{MaxBid}}{2} -P_{Oracle} \right)+P_{Oracle}</span></p>

* *P*<sub>*SyntheticPerp1*</sub> is an approximation of the perpetual price obtained by adding a decaying amount of the funding rate to the spot oracle price. This is appropriate because the funding rate is a long-term measure of the difference between prices on the perpetual and spot markets over a defined interval. It decays because the further from the measurement, the less valid the correction becomes. This strategy is employed by both [BitMex](https://www.bitmex.com/app/fairPriceMarking#Calculation-of-Fair-Price-for-Perpetual-Contracts) and [Binance](https://www.binance.com/en/support/faq/detail/360033525071).
* *P*<sub>*Book*</sub> is a median of a sample of the current order book statistics:
  * *P*<sub>*MinAsk*</sub> is the minimum ask price of valid (non-expired) orders on the perpetual.
  * *P*<sub>*MaxBid*</sub> is the maximum bid price of valid (non-expired) orders on the perpetual.
  * *P*<sub>*MinAsk*</sub> is the last price of a trade on the perpetual.
* *P*<sub>*SyntheticPerp2*</sub> is another approximation of the perpetual price obtained by taking a moving average of the difference between the spot oracle price and a sum of the minimum ask and maximum bid prices, averaged over 30 samples, later adding it to the current oracle price. This algorithm is employed by [Binance](https://www.binance.com/en/support/faq/detail/360033525071).
* $$\operatorname{MA30}$$ is a 30-sample Moving Average.
