--- title: Trade Like an Automated Market Maker author: "Jalah" date: "2024/09/24" tags: dAMM --- # Trade Like an Automated Market Maker ## Non technical summary - Many traders try to time the market by buying low and selling high, but doing it right is hard. - This post presents EARN, a disciplined way to trade and farm price fluctuations. - Inspired from the functioning of AMMs' pricing curve, traders determine the traded amount thanks to a fixed decumulation curve. - They still retain the ability to decide when to buy and sell (or equivalently at what price). - Doing so, they are sure to make a profit every time the price loops. ## Introduction <center> ![image](https://hackmd.io/_uploads/HkoY87Y90.png =500x) </center> If, like this meme character, you trade around assets without a long term plan, you might need a trading framework. A framework imposes meaningful constraints on the way you exchange assets. It's similar to an automated trading rule, but allows you to retain some control over when or how much to trade. ## Getting inspiration from AMMs A trading framework can be quite profitable to P&L . Take the example of Automated Market Makers (AMMs), like Uniswap or Curve. In a way, AMMs are like traders: they buy and sell assets on behalf of liquidity providers with the goal of making a profit. However, their adherence to an immutable pricing curve makes their trades highly consistent. Most notably, if the price makes a round trip, AMMs always profit. Traders can learn valuable insights from how AMMs farm price fluctuations in a systematic way. Adapting for human traders the way AMMs trade is not straightforward at first glance, though, as the two operate very differently. Traders choose how much to trade at a price they don't control, while AMMs quote the price at which they are willing to buy or sell but don't control the quantity they exchange. Traders also decide when to trade, contrary to AMMs.<center> | | AMMs | Traders | EARN | | -------- | -------- | -------- | -------- | | Price | Maker | Taker | Taker | | Quantities | Taker | Maker | Taker | | Timing | Taker | Maker | Maker | </center> This post presents EARN, a framework to turn an AMMs' pricing curve into an operational trading function designed for traders. By adhering to it, they forego the capacity to choose how much to trade but retain the ability to time their trades. In exchange, they benefit from systematically farming price volatility, similar to how AMMs do.<center> ![image](https://hackmd.io/_uploads/BygMwx09C.png =450x) </center> ## Objective Our goal is to determine how much to buy or sell so that the P&L is always positive at the end of a price loop. With $p_t$ the price of asset X vs. Y at date $t$, a loop is a sequence of prices in which the first and last prices are equal: $$ p_0 \rightarrow p_1 \rightarrow ... \rightarrow p_{n-1} \rightarrow p_n = p_0 $$ A trade occurs at every price of the loop. $\Delta x_t = x_t - x_{t-1} > 0$ is how much asset X are bought and $\Delta x_t < 0$ how much are sold at date $t=1, 2, ..., n$, with $x_t$ the holding of X at date $t$. We want the P&L of the trade sequence $(\Delta x_1, \Delta x_2, ..., \Delta x_n)$, expressed in the quote asset Y, to be positive: $$ -p_1 \Delta x_1 - p_2 \Delta x_2 - ... - p_0 \Delta x_n \geq 0 $$ under the constraint that the holding of X doesn't decrease after the loop: $$ \Delta x_1 + \Delta x_1 + ... + \Delta x_n \geq 0 $$ This should be true for any price loop. ## Buying low and selling high is not always profitable Buying X when the price goes up and selling it when it goes down, as do AMMs, is a necessary step but is not sufficient. As an example, take the price loop: $$ 2000 \rightarrow 2100 \rightarrow 2500 \rightarrow 2400 \rightarrow 2000 $$ The trade sequence $(\Delta x_1, ..., \Delta x_4) = (-1, -9, 9, 1)$ sells X when the price is up and buys X when it's down, yet generates a negative P&L: $$ -9 \times 2100 - 1 \times 2500 + 9 \times 2400 + 1 \times 2000 = -1000 $$<center> ![image](https://hackmd.io/_uploads/BJ9gYc5cA.png =350x) </center> We need a bit more constraints on the way assets are traded. ## EARN, a trading framework ### Selling function We start by setting a minimum price $p_{inf}$ and a maximum price $p_{sup}$ within which all trades will take place. Similar to Uniswap V3, some extent of capital concentration is more efficient than spreading the liquidity over the whole price interval. $q(p) \in [0,1]$, defined over the price interval $[p_{inf}, p_{sup}]$, is the rate at which X is made available to sell as the price is increasing, or buy as the price is decreasing. $Q(p)$ is the decumulative of $q(p)$ or "selling function": $$ Q(p) = \int_{p}^{p_{sup}} q(p)dp $$ which is decreasing with $p$ decresing. It indicates how much asset X have to be sold at price $p$. $Q(p_{sup}) = 0$ meaning that all assets are sold when the price reaches its uppel bond. ### Trading rule The trading rule can now be stated. Suppose the last time the asset X was traded was at price $p_{t-1}$. If a new trade is decided at price $p_t$, the exchanged quantity is: $$ \Delta x_t = \int_{p_{t-1}}^{p_t} q(p) dp = Q(p_t) - Q(p_{t-1}) $$ Suppose the price increased since the last trade ($p_t > p_{t-1}$). The quantity $Q(p_{t-1})$ has already been sold so only the difference $Q(p_t) - Q(p_{t-1})$ remains to sell. Conversely, if the price decreased since the last trade ($p_t < p_{t-1}$), the quantity $Q(p_{t-1})$ sold in the past is larger than the new target $Q(p_t)$. The trader buys the difference $Q(p_t) - Q(p_{t-1})$ to restore the target. Similar to AMMs, assets are bought if the price decreases and sold if it increases. In addition, the higher the distance between the last price and the current one, the larger the quantity to buy or sell. How much larger is determined by the decumulation rate $q(p)$. ## Illustration A trader is endowed with $1$ X and $0$ Y at price 2000. His price interval is $[2000,4000]$ and rate of decumulation is constant and equal to $$ q(p) = \dfrac{1}{p_{sup} - p_{inf}} = 0.0005 $$ The decumulation function is linearly decreasing and equal to $$ Q(p) = \dfrac{p_{sup}-p}{p_{sup} - p_{inf}} = \dfrac{4000-p}{2000} $$<center> ![image](https://hackmd.io/_uploads/rk5AD6p9C.png =480x) </center> Let's illustrate with two numerical examples, one with two prices, the other with four prices. ### A two-price loop Suppose the price goes from 2500 to 3000. The amount to sell is: $$ \Delta x_1 = Q(3000) - Q(2500) = 0.5 - 0.75 = - 0.25 $$ If the price goes back to 2500, the amount to buy is: $$ \Delta x_2 = Q(2500) - Q(3000) = 0.75 - 0.5 = 0.25 $$ The P&L after the price loop is positive and equal to: $$ -p_1 \Delta x_1 -p_2 \Delta x_2 = 3000 \times 0.25 - 2500 \times 0.25 = 125 $$ ### A four-price loop Suppose that we trade the price loop $$ 2000 \rightarrow 2100 \rightarrow 2500 \rightarrow 2400 \rightarrow 2000 $$ which is the loop of the first example in which the trader lost money despite buying the depreciating asset. The P&L after the price loop is $-\sum_{t=1}^4 p_t \Delta x_t$: \begin{aligned} -& 2100 \times \big( Q(2100) - Q(2000) \big) - 2500 \times \big( Q(2500) - Q(2100) \big) \\ -& \; 2400 \times \big( Q(2400) - Q(2500) \big) - 2000 \times \big( Q(2000) - Q(2400) \big) \\ =& \; - 2100 \times (0.95 - 1) - 2500 \times (0.75 - 0.95) \\ -& \; 2400 \times (0.8 - 0.75) -2000 \times (1 - 0.8) \\ =& \; 105 + 500 - 120 - 400 = 85 \end{aligned} which is now positive. ## Farming Volatility These two examples beg a central question: Are we guaranteed to make a positive profit for *any* price loop of *any* length and with *any* decumulation rate $q(p) \in (0,1)$? The answer is positive. This result is important. Similar to AMMs, traders can't lose money by trading a price loop. More broadly, it means that as long as the price doesn't exit the trading interval, the chance of losing money tends to zero over time. Profits build up with the number of price loops and buy-low/sell-high trades. The broader the price interval, the lower the risk of the price escaping the interval, but the slower the profits accumulate. The risk/return trade-off is very similar to what liquidity providers experience in Uniswap V3. There are notable differences, though. In Uniswap V3, the larger the trade, the higher the price impact. This is reversed in EARN: the higher the price difference between two trades, the larger the quantity traded. In an AMM, the invariant is an xyk-type pricing curve. In EARN, it is the decumulation rate $q(p)$. Second, the frequency of trades and exchanged quantities are beyond the control of AMMs. In EARN, the trader can decide when to trade (or equivalently at what price) and try to time the market. Third, similar to how a small fee rate introduces a gap between the buying and selling prices, traders earns by selling at a higher price than they buy. However, they can select a larger spread than the fee rate applied by AMMs. ## Conclusion Akin to what liquidity providers experience in AMM pools, users of EARN buy the asset when price is decreasing and sell it when the price is increasing. Both actions are done in a way that guarantees a profit every time a price loop is traded. A risk of impermanent loss exists, especially if the price exits the range. However, unlike liquidity providers, traders are granted more flexibility. They can determine the rate $q(p)$ at which they sell the appreciating asset. They can choose the spread they trade on, and they can time the market to try to sell the top and buy the bottom. <center> ![image](https://hackmd.io/_uploads/SJL62k09A.png =380x) </center> <!-- which tends to overstate the Better proof: add prices at which the trader trades. Example: 5 -> 3 (buy) -> 1 (buy) -> 4 (sell) -> 7 (sell) -> 5 (buy) becomes: 5 -> 4 buy -> 3 (buy) -> 1 (buy) -> 3 (sell) -> 4 (sell) -> 5 (sell) -> 7 (sell) -> 5 (buy) groups: 1 buy and 3 sell 3 buy and 4 sell 5 buy and 7 sell 4 buy and 5 sell To understand how to implement a similar strategy for humans, let's take a market with two assets ETH and DAI. The price of ETH/DAI is $p$. When the price increases from $p_0$ to $p_1$ then returns back to $p_0$, the trader sells $\delta_0 >0$ ETH at price $p_1$, then buys back $\delta_1>0$ ETH at price $p_0$. He pockets a sure profit if his holdings of ETH and DAI have not decreased and at least one of the two has increased: \begin{aligned} \Delta x_0 - \delta_1 \geq 0 \\ -\delta_0 p_1 + \delta_1 p_0 \geq 0 \end{aligned} with at least one of the two inequality is strict. $$ \dfrac{p_1}{p_0} \leq \dfrac{\delta_1}{\delta_0} \leq 1 $$ ## Path independence Beyond the wide variety of pricing curves used by AMM, a key characteristic is path independence (Buterin, 2017, Angeris and Chitra, 2020). Let's explain the property with an example. Suppose an AMM holds the quantities $x$ and $y$ of ETH and Y of DAI and that the price of ETH/DAI goes into a loop, e.g. goes from 3000 to 3500 then returns to 3000. Path independence means that, at the end of the loop, the AMM cannot lose assets. It holds at least $x' \geq x$ ETH and $y' \geq y$ DAI. Remarkably, this result doesn't depend on how much exchanges and at which price the AMM does with traders, or whether the price makes 1 or 10 loops before returning to its initial level. The AMM takes back its initial inventories whatever the price and trade history. Path independence means that (the loop could be first decreasing or encompass sub-loops without loss of generality) This strategy is an example of volatility harvesting because the returns compound with price fluctuations. $delta x$ of its ETH are exchanged against $delta y$ of DAO in an arbitrary number of trades. A pricing curve is path independent if each exchange rate is associated with an invariant quantity of the two assets available in the pool. It follows that every time the exchange rate returns to a previous level, the quantities of assets in the pool are also restored. This is true whatever the historical path followed by the price, hence the expression. This property is an important safeguard for AMMs which exchange rate is set without knowledge of the external market price. This extreme case of asymmetric information (Kyle, 1985) protects LPs from being out-traded by informed traders and arbitrageurs as long as the price goes back and forth. They act like a two-sided platforms in which traders interact with liquidity providers (LPs) through a pricing curve which determines the price at which the two assets in the pool are exchanged. adapted definition: traders don't trade on a constant function, and make profits Main benefits: properly set, you can't mix up and buy-high / sell-low Fixed price interval: similar to Uniswap V3 But can do better: this is trading by forming expectations on future price interval, price direction. You can reset your expected price interval every completed two-way trade. Think of a trading framework as a house. It limits how you can move inside, but still lets you choose which room to enter. So, let's build a robust house for your trading strategy.<center> ![image](https://hackmd.io/_uploads/S1jou6KcC.png =450x) </center> By buying the depreciating asset and selling the appreciating one, we get the foundations of the house, but still need a few more walls inside. -->