-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbinance_api.py
308 lines (268 loc) · 13.2 KB
/
binance_api.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from datetime import datetime
from candle import Candle
import requests
import json
from types import SimpleNamespace
import math
import pytz
def count_m1_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 60000)) / 1000)))
def count_m5_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 300000)) / 1000)))
def count_m15_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 900000)) / 1000)))
def count_m30_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 1800000)) / 1000)))
def count_h1_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 3600000)) / 1000)))
def count_h2_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 7200000)) / 1000)))
def count_h4_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 14400000)) / 1000)))
def count_d1_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 86400000)) / 1000)))
def count_w1_candles(start_timestamp, end_timestamp):
return int(math.ceil((round(((end_timestamp - start_timestamp) / 604800000)) / 1000)))
def get_candles_in_range(symbol, interval, start_time, end_time, limit='1000'):
start_time = int(start_time)
end_time = int(end_time)
headers = {'Content-Type': 'application/json; charset=utf-8', 'X-MBX-APIKEY': 'i8322dmyBsrk9A8dNBliUExbVHmDPbNn1cqvOAHZPWdYc7KGJ6kX767V3Dg7jWmo'}
r = requests.get(f"https://api.binance.com/api/v3/klines?symbol={symbol}&limit={limit}&startTime={start_time}&endTime={end_time}&interval={interval}", headers=headers)
m = json.loads(r.content, object_hook=lambda d: SimpleNamespace(**d))
candles = []
i = 0
for u in m:
otime = datetime.utcfromtimestamp(int(''.join(str(u[0]).split())[:-3])).replace(tzinfo=pytz.UTC)
ctime = datetime.utcfromtimestamp(int(''.join(str(u[6]).split())[:-3])).replace(tzinfo=pytz.UTC)
i += 1
candles.append(Candle(otime.astimezone(pytz.UTC).strftime('%Y-%m-%d %H:%M:%S'), float(u[1]), float(u[2]), float(u[3]), float(u[4]), u[5], ctime.astimezone(pytz.UTC).strftime('%Y-%m-%d %H:%M:%S')))
return candles
def get_candles_m1(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (60000 * 999) + start_timestamp_temp
total_candles_count = count_m1_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading m1 candles...")
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'1m', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'1m', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 60000
end_date_timestamp_temp = (60000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m1 candles finished.")
return arr
def get_candles_m5(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (300000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m5 candles...")
total_candles_count = count_m5_candles(start_timestamp, end_timestamp)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'5m', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'5m', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 300000
end_date_timestamp_temp = (300000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m5 candles finished.")
return arr
def get_candles_m15(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (900000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m15 candles...")
total_candles_count = count_m15_candles(start_timestamp, end_timestamp)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if(end_date_timestamp_temp <= end_timestamp):
candles = get_candles_in_range(symbol, f'15m', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'15m', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 900000
end_date_timestamp_temp = (900000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m15 candles finished.")
return arr
def get_candles_m30(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (1800000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m30 candles...")
total_candles_count = count_m30_candles(start_timestamp, end_timestamp)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if(end_date_timestamp_temp <= end_timestamp):
candles = get_candles_in_range(symbol, f'30m', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'30m', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 1800000
end_date_timestamp_temp = (1800000 * 999) + start_timestamp_temp
if show_log:
print("Downloading m30 candles finished.")
return arr
# This function is not tested completely:
def get_candles_h1(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (3600000 * 999) + start_timestamp_temp
total_candles_count = count_h1_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading h1 candles...")
print("total_candles_count:", total_candles_count)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'1h', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'1h', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 3600000
end_date_timestamp_temp = (3600000 * 999) + start_timestamp_temp
if show_log:
print("Downloading h1 candles finished.")
return arr
def get_candles_h2(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (7200000 * 999) + start_timestamp_temp
total_candles_count = count_h2_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading h2 candles...")
print("total_candles_count:", total_candles_count)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'2h', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'2h', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 7200000
end_date_timestamp_temp = (7200000 * 999) + start_timestamp_temp
if show_log:
print("Downloading h2 candles finished.")
return arr
# This function is not tested completely:
def get_candles_h4(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (14400000 * 999) + start_timestamp_temp
total_candles_count = count_h4_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading h4 candles...")
print("total_candles_count:", total_candles_count)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'4h', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'4h', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 14400000
end_date_timestamp_temp = (14400000 * 999) + start_timestamp_temp
if show_log:
print("Downloading h4 candles finished.")
return arr
# This function is not tested completely:
def get_candles_d1(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (86400000 * 999) + start_timestamp_temp
total_candles_count = count_d1_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading d1 candles...")
print("total_candles_count:", total_candles_count)
total_candles_count = count_d1_candles(start_timestamp, end_timestamp)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'1d', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'1d', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 86400000
end_date_timestamp_temp = (86400000 * 999) + start_timestamp_temp
if show_log:
print("Downloading d1 candles finished.")
return arr
# This function is not tested completely:
def get_candles_w1(start_timestamp, end_timestamp, symbol, show_log=True):
arr = []
start_timestamp = start_timestamp
start_timestamp_temp = start_timestamp
end_date_timestamp_temp = (604800000 * 999) + start_timestamp_temp
total_candles_count = count_w1_candles(start_timestamp, end_timestamp)
if show_log:
print("Downloading w1 candles...")
print("total_candles_count:", total_candles_count)
total_candles_count = count_w1_candles(start_timestamp, end_timestamp)
for item in range(total_candles_count):
if show_log and (item % 10 == 0 or item == total_candles_count - 1):
print(item, "/", total_candles_count)
if end_date_timestamp_temp <= end_timestamp:
candles = get_candles_in_range(symbol, f'1w', str(start_timestamp_temp), str(end_date_timestamp_temp), '1000')
else:
candles = get_candles_in_range(symbol, f'1w', str(start_timestamp_temp), str(end_timestamp), '1000')
for candle in candles:
arr.append(candle)
start_timestamp_temp = end_date_timestamp_temp + 604800000
end_date_timestamp_temp = (604800000 * 999) + start_timestamp_temp
if show_log:
print("Downloading w1 candles finished.")
return arr
def get_candles(timeframe, start_timestamp, end_timestamp, symbol, show_log=True):
if timeframe == "m1":
return get_candles_m1(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "m5":
return get_candles_m5(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "m15":
return get_candles_m15(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "m30":
return get_candles_m30(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "h1":
return get_candles_h1(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "h2":
return get_candles_h2(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "h4":
return get_candles_h4(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "d1":
return get_candles_d1(start_timestamp, end_timestamp, symbol, show_log)
if timeframe == "w1":
return get_candles_w1(start_timestamp, end_timestamp, symbol, show_log)
return None