forked from slavazhulanov/BinanceBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data.py
More file actions
42 lines (31 loc) · 1.31 KB
/
get_data.py
File metadata and controls
42 lines (31 loc) · 1.31 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
import requests
import json
import pandas as pd
symbol = 'BTCUSDT'
url = 'https://api.binance.com/api/v3/klines'
params = {
'symbol': symbol,
'interval': '15m',
'startTime': '1503038700000', # начало данных в миллисекундах
'endTime': '1681627382188',
'limit': 1000, # максимальное количество свечей за один запрос
}
headers = {
'Accept': 'application/json',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
}
data = []
while True:
response = requests.get(url, headers=headers, params=params)
response_json = json.loads(response.text)
data += response_json
if len(response_json) < 1000:
break
params['startTime'] = response_json[-1][0] + 1
df = pd.DataFrame(data, columns=['timestamp', 'open', 'high', 'low',
'close', 'volume', 'close_time', 'quote_asset_volume',
'number_of_trades', 'taker_buy_base_asset_volume',
'taker_buy_quote_asset_volume', 'ignore'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='ms')
df['close_time'] = pd.to_datetime(df['close_time'], unit='ms')
df.to_csv('BTCUSDT.csv', index=False)