Skip to content

Commit

Permalink
Add Demeter adapter (#1055)
Browse files Browse the repository at this point in the history
- Allow us to use Demeter Uniswap LP backtesting framework with data from Trading Strategy client: `tradeexecutor.strategy.demeter` module
- Note that currently dependency resolution does not work because `zelos-demeter` requires different Python and package versions as we do, but those are only specification conflicts and not real conflicts
  • Loading branch information
miohtama authored Oct 6, 2024
1 parent f1262ff commit 681baf1
Show file tree
Hide file tree
Showing 10 changed files with 2,019 additions and 1,832 deletions.
3,667 changes: 1,842 additions & 1,825 deletions poetry.lock

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ packages = [
{ include = "tradeexecutor" },
]
[tool.poetry.dependencies]
python = ">=3.10,<=3.12"
python = ">=3.11,<=3.12"


# Use these during development
Expand Down Expand Up @@ -108,6 +108,13 @@ google-auth = {version="^2.32.0", optional = true}
zstandard = "^0.23.0"
pytest-timeout = "^2.3.1"

#
# Demeter
# https://github.com/zelos-alpha/demeter
#

# TODO: Disabled. Install wiht pip until dependency version incompatibilies are solved.
# zelos-demeter = {version="^0.7.2", optional = true}

[tool.poetry.extras]

Expand Down Expand Up @@ -148,6 +155,11 @@ trendsspotter = [
# for generating advanced statistical reports
# quantstats = ["quantstats"]

# Demeter LP backtesting integration
demeter = [
"zelos-demeter",
]

[tool.poetry.dev-dependencies]
pytest = "^7.2.2"
ipdb = "^0.13.9"
Expand Down
2 changes: 1 addition & 1 deletion tests/mainnet_fork/test_enzyme_guard_credit_positions.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,5 @@ def test_enzyme_guard_credit_positions(
state: State = State.from_json(inp.read())
assert len(list(state.portfolio.get_all_trades())) == 2 # supply aPolUSDC, withdraw aPolUSDC

assert sync_interests.call_count == 5
assert sync_interests.call_count >= 5 # Flaky: Sometimes 6?

1 change: 1 addition & 0 deletions tradeexecutor/analysis/trade_analyser.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ def calculate_summary_statistics(

daily_returns = calculate_daily_returns(state, freq="D")
equity_curve = calculate_equity_curve(state)

original_returns = calculate_returns(equity_curve)
compounding_returns = calculate_compounding_realised_trading_profitability(state)

Expand Down
149 changes: 149 additions & 0 deletions tradeexecutor/strategy/demeter/adapter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"""Demeter integration functions.
- See https://github.com/zelos-alpha/demeter
- Do not import this module unless you have Demeter installed
"""
import logging
import datetime

try:
import demeter
except ImportError as e:
raise ImportError("Demeter framework not installed, see https://github.com/zelos-alpha/demeter")

import pandas as pd

from demeter import TokenInfo
from demeter.uniswap import UniV3Pool, UniLpMarket
from demeter.uniswap.data import fillna

from tradeexecutor.state.identifier import AssetIdentifier, TradingPairIdentifier
from tradingstrategy.pair import DEXPair
from tradingstrategy.token import Token


logger = logging.getLogger(__name__)


def to_demeter_token(asset: AssetIdentifier | Token) -> TokenInfo:
"""Adapt Trading Strategy asset types to Demeter TokeInfo type."""
match asset:
case AssetIdentifier():
return TokenInfo(
name=asset.token_symbol,
decimal=asset.decimals,
address=asset.address,
)
case Token():
return TokenInfo(
name=asset.symbol,
decimal=asset.decimals,
address=asset.address,
)
case _:
raise NotImplementedError(f"Unsupported asset type: {asset.__class__}")


def to_demeter_uniswap_v3_pool(trading_pair: TradingPairIdentifier | DEXPair) -> UniV3Pool:
"""Adapt Trading Strategy trading pair types to Demeter pool type."""

match trading_pair:
case TradingPairIdentifier():
base = to_demeter_token(trading_pair.base)
quote = to_demeter_token(trading_pair.quote)
pool = UniV3Pool(token0=base, token1=quote, fee=trading_pair.fee, quote_token=quote)
return pool
case DEXPair():
base = to_demeter_token(trading_pair.get_base_token())
quote = to_demeter_token(trading_pair.get_quote_token())
fee = trading_pair.fee_tier * 100 # 0.05 = 5 bps in Demeter
assert fee > 0
pool = UniV3Pool(
token0=base,
token1=quote,
fee=fee,
quote_token=quote
)
assert pool.tick_spacing != 0
return pool

case _:
raise NotImplementedError(f"Unsupported trading pair type: {trading_pair.__class__}")


def load_clmm_data_to_uni_lp_market(
market: UniLpMarket,
df: pd.DataFrame,
start_date: datetime.datetime,
end_date: datetime.datetime,
):
assert isinstance(market, UniLpMarket)
assert isinstance(df, pd.DataFrame)

logger.info(
"Loading data to Demeter market %s, entries %d",
market,
len(df)
)

#
# Remap columns
#

# columns = [
# "timestamp",
# "netAmount0",
# "netAmount1",
# "closeTick",
# "openTick",
# "lowestTick",
# "highestTick",
# "inAmount0",
# "inAmount1",
# "currentLiquidity",
# ]

# Index(['pair_id', 'bucket', 'open_tick', 'close_tick', 'high_tick', 'low_tick',
# 'current_liquidity', 'net_amount0', 'net_amount1', 'in_amount0',
# 'in_amount1'],

df = df.rename(columns={
"bucket": "timestamp",
"close_tick": "closeTick",
"open_tick": "openTick",
"low_tick": "lowestTick",
"high_tick": "highestTick",
"in_amount0": "inAmount0",
"in_amount1": "inAmount1",
"current_liquidity": "currentLiquidity",
"net_amount0": "netAmount0",
"net_amount1": "netAmount1",
})

del df["pair_id"] # Fails in fillna() below

#
# Following is copy paste from market.py from Demeter
# I have no idea what it is supposed to do
#

df["timestamp"] = pd.to_datetime(df["timestamp"])
df.set_index("timestamp", inplace=True)

# fill empty row (first minutes in a day, might be blank)
full_indexes = pd.date_range(
start=start_date,
end=datetime.datetime.combine(end_date, datetime.time(0, 0, 0)) + datetime.timedelta(days=1) - datetime.timedelta(minutes=1),
freq="1min",
)
df = df.reindex(full_indexes)
# df = Lines.from_dataframe(df)
# df = df.fillna()
df: pd.DataFrame = fillna(df)
if pd.isna(df.iloc[0]["closeTick"]):
df = df.bfill()

market.add_statistic_column(df)
market.data = df

7 changes: 7 additions & 0 deletions tradeexecutor/strategy/trading_strategy_universe.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,13 @@ def create_from_dataset(
stop_loss_candle_universe = None

if dataset.liquidity is not None:

if isinstance(dataset.liquidity.index, pd.MultiIndex):
# The hack we had to do to in CachedHTTPTransport.fetch_tvl_by_pair_ids() because
# for some reason pd.concat() kept failing on Github.
# Goes from (pair_id, timestamp) -> (timestamp) index
dataset.liquidity = dataset.liquidity.reset_index(level=0, drop=True)

assert isinstance(dataset.liquidity.index, pd.DatetimeIndex), f"Got {dataset.liquidity.index.__class__}"
liquidity_universe = GroupedLiquidityUniverse(
dataset.liquidity,
Expand Down
3 changes: 2 additions & 1 deletion tradeexecutor/visual/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ def visualise_equity_curves(
- colour: Plotly colour name
- curve: Equity curve type
- curve: Equity curve type. See :py:class:`CurveType`.
:param height:
Height in pixels
Expand Down Expand Up @@ -832,6 +832,7 @@ def visualise_equity_curves(

return fig


def visualise_portfolio_interest_curve(
name: str,
state: State,
Expand Down
4 changes: 2 additions & 2 deletions tradeexecutor/visual/equity_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ def calculate_returns(equity_curve: pd.Series) -> pd.Series:

if len(equity_curve) == 0:
series = pd.Series([], index=pd.to_datetime([]), dtype='float64')

series = equity_curve.pct_change().fillna(0.0)
else:
series = equity_curve.pct_change().fillna(0.0)

series.attrs = equity_curve.attrs.copy()
series.attrs["curve"] = CurveType.returns
Expand Down

0 comments on commit 681baf1

Please sign in to comment.