---
# System prepended metadata

title: Pricing_Uniswap_v3
tags: [AMM, Research]

---

# The Uniswap v3 Puzzle: Finding the Optimal Time to Stop and Hedge


## Introduction

### Background
Decentralized finance (DeFi) has revolutionized traditional financial systems by enabling permissionless access to financial services through blockchain technology. Among the key innovations in the DeFi space are Decentralized Exchanges (DEXs), which allow users to trade cryptocurrencies without intermediaries. Uniswap v3 has emerged as a leading DEX, pioneering the concept of concentrated liquidity. This mechanism enables *liquidity providers (LPs)* to allocate capital to specific price ranges, offering greater control and potential for increased returns compared to previous versions. However, concentrated liquidity also introduces unique challenges in managing risks and optimizing returns.

### Concentrated Liquidity
Concentrated liquidity allows LPs to concentrate their capital within a specific price range, providing greater flexibility and potential for increased returns compared to previous versions where liquidity was distributed uniformly along the entire bonding curve. While this mechanism amplifies capital efficiency and potential earnings, it also exposes LPs to increased risk if the price moves outside their active interval.

### Research Objectives
This research delves into two critical optimization problems faced by LPs on Uniswap v3:

* **Optimal Stopping:** Determining the optimal time to withdraw liquidity to maximize profits, considering factors such as market volatility, price movements, and accumulated fees.
* **Hedging:** Developing strategies to mitigate the risks associated with concentrated liquidity, particularly impermanent loss, which occurs when the value of an LP's holdings changes due to price fluctuations.

By formulating and solving these problems, this research aims to provide LPs with valuable insights and tools to navigate the complexities of concentrated liquidity on Uniswap v3 and optimize their strategies for maximizing returns while minimizing risks. The findings will contribute to a better understanding of the dynamics of concentrated liquidity and aid in developing more effective risk management and profit optimization strategies for LPs in the DeFi space.



## Uniswap v3 Dynamics


### Concentrated Liquidity
Uniswap v3 introduces the novel concept of *concentrated liquidity*, enabling LPs to allocate capital to a specific price range $[p_l,,p_r]$. This contrasts with previous versions where liquidity was distributed uniformly along the entire bonding curve. Concentrated liquidity allows LPs to increase capital efficiency and potentially earn higher fees within their chosen range. However, it also exposes them to increased risk if the price moves outside their active interval.

As shown in the graph, LPs can concentrate their liquidity within a specific price range (shaded area). This allows for greater capital efficiency compared to previous versions (dotted line), where liquidity was spread across the entire bonding curve.

![image](https://hackmd.io/_uploads/B1Mi4ohskl.png)


### Dynamics of Token Holdings and Fee Earnings
An LP's token holdings in a Uniswap v3 pool are dynamic, changing with price fluctuations within their defined range. Let $x$ and $y$ represent the quantities of token 0 and token 1 held by the LP, respectively. These quantities are functions of the current price p and the chosen price range:

\begin{split}
x &= L\left\{ \left(\frac1{\sqrt p} - \frac1{\sqrt{p_r}} \right)^+ - \left(\frac1{\sqrt p} - \frac1{\sqrt{p_l}} \right)^+ \right\} \\
&= L\left\{ \left(\frac1q - \frac1{q_r} \right)^+ - \left(\frac1q - \frac1{q_l} \right)^+ \right\} \\
y &= L\left\{ \left(\sqrt p - \sqrt{p_l} \right)^+ - \left(\sqrt p - \sqrt{p_r} \right)^+ \right\} \\
&= L\left\{ \left(q - q_l \right)^+ - \left(q - q_r \right)^+ \right\}
\end{split}

where $L$ is the liquidity level and $q = \sqrt{p}$ is the root price that simplifies the expressions.

As trading activity occurs, LPs earn fees, denoted by $f$, which are proportional to the trading volume within their active range. The fee dynamics are given by:

\begin{split}
d f^x_t
&= \frac{1-\gamma}{2\gamma} L p_t^{-3/2} \pmb{1}_{[p_l, p_r]}(p_t) dp^-_t = \frac{1-\gamma}{\gamma} L q^{-2}_t \pmb{1}_{[q_l, q_r]}(q_t) dq_t^- \\
d f^y_t
&= \frac{1-\gamma}{2\gamma} L p_t^{-1/2} \pmb{1}{[p_l, p_r]}(p_t) dp^+_t = \frac{1-\gamma}{\gamma} L \pmb{1}_{[q_l, q_r]}(q_t) dq_t^+
\end{split}

where $\gamma$ represents the fee parameter. In particular, $p_t$ and $q_t$ need to be of finite variation.

These equations illustrate the dynamic interplay between price movements, token holdings, and fee earnings for LPs in a Uniswap v3 pool. Understanding these dynamics is crucial for developing effective strategies to optimize returns and manage risks.


### Price and Fee Dynamics
The price and fee dynamics are governed by the following system of SDEs:

\begin{split}
\frac{d S_t}{S_t} &= \mu dt + \sigma dW_t \\
\frac{d Q_t}{Q_t} &= (u^+_t - u^-_t) dt \\
df^x_t &= L \frac{1-\gamma}{\gamma} \pmb{1}_{[q_l, q_r]}(Q_t) Q_t^{-1} u^+_t dt \\
df^y_t &= L \frac{1-\gamma}{\gamma} \pmb{1}_{[q_l, q_r]}(Q_t) Q_t u^-_t dt
\end{split}

where
* $S_t$: Fair market price of the asset, modeled as a GBM with drift $\mu$ and volatility $\sigma$.
* $Q_t = \sqrt{P_t}$: Root price of the asset within the Uniswap v3 pool.
* $Z_t = \ln \frac{S_t}{P_t} = \ln \frac{S_t}{Q^2_t}$: Mispricing between the reference market and AMM.
* $u^+_t = g_+(Z_t)$ and $u^-_t = g_-(Z_t)$: Buy and sell order flow rates, respectively, driven by mispricing.
* $f^x_t$ (resp. $f^y_t$): Cumulative fees in token 0 (resp. token 1) earned by the LP up to time $t$.

#### Model Intuition
This model captures the interplay between the fair price $S_t$, pool price $P_t$, and order flow $u^+_t$, $u^-_t$. The log-transformed price ratio,

$$
Z_t := \ln (S_t / P_t) = \ln S_t - 2 \ln Q_t,
$$

approximates a mean-reverting process.

Specifically, the model incorporates:
* **Mean Reversion:** The drift of $Z_t$ pushes it towards the no-arbitrage interval $[\ln \gamma, -\ln \gamma]$, where arbitrage is limited due to fees.
* **Arbitrage Activity:** Significant buy (sell) orders are triggered when the pool price deviates above (below) the fair price, adjusted for fees.
* **Noise Trading:** Order flow represents trading activity unrelated to arbitrage.

#### Examples
1. Arbitrage-Driven: The order flow $u^+_t$ (resp. $u^-_t$) is driven by a reflection of $Z_t$ at $\ln \gamma$ (resp. $-\ln \gamma$).
2. OU-Driven: $g^+(z) = - \kappa_+ z + \beta_+$ and $g^-(z) = \kappa_- z + \beta_-$, where
    * $\kappa_+, \kappa_->0$: Speed of arbitrage order flow.
    * $\beta_+$ and $\beta_-$: Baseline buy and sell order flow from noise traders.



## Optimal Stopping Problem

### Problem Formulation
The optimal stopping problem for a Uniswap v3 LP involves determining the optimal time to withdraw their liquidity to maximize their total profit. Mathematically, this can be expressed as:

$$
\max_{\tau \in \mathcal{T}} \ \mathbb{E} \left[G(\tau, S_\tau, Q_\tau, f_\tau) \right]
$$

where:

* $\tau$ is the stopping time (when the LP withdraws liquidity)
* $\mathcal{T}$ is the set of all possible stopping times
* $S_\tau$ is the price of the asset at time $\tau$
* $Q_\tau$ is the root price of the asset in the pool at time $\tau$
* $f_\tau$ is the total fees earned by the LP up to time $\tau$
* $G$ is the function representing the LP's total profit, considering the initial investment, changes in token holdings, and fees earned, with a liquidation cost $c$ and risk-free rate $r$:
$$
G(t,s,q,f) = e^{-rt}\left[cs L\left\{\left(\frac1q - \frac1{q_r}\right)^+ - \left(\frac1q - \frac1{q_l}\right)^+\right\} + L\left\{\left(q - q_l\right)^+ - \left(q - q_r\right)^+\right\} + f\right]
$$


### Hamilton-Jacobi-Bellman (HJB) Equation
The HJB equation is a fundamental tool for solving optimal stopping problems. It characterizes the value function, which represents the maximum expected profit achievable from a given state. For this problem, the HJB equation is:

$$
\begin{cases}
\max \left\{v_t - rv + \mathcal{L}v + h, g-v \right\} = 0, \ (t,s,q) \in [0, T) \times \mathbb{R}^2_+ \\
v(T,s,q) = g(s,q)
\end{cases}
$$

where:
* $v$ is the value function, representing the maximum expected profit given the current state and time.
* $\mathcal{L} = \frac12 \sigma^2 s^2 \partial_{ss} + \mu s \partial_{s} + (u^+_t - u^-_t) q \partial_{q}$ is the infinitesimal generator of the state process ($S_t$, $Q_t$), capturing the instantaneous change in the value function due to the evolution of these state variables.
* $h(s,q) = L \frac{1-\gamma}{\gamma} (s q^{-1} u^+ + q u^-) \pmb{1}_{[q_l, q_r]}(q)$ is the earning rate of the state process, representing the instantaneous change in the value function due to accumulated fees.
* $g(s,q) = cs L\left\{\left(\frac1q - \frac1{q_r}\right)^+ - \left(\frac1q - \frac1{q_l}\right)^+\right\} + L\left\{\left(q - q_l\right)^+ - \left(q - q_r\right)^+\right\}$ is the profit function upon stopping, representing the terminal value of the LP's position when they choose to withdraw liquidity.


### Penalty Approximation Method
To solve the HJB equation numerically, we employ a penalty approximation method. This involves considering a penalized version of the HJB equation:

$$
(*)
\begin{cases}
v_t - rv + \mathcal{L}v + h + K(g-v)^+ = 0, \ (t,s,q) \in [0, T) \times \mathbb{R}^2_+ \\
v(T,s,q) = g(s,q) \quad
\end{cases}
$$

where
* $K>0$: Penalty factor, a large positive constant.
* $(g-v)^+ = \max(g-v, 0)$: Positive part of the difference between the terminal wealth function g and the value function $v$.

#### Properties
* **Convergence:** As the penalty factor $K$ increases, the solution to the penalized equation $(∗)$ converges to the solution of the original HJB equation. This is because a larger K imposes a stronger penalty, forcing the solution of $(∗)$ to stay closer to the constraint $v \geq g$.
* **Stochastic Control Interpretation:** The penalized equation $(∗)$ can be reinterpreted as a stochastic control problem. By introducing a control variable $u \in \{0,1\}$, we can rewrite $(∗)$ as: $$rv_t + h + \max_{u \in \{0,1\}} \{ \mathcal{L}v - Kuv + Kug \} = 0$$ This formulation highlights the connection between the penalty approximation and a controlled process where the control $u$ determines whether to stop or not.

This penalty method effectively approximates the original HJB equation by introducing a penalty term, $K(g−v)^+$, that discourages the value function from falling below the terminal wealth function. This term essentially penalizes any violation of the constraint $v \ge g$. The advantage of this method is that it transforms the original free-boundary problem into a nonlinear equation, which is often easier to solve numerically.


### Entropy Regularization
To further enhance the numerical stability and efficiency of the penalty approximation, we introduce an entropy regularization technique. This involves incorporating a weighted entropy penalty term into the penalized HJB equation:

$$
v_t - rv + \mathcal{L}v + h + \max_{\pi \in [0,1]} \left\{ \pi K(g-v) - \frac{1}{\beta}\left[\pi\ln\pi + (1-\pi)\ln(1 - \pi)\right] \right\} = 0,
$$

where:
* $\pi$: Randomized control variable, representing the probability of applying the penalty.
* $\beta>0$: Regularization parameter controlling the strength of the entropy penalty.
* The entropy penalty term, $\frac{1}{\beta}\left[\pi\ln\pi + (1-\pi)\ln(1 - \pi)\right]$, encourages exploration by penalizing deterministic policies (where $\pi$ is close to 0 or 1). This promotes smoother solutions and can improve the convergence of numerical methods.

#### Derivation of the Semilinear PDE
The maximization problem within the regularized HJB equation can be solved analytically. The optimal value of the control variable 

$$
\pi^* = \frac{e^{\beta K(g-v)}}{1 + e^{\beta K(g-v)}}
$$

Substituting this back into the regularized HJB equation, we obtain the following semilinear PDE:

$$
v_t - rv + \mathcal{L}v + h + \frac{1}{\beta} \ln\left\{ e^{\beta K(g-v)} + 1 \right\} = 0,
$$ 

with the terminal condition $v(T, s, q) = g(s, q)$.

#### Convergence to the Original Problem
The solution to this regularized problem converges to the solution of the original optimal stopping problem as the penalty factor $K$ and the regularization parameter $\beta$ tend to infinity. More precisely, the value function of the original problem is given by:

$$
\lim_{K\to\infty}\lim_{\beta\to\infty} v_{K, \beta}(t, s, q),
$$

where $v_{K, \beta}$ denotes the solution to the rHJB equation with parameters $K$ and $\beta$.

The optimal stopping time is determined by the first hitting time of the boundary where the value function equals the terminal profit function:

$$
\tau^* = \inf \left\{t \geq 0, \lim_{K\to\infty}\lim_{\beta\to\infty} v_{K,\beta}(t,s,q) = G(t,s,q) \right\}
$$

**Question:** How to solve the optimal stopping problem numerically?



## Hedging a Uniswap v3 Position

### Problem Formulation
The hedging problem for a Uniswap v3 LP involves finding the optimal trading strategy in an external market to mitigate the risks associated with their static Uniswap v3 position. This can be formulated as minimizing the expected residual hedging error, considering different time horizons:

1. **Finite time horizon:**
$$
\min_{\theta \in \Theta} \ \mathbb{E}\left[ (V_T - H_T^\theta)^2 \right]
$$
2. **Infinite time horizon:**
$$
\min_{\theta \in \Theta} \ \mathbb{E}\left[ \int_0^\infty e^{-rt} (V_t - H_t^\theta)^2 dt \right]
$$

where:
* $\theta$: Represents the trading strategy, specifying the allocation of assets in the external market over time.
* $\Theta$: The set of all admissible trading strategies.
* $H_t^\theta$: The value of the hedging portfolio at time $t$ under strategy $\theta$.
* $V_t$: The value of the LP's position at time $t$, including fees.
* $r$: Risk-free rate.


### Inventory Dynamics
To accurately model the hedging scenario, it's essential to consider the LP's token holdings $(x_t, y_t)$ within the Uniswap v3 pool and the value of their hedging portfolio in the external market. The dynamics of the LP's token holdings are captured by the following equations:

\begin{split} dx^\theta_t &= \frac{1}{2} L p_t^{-3/2} \pmb{1}_{[p_l, p_r]}(p_t) \left[ \gamma^{-1} dp^-_t - dp^+_t \right] - \theta_t dt \\ dy^\theta_t &= \frac{1}{2} L_t p_t^{-1/2} \pmb{1}_{[p_l, p_r]}(p_t) \left[ \gamma^{-1} dp^+_t - dp^-_t \right] + S_t \theta_t dt \end{split} 

where $\theta_t$ represents the trading rate of token 0 in the external market.


### Challenges of Hedging a Static Uniswap v3 Position
Hedging a static Uniswap v3 position presents unique challenges due to the concentrated liquidity mechanism. The LP's risk exposure is not constant but depends on the price range they choose and the price dynamics of the underlying asset. As the price fluctuates, the LP's token holdings and impermanent loss vary, requiring dynamic adjustments to the hedging strategy. Moreover, the concentrated liquidity mechanism can lead to sudden shifts in risk exposure, making it crucial to have a responsive and adaptive hedging strategy.


### Need for a Reasonable Order Flow/Price Model
Developing an effective hedging strategy necessitates a reasonable model for the order flow and price dynamics within the Uniswap v3 pool. This model should capture the interplay between arbitrage activity, price impact, and the concentrated liquidity mechanism. Accurate modeling of these factors is crucial for predicting the LP's risk exposure and devising appropriate hedging strategies. A well-calibrated model can provide valuable insights into the potential price movements and order flow patterns, enabling LPs to anticipate and mitigate risks effectively.


### Controlling the Fee Tier in Future Uniswap Versions
Current versions of Uniswap, such as Uniswap v4, allow LPs to control the fee tier of their positions. This introduces an additional dimension to the hedging problem, as the LP can now adjust their fee earnings in response to market conditions. Incorporating this feature into hedging strategies could further enhance risk management and optimize returns for LPs. By strategically adjusting the fee tier, LPs can potentially offset losses or enhance gains, depending on the prevailing market conditions and their risk appetite.

**Questions:**
1. How can we solve the optimal hedging problem?
2. What is the optimal dynamic fee structure that minimizes the objective function?



## References
* Dai, Min and Sun, Yu and Xu, Zuo Quan and Zhou, Xunyu, Learning to Optimally Stop Diffusion Processes, with Financial Applications (September 08, 2024). http://dx.doi.org/10.2139/ssrn.4928749
* Dianetti, J., Ferrari, G., & Xu, R. (2024). Exploratory optimal stopping: A singular control formulation. arXiv preprint arXiv:2408.09335.
* Fukasawa, M., Maire, B., & Wunsch, M. (2024). Model-Free Hedging of Impermanent Loss in Geometric Mean Market Makers with Proportional Transaction Fees. Applied Mathematical Finance, 31(2), 108–129. https://doi.org/10.1080/1350486X.2024.2404058
* Lipton, A., Lucic, V., & Sepp, A. (2024). Unified approach for hedging impermanent loss of liquidity provision. arXiv preprint arXiv:2407.05146.
* Wang, M., & Wang, T. H. (2023). Relative entropy-regularized robust optimal order execution. arXiv preprint arXiv:2311.06476.