This repository contains several python scripts to simulate the performance of Uniswap v3 liquidity provision strategies performance and evaluate risks. The main scripts of the package are:
- ActiveStrategyFramework.py base code of the framework which executues a strategy, conducting either back-testing simulations (
simulate_strategyfunction and passing in historical swap data), or conducting a live implementation of the strategy. - ML_Strategy.py strategy uses an AR(1)-GARCH(1,1) model to predict volatility alongside with XGBoost to predict base asset price movement.
- GetPoolData.py which downloads the data necessary for the simulations.
- UNI_v3_funcs.py python implementation of Uniswap v3's liquidity math.
In order to provide an illustration of potential usage, i have included Jupyter Notebook that show how to use the framework:
Have constructed a flexible framework for active LP strategy simulations that uses the full Uniswap v3 swap history in order to improve accurracy of fee income. Thefore simulations are available in the time period since Unsiwap v3 was released (May 5th 2021 is when swap data starts to show up consistently).
For live performance i choosed pattern with:
- Every week XGBoost model retrained on new coming data
- Every 10 min we predict and reset strategy
To see instructions see airflow_mlflow.md
The Graph + Bitquery + Flipside Crypto
The pattern to use these data sources can be seen in 2_AutoRegressive_Strategy_Example.ipynb. The data sources are:
- The Graph: We obtain the full history of Uniswap v3 swaps from whatever pool we need, in order to accurately simulate the performance of the simulated strategy.
- Bitquery: We obtain historical token prices from Uniswap v2 and v3.
- Flipside Crypto: We obtain the virtual liquidity of the pool at every block, which is used to approximate the fee income earned in the pool, as described in their documentation.
Instructions
- Obtain a free API key from Bitquery.
- Save it in a file in
config.pyin the directory where the ActiveStrategyFramework is stored as a variable calledBITQUERY_API_TOKEN(eg.BITQUERY_API_TOKEN = XXXXXXXX). - Generate a new Flipside Crypto query, with the
pool_addressfor the pair that you are interested. Note that due to a 100,000 row limit, we generate two queries for the USDC/WETH 0.3%, which explains theBLOCK_IDcondition, to split the data into reasonable chunks. A less active pool might not need this split.