Implementation:51bitquant Ai hedge fund crypto BinanceDataProvider get history klines with end time
| Knowledge Sources | |
|---|---|
| Domains | Data_Acquisition, Crypto_Markets |
| Last Updated | 2026-10-28 14:00 GMT |
Overview
Concrete tool for fetching recent OHLCV candlestick data bounded by an end timestamp from Binance futures provided by the `BinanceDataProvider` class.
Description
The get_history_klines_with_end_time method retrieves up to `limit` candles ending at `end_time` for a symbol/timeframe. It delegates to the vendored `Client.futures_historical_klines_with_end_time`, builds a `pd.DataFrame` with the canonical `COLUMNS` schema, coerces numerics, and converts timestamps to datetime. Unlike get_historical_klines it performs no CSV caching and returns an empty `DataFrame` on error.
Usage
Use this method in live trading and per-bar backtest steps where you need the most recent candles up to a specific point in time (e.g. the close time of the current bar). It is called by `DataNode.__call__` for each ticker and interval during the LangGraph pipeline.
Code Reference
Source Location
- Repository: ai-hedge-fund-crypto
- File: src/utils/binance_data_provider.py
Signature
class BinanceDataProvider:
def get_history_klines_with_end_time(
self,
symbol: str,
timeframe: str,
end_time: datetime,
limit: int = 500,
) -> 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"` |
| timeframe | str | Yes | Kline interval e.g. `"1h"` |
| end_time | datetime | Yes | Upper bound for the returned candles |
| limit | int | No | Maximum candles to fetch (default `500`) |
Outputs
| Name | Type | Description |
|---|---|---|
| return | pd.DataFrame | OHLCV DataFrame with `COLUMNS` schema; `open_time`/`close_time` as datetime; empty on error |
Usage Examples
Live Multi-Timeframe Fetch
from datetime import datetime, timedelta
from src.utils.binance_data_provider import BinanceDataProvider
provider = BinanceDataProvider()
end = datetime.now()
# Fetch the most recent 500 1h candles ending now
df = provider.get_history_klines_with_end_time(
symbol="BTCUSDT",
timeframe="1h",
end_time=end,
limit=500,
)
print(df[["open_time", "close_time", "close"]].tail())
Used Inside a DataNode (per-bar)
# Reproduces the DataNode.__call__ data fetch
end_time = state["data"]["end_date"] + timedelta(milliseconds=500)
df = provider.get_history_klines_with_end_time(
symbol=ticker,
timeframe=interval.value,
end_time=end_time,
)