reconstruct_position_history

Documentation for eth_defi.hyperliquid.position.reconstruct_position_history function.

reconstruct_position_history(fills)

Reconstruct position open/close events from fill history.

Processes fills chronologically to detect position state changes:

  • Open: New position from flat (size was 0)

  • Close: Position closed to flat (size becomes 0)

  • Increase: Position size increased (same direction)

  • Decrease: Position size decreased (same direction, not to 0)

When a trade flips position direction (e.g., long to short), it generates two events: a close of the old position and an open of the new position.

Example:

from eth_defi.hyperliquid.position import (
    fetch_vault_fills,
    reconstruct_position_history,
)

fills = fetch_vault_fills(session, vault_address)
events = list(reconstruct_position_history(fills))

# Filter for just opens and closes
trades = [
    e
    for e in events
    if e.event_type
    in (
        PositionEventType.open,
        PositionEventType.close,
    )
]

for trade in trades:
    print(f"{trade.timestamp}: {trade.event_type.value} {trade.direction.value} {trade.coin}")
Parameters

fills (Iterable[eth_defi.hyperliquid.position.Fill]) – Iterable of fills sorted by timestamp ascending (oldest first). Use fetch_vault_fills() or fetch_vault_fills_iterator() to obtain this.

Returns

Iterator of position events in chronological order

Return type

Iterator[eth_defi.hyperliquid.position.PositionEvent]