-
Notifications
You must be signed in to change notification settings - Fork 7
/
coinbase.py
103 lines (95 loc) · 3.86 KB
/
coinbase.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
from openmesh.stream_processing.standardiser import Standardiser
from openmesh.off_chain import Coinbase
from decimal import Decimal
from dateutil import parser
import logging
class CoinbaseStandardiser(Standardiser):
exchange = Coinbase
async def _trade(self, message):
msg = dict(
symbol=self.normalise_symbol(message['product_id']),
price=Decimal(message['price']),
size=Decimal(message['size']),
taker_side=message['side'],
trade_id=str(message['trade_id']),
maker_order_id=message['maker_order_id'],
taker_order_id=message['taker_order_id'],
event_timestamp=int(parser.isoparse(
message['time']).timestamp() * 1000),
atom_timestamp=message['atom_timestamp']
)
await self.send_to_topic("trades_l3", **msg)
async def _open(self, message):
msg = dict(
symbol=self.normalise_symbol(message['product_id']),
price=Decimal(message['price']),
size=Decimal(message['remaining_size']),
side=message['side'],
order_id=message['order_id'],
event_timestamp=int(parser.isoparse(
message['time']).timestamp() * 1000),
atom_timestamp=message['atom_timestamp']
)
await self.send_to_topic("lob_l3", **msg)
async def _done(self, message):
if 'price' not in message or not message['price']:
return
msg = dict(
symbol=self.normalise_symbol(message['product_id']),
price=Decimal(message['price']),
size=Decimal(message['remaining_size']),
side=message['side'],
order_id=message['order_id'],
event_timestamp=int(parser.isoparse(
message['time']).timestamp() * 1000),
atom_timestamp=message['atom_timestamp']
)
await self.send_to_topic("lob_l3", **msg)
async def _change(self, message):
if 'price' not in message or not message['price']:
return
msg = dict(
symbol=self.normalise_symbol(message['product_id']),
price=Decimal(message['price']),
size=Decimal(message['new_size']),
side=message['side'],
order_id=message['order_id'],
event_timestamp=int(parser.isoparse(
message['time']).timestamp() * 1000),
atom_timestamp=message['atom_timestamp']
)
await self.send_to_topic("lob_l3", **msg)
async def _ticker(self, message):
msg = dict(
symbol=self.normalise_symbol(message['product_id']),
ask_price=Decimal(message['best_ask']),
bid_price=Decimal(message['best_bid']),
event_timestamp=int(parser.isoparse(
message['time']).timestamp() * 1000),
atom_timestamp=message['atom_timestamp'],
ask_size=-1,
bid_size=-1
)
await self.send_to_topic("ticker", **msg)
async def handle_message(self, msg):
if 'type' in msg:
if msg['type'] == 'match' or msg['type'] == 'last_match':
await self._trade(msg)
elif msg['type'] == 'open':
await self._open(msg)
elif msg['type'] == 'done':
await self._done(msg)
elif msg['type'] == 'change':
await self._change(msg)
elif msg['type'] == 'batch_ticker' or msg['type'] == 'ticker':
await self._ticker(msg)
elif msg['type'] == 'received':
pass
elif msg['type'] == 'activate':
pass
elif msg['type'] == 'subscriptions':
pass
else:
logging.warning(f"{self.id}: Unhandled message: {msg}")
else:
logging.warning(f"{self.id}: Unhandled message: {msg}")