-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitmex.py
More file actions
147 lines (126 loc) · 4.96 KB
/
bitmex.py
File metadata and controls
147 lines (126 loc) · 4.96 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
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
import requests
import datetime
import time
import bitmex_auth
import config
baseurl = 'https://www.bitmex.com/api/v1/'
auth = bitmex_auth.APIKeyAuth()
def print_request_info(request):
print("URL: " + request.url)
if "x-ratelimit-remaining" in request.headers:
print("x-ratelimit-remaining: %d " % (int(request.headers["x-ratelimit-remaining"])))
else:
print("x-ratelimit-remaining: HEADER DOES NOT EXIST!")
def get_1day_data(start_datetime, print_info=False):
url = baseurl + "trade/bucketed?binSize=5m&partial=false&symbol=XBT&count=288&startTime=" + start_datetime.isoformat()[:-9]
r = requests.get(url, auth=auth)
if (print_info):
print_request_info(r)
return r.json()
def get_1hour_data(start_datetime, print_info=False, count=60):
url = baseurl + "trade/bucketed?binSize=1m&partial=false&symbol=XBT&count=" + str(count) + "&startTime=" + start_datetime.isoformat()[:-9]
r = requests.get(url, auth=auth)
if (print_info):
print_request_info(r)
return r.json()
def get_5minutes_all_data(start_5minutes_datetime, print_info=True, limit=True): # May need optimizing
end_5minutes_datetime = start_5minutes_datetime + datetime.timedelta(minutes=5)
have_all_data = False
start = 0
data_5minutes_all = []
while (not have_all_data):
url = baseurl + "trade?symbol=XBT&count=500&columns=price&startTime=" + start_5minutes_datetime.isoformat()[:-9] \
+ "&endTime=" + end_5minutes_datetime.isoformat()[:-9] + "&start=" + str(start)
r = requests.get(url, auth=auth)
if (print_info):
print_request_info(r)
one_query_data = r.json()
data_5minutes_all.append(one_query_data)
if len(one_query_data) < 500:
have_all_data = True
elif len(one_query_data) == 500:
start += 500
# and make another query
else:
print("WTF, error.")
sys.exit()
if (limit):
if "x-ratelimit-remaining" in r.headers and int(r.headers["x-ratelimit-remaining"]) < 10:
print("x-ratelimit-remaining below 10, waiting 10 seconds to say in 30/requests per minute...")
time.sleep(10)
return data_5minutes_all
def trade_buy(start_price):
params={
'symbol': 'XBTUSD',
'ordType': 'Market',
'orderQty': config.one_trade_amount
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
# Add stop loss
params={
'symbol': 'XBTUSD',
'ordType': 'Stop',
'orderQty': -config.one_trade_amount,
'stopPx': start_price - config.stop_loss_diff
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
def close_buy_trade():
if position_is_open(): # Position may be closed by stop-loss order
params={
'symbol': 'XBTUSD',
'ordType': 'Market',
'orderQty': -config.one_trade_amount
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
# Add cancel stop loss
r = requests.delete(baseurl + 'order/all', auth=auth)
print(r.json())
def trade_sell(start_price):
params={
'symbol': 'XBTUSD',
'ordType': 'Market',
'orderQty': -config.one_trade_amount
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
# Add stop loss
params={
'symbol': 'XBTUSD',
'ordType': 'Stop',
'orderQty': config.one_trade_amount,
'stopPx': start_price + config.stop_loss_diff
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
def close_sell_trade():
if position_is_open(): # Positions may be closed by stop-loss order
params={
'symbol': 'XBTUSD',
'ordType': 'Market',
'orderQty': config.one_trade_amount
}
r = requests.post(baseurl + 'order', params=params, auth=auth)
print(r.json())
# Add cancel stop loss
r = requests.delete(baseurl + 'order/all', auth=auth)
print(r.json())
def position_is_open():
r = requests.get(baseurl + 'position', auth=auth)
json_data = r.json()
return json_data[0]['isOpen']
def get_wallet_info():
r = requests.get(baseurl + 'user/walletHistory', auth=auth)
json_data = r.json()
print("x-ratelimit-remaining check: %d " % (int(r.headers["x-ratelimit-remaining"])))
return json_data[0]
def print_wallet():
wallet_info = get_wallet_info()
bitcoin_count_in_wallet = wallet_info['walletBalance']
last_transaction_amount = wallet_info['amount']
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$ Wallet balance right now: >> " + str(bitcoin_count_in_wallet / 100000000) + " BTC << $$$")
print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$")
print("$$$ Last transaction amount: " + str(last_transaction_amount) + " satoshis")