> ## Documentation Index
> Fetch the complete documentation index at: https://docs.rails.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Mark Price

The mark price is the reference price Rails uses for a perpetual market. Margin requirements,
unrealized PnL, and liquidations are all based on the mark price rather than the index
price — see [Position Metrics](/latest/perps/formulas/position-metrics) and
[Risk Management](/latest/perps/guides/risk-management) for how it feeds those calculations.
Funding is the one calculation that stays on the index price directly — see
[Funding](/latest/perps/formulas/funding).

## Key Definitions

| Term            | Description                                                                                                                                                                     |
| --------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `indexPrice`    | The external index price — the asset's spot price aggregated from major markets outside Rails (see [Index Price Sources](/latest/perps/guides/market-data#index-price-sources)) |
| `bid`, `ask`    | Rails best bid and best ask                                                                                                                                                     |
| `last`          | Last trade price on Rails                                                                                                                                                       |
| `railsMedian`   | `median(bid, ask, last)` on Rails — where Rails' own market currently prices the asset                                                                                          |
| `premiumSample` | `(bid + ask) / 2 - indexPrice` — the instantaneous gap between Rails' mid-price and the index, before the EMA smooths it over time                                              |
| `t`             | Seconds elapsed since the previous update — the weight of the current premium sample                                                                                            |
| `EMA`           | Exponential moving average of `premiumSample`, with a 2.5-minute time constant — see [The Premium EMA](#the-premium-ema) below                                                  |
| `adjustedIndex` | `indexPrice + EMA` — the index price plus the exponential moving average of the premium of Rails' market over the index                                                         |

## How It's Calculated

The mark price is the **median of three values**:

```
markPrice = median(adjustedIndex, railsMedian, indexPrice)
```

| Value           | What it represents                                                         |
| --------------- | -------------------------------------------------------------------------- |
| `indexPrice`    | The external index price                                                   |
| `railsMedian`   | `median(bid, ask, last)` on Rails                                          |
| `adjustedIndex` | `indexPrice + EMA` — the index price plus the smoothed premium (see below) |

Two of the three values are anchored to the external index. That means Rails' own order book
can pull the mark price toward itself, but it can never run away with it: the mark can only
deviate from the index by as much as the *smoothed, time-averaged* premium, never by the size
of a momentary spike.

## The Premium EMA

Rails' market naturally trades slightly above or below the index — that gap is the premium:

```
premiumSample = (bid + ask) / 2 - indexPrice
```

Rather than using the instantaneous gap, `adjustedIndex` uses an **exponential moving average
(EMA) of the premium with a 2.5-minute time constant**. Recent samples count the most and older
samples fade out smoothly, and samples are weighted by how long they were in effect. A
one-second spike in the book barely moves the average; a persistent premium shifts it over
minutes.

The EMA is computed from a pair of decaying sums, updated on every tick, `t` seconds after the previous one:

```
numerator   → numerator   × e^(-t / 150s) + premiumSample × t
denominator → denominator × e^(-t / 150s) + t

EMA = numerator / denominator
```

## Update Frequency

The mark price updates on every index price tick, at most once every **250 milliseconds** per
market.

All arithmetic is exact decimal (no floating point drift), and the published price is rounded
to the price increment of `0.0000001`.

## When Inputs Are Unavailable

A market can temporarily be missing some of its own input. Whenever that happens, the mark
price falls back to the **index price** instead of going stale:

| Situation                              | Mark price                                                                                                                |
| -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- |
| No Rails quotes yet (empty order book) | Index price                                                                                                               |
| Quotes exist, but no trades yet        | Index price — the premium EMA already starts accumulating from the quotes, so it is warmed up when the first trade prints |
| Quotes and at least one trade exist    | Full formula                                                                                                              |

Fallback updates follow the same 250 millisecond cadence as normal updates.

## Usage in API

The mark price is published as a `MARK` entry from the
[Get Market Data](/latest/perps/rest-api/get-market-data) endpoint, with `value` holding the
price.

See [Position Metrics](/latest/perps/formulas/position-metrics#unrealized-pnl) for the PnL
formula and [Risk Management](/latest/perps/guides/risk-management#liquidation) for how the
mark price drives liquidation.
