-
Notifications
You must be signed in to change notification settings - Fork 50
/
fetchTrade.py
65 lines (55 loc) · 1.65 KB
/
fetchTrade.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
import argparse
import json
import csv
import sys
import time
from lib import bitmex
from settings import API_BASE
parser = argparse.ArgumentParser(description='Fetch trade history from BitMEX.')
parser.add_argument('--path', type=str, help='Path')
parser.add_argument('--symbol', type=str, help='Symbol filter.')
parser.add_argument('--filter', type=str, help='Query filter as JSON.')
parser.add_argument('--binSize', type=str, help='Bin Size.')
parser.add_argument('--sleep', type=float, help='Sleep time for big requests.')
args = parser.parse_args()
# Validate Args
if args.filter:
# Verify if it's proper JSON
try:
json.loads(args.filter)
except ValueError as e:
raise ValueError("Filter is not valid JSON! Make sure to single-quote the string.")
# Create connector
connector = bitmex.BitMEX(base_url=API_BASE)
# Do trade history query
path = "trade"
if args.path:
path = args.path
count = 500 # max API will allow
query = {
'reverse': 'false',
'start': 0,
'count': count,
'filter': args.filter
}
if args.symbol:
query['symbol'] = args.symbol
if args.binSize:
query['binSize'] = args.binSize
csvwriter = None
while True:
data = connector._curl_bitmex(path=path, verb="GET", query=query, timeout=10)
if args.sleep:
time.sleep(args.sleep)
if csvwriter is None:
# csv requires dict keys
if len(data) == 0:
break
keys = sorted(data[0].keys())
# Write to stdout
csvwriter = csv.DictWriter(sys.stdout, fieldnames=keys)
csvwriter.writeheader()
csvwriter.writerows(data)
query['start'] += count
if len(data) < count:
break