This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
bitmex.py
785 lines (632 loc) · 27.5 KB
/
bitmex.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
"""
trading-server is a multi-asset, multi-strategy, event-driven execution
and backtesting platform (OEMS) for trading common markets.
Copyright (C) 2020 Sam Breznikar <[email protected]>
Licensed under GNU General Public License 3.0 or later.
Some rights reserved. See LICENSE.md, AUTHORS.md.
"""
from requests.packages.urllib3.util.retry import Retry
from datetime import timezone, datetime, timedelta
from requests.adapters import HTTPAdapter
from requests import Request, Session
from requests.auth import AuthBase
from urllib.parse import urlparse
from bitmex_ws import Bitmex_WS
from exchange import Exchange
from dateutil import parser
import traceback
import requests
import hashlib
import json
import hmac
import time
class Bitmex(Exchange):
"""
BitMEX exchange model.
"""
MAX_BARS_PER_REQUEST = 750
TIMESTAMP_FORMAT = '%Y-%m-%d%H:%M:%S.%f'
REQUEST_TIMEOUT = 10
BASE_URL = "https://www.bitmex.com/api/v1"
BASE_URL_TESTNET = "https://testnet.bitmex.com/api/v1"
WS_URL = "wss://www.bitmex.com/realtime"
BARS_URL = "/trade/bucketed?binSize="
TICKS_URL = "/trade?symbol="
POSITIONS_URL = "/position"
ORDERS_URL = "/order"
BULK_ORDERS_URL = "/order/bulk"
TRADE_HIST_URL = "/execution/tradeHistory"
def __init__(self, logger):
super()
self.logger = logger
self.name = "BitMEX"
self.symbols = ["XBTUSD"] # "ETHUSD", "XRPUSD", "BCHUSD", "LTCUSD", "LINKUSDT"]
# Minimum price increment for each instrument.
self.symbol_min_increment = {
'XBTUSD': 0.5,
'ETHUSD': 0.05,
'XRPUSD': 0.0001,
'BCHUSD': 0.05,
'LTCUSD': 0.01,
'LINKUSDT': 0.0005}
# Websocket subscription channels.
self.channels = ["trade"]
# Not needed but saves a few rest polls/saves time.
self.origin_tss = {
"XBTUSD": 1483228800,
"ETHUSD": 1533200520,
"XRPUSD": 1580875200,
'BCHUSD': 1592280000,
'LTCUSD': 1596081600,
'LINKUSDT': 1602820800}
self.api_key, self.api_secret = self.load_api_keys()
# Connect to websocket stream.
self.ws = Bitmex_WS(
self.logger, self.symbols, self.channels, self.WS_URL,
self.api_key, self.api_secret)
if not self.ws.ws.sock.connected:
self.logger.info("Failed to to connect to BitMEX websocket.")
# Set default https request retry behaviour.
retries = Retry(
total=5,
backoff_factor=0.25,
status_forcelist=[502, 503, 504],
method_whitelist=False)
self.session = Session()
self.session.mount('https://', HTTPAdapter(max_retries=retries))
# Non persistent storage for ticks and new 1 min bars.
self.bars = {}
self.ticks = {}
# Note, for future channel subs, create new Bitmex_WS in new process.
def parse_ticks(self):
if not self.ws.ws:
self.logger.info("BitMEX websocket disconnected.")
else:
all_ticks = self.ws.get_ticks()
target_minute = datetime.now().minute - 1
ticks_target_minute = []
tcount = 0
# Search from end of tick list to grab newest ticks first.
for i in reversed(all_ticks):
try:
ts = i['timestamp']
if type(ts) is not datetime:
ts = parser.parse(ts)
except Exception:
self.logger.info(traceback.format_exc())
# Scrape prev minutes ticks.
if ts.minute == target_minute:
ticks_target_minute.append(i)
ticks_target_minute[tcount]['timestamp'] = ts
tcount += 1
# Store the previous-to-target bar's last
# traded price to use as the open price for target bar.
if ts.minute == target_minute - 1:
ticks_target_minute.append(i)
ticks_target_minute[tcount]['timestamp'] = ts
break
ticks_target_minute.reverse()
# Group ticks by symbol.
self.ticks = {i: [] for i in self.symbols}
for tick in ticks_target_minute:
self.ticks[tick['symbol']].append(tick)
# Build bars from ticks.
self.bars = {i: [] for i in self.symbols}
for symbol in self.symbols:
bar = self.build_OHLCV(self.ticks[symbol], symbol)
self.bars[symbol].append(bar)
def get_bars_in_period(self, symbol, start_time, total):
if total >= self.MAX_BARS_PER_REQUEST:
total = self.MAX_BARS_PER_REQUEST
# Convert epoch timestamp to ISO 8601.
start = datetime.utcfromtimestamp(start_time).isoformat()
timeframe = "1m"
payload = (
f"{self.BASE_URL}{self.BARS_URL}{timeframe}&"
f"symbol={symbol}&filter=&count={total}&"
f"startTime={start}&reverse=false")
# self.logger.info("API request string: " + payload)
bars_to_parse = requests.get(payload).json()
# Store only required values (OHLCV) and convert timestamp to epoch.
new_bars = []
for bar in bars_to_parse:
new_bars.append({
'symbol': symbol,
'timestamp': int(parser.parse(bar['timestamp']).timestamp()),
'open': bar['open'],
'high': bar['high'],
'low': bar['low'],
'close': bar['close'],
'volume': bar['volume']})
return new_bars
def get_origin_timestamp(self, symbol: str):
if self.origin_tss[symbol] is not None:
return self.origin_tss[symbol]
else:
payload = (
f"{self.BASE_URL}{self.BARS_URL}1m&symbol={symbol}&filter=&"
f"count=1&startTime=&reverse=false")
response = requests.get(payload).json()[0]['timestamp']
timestamp = int(parser.parse(response).timestamp())
self.logger.info(
"BitMEX" + symbol + " origin timestamp: " + str(timestamp))
return timestamp
def get_recent_bars(self, timeframe, symbol, n=1):
payload = str(
self.BASE_URL + self.BARS_URL + timeframe +
"&partial=false&symbol=" + symbol + "&count=" +
str(n) + "&reverse=true")
result = requests.get(payload).json()
bars = []
for i in result:
bars.append({
'symbol': symbol,
'timestamp': i['timestamp'],
'open': i['open'],
'high': i['high'],
'low': i['low'],
'close': i['close'],
'volume': i['volume']})
return bars
def get_recent_ticks(self, symbol, n=1):
# Find difference between start and end of period.
delta = n * 60
# Find start timestamp and convert to ISO1806.
start_epoch = self.previous_minute() + 60 - delta
start_iso = datetime.utcfromtimestamp(start_epoch).isoformat()
# find end timestamp and convert to ISO1806
end_epoch = self.previous_minute() + 60
end_iso = datetime.utcfromtimestamp(end_epoch).isoformat()
# Initial poll.
time.sleep(1)
payload = str(
self.BASE_URL + self.TICKS_URL + symbol + "&count=" +
"1000&reverse=false&startTime=" + start_iso + "&endTime" + end_iso)
ticks = []
initial_result = requests.get(payload).json()
for tick in initial_result:
ticks.append(tick)
# If 1000 ticks in result (max size), keep polling until
# we get a response with length <1000.
if len(initial_result) == 1000:
maxed_out = True
while maxed_out:
# Dont use endTime as it seems to cut off the final few ticks.
payload = str(
self.BASE_URL + self.TICKS_URL + symbol + "&count=" +
"1000&reverse=false&startTime=" + ticks[-1]['timestamp'])
interim_result = requests.get(payload).json()
for tick in interim_result:
ticks.append(tick)
if len(interim_result) != 1000:
maxed_out = False
# Check median tick timestamp matches start_iso.
median_dt = parser.parse(ticks[int((len(ticks) / 2))]['timestamp'])
match_dt = parser.parse(start_iso)
if median_dt.minute != match_dt.minute:
raise Exception("Tick data timestamp error: timestamp mismatch.")
# Populate list with matching-timestamped ticks only.
final_ticks = [
i for i in ticks if parser.parse(
i['timestamp']).minute == match_dt.minute]
return final_ticks
def get_position(self, symbol):
prepared_request = Request(
'GET',
self.BASE_URL_TESTNET + self.POSITIONS_URL,
params='').prepare()
request = self.generate_request_headers(prepared_request, self.api_key,
self.api_secret)
response = self.session.send(request).json()
for pos in response:
if pos['symbol'] == symbol:
status = "OPEN" if pos['isOpen'] is True else "CLOSED"
direction = "LONG" if pos['currentQty'] > 0 else "SHORT"
return {
'size': pos['currentQty'],
'avg_entry_price': pos['avgEntryPrice'],
'symbol': symbol,
'direction': direction,
'currency': pos['quoteCurrency'],
'opening_timestamp': pos['openingTimestamp'],
'opening_size': pos['openingQty'],
'status': status}
def get_executions(self, symbol, start_timestamp=None, end_timestamp=None, count=500):
# Convert epoch ts's to utc human-readable
start = str(datetime.utcfromtimestamp(start_timestamp)) if start_timestamp else None
end = str(datetime.utcfromtimestamp(end_timestamp)) if end_timestamp else None
payload = {
'symbol': symbol,
'count': count,
'startTime': start,
'endTime': end,
'reverse': True}
prepared_request = Request(
'GET',
self.BASE_URL_TESTNET + self.TRADE_HIST_URL,
json=payload,
params='').prepare()
request = self.generate_request_headers(
prepared_request,
self.api_key,
self.api_secret)
response = self.session.send(request).json()
executions = []
for res in response:
fee_type = "TAKER" if res['lastLiquidityInd'] == "RemovedLiquidity" else "MAKER"
direction = "LONG" if res['side'] == "Buy" else "SHORT"
if res['ordStatus'] == "Filled":
fill = "FILLED"
elif res['ordStatus'] == "Canceled":
fill = "CANCELLED"
elif res['ordStatus'] == "New":
fill = "NEW"
elif res['ordStatus'] == "PartiallyFilled":
fill = "PARTIAL"
else:
raise Exception(res['ordStatus'])
if res['ordType'] == "Limit":
order_type = "LIMIT"
elif res['ordType'] == "Market":
order_type = "MARKET"
elif res['ordType'] == "StopLimit":
order_type = "STOP_LIMIT"
elif res['ordType'] == "Stop":
order_type = "STOP"
else:
raise Exception(res['ordType'])
executions.append({
'order_id': res['clOrdID'],
'venue_id': res['orderID'],
'timestamp': int(parser.parse(res['timestamp']).timestamp()),
'avg_exc_price': res['avgPx'],
'currency': res['currency'],
'symbol': res['symbol'],
'direction': direction,
'size': res['lastQty'],
'order_type': order_type,
'fee_type': fee_type,
'fee_amt': res['commission'],
'total_fee': res['execComm'] / res['avgPx'],
'status': fill})
return executions
def close_position(self, symbol, qty=None, direction=None):
position = self.get_position(symbol)
if direction == "LONG":
amt = -qty
elif direction == "SHORT":
amt = qty
else:
raise Exception(direction)
if qty and direction:
payload = {
'symbol': symbol,
'orderQty': amt,
'ordType': "Market"}
else:
payload = {
'symbol': symbol,
'orderQty': -position['currentQty'],
'ordType': "Market"}
# Don't do anything if closing size or position size is 0.
if payload['orderQty'] != 0 and position['currentQty'] != 0:
prepared_request = Request(
'POST',
self.BASE_URL_TESTNET + self.ORDERS_URL,
json=payload,
params='').prepare()
request = self.generate_request_headers(
prepared_request,
self.api_key,
self.api_secret)
response = self.session.send(request).json()
if response['ordStatus'] == "Filled":
return True
else:
return False
else:
return False
def get_orders(self, symbol=None, start_timestamp=None, count=500):
# Convert epoch ts's to utc human-readable
start = str(datetime.utcfromtimestamp(start_timestamp)) if start_timestamp else None
payload = {
'symbol': symbol,
'count': count,
'startTime': start,
'reverse': True}
prepared_request = Request(
'GET',
self.BASE_URL_TESTNET + self.ORDERS_URL,
params='', json=payload).prepare()
request = self.generate_request_headers(prepared_request, self.api_key,
self.api_secret)
response = self.session.send(request).json()
orders = []
for res in response:
if res['clOrdID']:
direction = "LONG" if res['side'] == "Buy" else "SHORT"
if res['ordStatus'] == "Filled":
fill = "FILLED"
elif res['ordStatus'] == "Canceled":
fill = "CANCELLED"
elif res['ordStatus'] == "New":
fill = "NEW"
elif res['ordStatus'] == "PartiallyFilled":
fill = "PARTIAL"
else:
raise Exception(res['ordStatus'])
if res['ordType'] == "Limit":
order_type = "LIMIT"
elif res['ordType'] == "Market":
order_type = "MARKET"
elif res['ordType'] == "StopLimit":
order_type = "STOP_LIMIT"
elif res['ordType'] == "Stop":
order_type = "STOP"
else:
raise Exception(res['ordType'])
# If "\n" in response text field, use substring after "\n".
if "\n" in res['text']:
text = res['text'].split("\n")
metatype = text[1]
elif (
res['text'] == "ENTRY" or res['text'] == "STOP" or
res['text'] == "TAKE_PROFIT" or
res['text'] == "FINAL_TAKE_PROFIT"):
metatype = res['text']
else:
# raise Exception("Order metatype error:", res['text'])
metatype = None
orders.append({
'order_id': res['clOrdID'],
'venue_id': res['orderID'],
'timestamp': int(parser.parse(res['timestamp']).timestamp()),
'price': res['price'],
'avg_fill_price': res['avgPx'],
'currency': res['currency'],
'venue': "BitMEX",
'symbol': res['symbol'],
'direction': direction,
'size': res['orderQty'],
'order_type': order_type,
'metatype': metatype,
'void_price': res['stopPx'],
'status': fill})
return orders
def place_single_order(self, order):
payload = self.format_orders([order])[0]
prepared_request = Request(
'POST',
self.BASE_URL_TESTNET + self.ORDERS_URL,
json=payload,
params='').prepare()
request = self.generate_request_headers(
prepared_request,
self.api_key,
self.api_secret)
response = self.session.send(request)
return response
def place_bulk_orders(self, orders):
# Separate market orders as BitMEX doesnt allow bulk market orders.
m_o = [o for o in orders if o['order_type'] == "MARKET"]
nm_o = [o for o in orders if o not in m_o]
# Send market orders individually amd store responses.
responses = [self.place_single_order(o) for o in m_o if m_o]
# Submit non-market orders in a single batch.
response = None
if nm_o:
payload = {'orders': self.format_orders(nm_o)}
prepared_request = Request(
'POST',
self.BASE_URL_TESTNET + self.BULK_ORDERS_URL,
json=payload,
params='').prepare()
request = self.generate_request_headers(
prepared_request,
self.api_key,
self.api_secret)
response = self.session.send(request)
# Unpack successful order confirmations and handle errors.
order_confirmations = []
for r in responses + [response]:
if r.status_code == 200:
res = r.json()
if isinstance(res, list):
for item in res:
order_confirmations.append(item)
elif isinstance(res, dict):
order_confirmations.append(res)
elif 401 <= r.status_code <= 404:
# Syntax, auth or system limit error messages, raise exception.
# Code likely wrong if this occurs.
raise Exception(r.status_code, r.json()['error']['message'])
elif r.status_code == 503:
# Server overloaded, retry after 500ms, dont raise exception.
self.logger.info(
str(r.status_code) + " " + r.json()['error']['message'])
else:
self.logger.info(str(r.status_code) + " " + str(r.json()))
updated_orders = []
if order_confirmations:
for res in order_confirmations:
for order in orders:
if order['order_id'] == res['clOrdID']:
if res['ordStatus'] == "Filled":
fill = "FILLED"
elif res['ordStatus'] == "Canceled":
fill = "CANCELLED"
elif res['ordStatus'] == "New":
fill = "NEW"
elif res['ordStatus'] == "PartiallyFilled":
fill = "PARTIAL"
else:
raise Exception(res['ordStatus'])
price = res['stopPx'] if res['stopPx'] else res['price']
new = {
'trade_id': order['trade_id'],
'order_id': order['order_id'],
'venue': order['venue'],
'symbol': order['symbol'],
'order_type': order['order_type'],
'metatype': order['metatype'],
'void_price': order['void_price'],
'direction': order['direction'],
'reduce_only': order['reduce_only'],
'post_only': order['post_only'],
'batch_size': order['batch_size'],
'size': order['size'],
'trail': order['trail'],
'timestamp': int(parser.parse(res['timestamp']).timestamp()),
'avg_fill_price': res['avgPx'],
'currency': res['currency'],
'venue_id': res['orderID'],
'price': price,
'status': fill}
updated_orders.append(new)
return updated_orders
def cancel_orders(self, order_ids: list):
if order_ids[0] is not None:
payload = {"orderID": order_ids}
prepared_request = Request(
"DELETE",
self.BASE_URL_TESTNET + self.ORDERS_URL,
json=payload,
params='').prepare()
request = self.generate_request_headers(
prepared_request,
self.api_key,
self.api_secret)
response = self.session.send(request).json()
response = [response] if not isinstance(response, list) else response
cancel_confs = {}
for i in response:
try:
price = i['stopPx'] if i['ordType'] == "Stop" else i['price']
except KeyError:
print(json.dumps(response, indent=2))
raise Exception("Unexpected response format: ", i)
try:
# Order was filled or cancelled previously.
if i['error'] is not None:
if i['error'] == "Unable to cancel order due to existing state: Filled":
cancel_confs[i['orderID']] = {
'venue_id': i['orderID'],
'order_id': i['clOrdID'],
'status': "FILLED",
'order_type': i['ordType'],
'price': price
}
elif i['error'] == "Unable to cancel order due to existing state: Canceled":
cancel_confs[i['orderID']] = {
'venue_id': i['orderID'],
'order_id': i['clOrdID'],
'status': "CANCELLED",
'order_type': i['ordType'],
'price': price
}
else:
print(json.dumps(i['error'], indent=2))
raise Exception("Unhandled cancellation message case: ", i['error'])
# Order state unchanged since placement.
except KeyError:
if i['ordStatus'] == "Canceled":
cancel_confs[i['orderID']] = {
'venue_id': i['orderID'],
'order_id': i['clOrdID'],
'status': "CANCELLED",
'order_type': i['ordType'],
'price': price
}
else:
print(json.dumps(i['ordStatus'], indent=2))
raise Exception("Unhandled cancellation message case: ", i['ordStatus'])
return cancel_confs
else:
return None
def format_orders(self, orders):
formatted = []
for order in orders:
price = self.round_increment(order['price'], order['symbol'])
# TODO: add logic for execInst and stopPx
execInst = None
stopPx = None
timeInForce = None
symbol = order['symbol']
side = "Buy" if order['direction'] == "LONG" else "Sell"
orderQty = self.round_increment(order['size'], order['symbol'])
clOrdID = order['order_id']
text = order['metatype']
if order['order_type'] == "LIMIT":
ordType = "Limit"
timeInForce = 'GoodTillCancel'
elif order['order_type'] == "MARKET":
ordType = "Market"
price = None
timeInForce = 'ImmediateOrCancel'
elif order['order_type'] == "STOP_LIMIT":
ordType = "StopLimit"
timeInForce = 'GoodTillCancel'
elif order['order_type'] == "STOP":
ordType = "Stop"
stopPx = price
price = None
timeInForce = 'ImmediateOrCancel'
else:
raise Exception("Incorrect order type specified.")
formatted.append({
'symbol': symbol,
'side': side,
'orderQty': orderQty,
'price': price,
'stopPx': stopPx,
'clOrdID': order['order_id'],
'ordType': ordType,
'timeInForce': timeInForce,
'execInst': execInst,
'text': text})
return formatted
def generate_request_signature(self, secret, request_type, url, nonce,
data):
"""
Generate BitMEX-compatible authenticated request signature header.
Args:
secret: API secret key.
request_type: Request type (GET, POST, etc).
url: full request url.
validity: seconds request will be valid for after creation.
Returns:
signature: hex(HMAC_SHA256(apiSecret, verb + path + expires + data)
Raises:
None.
"""
parsed_url = urlparse(url)
path = parsed_url.path
if parsed_url.query:
path = path + '?' + parsed_url.query
if isinstance(data, (bytes, bytearray)):
data = data.decode('utf8')
message = str(request_type).upper() + path + str(nonce) + data
signature = hmac.new(bytes(secret, 'utf8'), bytes(message, 'utf8'),
digestmod=hashlib.sha256).hexdigest()
return signature
def generate_request_headers(self, request, api_key, api_secret):
"""
Add BitMEX-compatible authentication headers to a request object.
Args:
api_key: API key.
api_secret: API secret key.
request: Request object to be amended.
Returns:
request: Modified request object.
Raises:
None.
"""
nonce = str(int(round(time.time()) + self.REQUEST_TIMEOUT))
request.headers['api-expires'] = nonce
request.headers['api-key'] = self.api_key
request.headers['api-signature'] = self.generate_request_signature(
self.api_secret, request.method, request.url, nonce, request.body or '') # noqa
request.headers['Content-Type'] = 'application/json'
request.headers['Accept'] = 'application/json'
request.headers['X-Requested-With'] = 'XMLHttpRequest'
return request