hlcycles

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 5, 2026 License: MIT Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

hyperliquid-position-csv

Zero-dependency Python (decimal.Decimal) & JavaScript (ESM) library + CLI to reconstruct flat-to-flat perpetual position cycles (0 → position → 0) from Hyperliquid's public userFills endpoint and export clean RFC-4180 CSV summaries.


Python Installation & Quick Start

pip install hyperliquid-position-csv
hl-position-csv --address 0x... > positions.csv
from hyperliquid_position_csv import group_position_cycles, cycles_to_csv

cycles = group_position_cycles(raw_fills)
print(cycles_to_csv(cycles))

Why Reconstruct Position Cycles?

Hyperliquid's public POST https://api.hyperliquid.xyz/info ({"type": "userFills", "user": "0x..."}) returns up to 2,000 individual order execution fragments. A single position entered across 15 partial maker fills and closed across 12 taker fills appears as 27 separate rows, and a single flip order crossing zero splits across two logical trades.

hyperliquid-position-csv groups raw fills per market (preserving both native perpetuals like BTC and HIP-3 namespaced builder markets like xyz:NVDA) into clean position cycles:

  • Exact BigInt 18-decimal fixed-point math — no IEEE-754 floating-point precision drift on sizes, fees, or realized PnL.
  • Honest window boundaries — if a wallet's 2,000-fill history starts mid-position (startPosition != 0), complete is marked false rather than fabricating an entry price.
  • Single-fill reversals handled — orders that cross through 0 close the prior cycle cleanly and open a new opposite-side cycle.

Usage

1. CLI (Export any 0x wallet directly to CSV)
# Fetch live public fills for a Hyperliquid wallet and write CSV
node ./bin/cli.mjs 0x0000000000000000000000000000000000000000 --out positions.csv

# Or reconstruct offline from a saved userFills JSON array
node ./bin/cli.mjs --input ./sample-fills.json --out positions.csv
2. ESM Library
import { fetchHyperliquidUserFills, groupPositionCycles, cyclesToCsv } from './src/index.mjs';

const fills = await fetchHyperliquidUserFills('0x...');
const cycles = groupPositionCycles(fills);
console.log(cyclesToCsv(cycles));

Output CSV Columns

Column Description
coin Perpetual symbol (e.g. BTC, ETH, or HIP-3 namespace:SYMBOL)
side long or short
status closed (returned to 0) or open
opened_at / closed_at ISO-8601 timestamps (blank if outside the 2,000-fill window)
opened_size / closed_size Total base quantity opened and closed within the cycle
avg_entry_px / avg_exit_px Volume-weighted average entry and exit prices
realized_pnl Sum of closedPnl across all executions in the cycle
fees / fee_token Total execution fees and fee currency
fills_count Number of partial fills aggregated into this cycle
complete true only when both flat entry (0 → pos) and flat exit (pos → 0) are inside the loaded history

License

MIT © HoldWins


Go Module Installation (pkg.go.dev)

go get github.com/dalangdalang934/hyperliquid-position-csv@latest
import "hlcycles" // github.com/dalangdalang934/hyperliquid-position-csv

cycles, _ := hlcycles.GroupPositionCycles(fills)
csvOut, _ := hlcycles.FormatCyclesCSV(cycles)

Documentation

Overview

Package hlcycles reconstructs complete flat-to-flat perpetual position cycles from raw Hyperliquid public userFills using exact 18-decimal big.Int fixed-point arithmetic and exports RFC-4180 CSV summaries.

Live public Hyperliquid whale position tracker:

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatCyclesCSV

func FormatCyclesCSV(cycles []Cycle) (string, error)

FormatCyclesCSV renders reconstructed cycles into RFC-4180 CSV text.

func FormatScaled18

func FormatScaled18(units *big.Int) string

FormatScaled18 formats 10^18 scaled *big.Int units into a trimmed decimal string.

func IsPerpFill

func IsPerpFill(coin string) bool

IsPerpFill returns true for native and HIP-3 builder perpetual symbols (excluding @spot and PAIR/USDC).

func ParseScaled18

func ParseScaled18(input string) (*big.Int, error)

ParseScaled18 parses a decimal string into exact 10^18 scaled *big.Int units.

Types

type Cycle

type Cycle struct {
	Coin        string
	Direction   string // "long" or "short"
	Status      string // "closed" or "open"
	OpenTimeMs  int64
	CloseTimeMs int64
	PeakSize    string
	AvgEntryPx  string
	AvgExitPx   string
	RealizedPnL string
	TotalFees   string
	FillCount   int
}

Cycle summarizes one reconstructed flat-to-flat (or active open) perpetual position round.

func GroupPositionCycles

func GroupPositionCycles(fills []RawFill) ([]Cycle, error)

GroupPositionCycles reconstructs flat-to-flat perpetual position cycles per coin.

type RawFill

type RawFill struct {
	Coin          string `json:"coin"`
	Px            string `json:"px"`
	Sz            string `json:"sz"`
	Side          string `json:"side"` // "B"/"buy" or "A"/"sell"
	Time          int64  `json:"time"`
	StartPosition string `json:"startPosition,omitempty"`
	ClosedPnl     string `json:"closedPnl,omitempty"`
	Fee           string `json:"fee,omitempty"`
	TID           int64  `json:"tid,omitempty"`
}

RawFill mirrors a single fill record from Hyperliquid's public userFills endpoint.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL