hyperliquid.trade_history
Documentation for eth_defi.hyperliquid.trade_history Python module.
Hyperliquid account trade history reconstruction.
Reconstructs historical closed positions, open positions, and round-trip trades for any Hyperliquid account (vaults or normal addresses) by combining:
Fill history from
userFillsByTimeFunding payments from
userFundingCurrent positions from
clearinghouseState
The module groups individual fills into round-trip trades (open→close) with VWAP entry/exit prices, funding costs, and realised PnL.
API endpoints used
userFillsByTime— paginated fill history (max 10K fills accessible)userFunding— paginated funding payment historyclearinghouseState— current open positions with unrealised PnL
Example:
from eth_defi.hyperliquid.session import create_hyperliquid_session
from eth_defi.hyperliquid.trade_history import fetch_account_trade_history
session = create_hyperliquid_session()
address = "0x1e37a337ed460039d1b15bd3bc489de789768d5e"
history = fetch_account_trade_history(session, address)
print(f"Open trades: {len(history.open_trades)}")
print(f"Closed trades: {len(history.closed_trades)}")
for trade in history.closed_trades[:5]:
print(f" {trade.coin} {trade.direction.value}: PnL={trade.net_pnl:.2f}")
Module Attributes
Maximum funding payments returned per API request |
Functions
|
Attach funding payments to the appropriate round-trip trades. |
|
Compute event-accurate share prices from actual ledger events. |
|
Convert share price events to a pandas DataFrame. |
|
Convert round-trip trades to a pandas DataFrame for tabular display. |
|
Fetch all funding payments for an account with automatic pagination. |
|
Fetch and reconstruct complete trade history for a Hyperliquid account. |
|
Group position events into round-trip trades. |
Classes
Complete trade history snapshot for a Hyperliquid account. |
|
A single funding payment from the Hyperliquid |
|
A round-trip trade from position open through close. |
|
A single event in the share price time series. |
- MAX_FUNDING_PER_REQUEST = 500
Maximum funding payments returned per API request
- class FundingPayment
Bases:
objectA single funding payment from the Hyperliquid
userFundingendpoint.Funding is paid/received periodically (typically hourly) for open perpetual positions. Negative USDC means funding was paid by the holder; positive means funding was received.
- funding_rate: decimal.Decimal
Funding rate applied
- usdc: decimal.Decimal
USD amount (negative = paid, positive = received)
- position_size: decimal.Decimal
Position size at the time of the funding event
- timestamp: datetime.datetime
Funding event timestamp
- classmethod from_api_response(data)
Parse a funding payment from API response data.
The
userFundingAPI returns entries with adeltasub-object containing the actual funding fields (coin, usdc, szi, fundingRate).- Parameters
data (dict) – Raw funding dict from
userFundingAPI response.- Returns
Parsed FundingPayment object.
- Return type
- __init__(coin, funding_rate, usdc, position_size, timestamp, timestamp_ms, hash=None, n_samples=1)
- Parameters
coin (str) –
funding_rate (decimal.Decimal) –
usdc (decimal.Decimal) –
position_size (decimal.Decimal) –
timestamp (datetime.datetime) –
timestamp_ms (int) –
hash (str | None) –
n_samples (int) –
- Return type
None
- class RoundTripTrade
Bases:
objectA round-trip trade from position open through close.
Groups individual fills from position-open to position-close with volume-weighted average entry/exit prices, funding costs, and PnL.
For positions that were already open before the data window,
is_completeisFalseandentry_pricemay be unknown.- direction: eth_defi.hyperliquid.position.PositionDirection
Trade direction
- is_complete: bool
Whether we have the full open→close lifecycle (False when position was already open before data window)
- opened_at: datetime.datetime
Timestamp when the position was first opened (or earliest known fill)
- closed_at: datetime.datetime | None
Timestamp when the position was closed (None if still open)
- entry_price: decimal.Decimal
Volume-weighted average entry price
- exit_price: decimal.Decimal | None
Volume-weighted average exit price (None if still open or no exits)
- max_size: decimal.Decimal
Maximum position size reached during the trade
- current_size: decimal.Decimal
Current position size (0 if closed)
- realised_pnl: decimal.Decimal
Realised PnL from fills (sum of closed_pnl)
- funding_pnl: decimal.Decimal
Total funding payments during this trade
- total_fees: decimal.Decimal
Total fees paid across all fills
- net_pnl: decimal.Decimal
Net PnL (realised_pnl + funding_pnl - total_fees)
- unrealised_pnl: decimal.Decimal | None
Unrealised PnL from clearinghouse state (open trades only)
- fills: list[eth_defi.hyperliquid.position.Fill]
All fills belonging to this trade
- funding_payments: list[eth_defi.hyperliquid.trade_history.FundingPayment]
All funding payments during this trade’s lifetime
- duration: datetime.timedelta | None
Duration of the trade
- __init__(coin, direction, is_open, is_complete, opened_at, closed_at, entry_price, exit_price, max_size, current_size, realised_pnl, funding_pnl, total_fees, net_pnl, unrealised_pnl, fills=<factory>, funding_payments=<factory>, duration=None, fill_count=0, _entry_cost=Decimal('0'), _entry_size=Decimal('0'), _exit_cost=Decimal('0'), _exit_size=Decimal('0'))
- Parameters
coin (str) –
direction (eth_defi.hyperliquid.position.PositionDirection) –
is_open (bool) –
is_complete (bool) –
opened_at (datetime.datetime) –
closed_at (datetime.datetime | None) –
entry_price (decimal.Decimal) –
exit_price (decimal.Decimal | None) –
max_size (decimal.Decimal) –
current_size (decimal.Decimal) –
realised_pnl (decimal.Decimal) –
funding_pnl (decimal.Decimal) –
total_fees (decimal.Decimal) –
net_pnl (decimal.Decimal) –
unrealised_pnl (decimal.Decimal | None) –
fills (list[eth_defi.hyperliquid.position.Fill]) –
funding_payments (list[eth_defi.hyperliquid.trade_history.FundingPayment]) –
duration (datetime.timedelta | None) –
fill_count (int) –
_entry_cost (decimal.Decimal) –
_entry_size (decimal.Decimal) –
_exit_cost (decimal.Decimal) –
_exit_size (decimal.Decimal) –
- Return type
None
- class AccountTradeHistory
Bases:
objectComplete trade history snapshot for a Hyperliquid account.
Combines current open positions, historical closed trades, open trades, all fills, and funding payments into a single result.
- address: eth_typing.evm.HexAddress
Account address (vault or user)
- snapshot_time: datetime.datetime
Timestamp when this snapshot was taken
- open_positions: list[eth_defi.hyperliquid.api.AssetPosition]
Current open positions from clearinghouse state
- closed_trades: list[eth_defi.hyperliquid.trade_history.RoundTripTrade]
Historical closed round-trip trades
- open_trades: list[eth_defi.hyperliquid.trade_history.RoundTripTrade]
Currently open round-trip trades
- fills: list[eth_defi.hyperliquid.position.Fill]
All individual fills in the time range
- funding_payments: list[eth_defi.hyperliquid.trade_history.FundingPayment]
All funding payments in the time range
- margin_summary: eth_defi.hyperliquid.api.MarginSummary
Perp account margin summary
- start_time: datetime.datetime
Time range start
- end_time: datetime.datetime
Time range end
- __init__(address, snapshot_time, open_positions, closed_trades, open_trades, fills, funding_payments, margin_summary, start_time, end_time, fills_truncated)
- Parameters
address (eth_typing.evm.HexAddress) –
snapshot_time (datetime.datetime) –
open_positions (list[eth_defi.hyperliquid.api.AssetPosition]) –
closed_trades (list[eth_defi.hyperliquid.trade_history.RoundTripTrade]) –
open_trades (list[eth_defi.hyperliquid.trade_history.RoundTripTrade]) –
fills (list[eth_defi.hyperliquid.position.Fill]) –
funding_payments (list[eth_defi.hyperliquid.trade_history.FundingPayment]) –
margin_summary (eth_defi.hyperliquid.api.MarginSummary) –
start_time (datetime.datetime) –
end_time (datetime.datetime) –
fills_truncated (bool) –
- Return type
None
- fetch_account_funding(session, address, start_time=None, end_time=None, timeout=30.0)
Fetch all funding payments for an account with automatic pagination.
Fetches funding payment history from the Hyperliquid API using the
userFundingendpoint with automatic forward pagination.Payments are yielded in chronological order (oldest first).
Example:
from datetime import datetime, timedelta from eth_defi.hyperliquid.session import create_hyperliquid_session from eth_defi.hyperliquid.trade_history import fetch_account_funding session = create_hyperliquid_session() address = "0x1e37a337ed460039d1b15bd3bc489de789768d5e" payments = list( fetch_account_funding( session, address, start_time=datetime.now() - timedelta(days=7), ) ) print(f"Fetched {len(payments)} funding payments")- Parameters
session (eth_defi.hyperliquid.session.HyperliquidSession) – Session from
create_hyperliquid_session().address (eth_typing.evm.HexAddress) – Account address (vault or user).
start_time (datetime.datetime | None) – Start of time range (inclusive). Defaults to 30 days ago.
end_time (datetime.datetime | None) – End of time range (inclusive). Defaults to current time.
timeout (float) – HTTP request timeout in seconds.
- Returns
Iterator of funding payments sorted by timestamp ascending (oldest first).
- Raises
requests.HTTPError – If the HTTP request fails after retries.
- Return type
- group_fills_into_trades(events, fills=None)
Group position events into round-trip trades.
Processes position events chronologically and groups them into round-trip trades. Each trade tracks a position from open to close (or to the current state if still open).
When a position was already open before the data window (i.e. the earliest fill has a non-zero
start_position), the trade is marked asis_complete=False.- Parameters
events (Iterable[eth_defi.hyperliquid.position.PositionEvent]) – Position events from
reconstruct_position_history(). Must be in chronological order.fills (list[eth_defi.hyperliquid.position.Fill] | None) – Optional original fills list for detecting incomplete trades via
start_position.
- Returns
Tuple of
(closed_trades, open_trades).- Return type
tuple[list[eth_defi.hyperliquid.trade_history.RoundTripTrade], list[eth_defi.hyperliquid.trade_history.RoundTripTrade]]
- attach_funding_to_trades(closed_trades, open_trades, funding_payments)
Attach funding payments to the appropriate round-trip trades.
For each funding payment, finds the trade for that coin active at the payment timestamp and attributes the funding cost/income to it.
Modifies trades in place.
- Parameters
closed_trades (list[eth_defi.hyperliquid.trade_history.RoundTripTrade]) – List of closed round-trip trades.
open_trades (list[eth_defi.hyperliquid.trade_history.RoundTripTrade]) – List of open round-trip trades.
funding_payments (list[eth_defi.hyperliquid.trade_history.FundingPayment]) – Funding payments sorted chronologically.
- Return type
None
- fetch_account_trade_history(session, address, start_time=None, end_time=None, timeout=30.0)
Fetch and reconstruct complete trade history for a Hyperliquid account.
Orchestrates fetching fills, funding payments, and current positions, then reconstructs round-trip trades with full PnL accounting.
Example:
from eth_defi.hyperliquid.session import create_hyperliquid_session from eth_defi.hyperliquid.trade_history import fetch_account_trade_history session = create_hyperliquid_session() address = "0x1e37a337ed460039d1b15bd3bc489de789768d5e" history = fetch_account_trade_history(session, address) for trade in history.closed_trades: print(f"{trade.coin} {trade.direction.value}: entry={trade.entry_price} exit={trade.exit_price} PnL={trade.net_pnl}")- Parameters
session (eth_defi.hyperliquid.session.HyperliquidSession) – Session from
create_hyperliquid_session().address (eth_typing.evm.HexAddress) – Account address (vault or user).
start_time (datetime.datetime | None) – Start of time range. Defaults to 30 days ago.
end_time (datetime.datetime | None) – End of time range. Defaults to now.
timeout (float) – HTTP request timeout in seconds.
- Returns
Complete trade history snapshot.
- Return type
- create_trade_summary_dataframe(trades)
Convert round-trip trades to a pandas DataFrame for tabular display.
- Parameters
trades (list[eth_defi.hyperliquid.trade_history.RoundTripTrade]) – List of round-trip trades from
fetch_account_trade_history().- Returns
DataFrame with one row per trade.
- Return type
Bases:
objectA single event in the share price time series.
Each event represents a state change that affects the vault’s share price: a deposit, withdrawal, trading PnL, or funding payment.
Event timestamp
Type of event: “deposit”, “withdraw”, “fill_pnl”, “funding”
Total assets (NAV) after this event
Total supply of shares after this event
Share price after this event
Change in assets from this event
Whether an epoch reset occurred
Coin involved (for fill_pnl and funding events)
Compute event-accurate share prices from actual ledger events.
Unlike the portfolio-history-derived share prices (which suffer from resolution artefacts — see
README-hyperliquid-vault-limitations.md), this function uses actual event data:Deposits: Exact amounts from
userNonFundingLedgerUpdatesWithdrawals: Exact amounts from
userNonFundingLedgerUpdatesTrading PnL: Realised PnL from individual fills (
closed_pnl)Funding payments: USD amounts from
userFunding
This eliminates the resolution-dependent netflow derivation that causes share price spikes in the current pipeline.
The share price model follows ERC-4626 mechanics:
share_price = total_assets / total_supplyDeposits mint shares:
shares_minted = deposit_amount / share_priceWithdrawals burn shares:
shares_burned = withdrawal_amount / share_pricePnL and funding change
total_assetsbut nottotal_supplyShare price starts at 1.00 at first deposit
- Parameters
fills (list[eth_defi.hyperliquid.position.Fill]) – List of fills sorted chronologically.
funding_payments (list[eth_defi.hyperliquid.trade_history.FundingPayment]) – List of funding payments sorted chronologically.
ledger_events (list) – List of VaultDepositEvent objects sorted chronologically. Import from
eth_defi.hyperliquid.deposit.initial_total_assets (float) – Starting total assets (usually 0 for a new vault).
- Returns
List of SharePriceEvent objects in chronological order.
- Return type
Convert share price events to a pandas DataFrame.
- Parameters
events (list[eth_defi.hyperliquid.trade_history.SharePriceEvent]) – List of SharePriceEvent from
compute_event_share_prices().- Returns
DataFrame with timestamp index and columns for total_assets, total_supply, share_price, event_type, delta, epoch_reset.
- Return type