Mellow backtesting SDK & datasets
The DeFi landscape is rapidly becoming more complex. In terms of products and structure, it is becoming similar to the TradFi ecosystem, only built on fundamentally different rails: transparency, control of your funds, and privacy.
However, the DeFi ecosystem is still at the very early stages (like a 4-year-old kid), and some parts are still missing. One of these missing (or almost missing) parts is the analytical tools for DeFi space. Ofc you can say there is Dune and Nansen, which can be used as BI solutions, and Gauntlet for stress tests and simulations. But these tools can’t fill all the needs in DeFi analytics. Modeling and testing strategies are one of those places where analytic tools can be very useful.
Here at Mellow, we are building a platform that we hope can address this need and enable the creation of economically viable and validated asset management strategies. We already use this tool by ourselves to research market structure and look for highly profitable strategies. But Web3 is about open solutions. So today, we want to present you the Mellow SDK — an open-source (now!) solution for developing asset management strategies in DeFi. This SDK will allow to weigh the risk of strategies and their potential returns and, ultimately, allow for the creation of more structured products. And everyone will benefit from this — strategists, liquidity providers, and the entire market.
Blockchain systems are at the intersection of technology and finance. While we believe that blockchain technology opens up a new paradigm shift in the way people interact, we are also convinced that it creates a new set of challenges not found in traditional finance:
- Transaction costs (even though gas prices in 2022 fell compared to 2021, it is imperative to consider gas costs and other losses when making transactions)
- In distributed and open networks, the behavior of the system and the experience of using the software is determined not only by the strategist. There are a lot of different effects that should be taken into account while building a strategy: other participants’ actions, hacker attacks, and market manipulations (JIT attacks, flash loan attacks, etc.)
- It is necessary to reproduce invariants and other algorithm parts that are often used in existing DeFi protocols (with the growth of Uniswap seems everyone knows x*y=k)
We at Mellow want to develop new standards that respond to these challenges and can form the basis for creating new and better strategies in DeFi.
Mellow SDK is an analytical tool that allows you to evaluate the profitability and risks of strategies based on historical data. The main focus of the Mellow SDK is functionality for working with DeFi tools, but it can also work with CeFi crypto services.
Using the Mellow SDK, you can create your algorithmic strategy for managing a portfolio that includes UniV3 positions, currency pairs, and deposits. The analysis tools implemented in the SDK allow you to calculate the performance metrics of your strategy and visualize them (for example, APY, impermanent loss, collected fees, portfolio value, etc.). Note that the framework is flexible for customization; you can add your own metric to the calculation.
The Mellow SDK allows you to use actual UniswapV3 pool data from Mellow Open Data storage, generate synthetic data based on Geometric Brownian Motion, or use your Binance API to download data from Binance.
In this section, we will consider three strategies that allow you to evaluate the capabilities of the platform:
- LP-ing to Uniswap V2
- Passive LP-ing to Uniswap V3
- Active LP-ing to Uniswap V3
LPing to Uniswap V2
The visualization shows portfolio growth and APY you get.
LPing to Uniswap V3
You can select a price range to deposit funds in Uniswap and look at how it behaves on historical data. The issue here is that you know actual price fluctuations to predict prices. For example, you can get range [15,16] for WBTC/WETH pair and backtest this strategy:
The knowledge of future prices opens you great opportunity to grow your wealth. How sad that it works only on backtest🧐
Active LPing in Uniswap V3
You can say strategies that do nothing are boring and be right. So let’s build an active strategy.
For example, when the price is 60 minutes outside the interval, the strategy withdraws funds and puts it to a new very narrow interval — (price-0.3, price+ 0.3). That is, the interval will constantly chase the price. It is pretty clear that such a strategy can go bankrupt due to the cost of gas, so we put
gas_cost=0 (what a joke! But it’s only a showcase).
You can see here how the interval changed, but unfortunately, this strategy did not get income even with 0 transaction costs since impermanent loss always stayed higher than earned fees:
How to work with SDK
This section will briefly describe how to work with the Mellow SDK. An example of a Jupiter notebook can be found on our GitHub.
We also recommend checking our documentation, where you can find more explanations, tutorials, and how to get started. And ofc ask any questions on Discord.
Create a virtual environment and install the package.
python3 -m venv .venv
pip install mellow_strategy_sdk
Choose WBTC/WETH 0.3% pool
POOLS — dictionary with pools available in SDK. For example, choose WBTC/WETH pool with middle fees.
pool_num = 1
pool = Pool(
Usually, extracting data correctly from blockchain has a lot of troubles (for example, the Graph has some gaps in the data, which can be critical for strategies backtest). Fortunately, we have already done that job for a large set of pools — you can simply download and use them.
Let’s load data from Mellow Open Data. The data consists of transactions (mint, burn, swap events) with UniswapV3. The data is updated once a day.
data = RawDataUniV3(pool=pool, data_dir='data').load_from_folder()
Data contains three data frames — data.mints, data.swaps, data.burns. Respectively, historical data on mint (deposit), burn (withdraw), and swap operations performed in the pool.
Use standard strategy
The strategies.py file contains some ready-made strategies that can be used as tutorials:
- Hold — buy & hold strategy
- UniV3Passive — mint to specified interval&wait
- StrategyByAddress — backtests actions in the pool at the wallet address
As the V2 interval corresponds to V3 (0, ∞), you can use UniV3Passive for V2 to
v2_strat = UniV3Passive(
The backtest sequentially passes historical data at each step feeding it to the input of the strategy, after which it takes a snapshot of the portfolio.
By the end of the backtest, we could remember the state of the portfolio at each point in time.
bt = Backtest(strategy=v2_strat)
portfolio_history, rebalance_history, uni_history = bt.backtest(df=data.swaps)
The Grand Plan
Our goal is to complete the ecosystem with universal vaults that everyone can use to build any strategies they want and deploy using Mellow infrastructure.
Initially, we developed the SDK for internal use, and now we make it public so every builder can use it for backtesting his strategies and ensuring that he’s on the right track.
We want to help the community build better products and strategies, and we’ll be happy to discuss your ideas or answer any questions on our Discord or Twitter.
Come! Build! Earn!