agent0.chainsync.analysis
Analysis for trading.
Submodules
Functions
|
Calculate closeout value of agent positions. |
|
Calculate the closeout value for a single position. |
|
Fills in the unrealized and realized pnl for each position. |
|
Function to query postgres data tables and insert to analysis tables. |
|
Function to query the trade events table and takes a snapshot |
Package Contents
- agent0.chainsync.analysis.calc_closeout_value(current_positions: pandas.DataFrame, checkpoint_info: pandas.DataFrame, interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, coerce_float: bool) pandas.Series
Calculate closeout value of agent positions.
- Parameters:
current_positions (pd.DataFrame) – A dataframe resulting from get_current_wallet that describes the current wallet position.
checkpoint_info (pd.DataFrame) – A dataframe resulting from get_checkpoint_info that describes all checkpoints.
interface (HyperdriveReadInterface) – The hyperdrive read interface.
coerce_float (bool) – If True, will coerce underlying Decimals to floats.
- Returns:
A series matching the current_wallet input that contains the values of each position.
- Return type:
pd.Series
- agent0.chainsync.analysis.calc_single_closeout(position: pandas.Series, interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, hyperdrive_state: agent0.ethpy.hyperdrive.state.PoolState, checkpoint_share_prices: pandas.Series, coerce_float: bool) decimal.Decimal | float
Calculate the closeout value for a single position.
- Parameters:
position (pd.DataFrame) – The position to calculate the closeout value for (one row in current_wallet).
interface (HyperdriveReadInterface) – The hyperdrive read interface.
hyperdrive_state (PoolState) – The hyperdrive pool state.
checkpoint_share_prices (pd.Series) – A series with the index as checkpoint time and the value as the share prices.
coerce_float (bool) – If True, will coerce underlying Decimals to floats.
- Returns:
The closeout position value. Type depends on the coerce_float argument.
- Return type:
Decimal | float
- agent0.chainsync.analysis.fill_pnl_values(in_df: pandas.DataFrame, db_session: sqlalchemy.orm.Session, interface: agent0.ethpy.hyperdrive.HyperdriveReadInterface, coerce_float: bool) pandas.DataFrame
Fills in the unrealized and realized pnl for each position.
- Parameters:
in_df (pd.DataFrame) – A dataframe of positions from get_current_positions.
db_session (Session) – The database session.
interface (HyperdriveReadInterface) – The hyperdrive read interface attached to a hyperdrive pool.
coerce_float (bool) – If True, will coerce all numeric columns to float.
- Returns:
The in_df with unrealized value and pnl columns added.
- Return type:
pd.DataFrame
- agent0.chainsync.analysis.db_to_analysis(db_session: sqlalchemy.orm.Session, interfaces: list[agent0.ethpy.hyperdrive.HyperdriveReadInterface], block_number: int, calc_pnl: bool = True) None
Function to query postgres data tables and insert to analysis tables. Executes analysis on a batch of blocks, defined by start and end block.
- Parameters:
db_session (Session) – The initialized db session.
interfaces (list[HyperdriveReadInterface]) – A collection of Hyperdrive interface objects, each connected to a pool.
block_number (int) – The block number to run analysis on.
calc_pnl (bool, optional) – Whether to calculate pnl. Defaults to True.
- agent0.chainsync.analysis.snapshot_positions_to_db(interfaces: list[agent0.ethpy.hyperdrive.HyperdriveReadInterface], wallet_addr: str | None, calc_pnl: bool, db_session: sqlalchemy.orm.Session, block_number: int)
Function to query the trade events table and takes a snapshot of the current positions and pnl.
Note
This function does not scale well in simulation mode, as this table grows for all wallets, for all positions, for every snapshot period (currently set to every block).
We can try to alleviate this by (1) increasing the snapshot period, and (2) removing duplicate entries of closed positions (since their realized_value never changes).
This shouldn’t be a problem for remote mode, as we limit this table to (1) only agents managed by agent0, and (2) only adds an entry for every explicit “get_all_positions” call.
- Parameters:
interfaces (list[HyperdriveReadInterface]) – A collection of Hyperdrive interface objects, each connected to a pool.
wallet_addr (str | None) – The wallet address to query. If None, will not filter events by wallet addr.
db_session (Session) – The database session.
calc_pnl (bool) – Whether to calculate pnl.
block_number (int) – The block number to snapshot positions on.