---
title: Dynamic liquidity shape in discrete AMMs
author: "Prevert"
date: "2023/05/05"
description: post
tags: dAMM
---
# Dynamic liquidity shape in discrete AMMs
This post presents a principled methodology to construct dynamic liquidity shapes from the ground in discrete AMM (based on posting orders in order books). The dynamic AMM is first broken down into basic AMMs called *market making automata* (MMA, or just automata). A MMA is the simplest operational AMM which can be designed among the whole family of AMMs. It is customizable and can be assembled with other automata, to implement a rich set of market making strategies.
## Automaton
In the context of a market $X/Y$ (e.g. ETH/DAI) and a market price $p$ (e.g. 2000), a MMA is defined by:
- two prices $p_0$ and $p_1 > p_0$
- two reserves $x, y \geq 0$ in assets $X$ and $Y$ (resp.)
We get a basic AMM $(p_0,p_1,x,y)$ which performs two tasks:
1) Exchange at most $x$ against Y at rate $p_1$
This happens when the price is higher than $p_1$. Reserves are in Y, yellow in the example: <center/>

</center>
2) Exchange at most $y$ against X at rate $1/p_0$
This occurs when the price is lower than $p_0$. Reserves are in X, blue in the example: <center/>

</center>
A MMA can therefore be in two possible states, either with X or Y reserves. It perpetually flips liquidity between two price thresholds upon market price crossing them from the inside to the outside.
### Return
For every loop $p \longrightarrow p_0^- \longrightarrow p_1^+$ or $p \longrightarrow p_1^+ \longrightarrow p_0^-$, the automaton evolves according to the path
$$
(p_0,p_1,0,y) \longrightarrow (p_0,p_1,\dfrac{x}{p_0},0) \longrightarrow (p_0,p_1, \dfrac{p_1}{p_0} x,0)
$$
or:
$$
(p_0,p_1,x,0) \longrightarrow (p_0,p_1,0,p_1 x) \longrightarrow (p_0,p_1, \dfrac{p_1}{p_0} x,0)
$$
which in both cases brings the return:
$$
\dfrac{p_1-p_0}{p_0}
$$
The optimal spacing between the two bounds is a trade-off between a higher return per loop and a higher frequency to get the return.
## Risk
Like any AMM which sells at a lower price than it buys back, the risk is that the price continues to fall after having sold or to increase after having bought back. This risk, called impermanent loss, can be visualized with a numerical example.

If the price crosses $2000$ upward (green lines), impermanent loss is the risk that the price keeps increasing. If the price crosses $1700$ downward, impermanent loss is the risk that the price keeps decreasing.
<!--
Notes: In the base layer, users can create automata in which anyone can deposit liquidity. The remaining of the note describes what can be done by strategists on the upper layer.
-->
## Combining automata
This is where it gets interesting: automata can be combined. Example with three automata:<center/>

</center>
Together, they can make up a broad set of market making strategies. You've just designed a Uniswap-V3 style AMM: <center/>

</center>
Liquidity distribution can be customized, e.g.: <center/>

</center>
The result is a fully flexible and non-parametric AMM.
## Overlapping automata
Automata can have overlapping price intervals with common or different interval lengths. Consider two strategies, one with overlapping price ranges and another one without:<center/>

</center>
Both strategies share the same assumption about the range over which the price varies, yet liquidity is distributed very differently. In strategy 2, more base assets are sold when the price decreases compared to strategy 1. As shown in the lower diagram, liquidity is posted more leftward in the first strategy. Strategy 1 bets that the price will decrease further before buying more base asset.
Strategy 1 goes into price loops less frequently but brings higher returns per loop. Strategy 1 assumes further that the price low-frequency volatility over this range will be larger than assumed by Strategy 2.
Automata with different price steps can be assembled, like in the diagram.<center/>

</center>
which is similar in spirit to depositing liquidity in two Uniswap pools in the same market, one with 0.3 fee rate and one with 1\%.
Automata with various positions and price intervals can be combined to produce more granular strategies with the aim of catching price loops of different amplitudes and price zones, as illustrated by the diagram:<center/>

</center>
Here, the bottom and middle layers are intended to catch rare but highly profitable price loops at various price zones. The top layer catches price loops at higher frequency.
### Why overlapping automata are useful?
There is a broad consensus among researchers in AMM that the fee rate or the price spacing should increase with volatility (e.g. [Guillaume Lambert](https://lambert-guillaume.medium.com/designing-a-constant-volatility-amm-e167278b5d61)). Several AMMs have already implemented this property ([Trader Joe](https://medium.com/avalancheavax/trader-joe-presents-liquidity-book-a-new-amm-design-for-defi-39abf87e0d7f), [Kyberswap](https://docs.kyberswap.com/liquidity-solutions/kyberswap-classic/concepts/flexible-fee-adjustment)).
Assume that the optimal price spacing is \$100, so that a non-overlapping price range could be
$$
[1500-1600-1700-1800-1900-2000-2100]
$$
This range decomposition is however arbitrary. We could choose instead
$$
[1450-1550-1650-1750-1850-1950-2050-2150]
$$
Actually, it is possible that the price loops this way:
$$
p \longrightarrow 1750^+ \longrightarrow 1650^- \longrightarrow 1750^+
$$
The first decomposition would miss those loops while the second one would catch them. Or, conversely, if the price loops this way:
$$
p \longrightarrow 1800^+ \longrightarrow 1700^- \longrightarrow 1800^+
$$
Strategy 1 would be more profitable. The problem is solved with overlapped automata.
This is similar to mixing Strategies 1 and 2 into a global strategy in which risks are better diversified.
Moreover, commiting to a single price step is like placing a bet on a price volatility pattern. A base step could be too narrow if volatility is high or too large if volatility is low. Instead, a strategist can diversify his risk further by combining automata with different price steps.
## Volatility sensors
Once automata of various sizes overlap, they can serve as volatility sensors for their close or remote peers. Each automaton signals a certain type of volatility every time the price completes a loop.
Consider a group of three automata in the left-hand side diagram:<center/>

</center>
The bottom automaton is a sensor of low-frequency volatility whereas the top automata are sensors of higher-frequency volatility. In the example, the former has gone through three price loops. The two top automata have gone through at least the same number of price loops plus any price loops of smaller amplitude.
The absence of smaller price loops means that it would have been profitable to move capital to the top automaton. Assuming that the volatility pattern is persistent, some amount of capital could be transferred to the bottom automaton, as illustrated in the right-hand side diagram.
In the next diagram, price loops concentrate on the left-hand side of the price interval and to a lesser extent on the whole interval. According to the same logic, capital is moved to the left-hand side top automaton.<center/>

</center>
On the other hand, if price loops reflect low volatility with few price loops over the bottom automaton, capital may flow to the top automata.<center/>

</center>
In case of automata of same interval length, the capital reallocation mechanism is the same. In the diagram, price loops concentrate on the bottom right-hand side automaton, which receives liquidity from the two other automata as a consequence.<center/>

</center>
## Strategies: From simplest to most complex
Strategists can of course mimic any Uniswap V3 strategy by assembling automata of narrow length and close to each others. But they can do much more.
### Constant volatility
Pick a type of volatility and diversify the location risk. Example with low-frequency volatility:<center/>

</center>
Location diversification is particularly useful for low frequency automata.
This type of strategies, betting on low frequency volatility, cannot be done by continuous AMMs.
### Multiple volatility patterns
Build a wall of automata spanning multiple locations and volatility frequencies. Example with four volatility patterns:<center/>

</center>
<!--This constitutes the infrastructure of the AMM, or the [protocol](https://twitter.com/euler_mab/status/1723001608810664011) part: minimalistic, immutable and permissionless. -->
### Reallocate liquidity between volatility patterns
The strategy consists in reallocating liquidity between blocks of automata, each one aiming at catching a volatility pattern:<center/>

</center>
### Concentrate liquidity where volatility patterns appear
Each time an automaton makes a profit after a price loop, the additionak liquidity is deposited in the same automaton, exposing more capital where it matters.
### Full allocation problem
This is just a sketch of more sophisticated strategies. Select liquidity at the automaton level $x_{ij}$ in X and $x_{ij}$ in Y for each brick $(i,j)$:<center/>

</center>
Liquidity is reallocated based on volatility signals transmitted by automata:<center/>

</center>
:::info
Note that the relevant signal for a market maker is not where the price is located or is going to but where price loops occur and at what frequency.
:::
The problem consists in choosing $(x_{i,j}, y_{i,j}$) based on signal intensities $(s_{i,j})$ emitted by automata.
The problem can be simplified by focusing on active automata near the market price and putting unused liquidity aside (big squarres in Diagram) :<center/>

</center>
With $n$ layers of automata, $2n+2$ liquidity allocations have to be selected at any moment.
This is still a complex problem which could be solved off-chain by machine learning.
<!-- Users deposit in vaults of various price intervals and managed by risk managers. -->
## Conclusion
MMAs are operational units which together provide an efficient framework to build market making strategies. They serve as basic bricks based on which market makers, fund managers and strategists can create customized strategies.<center/>

</center>
They present interesting properties:
- They allow composability between simpler and stand-alone strategies
- They serve as price and volatility oracles for themselves and other automata
- They are path independent (in the sense that if the price returns to its inital level, the individual strategies and therefore the global one can only earn a positive return)
- They are still path independent if liquidity is moved between blocks after the completion of a full loop.
<!--
### Appendix Density
The *density* of a strategy is the capital expressed in $X$ active for a specified range:
$$
\dfrac{x+y/p_0}{p_1-p_0}
$$
In the example, the first automaton's density is:
$$
\dfrac{80000/1400}{1600-1400} = 0.285
$$
The overall density of the whole strategy can be computed the same way:
$$
\dfrac{80000/1400 + 122500/1600 + 90 + 80 + 60}{2200-1400} = 0.789
$$
The *density ratio* of a specificed range is its density divided by the overall density.
In previous example, the first automaton's density ratio is:
$$
\dfrac{0.285}{0.789} = 0.362
$$
A ratio less than $1$ means that the price range has less capital than other price ranges on average.
The density of each interval is plotted in the graph:

The return to a full loop for each price interval is:

:::info
Example: in the ETH/USDC market, five automata can be combined:
1. ($1600$, $1750$, $0$, $122500$)
2. ($1750$, $1850$, $90$, $0$)
3. ($1850$, $2000$, $80$, $0$)
4. ($2000$, $2200$, $60$, $0$)

Market price is at 1850.
:::
:::info
Strategy 1 (overlapping)
1. ($1500$, $1900$, $0$, $90000$)
2. ($1700$, $2100$, $0$, $100000$)
3. ($1900$, $2300$, $75$, 0)
Strategy 2 (non overlapping)
1. ($1500$, $1700$, $0$, $45000$)
2. ($1700$, $1900$, $0$, $95000$)
3. ($1900$, $2100$, $67$, $0$)
4. ($2100$, $2300$, $37.5$, $0$)
:::
The second strategy is more profitable
The two strategies have different risk/return asumptions. The return of every loop in Strategy 1 is:

whereas it is in Strategy 2:

AMM gained considerable traction following the launch of Uniswap V2 in May 2020. Since then, new AMMs have popped up adding capital efficiency (Uniswap V3) and/or more sophisticated market making strategies (e.g. [Maverick](https://www.mav.xyz/) or [Ambiant](https://ambient.finance/)). Those improvements comes at at the cost of increased complexity and risk and mostly benefit to traders qualified enough.