Converting a Price to a Tick Podcast
Converting a Price to a Tick that can be Initialized¹ is done using the Following Formula:
Converting a Price to an Initializable Tick on Goblins
Converting a price to an initializable tick involves several steps. Here’s a detailed guide, including additional information and considerations:
Steps to Convert a Price to a Tick
- Turn the Price into a SqrtPrice²:
- Calculate Ratio: Obtain the ratio value using 1 token of BCH (token1) and the desired price of USDT (token0). This example uses the pool ratio:
- Example Price: TickLow_USDT = 1000 USDT for 1 BCH
- Calculate Ratio: Obtain the ratio value using 1 token of BCH (token1) and the desired price of USDT (token0). This example uses the pool ratio:
- Select a Price:
- Price Selection: Choose the desired price for the conversion.
- Convert Price to Lowest Decimal Format:
- Decimal Conversion: Multiply the price by 10 to the power of the token0 decimals (USDT has 6 decimals).
- Formatted Price: TickLowFormatted = TickLow_USDT * 10 to the 6th power
- Decimal Conversion: Multiply the price by 10 to the power of the token0 decimals (USDT has 6 decimals).
- Encode the SqrtPrice Ratio:
- Token Reserve Ratios: Use the lowest decimal values of the tokens as the pool ratio reserves³.
- Example Values:
- Token1ReserveRatio (BCH) = 1 BCH in wei (10 to the 18th power)
- Token0ReserveRatio (USDT) = Formatted USDT price (10 to the 9th power)
- Example Values:
- Calculate SqrtPriceRatio: Divide the token1 amount by the token0 amount.
- Formula: sqrtPriceRatio = Token1ReserveRatio / Token0ReserveRatio
- Token Reserve Ratios: Use the lowest decimal values of the tokens as the pool ratio reserves³.
- Get the Tick at the SqrtPrice:
- Calculate Tick: Use the logarithm of the sqrtPriceRatio with base 1.0001.
- Formula: tickForPrice = floor(log(sqrtPriceRatio) / log(1.0001))
- Calculate Tick: Use the logarithm of the sqrtPriceRatio with base 1.0001.
- Floor Value and Remove Decimals:
- Tick Value: This step gives you the tick for the price after taking the rounded-down floor value and removing decimals.
- Example Result: tickForPrice = 207243
- Tick Value: This step gives you the tick for the price after taking the rounded-down floor value and removing decimals.
- Adjust to Tick Spacing:
- Valid Initializable Tick: Adjust the tick to the tick spacing of the pool to get a valid initializable tick.
- Calculate Closest Low Tick: Divide the tick by the tick spacing, floor the value, then multiply by the tick spacing.
- Formula: closestLowTick = floor(tickForPrice / tickSpacing) * tickSpacing
- Calculate Closest High Tick: Take the ceiling of the tick divided by the tick spacing for the upper bound.
- Formula: closestHighTick = ceil(tickForPrice / tickSpacing) * tickSpacing
- Calculate Closest Low Tick: Divide the tick by the tick spacing, floor the value, then multiply by the tick spacing.
- Valid Initializable Tick: Adjust the tick to the tick spacing of the pool to get a valid initializable tick.
Example Calculation
- Set Desired Price:
- TickLow_USDT = 1000 USDT for 1 BCH
- Convert to Decimal Format:
- TickLowFormatted = 1000 * 10 to the 6th power = 1,000,000,000
- Encode the SqrtPrice Ratio:
- Token1ReserveRatio = 1,000,000,000,000,000,000 (1 BCH in wei)
- Token0ReserveRatio = 1,000,000,000 (formatted USDT)
- sqrtPriceRatio = 1,000,000,000,000,000,000 / 1,000,000,000 = 1,000,000
- Calculate Tick at SqrtPrice:
- tickForPrice = floor(log(1,000,000) / log(1.0001)) = 207243
- Adjust to Tick Spacing:
- Assume tickSpacing = 60
- closestLowTick = floor(207243 / 60) * 60 = 207240
- closestHighTick = ceil(207243 / 60) * 60 = 207300
Tips and Considerations
- Precision:
- Decimals and Rounding: Ensure you handle decimal places and rounding correctly to avoid calculation errors.
- Pool Parameters:
- Check Tick Spacing: Verify the tick spacing for the specific pool, as it varies by fee tier.
- Monitor Market Conditions:
- Price Changes: Be aware of market conditions and how price changes may impact your tick calculations and liquidity positions.
- Use Tools:
- Calculators and Scripts: Utilize available tools, calculators, or scripts to automate and verify your calculations for accuracy.
By following these steps and considering the additional tips, you can accurately convert a price to an initializable tick on the Goblins Protocol, ensuring efficient and effective liquidity management.
Footnotes:
- A tick that can be initialized must be evenly divisible by the tick spacing of the pool. Initialized ticks are used for liquidity positions.
- SqrtPrice is the current price of the pool.
- SqrtPriceRatio / pool ratio reserves. This is the ratio of tokens that would be in the pool. Imagine these two numbers as the initial liquidity deposit that sets the price of the pool.