Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:51bitquant Ai hedge fund crypto BinanceDataProvider get historical klines

From Leeroopedia
Knowledge Sources
Domains Data_Acquisition, Crypto_Markets
Last Updated 2026-10-28 14:00 GMT

Overview

Concrete tool for fetching historical OHLCV candlestick data from Binance with on-disk CSV caching provided by the `BinanceDataProvider` class.

Description

The get_historical_klines method retrieves candlestick (kline) data for a symbol and timeframe over a date range. It delegates to the vendored `Client.get_historical_klines`, builds a `pd.DataFrame` using the canonical `COLUMNS` schema, coerces `NUMERIC_COLUMNS` and converts `open_time`/`close_time` to datetime. When `use_cache=True` (default) it reads from `./cache/{symbol}_{timeframe}_{start}_{end}.csv` if present, otherwise fetches and writes the cache. On error it returns an empty `DataFrame`.

Usage

Use this method to pre-fetch the full historical data window for a backtest. It is called by `Backtester.prefetch_data()` once per ticker for the `primary_interval`. For live/current data bounded by an end time, use Implementation:51bitquant_Ai_hedge_fund_crypto_BinanceDataProvider_get_history_klines_with_end_time instead.

Code Reference

Source Location

Signature

class BinanceDataProvider:
    def __init__(self, api_key: Optional[str] = None, api_secret: Optional[str] = None):
        self.client = Client(api_key=api_key, api_secret=api_secret)
        self.cache_dir = Path("./cache")
        self.cache_dir.mkdir(exist_ok=True)

    def get_historical_klines(
        self,
        symbol: str,
        timeframe: str,
        start_date: Optional[datetime] = None,
        end_date: Optional[datetime] = None,
        use_cache: bool = True,
    ) -> pd.DataFrame:

Import

from src.utils.binance_data_provider import BinanceDataProvider

I/O Contract

Inputs

Name Type Required Description
symbol str Yes Trading symbol e.g. `"BTCUSDT"` (slashes stripped)
timeframe str Yes Kline interval e.g. `"1h"`, `"30m"`, `"1d"`
start_date datetime No Backtest start; defaults to `now - 30 days`
end_date datetime No Backtest end; defaults to `now`
use_cache bool No Read/write CSV cache (default `True`)

Outputs

Name Type Description
return pd.DataFrame OHLCV DataFrame with `COLUMNS` schema; `open_time`/`close_time` as datetime; empty on error
side effect CSV file Writes `./cache/{symbol}_{tf}_{start}_{end}.csv` when caching

Usage Examples

Backtest Pre-fetch

from datetime import datetime, timedelta
from src.utils.binance_data_provider import BinanceDataProvider

provider = BinanceDataProvider()
start = datetime(2024, 1, 1)
end = datetime(2024, 6, 1)

# Fetch BTCUSDT 1h candles for the full backtest window
df = provider.get_historical_klines(
    symbol="BTCUSDT",
    timeframe="1h",
    start_date=start,
    end_date=end,
    use_cache=True,
)
print(df[["open_time", "close", "volume"]].tail())

Disable Caching

# Always hit the API (no CSV read/write)
df = provider.get_historical_klines("ETHUSDT", "4h", start, end, use_cache=False)

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment