-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbitmex.py
68 lines (58 loc) · 2.1 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
import json
from base import CryptoExchange
class BitMEX(CryptoExchange):
url = "wss://www.bitmex.com/realtime"
all_markets = ['ada_btc', 'bch_btc', 'xrp_btc', 'btc_usd', 'eth_btc', 'ltc_btc']
need_common_names = True
# Thesse are all the BitMex supports, hence a static map
# Also FIXME, this is messy
names_map = {
# bitmex to normal humans
"ADAM18": 'ada_btc',
"BCHM18": 'bch_btc',
"XRPM18": 'xrp_btc',
"XBTUSD": 'btc_usd',
"ETHM18": 'eth_btc',
"LTCM18": 'ltc_btc',
# normal to bitmex
"ada_btc": "ADAM18",
'bch_btc': "BCHM18",
'xrp_btc': "XRPM18",
'btc_usd': "XBTUSD",
'eth_btc': "ETHM18",
'ltc_btc': "LTCM18",
}
# # {"op": "subscribe", "args":["instrument:ADAM18","instrument:XBTUSD"]}
# def __init__(self, markets):
# super().__init__(markets)
async def subscribe_ticker(self):
# {"op": "subscribe", "args":["instrument:ADAM18","instrument:XBTUSD"]}
# https://www.bitmex.com/app/wsAPI#Subscriptions
request = {
'op': 'subscribe',
'args': ['quote:{}'.format(m) for m in self.markets_native]
}
await self._ws.send(json.dumps(request))
async def on_message(self, json_payload):
try:
updates = json_payload['data']
for ticker_msg in updates:
market = self._market_names_map[ticker_msg['symbol']]
await self.on_ticker(market, ticker_msg)
except:
pass
async def on_ticker(self, market, data):
timestamp = CryptoExchange.iso8601_to_ts(data['timestamp'])
await self.update_ticker(
market,
best_bid=float(data['bidPrice']),
bid_size=float(data['bidSize']),
best_ask=float(data['askPrice']),
ask_size=float(data['askSize']),
timestamp=timestamp
)
@classmethod
def denormalize_market_name(cls, market_name: str):
# xxx_yyy -> XXXYYY
# Normalized to Exchange specific
return BitMEX.names_map[market_name]