# Price, tick index and sqrt price correlation calculation

## What's difference between price, tick index and sqrt price?

| Concept        | Definition                                   | Features                                             | Application                                           |
| -------------- | -------------------------------------------- | ---------------------------------------------------- | ----------------------------------------------------- |
| **Price**      | Token exchange ratio (e.g., `token1/token0`) | Continuous value, updates in real-time               | Directly determines trading rates                     |
| **Tick Index** | Discretized price interval index             | Spacing determined by fee tiers                      | Manages liquidity distribution ranges                 |
| **Sqrt Price** | Square root of price                         | Stored as fixed-point numbers (e.g., `sqrtPriceX64`) | Simplifies on-chain calculations, improves efficiency |

**Notes:**

1. **Price**: Represents the real-time ratio between two tokens (e.g., ETH/USDC).
2. **Tick Index**:
   * Derived from `P = 1.0001^i`, where `i` is the Tick Index.
   * Liquidity can only be provided at discrete Ticks (e.g., multiples of 10).
3. **Sqrt Price**:
   * Used for computational efficiency (avoids floating-point operations).
   * In Uniswap V3, stored as `sqrtPriceX64` (Q64 fixed-point format).

## 1.  Tick index to price

when you want to open position, you dont know how to set your tick\_lower and tick\_upper.

use `/src/math/tick.ts TickMath.`tickIndexToPrice`()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.tickIndexToPrice(tickIndex, decimalsA, decimalsB)
```

## 2.Price to tick index

use `/src/math/tick.ts TickMath.priceToTickIndex()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const tickIndex = TickMath.priceToTickIndex(price, decimalsA, decimalsB)
```

## 3. Tick index to sqrt price x64

use `/src/math/tick.ts TickMath.tickIndexToSqrtPriceX64()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'

const sqrtPriceX64 = TickMath.tickIndexToSqrtPriceX64(tickIndex)
```

## 4. Sqrt price x64 to tick index

use `/src/math/tick.ts TickMath.sqrtPriceToTickIndex()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'

const tickIndex = TickMath.sqrtPriceX64ToTickIndex(sqrtPriceX64)
```

## 5. Price to sqrt price

use `/src/math/tick.ts TickMath.`priceToSqrtPriceX64`()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const sqrtPriceX64 = TickMath.priceToSqrtPriceX64(price, decimalsA, decimalsB)
```

## 6. Sqrt price to price

use `/src/math/tick.ts TickMath.sqrtPriceX64ToPrice()`

```ts
import { TickMath } from '@cetusprotocol/cetus-sui-clmm-sdk'
// decimalsA and decimalsB means the decimal of coinA and coinB
const price = TickMath.sqrtPriceX64ToPrice(sqrtPriceX64, decimalsA, decimalsB)
```

## 7. Convert the tick index from i32 to u32

In the TS SDK, for easier display, we will convert u32 to i32. If you need to call the contract directly, you can also use the method below to convert i32 to u32.

<pre class="language-typescript"><code class="lang-typescript">import { asUintN } from '@cetusprotocol/cetus-sui-clmm-sdk'
<strong>
</strong><strong>const tickLowerI32 = -100
</strong>const tickLowerU32 = asUintN(BigInt(tickLowerI32)).toString()

const tickUpperI32 = 100
const tickUpperU32 = asUintN(BigInt(tickUpperI32)).toString()
</code></pre>


---

# 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://cetus-1.gitbook.io/cetus-developer-docs/developer/via-sdk/features-available/price-tick-index-and-sqrt-price-correlation-calculation.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.
