-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebsocket.py
183 lines (136 loc) · 3.73 KB
/
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
import asyncio
import json
from dataclasses import dataclass
import dataclasses
from typing import List, Tuple, Union
from enum import Enum
import websockets
WEBSOCKET_URI = "wss://bitnomial.com/exchange/ws"
class ChannelName(Enum):
Trade = "trade"
Book = "book"
Block = "block"
Status = "status"
@dataclass
class Channel:
name: ChannelName
product_codes: List[str]
class SubscribeType(Enum):
Subscribe = "subscribe"
Unsubscribe = "unsubscribe"
@dataclass
class SubscribeMessage:
type: SubscribeType
product_codes: List[str]
channels: List[Channel]
@dataclass
class DisconnectMessage:
type: str # always "disconnect"
reason: str
class MessageType(Enum):
Trade = "trade"
Level = "level"
Book = "book"
Block = "block"
Status = "status"
@dataclass
class Trade:
type: MessageType
ack_id: str
price: int
quantity: int
symbol: str
taker_side: str
timestamp: str
@dataclass
class Level:
type: MessageType
ack_id: str
price: int
quantity: int
side: str
symbol: str
timestamp: str
@dataclass
class Book:
type: MessageType
ack_id: str
asks: List[Tuple[int, int]]
bids: List[Tuple[int, int]]
symbol: str
timestamp: str
class Side(Enum):
Bid = "Bid"
Ask = "Ask"
@dataclass
class BlockTrade:
type: MessageType
ack_id: str
leader_side: str
price: int
quantity: int
symbol: str
timestamp: str
class MarketStatus(Enum):
Open = "Open"
Halt = "Halt"
Closed = "Closed"
@dataclass
class MarketStatusUpdate:
type: MessageType
ack_id: str
state: MarketStatus
symbol: str
timestamp: str
Message = Union[Trade, Level, Book, BlockTrade, MarketStatusUpdate]
class DataclassEnumEncoder(json.JSONEncoder):
def default(self, obj):
if dataclasses.is_dataclass(obj):
return dataclasses.asdict(obj)
if isinstance(obj, Enum):
return obj.value
return super().default(obj)
def parse_message(message: str):
data = json.loads(message)
msg_type = MessageType(data["type"])
if msg_type == MessageType.Trade:
return Trade(**data)
elif msg_type == MessageType.Level:
return Level(**data)
elif msg_type == MessageType.Book:
return Book(**data)
elif msg_type == MessageType.Block:
return BlockTrade(**data)
elif msg_type == MessageType.Status:
return MarketStatusUpdate(**data)
else:
raise ValueError(f"Unknown channel: {msg_type}")
class BitnomialWebSocketClient:
uri: str = WEBSOCKET_URI
def __init__(self, uri=WEBSOCKET_URI):
self.uri = uri
async def connect(self, message: SubscribeMessage):
async with websockets.connect(self.uri) as ws:
await self.send_message(ws, message)
await self.receive_message(ws)
async def send_message(self, ws, message: SubscribeMessage):
await ws.send(json.dumps(message, cls=DataclassEnumEncoder))
async def receive_message(self, ws):
async for message in ws:
parsed_message = parse_message(message)
self.handle_message(parsed_message)
def run(self, message: SubscribeMessage):
asyncio.run(self.connect(message))
def handle_message(self, message: Message):
print(message)
# Example use:
# client = BitnomialWebSocketClient("wss://bitnomial.com/exchange/ws")
# channels = [
# Channel(name=ChannelName.Trade, product_codes=["BUI"]),
# Channel(name=ChannelName.Book, product_codes=["BUI"]),
# Channel(name=ChannelName.Status, product_codes=["BUI"]),
# ]
# message = SubscribeMessage(
# type=SubscribeType.Subscribe, product_codes=["BUI", "BUSO"], channels=channels
# )
# client.run(message)