Crypto Data Cache
A Python library for efficiently downloading, caching, and managing cryptocurrency market data from Binance. This package provides a robust solution for fetching historical kline (candlestick), trade, and aggregate trade data with automatic retry mechanisms and local SQLite caching.
Features
- Efficient Data Fetching: Download historical cryptocurrency data from Binance
- Local Caching: SQLite-based storage for fast local access
- Multiple Data Types: Support for klines, trades, and aggregate trades
- Automatic Gap Detection: Identifies and fills missing data automatically
- Monthly/Daily Downloads: Optimized download strategy using monthly files when possible
- Robust Error Handling: Comprehensive error handling and logging
- Type Safety: Full type hints for better development experience
Installation
pip install crypto-data-cache
Quick Start
from crypto_data_cache import CryptoDataCache, DATA_TYPES
# Initialize the client (uses default database location)
cache = CryptoDataCache()
# Fetch Bitcoin 1-hour kline data
df = cache.fetch_data(
symbol="BTCUSDT",
data_type=DATA_TYPES.KLINE,
start_date_str="2024-01-01",
end_date_str="2024-01-31",
interval="1h"
)
print(f"Loaded {len(df)} rows of data")
print(df.head())
Advanced Usage (Direct Functions)
from crypto_data_cache.fetch_utils import fetch_historical_data
from crypto_data_cache.configurations import DATA_TYPES
from pathlib import Path
# Direct function usage with custom database path
df = fetch_historical_data(
symbol="BTCUSDT",
data_type=DATA_TYPES.KLINE,
start_date_str="2024-01-01",
end_date_str="2024-01-31",
db_file=Path("custom_crypto_data.db"),
interval="1h"
)
Supported Data Types
- KLINE: Candlestick/OHLCV data with various intervals (1m, 5m, 1h, 1d, etc.)
- TRADE: Individual trade data
- AGGTRADE: Aggregate trade data
Configuration
The library automatically handles:
- Database schema creation
- Data type validation
- Timestamp conversion (UTC microseconds)
- Missing data detection and download
API Reference
CryptoDataCache Class
The main client class providing a high-level interface:
CryptoDataCache(db_path=None)
Initialize the client with optional custom database path.
fetch_data()
Primary method for fetching historical data.
Direct Functions
fetch_historical_data()
Lower-level function for direct access.
Parameters:
symbol(str): Trading pair symbol (e.g., "BTCUSDT")data_type(DATA_TYPES): Type of data to fetchstart_date_str(str): Start date in "YYYY-MM-DD" formatend_date_str(str): End date in "YYYY-MM-DD" formatdb_file(Path): Path to SQLite database fileinterval(str, optional): Required for KLINE data (e.g., "1h", "1d")prefer_monthly(bool): Whether to prefer monthly downloads (default: True)
Returns:
pd.DataFrame: Historical data with properly typed columns
Data Types
from crypto_data_cache.configurations import DATA_TYPES
# Available data types
DATA_TYPES.KLINE # Candlestick data
DATA_TYPES.TRADE # Individual trades
DATA_TYPES.AGGTRADE # Aggregate trades
Database Storage
Data is stored in SQLite with the following features:
- Automatic table creation with proper schemas
- UTC timestamp storage (microsecond precision)
- Efficient querying and indexing
- Duplicate data prevention
Error Handling
The library includes comprehensive error handling:
- Network timeouts and retries
- Data validation
- Missing file handling (404 errors)
- Database connection management
Logging
Built-in logging provides detailed information about:
- Download progress
- Missing data detection
- Error conditions
- Performance metrics
Examples
Fetch Multiple Intervals
from crypto_data_cache import CryptoDataCache, DATA_TYPES
# Initialize once, reuse for multiple requests
cache = CryptoDataCache()
# Fetch different intervals
intervals = ["1h", "4h", "1d"]
for interval in intervals:
df = cache.fetch_data(
symbol="ETHUSDT",
data_type=DATA_TYPES.KLINE,
start_date_str="2024-01-01",
end_date_str="2024-01-31",
interval=interval
)
print(f"{interval}: {len(df)} rows")
Fetch Trade Data
# Fetch individual trade data
cache = CryptoDataCache()
trades_df = cache.fetch_data(
symbol="BTCUSDT",
data_type=DATA_TYPES.TRADE,
start_date_str="2024-01-01",
end_date_str="2024-01-02"
)
Custom Database Location
# Use custom database path
cache = CryptoDataCache(db_path="/path/to/my/crypto_data.db")
df = cache.fetch_data("ETHUSDT", DATA_TYPES.KLINE, "2024-01-01", "2024-01-31", "1d")
Requirements
- Python e 3.13
- pandas e 2.3.1
- requests e 2.32.4
- rich e 14.0.0
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
Support
If you encounter any issues or have questions, please file an issue on the GitHub repository.
Metadata
Release files for crypto-data-cache 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| crypto_data_cache-0.2.1.tar.gz | 29.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| crypto_data_cache-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.8 kB
Release files / crypto_data_cache-0.2.1.tar.gz
| Download URL | crypto_data_cache-0.2.1.tar.gz |
|---|---|
| Size | 29.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
31b76727fa6e9722ac0f35dcef925d3977d45c8d5af67cb3fd2958cdb2cdeb59
|
|
BLAKE2b-256 checksum How to use checksums |
89798f6a74f017137b7a182eb1c00cb4d009c968ce8c34787c7b2784e794b603
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.16
|
Release files / crypto_data_cache-0.2.1-py3-none-any.whl
| Download URL | crypto_data_cache-0.2.1-py3-none-any.whl |
|---|---|
| Size | 20.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
da99e04679b645dfc8fd82cb5b56454da5da25f8ce61b4a7dae1a8fec599891a
|
|
BLAKE2b-256 checksum How to use checksums |
65107435ea593301461ab9eb969c6010cae9ac0948ecb9f20413179a01dad442
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.16
|
Security for crypto-data-cache 0.2.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.