Downloading Historical Cryptocurrency Data Using Python and Pandas

If you work with cryptocurrency data, you often need to automate downloading historical price data. In this guide, you’ll learn how to…

Downloading Historical Cryptocurrency Data Using Python and Pandas

If you work with cryptocurrency data, you often need to automate downloading historical price data. In this guide, you’ll learn how to build a Python script that:

✅ Fetches OHLCV data (Open, High, Low, Close, Volume) for any trading pair from Binance.
✅ Supports a date range and configurable time intervals.
✅ Saves the data to a CSV file.
✅ Provides a command-line interface via argparse.
✅ Defaults the end date to tomorrow if not specified.

We’ll also cover creating a virtual environment and preparing a requirements.txt file.

🟢 1. Project Setup

First, create your project folder:

mkdir crypto_data_fetcher 
cd crypto_data_fetcher

Create and activate a Python virtual environment:

python -m venv venv 
# Linux/macOS 
source venv/bin/activate 
# Windows 
venv\Scripts\activate

Create a requirements.txt file with the necessary dependencies:

ccxt 
pandas

Install them:

pip install -r requirements.txt

🟢 2. Script Code

Create a file named fetch_ohlcv.py and paste this complete code:

#!/usr/bin/env python3 
import ccxt 
import pandas as pd 
from datetime import datetime, timedelta 
import argparse 
 
def fetch_ohlcv(symbol, since, until, interval='1h'): 
    """ 
    Download OHLCV data for a given symbol and time range. 
 
    Args: 
        symbol (str): e.g., 'BTC/USDT' 
        since (str): start date in 'YYYY-MM-DD' format 
        until (str): end date in 'YYYY-MM-DD' format 
        interval (str): timeframe (e.g., '1h', '1d') 
 
    Returns: 
        pd.DataFrame: DataFrame with datetime index and OHLCV columns 
    """ 
    exchange = ccxt.binance() 
    since_timestamp = exchange.parse8601(f"{since}T00:00:00Z") 
    until_timestamp = exchange.parse8601(f"{until}T23:59:59Z") 
 
    all_data = [] 
    fetch_since = since_timestamp 
    limit = 1000 
 
    while True: 
        ohlcv = exchange.fetch_ohlcv(symbol, timeframe=interval, since=fetch_since, limit=limit) 
        if not ohlcv: 
            break 
        all_data.extend(ohlcv) 
        last_timestamp = ohlcv[-1][0] 
        if last_timestamp >= until_timestamp: 
            break 
        fetch_since = last_timestamp + 1 
 
    df = pd.DataFrame(all_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume']) 
    df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms') 
    df.set_index('timestamp', inplace=True) 
    df = df[(df.index >= since) & (df.index <= until)] 
    return df 
 
def main(): 
    parser = argparse.ArgumentParser(description="Download OHLCV data from Binance.") 
    parser.add_argument("--symbol", required=True, help="Trading pair symbol, e.g., BTC/USDT") 
    parser.add_argument("--since", required=True, help="Start date YYYY-MM-DD") 
    parser.add_argument("--until", help="End date YYYY-MM-DD (default: tomorrow)") 
    parser.add_argument("--interval", default="1h", help="Time interval, e.g., 1m, 1h, 1d (default: 1h)") 
    parser.add_argument("--output", default="output.csv", help="CSV output file (default: output.csv)") 
 
    args = parser.parse_args() 
 
    if args.until: 
        until_date = args.until 
    else: 
        until_date = (datetime.utcnow() + timedelta(days=1)).strftime("%Y-%m-%d") 
 
    print(f"Fetching data from {args.since} to {until_date} for {args.symbol} at interval {args.interval}") 
 
    df = fetch_ohlcv(args.symbol, args.since, until_date, args.interval) 
 
    print(f"Downloaded {len(df)} rows.") 
    df.to_csv(args.output) 
    print(f"Saved to {args.output}") 
 
if __name__ == "__main__": 
    main()

🟢 3. Usage Examples

✅ Fetch BTC/USDT hourly data for January 2023:

python fetch_ohlcv.py --symbol BTC/USDT --since 2023-01-01 --until 2023-01-31 --interval 1h --output btc_jan.csv

✅ Fetch ETH/USDT daily data since June 2023 until tomorrow:

python fetch_ohlcv.py --symbol ETH/USDT --since 2023-06-01 --interval 1d --output eth_data.csv

If you omit --until, it defaults to tomorrow’s date automatically.

🟢 4. Deactivating the Environment

When you’re done, deactivate your environment:

deactivate

🟢 5. Requirements File

Your requirements.txt is simply:

ccxt 
pandas

If you prefer to generate it automatically, run:

pip freeze > requirements.txt

✅ That’s it! You now have a reusable Python tool for downloading cryptocurrency OHLCV data with clean csv output.