加密数字货币市场历史数据获取API(含源代码)
数字货币市场历史数据获取API(含源代码)
在之前的文章中,数字货币交易接口实现(含源代码),分享了交易接口以及实时行情数据接口的实现。
如果要进行量化回测分析, 就需要准备历史行情数据,今天分享3个API接口,用于访问加密货币的历史市场数据,1分钟级别的数据足够使用了。涵盖的API包括:
- Binance币安
- CoinGecko
- CryptoCompare
1. Binance API
币安提供全面的交易数据API,包括历史K线数据。下面是一个使用Python检索1分钟级数据的示例代码:
import requests
import pandas as pd
def get_binance_klines(symbol='BTCUSDT', interval='1m', limit=1000):
"""
获取 Binance 交易对的历史 K 线数据。
参数:
- symbol: 交易对(例如 BTCUSDT)
- interval: 时间间隔(例如 '1m', '5m', '1h', '1d')
- limit: 返回数据的数量限制(最多 1000)
返回:
- DataFrame 包含时间、开盘价、最高价、最低价、收盘价、交易量。
"""
url = "https://api.binance.com/api/v3/klines"
params = {
'symbol': symbol,
'interval': interval,
'limit': limit
}
response = requests.get(url, params=params)
data = response.json()
# 创建 DataFrame
columns = ['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume', 'Close Time',
'Quote Asset Volume', 'Number of Trades', 'Taker Buy Base Volume',
'Taker Buy Quote Volume', 'Ignore']
df = pd.DataFrame(data,