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
datetimeobject (e.g.,datetime(2023, 1, 1)) - end: End date as a
datetimeobject (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_timewith OHLCV (Open, High, Low, Close, Volume) columns - Trades: Indexed by
timestampwith trade details - Aggregated Trades: Indexed by
timestampwith 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file narf-0.2.2.tar.gz.
File metadata
- Download URL: narf-0.2.2.tar.gz
- Upload date:
- Size: 44.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be27df73e24447476ebb2fc26d28adddeaea571c0e99c0af98fa34a09051b280
|
|
| MD5 |
c5cb0e1235d490349cf1d4957e31eb8f
|
|
| BLAKE2b-256 |
232c4105af1ba9c9d9d7304ceb4cb898520b47f76864fd392a7a8fa2d87cc4b0
|
File details
Details for the file narf-0.2.2-py3-none-any.whl.
File metadata
- Download URL: narf-0.2.2-py3-none-any.whl
- Upload date:
- Size: 54.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5680d934a5df5b46372b0014c9e803ae4769677a4ec6dc4da6eca084d81d089f
|
|
| MD5 |
a79b6b7882f81d437c26938f4b4581da
|
|
| BLAKE2b-256 |
0a7021d296edcf8fd8a1537d6704f5f22ad1143f89d23775e88db1cb3a539e6a
|