Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in-kind redemptions fix for realised PnL #665

Merged
merged 4 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 30 additions & 66 deletions notebooks/live-strategy-analysis/analyse-balance-events.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,35 @@
"outputs": [],
"source": [
"trade_executors = [\n",
" \"https://enzyme-polygon-eth-usdc-sls.tradingstrategy.ai\",\n",
" \"https://enzyme-polygon-eth-usdc.tradingstrategy.ai\",\n",
"]\n",
"\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:26.565881Z",
"start_time": "2023-07-25T08:53:26.561871Z"
"end_time": "2023-11-20T14:15:42.895040Z",
"start_time": "2023-11-20T14:15:42.822895Z"
}
}
},
{
"cell_type": "code",
"execution_count": 2,
"outputs": [],
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: '../enzyme-polygon-eth-usdc.json'",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mFileNotFoundError\u001B[0m Traceback (most recent call last)",
"Cell \u001B[0;32mIn[2], line 4\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mpathlib\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m Path\n\u001B[1;32m 2\u001B[0m \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01mtradeexecutor\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mstate\u001B[39;00m\u001B[38;5;21;01m.\u001B[39;00m\u001B[38;5;21;01mstate\u001B[39;00m \u001B[38;5;28;01mimport\u001B[39;00m State\n\u001B[0;32m----> 4\u001B[0m state \u001B[38;5;241m=\u001B[39m \u001B[43mState\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mread_json_file\u001B[49m\u001B[43m(\u001B[49m\u001B[43mPath\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43m../enzyme-polygon-eth-usdc.json\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m\u001B[43m)\u001B[49m\n",
"File \u001B[0;32m~/code/ts/trade-executor/tradeexecutor/state/state.py:880\u001B[0m, in \u001B[0;36mState.read_json_file\u001B[0;34m(path)\u001B[0m\n\u001B[1;32m 873\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Read state from the JSON file.\u001B[39;00m\n\u001B[1;32m 874\u001B[0m \n\u001B[1;32m 875\u001B[0m \u001B[38;5;124;03m- Deal with all serialisation quirks\u001B[39;00m\n\u001B[1;32m 876\u001B[0m \u001B[38;5;124;03m\"\"\"\u001B[39;00m\n\u001B[1;32m 878\u001B[0m \u001B[38;5;28;01massert\u001B[39;00m \u001B[38;5;28misinstance\u001B[39m(path, Path), \u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mExpected Path, got \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mpath\u001B[38;5;241m.\u001B[39m\u001B[38;5;18m__class__\u001B[39m\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m\n\u001B[0;32m--> 880\u001B[0m \u001B[38;5;28;01mwith\u001B[39;00m \u001B[38;5;28;43mopen\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43mpath\u001B[49m\u001B[43m,\u001B[49m\u001B[43m \u001B[49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[38;5;124;43mrt\u001B[39;49m\u001B[38;5;124;43m\"\u001B[39;49m\u001B[43m)\u001B[49m \u001B[38;5;28;01mas\u001B[39;00m inp:\n\u001B[1;32m 881\u001B[0m \u001B[38;5;28;01mreturn\u001B[39;00m State\u001B[38;5;241m.\u001B[39mread_json_blob(inp\u001B[38;5;241m.\u001B[39mread())\n",
"\u001B[0;31mFileNotFoundError\u001B[0m: [Errno 2] No such file or directory: '../enzyme-polygon-eth-usdc.json'"
]
}
],
"source": [
"\n",
"from pathlib import Path\n",
Expand All @@ -45,8 +58,8 @@
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:27.814346Z",
"start_time": "2023-07-25T08:53:26.568632Z"
"end_time": "2023-11-20T14:15:46.296483Z",
"start_time": "2023-11-20T14:15:42.823988Z"
}
}
},
Expand All @@ -61,23 +74,8 @@
},
{
"cell_type": "code",
"execution_count": 3,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Number of reserve assets: 1\n",
"Reserves: <USDC at 0x2791bca1f2de4661ed88a30c99a7a9449aa84174> at 1.0 USDC / USD\n",
"Amount: 42.82953700000000044423131840 USDC\n",
"Equity: 42.829537 USD\n",
"Value: 42.829537 USD\n",
"Balance updates: -2.1552182261E-16 USDC\n",
"Number of balance updates events: 2\n",
"Number of events references in the portfolio overall: 1\n"
]
}
],
"execution_count": null,
"outputs": [],
"source": [
"\n",
"\n",
Expand All @@ -94,11 +92,7 @@
"print(f\"Number of events references in the portfolio overall: {len(state.sync.treasury.balance_update_refs)}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:27.818116Z",
"start_time": "2023-07-25T08:53:27.815760Z"
}
"collapsed": false
}
},
{
Expand All @@ -112,17 +106,8 @@
},
{
"cell_type": "code",
"execution_count": 4,
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vault deployment: <Deployment chain:polygon address:0x6E321256BE0ABd2726A234E8dBFc4d3caf255AE0 name:Degen Fault I token:DEGE1>\n",
"Treasury sync status: <Treasury updated:2023-07-23 18:04:40 cycle:2023-07-23 18:00:00 block scanned:45,431,463 refs:1>\n"
]
}
],
"execution_count": null,
"outputs": [],
"source": [
"treasury = state.sync.treasury\n",
"deployment = state.sync.deployment\n",
Expand All @@ -131,11 +116,7 @@
"print(f\"Treasury sync status: {treasury}\")"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:27.820762Z",
"start_time": "2023-07-25T08:53:27.818770Z"
}
"collapsed": false
}
},
{
Expand All @@ -151,42 +132,25 @@
},
{
"cell_type": "code",
"execution_count": 5,
"outputs": [
{
"data": {
"text/plain": " Cause At Quantity \\\n1 deposit 2023-07-04 19:07:40 7.165562 \n2 correction 2023-07-13 23:47:27 -7.16556200000000021552182261 \n\n Dollar value Address Notes \n1 7.165562 reinit() at block 44683317 \n2 -7.165562 Accounting correction based on the actual on-c... ",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>Cause</th>\n <th>At</th>\n <th>Quantity</th>\n <th>Dollar value</th>\n <th>Address</th>\n <th>Notes</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>1</th>\n <td>deposit</td>\n <td>2023-07-04 19:07:40</td>\n <td>7.165562</td>\n <td>7.165562</td>\n <td></td>\n <td>reinit() at block 44683317</td>\n </tr>\n <tr>\n <th>2</th>\n <td>correction</td>\n <td>2023-07-13 23:47:27</td>\n <td>-7.16556200000000021552182261</td>\n <td>-7.165562</td>\n <td></td>\n <td>Accounting correction based on the actual on-c...</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {},
"output_type": "display_data"
}
],
"execution_count": null,
"outputs": [],
"source": [
"from tradeexecutor.analysis.position import display_reserve_position_events\n",
"\n",
"df = display_reserve_position_events(position)\n",
"display(df)\n"
],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:27.829033Z",
"start_time": "2023-07-25T08:53:27.822416Z"
}
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"outputs": [],
"source": [],
"metadata": {
"collapsed": false,
"ExecuteTime": {
"end_time": "2023-07-25T08:53:27.830311Z",
"start_time": "2023-07-25T08:53:27.829469Z"
}
"collapsed": false
}
}
],
Expand Down
316 changes: 316 additions & 0 deletions notebooks/live-strategy-analysis/analyse-eth-usdc.ipynb

Large diffs are not rendered by default.

34 changes: 33 additions & 1 deletion tests/enzyme/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import logging
import pytest
from tradingstrategy.timebucket import TimeBucket
from tradingstrategy.universe import Universe

from eth_defi.enzyme.vault import Vault
from pytest import FixtureRequest

Expand All @@ -30,7 +33,8 @@
from tradeexecutor.ethereum.token import translate_token_details
from tradeexecutor.ethereum.universe import create_pair_universe
from tradeexecutor.state.identifier import AssetIdentifier, TradingPairIdentifier

from tradeexecutor.strategy.trading_strategy_universe import TradingStrategyUniverse
from tradeexecutor.testing.synthetic_exchange_data import generate_exchange

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -543,3 +547,31 @@ def pairs(weth_usdc_trading_pair, pepe_usdc_trading_pair, bob_usdc_trading_pair,
@pytest.fixture()
def multipair_universe(web3, uniswap_v2_exchange, pairs) -> PandasPairUniverse:
return create_pair_universe(web3, uniswap_v2_exchange, pairs)


@pytest.fixture()
def single_pair_strategy_universe(web3, uniswap_v2, usdc_asset, pair_universe) -> TradingStrategyUniverse:

uni_v2_exchange = generate_exchange(
exchange_id=1,
chain_id=ChainId(web3.eth.chain_id),
address=uniswap_v2.factory.address,
)

data_universe = Universe(
time_bucket=TimeBucket.d1,
chains={ChainId(web3.eth.chain_id)},
exchanges={uni_v2_exchange},
pairs=pair_universe,
candles=None,
liquidity=None
)

strategy_universe = TradingStrategyUniverse(
reserve_assets={usdc_asset},
data_universe=data_universe,
)

return strategy_universe


Loading
Loading