"""Export the liquidations of the last N hours to CSV with the Blockmetric Python SDK. BLOCKMETRIC_API_KEY=bm_live_... BLOCKMETRIC_BASE_URL=https:///api/v1 \ python export_liquidations.py --hours 24 --out liquidations.csv Columns: ts, venue, coin, side, price, qty, usd (newest first; times ISO 8601 UTC, usd = notional in USD). The look-back is capped by your plan's history (free key: 30 days). Venues whose single liquidations the API may not redistribute are not in the feed (meta.sources names what is); asking for one answers source_restricted. """ import argparse import csv import sys from typing import IO, Optional, Sequence from blockmetric import Blockmetric, BlockmetricError COLUMNS = ["ts", "venue", "coin", "side", "price", "qty", "usd"] def export(client: Blockmetric, out: IO[str], hours: int = 24, coin: Optional[str] = None, venue: Optional[str] = None) -> int: """Writes every liquidation of the window (follows the cursor pages) and returns the number of rows.""" writer = csv.DictWriter(out, fieldnames=COLUMNS, extrasaction="ignore", lineterminator="\n") writer.writeheader() rows = 0 for liq in client.paginate("liquidations", since_minutes=hours * 60, coin=coin, venue=venue, limit=1000): writer.writerow(liq) rows += 1 return rows def main(argv: Optional[Sequence[str]] = None) -> int: p = argparse.ArgumentParser(description=__doc__.split("\n")[0]) p.add_argument("--hours", type=int, default=24, help="look-back window in hours (default 24)") p.add_argument("--coin", help="only this coin, e.g. BTC") p.add_argument("--venue", help="only this venue, e.g. hyperliquid") p.add_argument("--out", default="-", help="output file (default: stdout)") args = p.parse_args(argv) try: client = Blockmetric() # BLOCKMETRIC_API_KEY, BLOCKMETRIC_BASE_URL if args.out == "-": n = export(client, sys.stdout, args.hours, args.coin, args.venue) else: with open(args.out, "w", newline="", encoding="utf-8") as f: n = export(client, f, args.hours, args.coin, args.venue) except BlockmetricError as e: print(f"error: {e}", file=sys.stderr) if e.doc_url: print(f" see {e.doc_url}", file=sys.stderr) return 1 print(f"{n} liquidations exported" + ("" if args.out == "-" else f" to {args.out}"), file=sys.stderr) return 0 if __name__ == "__main__": sys.exit(main())