-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmex_requests.py
75 lines (60 loc) · 2.28 KB
/
bitmex_requests.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
import requests;
import json;
import time;
from datetime import datetime
from datetime import timedelta
requestNum = 0;
#generates a key-value pair in string format
def generateFilter(currency):
filterDictionary = {};
filterDictionary["symbol"] = currency;
filter = json.dumps(filterDictionary);
return filter;
#makes api requests, doesn't exceed limit of entries per request
def getJsonChunk(currency, startTime, endTime, interval, columns):
filter = generateFilter(currency);
start = startTime;
end = endTime;
parameters = {"binSize": interval, "columns": columns, "filter": filter, "startTime": start, "endTime": end};
response = requests.get("https://www.bitmex.com/api/v1/trade/bucketed", params = parameters);
global requestNum;
requestNum += 1;
data = response.json();
print(response.status_code);
print (requestNum);
return data;
#writes the output json out to the output file
def writeOut(output, file):
with open(file, 'a') as outfile:
json.dump(output, outfile);
#returns a datetime object 480 minutes later
def jump(time, interval):
if interval == '1m':
nextTime = time + timedelta(minutes = 480);
if interval == '1h':
nextTime = time + timedelta(hours = 480);
if interval == '1d':
nextTime = time + timedelta(days = 480);
return nextTime;
#returns a string from the datetime object
def getTimeString(time):
timeString = time.strftime("%Y-%m-%d %H:%M");
return timeString;
#supports requesting data that exceed limit of entries per request
def writeJson(currency, startTime, endTime, interval, columns, file):
#creates an empty output file
open(file, "w").close();
currentTime = startTime;
output = [];
#makes api requests in 480 minutes chunks until the endTime is reached
while currentTime < endTime:
nextTime = jump(currentTime, interval);
print(getTimeString(currentTime));
output += getJsonChunk(currency, getTimeString(currentTime), getTimeString(nextTime), interval, columns);
currentTime = jump(currentTime, interval);
if requestNum > 120:
print("reached maximum amount of requests");
time.sleep(320);
global requestNum;
requestNum = 0;
writeOut(output, file);