Uniswap V3 & Hummingbot

Uniswap V3 & Hummingbot

by Paulo H. B. Rebouças

At the beginning of this month, the DeFi space received some great news: Uniswap V3 would be launched.

This was exciting news for anyone that is exploring the possibilities Decentralized Finance apps are creating.

What caught our attention on the Uniswap V3 announcement was the possibility to create different positions at different price ranges, making it resonate a lot with how traditional market-making strategies work.

And immediately, we started building!

Our team started to brainstorm the exciting ways we could make hummingbot interact with Uniswap V3, opening many possible strategies.

Last week we were pretty happy to show what can be done on the first version of the strategy. If you are more of a video person, make sure to check the live stream recording to see what we have achieved so far.

But you prefer to read a written guide, stay with me here to learn how the strategy works, start using it, and the plans of improvement.

How the Uniswap V3 strategy works

DISCLAIMER: The strategy is still in development, so use it at your own risk since some major bugs that our team still isn’t aware of might exist. None of the examples provided in this article are financial advice, so do your own research before doing any trade operation.

The first thing you need to understand is that Uniswap V3 is about managing different liquidity positions.

The first implementation of our hummingbot strategy aims to simulate the usual behavior of the `pure_market_making` strategy: Create opposing buy and sell orders around the mid-price, looking to capture the spread as the price moves around a specific range.

The key difference here is that, instead of profiting from capturing the order spreads, you will be receiving fees when a trade happens inside the range you are providing liquidity.

To do that, the first step is to choose a price range where liquidity provider expects the market to be trading. We could, for example, take the last high and low prices of the ETH-DAI pair:

79E3Wgr9PMk-tYscOQaJ7OUsL3dQUIpFcFU7wZvIHQgC55HZTVeqlsBOTcJKoouooF-rB hL6JXAIxFk-tBf31yEW40z6 OcZaIq3GFw=s1339

ETH-DAI trading pair

If you want to provide liquidity on Uniswap V3 manually, you just have to enter the lower and upper price range you want to provide liquidity and forget about it.

As long as the market is trading between these prices, you will receive trading fees proportional to your provided liquidity.

2-7CWUxRlfprIFB5lf5uwGeEsCwtVVu0cQaPok9fayREvw6PBwoU9cXdlVt E4n5zR8Qv1KbP98tQq i6Gm-fv s6Q0JynECEwSXi5fCVXkAPsWN42xUXG9vuxMWA2U5vZ1lhssA

ETH-DAI pool

For the automated strategy we implemented on hummingbot, we changed things a little.

Instead of creating a single position covering the entire price range, the bot will create two liquidity positions: One above (sell position) and one below (buy position) of the current market price.

Here is what it looks like on the chart:

PUZoL2jZGZ4B4t X1DK1Gtj4s2H8MUaJxdFW0foMbq5aJodffnvVWIiKqInvMTkouX2VTJhdLz-8EOAsNgdmQhbj6Gojb9PMxDLTEpE16arOxDnwrycv2N8acyDzSt00Nv PAOwh

ETH-DAI hummingbot positions

At first, this might seem a bit counterintuitive, since creating two positions will double the transaction gas cost, but starting the strategy with this model:

  • You can add different token amounts on each side
  • You can manage each side of the trade separately, allowing more flexibility to repositions
  • It simplifies the calculation of the size of each new position.

After the positions are created, every time a trade happens on the SELL side, that position will collect trading fees. The same is true for the buy position.

So, what happens when the trades start to happen outside the starting range?

Your positions will stop collecting fees, and they will become inactive until the market price goes back to the start range.

At that point, the liquidity provider has a decision to make: Change the starting position to a new range, or leave as it is, hoping the price will move back?

With hummingbot, once that happens (price moves out of the starting range), the most distant liquidity position will be moved to cover a new range.

What happens is the following logic when the price moves outside of the top price range:

  1. Price moves above the top price range
  2. The starting SELL position is now renamed as the BUY position
  3. Starting BUY position is removed from the pool
  4. The remaining tokens are 100% swapped for the opposing token
  5. A new SELL position is created with the traded tokens above the current market price
Jz3iTuHwsXN-pWvk8X9NXH9u9uii2NqUZKG3RgP-099TDFN0E6CLs0XKXwexq89J64On4bjlaer6-F6ZaLBDTJtHQXwznbYILZej9TF9Qi8yyyKslJlldwWyv1U-v87DPi9PswNy

Hummingbot changing positions

And that’s it! Keep hummingbot running, and it will monitor the pool price and change positions accordingly.

One final observation:

Be aware of transaction gas costs. For example, if you set the starting position range too small, the bot might end up removing and adding liquidity positions too often, and it might end up eating your profits.

How to start using the Uniswap V3 strategy

As I mentioned before, our team is still working on improving this strategy, so if you decide to try it out, be aware of the possibility of undiscovered bugs and behaviors. Because of that, you won’t find it on the latest (0.39) hummingbot version.

But if you want to try it out and experiment with it, you have to run hummingbot from source on the `feat/uniswap_v3` branch.

To install from source, you can find the proper instructions on our documentation page about installation.

You will also need the gateway API running from source, so make sure you also follow the instructions on our documentation page.

To change branches after pulling the source code, run the following command to change to the correct branch:

git checkout feat/uniswap_v3

After configuring and starting hummingbot on the right branch, you will be able to see the new strategy when entering the create command:

5oFrpJnWJoN-0ayu1CbBHlzrY3-ZVnku3kRS7uL2AqJA-EHemR5NeqQZTVpuEF-R072gfp3uYzDz5B5YatPHzc7j2fAdhS1NTTGn0ly4hGSP54WQKqinaSokc6GClJ3KfYoStuXN

Uniswap V3 Strategy creation

To proper configure the strategy, you will have to enter the following configurations (The exact questions asked might change in the future):

What is the pair you would like to provide liquidity?

Two critical observations here:

  1. If you want to provide liquidity on ETH pairs, you have to wrap your ETH into WETH tokens.
  2. On Uniswap V3, for the contract to find the proper pool, the order of the tokens matter. Ensure to check on https://info.uniswap.org/home#/pools what is the correct order of the tokens (i.e., for ETH-DAI pool, you will have to enter DAI-WETH). Our team is already working on improving this configuration to be possible to find the right pool with any token order)
What is the fee tier of the pool?

On Uniswap V3, liquidity pools can be created using one of the following fee structures: 0.05%, 0.3%, 1%.

Each fee structure is a different pool, so make sure to also check on https://info.uniswap.org/home#/pools what is the pool you want to provide liquidity.

What is the spread (in %) for the upper range?

The bot will set the upper limit of the provided liquidity based on the percentage value set here. The final price of the upper price will be defined by `current_price + current_price * spread`

What is the spread (in %) of the lower range?

Same as before, but here you set what will be the spread from the current market price to the lower limit of the positions.

How much of the (base/quote) token you want to use?

Here you set the amount for each position. The base is the first token you entered on the pair, and the quote is the second token. So, for For example, on the DAI-WETH pair, DAI is the base token, WETH is the quote token.

Now, all you have to do is enter startto create the first liquidity positions.

The road ahead (or what improvements to expect)

The Uniswap V3 protocol is fascinating. It allows a lot of different possible approaches when providing liquidity.

While this is the first version of the strategy, we have set some plans on how to improve it:

  • Track positions “profit”
  • Use positions profit to decide when to move a position
  • Use volatility to set better price ranges
  • Create and manage more than two liquidity positions
  • Use better ways to distribute the liquidity on different price ranges

We also value our community feedback, so if you have any idea or suggestions on how we could make this strategy better, feel free to reach our team on our Discord or create a new issue on our Github page.

Happy trading!