agent0.chainsync.analysis ========================= .. py:module:: agent0.chainsync.analysis .. autoapi-nested-parse:: Analysis for trading. .. !! processed by numpydoc !! Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/agent0/chainsync/analysis/calc_position_value/index /autoapi/agent0/chainsync/analysis/db_to_analysis/index Functions --------- .. autoapisummary:: agent0.chainsync.analysis.calc_closeout_value agent0.chainsync.analysis.calc_single_closeout agent0.chainsync.analysis.fill_pnl_values agent0.chainsync.analysis.db_to_analysis agent0.chainsync.analysis.snapshot_positions_to_db Package Contents ---------------- .. py:function:: 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. :param current_positions: A dataframe resulting from `get_current_wallet` that describes the current wallet position. :type current_positions: pd.DataFrame :param checkpoint_info: A dataframe resulting from `get_checkpoint_info` that describes all checkpoints. :type checkpoint_info: pd.DataFrame :param interface: The hyperdrive read interface. :type interface: HyperdriveReadInterface :param coerce_float: If True, will coerce underlying Decimals to floats. :type coerce_float: bool :returns: A series matching the current_wallet input that contains the values of each position. :rtype: pd.Series .. !! processed by numpydoc !! .. py:function:: 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. :param position: The position to calculate the closeout value for (one row in current_wallet). :type position: pd.DataFrame :param interface: The hyperdrive read interface. :type interface: HyperdriveReadInterface :param hyperdrive_state: The hyperdrive pool state. :type hyperdrive_state: PoolState :param checkpoint_share_prices: A series with the index as checkpoint time and the value as the share prices. :type checkpoint_share_prices: pd.Series :param coerce_float: If True, will coerce underlying Decimals to floats. :type coerce_float: bool :returns: The closeout position value. Type depends on the coerce_float argument. :rtype: Decimal | float .. !! processed by numpydoc !! .. py:function:: 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. :param in_df: A dataframe of positions from `get_current_positions`. :type in_df: pd.DataFrame :param db_session: The database session. :type db_session: Session :param interface: The hyperdrive read interface attached to a hyperdrive pool. :type interface: HyperdriveReadInterface :param coerce_float: If True, will coerce all numeric columns to float. :type coerce_float: bool :returns: The `in_df` with unrealized value and pnl columns added. :rtype: pd.DataFrame .. !! processed by numpydoc !! .. py:function:: 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. :param db_session: The initialized db session. :type db_session: Session :param interfaces: A collection of Hyperdrive interface objects, each connected to a pool. :type interfaces: list[HyperdriveReadInterface] :param block_number: The block number to run analysis on. :type block_number: int :param calc_pnl: Whether to calculate pnl. Defaults to True. :type calc_pnl: bool, optional .. !! processed by numpydoc !! .. py:function:: 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. :param interfaces: A collection of Hyperdrive interface objects, each connected to a pool. :type interfaces: list[HyperdriveReadInterface] :param wallet_addr: The wallet address to query. If None, will not filter events by wallet addr. :type wallet_addr: str | None :param db_session: The database session. :type db_session: Session :param calc_pnl: Whether to calculate pnl. :type calc_pnl: bool :param block_number: The block number to snapshot positions on. :type block_number: int .. !! processed by numpydoc !!