Metadata-Version: 2.1
Name: mellow-strategy-sdk
Version: 0.1.0
Summary: Framework for creating new Uniswap V3 strategies
Home-page: https://mellow.finance/
License: MIT
Author: Alex K
Author-email: ak@mellow.finance
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: PyYAML (>=6.0,<7.0)
Requires-Dist: SQLAlchemy (>=1.4.31,<2.0.0)
Requires-Dist: autodoc (>=0.5.0,<0.6.0)
Requires-Dist: boto3 (>=1.21.24,<2.0.0)
Requires-Dist: kaleido (==0.2.1)
Requires-Dist: myst-parser (>=0.17.0,<0.18.0)
Requires-Dist: nbformat (>=5.2.0,<6.0.0)
Requires-Dist: numpy (>=1.22.0,<2.0.0)
Requires-Dist: pandas (>=1.3.5,<2.0.0)
Requires-Dist: parameterized (>=0.8.1,<0.9.0)
Requires-Dist: plotly (>=5.5.0,<6.0.0)
Requires-Dist: poetry (>=1.1.13,<2.0.0)
Requires-Dist: polars (>=0.13.2,<0.14.0)
Requires-Dist: pyarrow (>=7.0.0,<8.0.0)
Requires-Dist: python-binance (>=1.0.15,<2.0.0)
Requires-Dist: sphinx-autodoc-typehints (>=1.17.0,<2.0.0)
Requires-Dist: sphinx-autodocgen (>=1.3,<2.0)
Requires-Dist: sphinx-rtd-theme (>=1.0.0,<2.0.0)
Requires-Dist: sphinxcontrib-napoleon (>=0.7,<0.8)
Requires-Dist: structlog (>=21.5.0,<22.0.0)
Requires-Dist: update (>=0.0.1,<0.0.2)
Project-URL: Documentation, https://mellow-strategy-sdk.readthedocs.io/en/main/
Project-URL: Repository, https://github.com/mellow-finance/mellow-strategy-sdk
Description-Content-Type: text/x-rst

SDK for creating new strategies on Uniswap V3

.. code-block:: bash

    git clone https://github.com/mellow-finance/mellow-strategy-sdk.git
    cd mellow-strategy-sdk
    python3 -m venv .venv
    source .venv/bin/activate
    pip install poetry
    poetry install

or

.. code-block:: bash

    pip install mellow_strategy_sdk


Getting Started
==============================


Import
~~~~~~~~~~~~

.. code-block:: python

    from mellow_sdk.primitives import Pool, POOLS
    from mellow_sdk.data import RawDataUniV3
    from mellow_sdk.strategies import AbstractStrategy
    from mellow_sdk.backtest import Backtest
    from mellow_sdk.viewers import RebalanceViewer, UniswapViewer, PortfolioViewer
    from mellow_sdk.positions import BiCurrencyPosition, UniV3Position

Choose a pool
~~~~~~~~~~~~~~~~

A typical notebook would start with downloading and preparing data for a specific pool.
``POOLS`` is a list of available pools, let's choose 1 it is WBTC/WETH, fee 0.3%

.. code-block:: python

    pool_num = 1
    my_pool = Pool(
        tokenA=POOLS[pool_num]['token0'],
        tokenB=POOLS[pool_num]['token1'],
        fee=POOLS[pool_num]['fee']
    )

Get data
~~~~~~~~~~~~

Аt the first run you need to download the data

.. code-block:: python

    data = RawDataUniV3(pool, 'data', reload_data=False).load_from_folder()


Use implemented strategy
~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: python

    univ3_passive = UniV3Passive(
        lower_price=data.swaps['price'].min() - 1,
        upper_price=data.swaps['price'].max() + 1,
        pool=pool,
        gas_cost=0.,
        name='passive'
    )


Backtest
~~~~~~~~~~~~

Next step is to run backtest using your strategy and data

.. code-block:: python

    bt = Backtest(univ3_passive)
    portfolio_history, rebalance_history, uni_history = bt.backtest(data.swaps)

Visualize
~~~~~~~~~~~~

Next visualize results

.. code-block:: python

    rv = RebalanceViewer(rebalance_history)
    uv = UniswapViewer(uni_history)
    pv = PortfolioViewer(portfolio_history, pool)

    # Draw portfolio stats, like value, fees earned, apy
    fig1, fig2, fig3, fig4, fig5, fig6 = pv.draw_portfolio()

    # Draw Uniswap intervals
    intervals_plot = uv.draw_intervals(data.swaps)

    # Draw rebalances
    rebalances_plot = rv.draw_rebalances(data.swaps)

    # Calculate df with portfolio stats
    stats = portfolio_history.calculate_stats()

If you have a powerful pc and a good connection you can remove render='svg'

.. code-block:: python

    intervals_plot.show(render='svg')

.. image:: https://raw.githubusercontent.com/mellow-finance/mellow-strategy-sdk/main/examples/getting_started_intervals.png


.. code-block:: python

    rebalances_plot.show(render='svg')

.. image:: https://raw.githubusercontent.com/mellow-finance/mellow-strategy-sdk/main/examples/getting_started_rebalances.png

.. code-block:: python

    fig2.show(render='svg')

.. image:: https://raw.githubusercontent.com/mellow-finance/mellow-strategy-sdk/main/examples/getting_started_fig2.png

.. code-block:: python

    fig4.show(render='svg')

.. image:: https://raw.githubusercontent.com/mellow-finance/mellow-strategy-sdk/main/examples/getting_started_fig4.png

.. code-block:: python

    fig6.show(render='svg')

.. image:: https://raw.githubusercontent.com/mellow-finance/mellow-strategy-sdk/main/examples/getting_started_fig6.png

Congratulations! Now you have the results of your strategy backtest on the real UniV3 data!

