File size: 771 Bytes
e7eba34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
"""
Binance API Handler – public OHLC data
"""
import requests, pandas as pd

BINANCE = "https://api.binance.com/api/v3"

def get_binance_data(symbols=None, limit=500):
    if symbols is None:
        symbols = ["BTCUSDT", "ETHUSDT", "BNBUSDT"]
    frames = []
    for s in symbols:
        url = f"{BINANCE}/klines?symbol={s}&interval=1d&limit={limit}"
        r = requests.get(url).json()
        df = pd.DataFrame(r, columns=[
            "t","o","h","l","c","v","ct","qv","tr","tb","tbv","ig"])
        df = df[["t","c"]].rename(columns={"t":"timestamp","c":"close"})
        df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
        df["close"] = df["close"].astype(float)
        df["symbol"] = s
        frames.append(df)
    return pd.concat(frames)