-
Notifications
You must be signed in to change notification settings - Fork 6
/
bitmex_websocket.py
executable file
·308 lines (262 loc) · 12.6 KB
/
bitmex_websocket.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
import websocket
import threading
import traceback
from time import sleep
import json
import logging
import urllib
import math
import datetime
import pathlib
from util.api_key import generate_nonce, generate_signature
from logging.handlers import TimedRotatingFileHandler
def setup_logger(symbol):
date_string = datetime.datetime.now().strftime("%Y%m%d") # %H:%M:%S"
# save logger info to files
logger = logging.getLogger()
# Change this to DEBUG if you want a lot more info
logger.setLevel(logging.INFO)
# add by xiandong
pathlib.Path('hft_data' + '/' + date_string + '/' +
symbol).mkdir(parents=True, exist_ok=True)
logFilePath = 'hft_data' + '/' + date_string + '/' + symbol + '/log'
handler = TimedRotatingFileHandler(logFilePath, when="M", interval=1)
# “M”: Minutes “H”: Hours “D”: Days
logger.addHandler(handler)
return logger
def format_data(data):
item_string = [', '.join(map(str, item.values())) for item in data]
return item_string
# return data
# Naive implementation of connecting to BitMEX websocket for streaming realtime data.
# The Marketmaker still interacts with this as if it were a REST Endpoint, but now it can get
# much more realtime data without polling the hell out of the API.
#
# The Websocket offers a bunch of data as raw properties right on the object.
# On connect, it synchronously asks for a push of all this data then returns.
# Right after, the MM can start using its data. It will be updated in realtime, so the MM can
# poll really often if it wants.
class BitMEXWebsocket:
# Don't grow a table larger than this amount. Helps cap memory usage.
MAX_TABLE_LEN = 200
def __init__(self, endpoint, symbol, api_key=None, api_secret=None):
'''Connect to the websocket and initialize data stores.'''
self.logger = setup_logger(symbol)
# self.logger.debug("Initializing WebSocket.")
self.endpoint = endpoint
self.symbol = symbol
if api_key is not None and api_secret is None:
raise ValueError('api_secret is required if api_key is provided')
if api_key is None and api_secret is not None:
raise ValueError('api_key is required if api_secret is provided')
self.api_key = api_key
self.api_secret = api_secret
self.data = {}
self.keys = {}
self.exited = False
# We can subscribe right in the connection querystring, so let's build that.
# Subscribe to all pertinent endpoints
wsURL = self.__get_url()
# self.logger.info("Connecting to %s" % wsURL)
self.__connect(wsURL, symbol)
# self.logger.info('Connected to WS.')
# Connected. Wait for partials
self.__wait_for_symbol(symbol)
if api_key:
self.__wait_for_account()
# self.logger.info('Got all market data. Starting.')
def exit(self):
'''Call this to exit - will close websocket.'''
self.exited = True
self.ws.close()
def get_instrument(self):
'''Get the raw instrument data for this symbol.'''
# Turn the 'tickSize' into 'tickLog' for use in rounding
instrument = self.data['instrument'][0]
instrument['tickLog'] = int(
math.fabs(math.log10(instrument['tickSize'])))
return instrument
def get_ticker(self):
'''Return a ticker object. Generated from quote and trade.'''
lastQuote = self.data['quote'][-1]
lastTrade = self.data['trade'][-1]
ticker = {
"last": lastTrade['price'],
"buy": lastQuote['bidPrice'],
"sell": lastQuote['askPrice'],
"mid": (float(lastQuote['bidPrice'] or 0) + float(lastQuote['askPrice'] or 0)) / 2
}
# The instrument has a tickSize. Use it to round values.
instrument = self.data['instrument'][0]
return {k: round(float(v or 0), instrument['tickLog']) for k, v in ticker.items()}
def funds(self):
'''Get your margin details.'''
return self.data['margin'][0]
def market_depth(self):
'''Get market depth (orderbook). Returns all levels.'''
# {'symbol': 'XBTUSD', 'id': 15500000000, 'side': 'Sell', 'size': 1, 'price': 1000000}
return [list(item.values()) for item in self.data['orderBook10']]
def open_orders(self, clOrdIDPrefix):
'''Get all your open orders.'''
orders = self.data['order']
# Filter to only open orders (leavesQty > 0) and those that we actually placed
return [o for o in orders if str(o['clOrdID']).startswith(clOrdIDPrefix) and o['leavesQty'] > 0]
def recent_trades(self):
'''Get recent trades.'''
# {'timestamp': '2019-01-05T11:31:45.560Z', 'symbol': 'XBTUSD', 'side': 'Sell', 'size': 100, 'price': 3818.5, 'tickDirection': 'ZeroMinusTick', 'trdMatchID': '829c01f2-8594-9a45-8cf2-aeb48a3ca122', 'grossValue': 2618800, 'homeNotional': 0.026188, 'foreignNotional': 100}
return [list(item.values()) for item in self.data['trade']]
#
# End Public Methods
#
def __connect(self, wsURL, symbol):
'''Connect to the websocket in a thread.'''
self.logger.debug("Starting thread")
self.ws = websocket.WebSocketApp(wsURL,
on_message=self.__on_message,
on_close=self.__on_close,
on_open=self.__on_open,
on_error=self.__on_error,
header=self.__get_auth())
self.wst = threading.Thread(target=lambda: self.ws.run_forever())
self.wst.daemon = True
self.wst.start()
# self.logger.debug("Started thread")
# Wait for connect before continuing
conn_timeout = 5
while not self.ws.sock or not self.ws.sock.connected and conn_timeout:
sleep(1)
conn_timeout -= 1
if not conn_timeout:
self.logger.error("Couldn't connect to WS! Exiting.")
self.exit()
raise websocket.WebSocketTimeoutException(
'Couldn\'t connect to WS! Exiting.')
def __get_auth(self):
'''Return auth headers. Will use API Keys if present in settings.'''
if self.api_key:
# self.logger.info("Authenticating with API Key.")
# To auth to the WS using an API key, we generate a signature of a nonce and
# the WS API endpoint.
expires = generate_nonce()
return [
"api-expires: " + str(expires),
"api-signature: " +
generate_signature(self.api_secret, 'GET',
'/realtime', expires, ''),
"api-key:" + self.api_key
]
else:
self.logger.info("Not authenticating.")
return []
def __get_url(self):
'''
Generate a connection URL. We can define subscriptions right in the querystring.
Most subscription topics are scoped by the symbol we're listening to.
'''
# You can sub to orderBook10 for all levels, or orderBook10 for top 10 levels & save bandwidth
# symbolSubs = ["execution", "instrument", "order", "orderBook10", "position", "quote", "trade"]
symbolSubs = ["instrument", "order",
"orderBook10", "position", "quote", "trade"]
genericSubs = ["margin"]
subscriptions = [sub + ':' + self.symbol for sub in symbolSubs]
subscriptions += genericSubs
urlParts = list(urllib.parse.urlparse(self.endpoint))
urlParts[0] = urlParts[0].replace('http', 'ws')
urlParts[2] = "/realtime?subscribe={}".format(','.join(subscriptions))
return urllib.parse.urlunparse(urlParts)
def __wait_for_account(self):
'''On subscribe, this data will come down. Wait for it.'''
# Wait for the keys to show up from the ws
while not {'margin', 'position', 'order', 'orderBook10'} <= set(self.data):
sleep(0.1)
def __wait_for_symbol(self, symbol):
'''On subscribe, this data will come down. Wait for it.'''
while not {'instrument', 'trade', 'quote'} <= set(self.data):
sleep(0.1)
def __send_command(self, command, args=None):
'''Send a raw command.'''
if args is None:
args = []
self.ws.send(json.dumps({"op": command, "args": args}))
def __on_message(self, message):
'''Handler for parsing WS messages.'''
message = json.loads(message)
# self.logger.debug(json.dumps(message))
table = message['table'] if 'table' in message else None
action = message['action'] if 'action' in message else None
try:
if table in ['trade', 'orderBook10']:
# add by xiandong as we only record 'trade', 'orderBook10' currently.
if 'subscribe' in message:
self.logger.debug("Subscribed to %s." %
message['subscribe'])
elif action:
if table not in self.data:
self.data[table] = []
# There are four possible actions from the WS:
# 'partial' - full table image
# 'insert' - new row
# 'update' - update row
# 'delete' - delete row
if action == 'partial':
self.logger.info("{}_partial {}".format(
table, format_data(message['data'])))
self.data[table] += message['data']
# Keys are communicated on partials to let you know how to uniquely identify
# an item. We use it for updates.
self.keys[table] = message['keys']
elif action == 'insert':
self.logger.info("{}_inserting {}".format(
table, format_data(message['data'])))
self.data[table] += message['data']
elif action == 'update':
self.logger.info("{}_updating {}".format(
table, format_data(message['data'])))
# Locate the item in the collection and update it.
for updateData in message['data']:
item = findItemByKeys(
self.keys[table], self.data[table], updateData)
if not item:
return # No item found to update. Could happen before push
item.update(updateData)
# Remove cancelled / filled orders
if table == 'order' and item['leavesQty'] <= 0:
self.data[table].remove(item)
elif action == 'delete':
self.logger.info("{}_deleting {}".format(
table, format_data(message['data'])))
# Locate the item in the collection and remove it.
for deleteData in message['data']:
item = findItemByKeys(
self.keys[table], self.data[table], deleteData)
self.data[table].remove(item)
else:
raise Exception("Unknown action: %s" % action)
except:
self.logger.error(traceback.format_exc())
def __on_error(self, error):
'''Called on fatal websocket errors. We exit on these.'''
if not self.exited:
self.logger.error("Error : %s" % error)
raise websocket.WebSocketException(error)
def __on_open(self):
'''Called when the WS opens.'''
self.logger.debug("Websocket Opened.")
def __on_close(self):
'''Called on websocket close.'''
self.logger.info('Websocket Closed')
# Utility method for finding an item in the store.
# When an update comes through on the websocket, we need to figure out which item in the array it is
# in order to match that item.
#
# Helpfully, on a data push (or on an HTTP hit to /api/v1/schema), we have a "keys" array. These are the
# fields we can use to uniquely identify an item. Sometimes there is more than one, so we iterate through all
# provided keys.
def findItemByKeys(keys, table, matchData):
for item in table:
matched = True
for key in keys:
if item[key] != matchData[key]:
matched = False
if matched:
return item