# Pricing Engine

## Introduction

Demex has a robust pricing engine to ensure markets have accurate and fair pricing for all market participants.&#x20;

### Oracles

Demex is built with an extremely fast, flexible and secure oracle protocol that is ran by the chain's validators. The oracles are able to submit price feeds and other real-world information with extremely high reliability and low latency. This is used to derive various fair prices of Demex's derivative markets.

{% hint style="info" %}
Oracles act as intermediary components that link blockchains with real-world data. These services act as connectors between blockchains and the external world, allowing smart contracts to access data sources beyond their own networks.&#x20;
{% endhint %}

## **Index Price**

When oracle votes representing 67% of bonded tokens has been submitted for a specific timestamp, the *median value of valid votes* will be utilized to generate the **Index Price** for a market.&#x20;

This typically happens within 1 second of any changes in the spot price.

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

The Mark Price is the price at which the futures contract is valued at when being traded. This can temporarily vary from the actual futures market price in order to protect market participants against manipulative trading. This can also vary from the underlying spot price due to time premiums ([contango](https://www.investopedia.com/terms/c/contango.asp#toc-contango) or [backwardation](https://www.investopedia.com/terms/c/contango.asp#toc-backwardation)) being applied on the derivative contract.

### **Fair Price Marking**

The mark price of a market is determined by finding a fair price based on the current market's order book and ensuring that it does not deviate too quickly or too much from the index price.

More specifically, the following formula is used to determine each market's mark price:

```
MPRaw = IndexPrice + EMA30s(FairPrice - IndexPrice)
MarkPrice = clamp(IndexPrice * (100 - MPBand / 2)%, MPRaw, IndexPrice * (100 + MPBand / 2)%)

// MPBand is a market parameter (see GET markets: mark_price_band) denoted in integer basis points,
// EMA30s means 30-second exponential moving average,
// clamp(min, x, max) means min if x < min, or max if x > max, else x.
```

Fair price can be determined by the following formula:

```
1. If either the buy or sell order book has no orders:
FairPrice = IndexPrice

2. Otherwise:
ImpactBid = max[averagePrice(sell orders up to ImpactSize lots), BestBidPrice - ImpactBand]
ImpactAsk = min[averagePrice(buy orders up to ImpactSize lots), BestAskPrice + ImpactBand]
FairPrice = (ImpactBid + ImpactAsk) / 2

// ImpactSize is a market parameter (see GET markets: impact_size) denominated in base tokens (same as lot size)
// ImpactBand is a global market parameter (see GET market params: impact_band) denominated in integer basis points.
```

### **Last Price Protected**&#x20;

In the case that the oracle network is down or the index price cannot be updated for any other reason, the mark price formulation falls back to using the last traded price of the market.

Users can detect this change by monitoring the `marking_strategy` from the GET Prices endpoint. In normal fair price marking, the value will be `fair`. When falling back to last price marking, the value will be `last`.

```
MPRaw = clamp(LastPrice * (100 - LPPBand / 2)%, LastPrice, LastPrice * (100 + LPPBand / 2)%)
MarkPrice = clamp(EMA30(MarkPrice) * (100 - SmoothenBand / 2)%, MPRaw, EMA30(MarkPrice) * (100 - SmoothenBand + 2)%)

// LPPBand is a market parameter (see GET markets: last_price_protected_band) denoted in integer basis points
// SmoothenBand is a global market parameter (see GET market params: smoothen_band) denoted in integer basis points
```

## Settlement

Futures on Demex are cash settled rather than settled by physical delivery. This means that at the settlement, the buyer of a futures contract will not buy the actual underlying, nor the seller will sell the underlying. There will only be a transfer of losses / gains at the settlement of the contract, based on the settlement price. Note that by nature, perpetual markets never settle.

### **Settlement Price** <a href="#settlement-price" id="settlement-price"></a>

The last 30 minutes time-weighted average of the `Index Price` before each futures contract expiry time is used for market settlement. At contract expiration, all open positions will be closed and the PnL (based on the difference between the position's average opening price and this settlement price) will be automatically realized.


---

# 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://guide.dem.exchange/trade/futures/pricing-engine.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.
