-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_binance_data.py
More file actions
30 lines (24 loc) · 895 Bytes
/
Copy pathget_binance_data.py
File metadata and controls
30 lines (24 loc) · 895 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from binance.client import Client
import pandas as pd
from datetime import datetime
# Initialize the client (no API keys needed for public data)
client = Client()
# Define your custom date range
symbol = "BTCUSDT"
interval = Client.KLINE_INTERVAL_1HOUR
start_date = "1 Jan, 2023"
end_date = "10 Jan, 2023"
# Fetch historical data
klines = client.get_historical_klines(symbol, interval, start_str=start_date, end_str=end_date)
# Convert to DataFrame
df = pd.DataFrame(klines, columns=[
"timestamp", "open", "high", "low", "close", "volume",
"close_time", "quote_asset_volume", "number_of_trades",
"taker_buy_base_volume", "taker_buy_quote_volume", "ignore"
])
# Convert timestamp
df["timestamp"] = pd.to_datetime(df["timestamp"], unit='ms')
df.set_index("timestamp", inplace=True)
# Save to CSV if needed
df.to_csv("BTCUSDT_1H_custom_range.csv")
print("✅ Data saved to CSV.")