Skip to main content

Market data loader library for cryptocurrency exchanges

Project description

Narf

Narf is a simple Python library for downloading historical cryptocurrency market data from Binance. Get klines (candlestick data), trades, and aggregated trades for spot and futures markets with just a few lines of code.

Installation

pip install narf

Quick Start

from datetime import datetime
from narf.data import binance

# Simplified interface - defaults to futures USD-M klines
df = binance.load("BTCUSDT", datetime(2023, 1, 1), datetime(2025, 11, 1))

# Or use the full path for specific data types
df = binance.futures.um.klines.load("BTCUSDT", datetime(2023, 1, 1), datetime(2025, 11, 1))

# Load spot market data (defaults to klines)
df = binance.spot.load("ETHUSDT", datetime(2024, 1, 1), datetime(2024, 12, 31), interval="1h")

# Load specific data types
df = binance.futures.um.klines.load("ETHUSDT", datetime(2024, 1, 1), datetime(2024, 12, 31), interval="1h")

# Load data up to now (end date is optional)
df = binance.futures.load("BTCUSDT", datetime(2024, 1, 1))

Features

  • Simple API: Intuitive interface for accessing Binance historical data
  • Date Range Support: Load data for any date range with automatic month-by-month fetching
  • Automatic Caching: Downloaded data is cached locally to avoid re-downloading
  • Pandas Integration: Returns pandas DataFrames ready for analysis
  • Multiple Markets: Support for spot, futures USD-M (UM), and futures Coin-M (CM)
  • Multiple Data Types: Klines (candlestick), trades, and aggregated trades

Available Markets

Simplified Interface

For quick access, you can use simplified interfaces that default to klines:

# Defaults to futures USD-M klines
binance.load(symbol, start, end=None, interval="1m")

# Defaults to futures USD-M klines
binance.futures.load(symbol, start, end=None, interval="1m")

# Defaults to spot klines
binance.spot.load(symbol, start, end=None, interval="1m")

Full Interface

For specific data types, use the full path:

Spot Market

binance.spot.klines.load(symbol, start, end=None, interval="1m")
binance.spot.trades.load(symbol, start, end=None, interval="1m")
binance.spot.aggTrades.load(symbol, start, end=None, interval="1m")

Futures Market - USD-M (UM)

binance.futures.um.klines.load(symbol, start, end=None, interval="1m")
binance.futures.um.trades.load(symbol, start, end=None, interval="1m")

Futures Market - Coin-M (CM)

binance.futures.cm.klines.load(symbol, start, end=None, interval="1m")
binance.futures.cm.trades.load(symbol, start, end=None, interval="1m")

Parameters

  • symbol: Trading pair symbol (e.g., "BTCUSDT", "ETHUSDT")
  • start: Start date as a datetime object (e.g., datetime(2023, 1, 1))
  • end: End date as a datetime object (optional, defaults to current date)
  • interval: Time interval for klines (default: "1m"). Examples: "1m", "5m", "1h", "1d"

Supported Intervals

Common intervals include: 1m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M

Data Format

All functions return pandas DataFrames with time-indexed data:

  • Klines: Indexed by open_time with OHLCV (Open, High, Low, Close, Volume) columns
  • Trades: Indexed by timestamp with trade details
  • Aggregated Trades: Indexed by timestamp with aggregated trade information

Examples

Load multiple years of Bitcoin futures data (simplified)

from datetime import datetime
from narf.data import binance

# Using simplified interface - defaults to futures USD-M klines
df = binance.load("BTCUSDT", datetime(2023, 1, 1), datetime(2025, 11, 1))

print(df.head())
print(f"Total records: {len(df)}")

Load multiple years of Bitcoin futures data (full path)

from datetime import datetime
from narf.data import binance

# Using full path for explicit data type
df = binance.futures.um.klines.load("BTCUSDT", datetime(2023, 1, 1), datetime(2025, 11, 1))

print(df.head())
print(f"Total records: {len(df)}")

Compare spot and futures prices

from datetime import datetime
from narf.data import binance

start = datetime(2024, 1, 1)
end = datetime(2024, 12, 31)

# Using simplified interfaces
spot = binance.spot.load("BTCUSDT", start, end, interval="1d")
futures = binance.futures.load("BTCUSDT", start, end, interval="1d")

# Compare closing prices
print(spot['close'].head())
print(futures['close'].head())

Load recent data up to now

from datetime import datetime
from narf.data import binance

# Using simplified interface - load all data from January 2024 to now
df = binance.load("BTCUSDT", datetime(2024, 1, 1))
print(df.tail())

Load aggregated trades for analysis

from datetime import datetime
from narf.data import binance

# Load aggregated trades for a specific period
agg_trades = binance.spot.aggTrades.load(
    "ETHUSDT", 
    datetime(2024, 1, 1), 
    datetime(2024, 1, 31),
    interval="1h"
)
print(agg_trades.head())

Caching

Narf automatically caches downloaded data in a local cache/ directory. This means:

  • First download: Data is fetched from Binance and saved
  • Subsequent requests: Data is loaded from cache (much faster)

To clear the cache, simply delete the cache/ directory.

Requirements

  • Python 3.12+
  • pandas
  • requests

License

See the repository for license information.

Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

narf-0.1.2.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

narf-0.1.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file narf-0.1.2.tar.gz.

File metadata

  • Download URL: narf-0.1.2.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for narf-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4fd5340350786ee5e472a8bc3c8d2938f9015faa4d5752a029cb7ec48631b2fa
MD5 c6f0e6c43d8c8281b26363a157946465
BLAKE2b-256 9a61dc279cecd0ee0335b90e5458bde2e4b51cc0c965c5c46cca42cfa165a83f

See more details on using hashes here.

File details

Details for the file narf-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: narf-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for narf-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 35373f22e5ebbcc9b740754b0da3d9b3b8b259dc822cf4f0844006821d15e424
MD5 13160015103214572cb739dae5865f94
BLAKE2b-256 0f5f89e644187a4e7cb38e28887105c9d53d38ee70e48f3ad2836b0dbc8b1509

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page