Skip to content

Commit

Permalink
improve calc_pnl functionality (#1207)
Browse files Browse the repository at this point in the history
- add `calc_pnl` flag to turn off PNL calculation during an experiment, useful only using terminal values in analysis
- value LP and Withdrawal Shares at `lp_share_price` without any smart contract calls
  • Loading branch information
wakamex authored Jan 10, 2024
1 parent fc11765 commit 8bba851
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 467 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Environment variables
*.env

# Data
*.csv
*.parquet
*.png

# Weights and biases
wandb

# user configs
.configs

Expand Down
15 changes: 10 additions & 5 deletions lib/agent0/agent0/hyperdrive/interactive/interactive_hyperdrive.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
from chainsync import PostgresConfig
from chainsync.dashboard.usernames import build_user_mapping
from chainsync.db.base import add_addr_to_username, get_addr_to_username, get_username_to_user, initialize_session
from chainsync.db.hyperdrive import get_checkpoint_info
from chainsync.db.hyperdrive import get_current_wallet as chainsync_get_current_wallet
from chainsync.db.hyperdrive import (
get_checkpoint_info,
get_latest_block_number_from_analysis_table,
get_pool_analysis,
get_pool_config,
Expand All @@ -27,6 +26,7 @@
get_wallet_deltas,
get_wallet_pnl,
)
from chainsync.db.hyperdrive import get_current_wallet as chainsync_get_current_wallet
from chainsync.exec import acquire_data, data_analysis
from eth_account.account import Account
from eth_typing import BlockNumber, ChecksumAddress
Expand Down Expand Up @@ -133,6 +133,8 @@ class Config:
The upper bound on the governance lp fee that governance can set.
max_governance_zombie_fee: FixedPoint
The upper bound on the governance zombie fee that governance can set.
calc_pnl: bool
Whether to calculate pnl. Defaults to True.
"""

# Environment variables
Expand All @@ -159,6 +161,7 @@ class Config:
max_flat_fee: FixedPoint = FixedPoint("0.0015") # 0.15%
max_governance_lp_fee: FixedPoint = FixedPoint("0.30") # 30%
max_governance_zombie_fee: FixedPoint = FixedPoint("0.30") # 30%
calc_pnl: bool = True

def __post_init__(self):
# Random generator
Expand Down Expand Up @@ -195,6 +198,7 @@ def __init__(self, chain: Chain, config: Config | None = None):
full_path = os.path.realpath(__file__)
current_file_dir, _ = os.path.split(full_path)
abi_dir = os.path.join(current_file_dir, "..", "..", "..", "..", "..", "packages", "hyperdrive", "src", "abis")
self.calc_pnl = config.calc_pnl

self.eth_config = EthConfig(
artifacts_uri="not_used",
Expand Down Expand Up @@ -381,6 +385,7 @@ def _run_blocking_data_pipeline(self, start_block: int | None = None) -> None:
db_session=self.db_session,
exit_on_catch_up=True,
suppress_logs=True,
calc_pnl=self.calc_pnl,
)

def _cleanup(self):
Expand Down Expand Up @@ -618,8 +623,7 @@ def get_checkpoint_info(self, coerce_float: bool = True) -> pd.DataFrame:
# DB read calls ensures data pipeline is caught up before returning
if self.chain.experimental_data_threading:
self._ensure_data_caught_up()
out = get_checkpoint_info(self.db_session, coerce_float=coerce_float)
return out
return get_checkpoint_info(self.db_session, coerce_float=coerce_float)

def _add_username_to_dataframe(self, df: pd.DataFrame, addr_column: str):
addr_to_username = get_addr_to_username(self.db_session)
Expand All @@ -637,7 +641,8 @@ def _adjust_base_positions(self, in_df: pd.DataFrame, value_column: str, coerce_
if coerce_float:
out_df.loc[row_idxs, value_column] += float(initial_balance)
else:
out_df.loc[row_idxs, value_column] += Decimal(str(initial_balance))
# Pandas is smart enough to handle "+=" for "Series[Unknown]" and "Decimal"
out_df.loc[row_idxs, value_column] += Decimal(str(initial_balance)) # type: ignore
return out_df

def get_current_wallet(self, coerce_float: bool = True) -> pd.DataFrame:
Expand Down
Loading

0 comments on commit 8bba851

Please sign in to comment.