-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_template.py
More file actions
363 lines (296 loc) · 10.9 KB
/
Copy pathbot_template.py
File metadata and controls
363 lines (296 loc) · 10.9 KB
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
"""CMI Exchange bot framework.
Lightweight bot base class for connecting to the CMI simulated exchange.
"""
import json
import time
from abc import ABC, abstractmethod
from collections.abc import Mapping
from dataclasses import asdict, dataclass
from enum import StrEnum
from functools import cached_property
from threading import Thread
from traceback import format_exc
from typing import Any, Callable, Literal
import requests
import sseclient
STANDARD_HEADERS = {"Content-Type": "application/json; charset=utf-8"}
class DictLikeFrozenDataclassMapping(Mapping):
"""Mixin class to allow frozen dataclasses behave like a dict."""
def __getitem__(self, key: str) -> Any:
return getattr(self, key)
def __iter__(self):
return iter(self.__annotations__)
def __len__(self) -> int:
return len(self.__annotations__)
def to_dict(self) -> dict:
return asdict(self)
def keys(self):
return self.__annotations__.keys()
def values(self):
return [getattr(self, k) for k in self.keys()]
def items(self):
return [(k, getattr(self, k)) for k in self.keys()]
@dataclass(frozen=True)
class Product(DictLikeFrozenDataclassMapping):
symbol: str
tickSize: float
startingPrice: int
contractSize: int
@dataclass(frozen=True)
class Trade(DictLikeFrozenDataclassMapping):
timestamp: str
product: str
buyer: str
seller: str
volume: int
price: float
@dataclass(frozen=True)
class Order(DictLikeFrozenDataclassMapping):
price: float
volume: int
own_volume: int
@dataclass(frozen=True)
class OrderBook(DictLikeFrozenDataclassMapping):
product: str
tick_size: float
buy_orders: list[Order]
sell_orders: list[Order]
class Side(StrEnum):
BUY = "BUY"
SELL = "SELL"
@dataclass(frozen=True)
class OrderRequest:
product: str
price: float
side: Side
volume: int
@dataclass(frozen=True)
class OrderResponse:
id: str
status: Literal["ACTIVE", "PART_FILLED"]
product: str
side: Side
price: float
volume: int
filled: int
user: str
timestamp: str
targetUser: str | None = None
message: str | None = None
class _SSEThread(Thread):
"""Background thread that consumes the CMI SSE stream and dispatches events."""
def __init__(
self,
bearer: str,
url: str,
handle_orderbook: Callable[[OrderBook], Any],
handle_trade_event: Callable[[Trade], Any],
):
super().__init__(daemon=True)
self._bearer = bearer
self._url = url
self._handle_orderbook = handle_orderbook
self._handle_trade_event = handle_trade_event
self._http_stream: requests.Response | None = None
self._client: sseclient.SSEClient | None = None
self._closed = False
def run(self):
while not self._closed:
try:
self._consume()
except (requests.exceptions.ReadTimeout, requests.exceptions.ConnectionError):
pass
except Exception:
if not self._closed:
print("SSE error, reconnecting...")
print(format_exc())
def close(self):
self._closed = True
if self._http_stream:
self._http_stream.close()
if self._client:
self._client.close()
def _consume(self):
headers = {
"Authorization": self._bearer,
"Accept": "text/event-stream; charset=utf-8",
}
self._http_stream = requests.get(self._url, stream=True, headers=headers, timeout=30)
self._client = sseclient.SSEClient(self._http_stream)
for event in self._client.events():
if event.event == "order":
self._on_order_event(json.loads(event.data))
elif event.event == "trade":
data = json.loads(event.data)
trades = data if isinstance(data, list) else [data]
trade_fields = {f.name for f in Trade.__dataclass_fields__.values()}
for t in trades:
self._handle_trade_event(Trade(**{k: v for k, v in t.items() if k in trade_fields}))
def _on_order_event(self, data: dict[str, Any]):
buy_orders = sorted(
[
Order(price=float(price), volume=v["marketVolume"], own_volume=v["userVolume"])
for price, v in data["buyOrders"].items()
],
key=lambda o: -o.price,
)
sell_orders = sorted(
[
Order(price=float(price), volume=v["marketVolume"], own_volume=v["userVolume"])
for price, v in data["sellOrders"].items()
],
key=lambda o: o.price,
)
self._handle_orderbook(OrderBook(data["productsymbol"], data["tickSize"], buy_orders, sell_orders))
class BaseBot(ABC):
"""Base bot for CMI Exchange.
"""
def __init__(self, cmi_url: str, username: str, password: str):
self._cmi_url = cmi_url.rstrip("/")
self.username = username
self._password = password
self._sse_thread: _SSEThread | None = None
# Incremental trade state
self.trades: list[Trade] = []
self._trade_watermark: str | None = None
self._last_trade_fetch: float | None = None
@cached_property
def auth_token(self) -> str:
response = requests.post(
f"{self._cmi_url}/api/user/authenticate",
headers=STANDARD_HEADERS,
json={"username": self.username, "password": self._password},
)
response.raise_for_status()
return response.headers["Authorization"]
# -- lifecycle --
def start(self) -> None:
if self._sse_thread:
raise RuntimeError("Bot already running. Call stop() first.")
self._sse_thread = _SSEThread(
bearer=self.auth_token,
url=f"{self._cmi_url}/api/market/stream",
handle_orderbook=self.on_orderbook,
handle_trade_event=self.on_trades,
)
self._sse_thread.start()
def stop(self) -> None:
if self._sse_thread:
self._sse_thread.close()
self._sse_thread.join(timeout=5)
self._sse_thread = None
# -- callbacks --
@abstractmethod
def on_orderbook(self, orderbook: OrderBook) -> None: ...
@abstractmethod
def on_trades(self, trade: Trade) -> None: ...
# -- market trades (incremental) --
def get_market_trades(self) -> list[Trade]:
"""Fetch new market trades from the exchange and append to self.trades.
Uses incremental loading: only requests trades newer than the last
seen timestamp. Returns the full accumulated list.
"""
params: dict[str, str] = {}
if self._trade_watermark:
params["from"] = self._trade_watermark
response = requests.get(
f"{self._cmi_url}/api/trade",
params=params,
headers=self._auth_headers(),
)
self._last_trade_fetch = time.monotonic()
if not response.ok:
print(f"Failed to fetch trades: {response.status_code}")
return self.trades
new_trades = []
for raw in response.json():
trade = Trade(**raw)
if self._trade_watermark is None or trade.timestamp > self._trade_watermark:
new_trades.append(trade)
if new_trades:
self.trades.extend(new_trades)
self._trade_watermark = new_trades[-1].timestamp
return self.trades
@property
def last_trade_fetch_age(self) -> float | None:
"""Seconds since last get_market_trades() call, or None if never called."""
if self._last_trade_fetch is None:
return None
return time.monotonic() - self._last_trade_fetch
# -- trading helpers --
def send_order(self, order: OrderRequest) -> OrderResponse | None:
response = requests.post(
f"{self._cmi_url}/api/order",
json=asdict(order),
headers=self._auth_headers(),
)
if response.ok:
return OrderResponse(**response.json())
print(f"Order failed: {response.text}")
return None
def send_orders(self, orders: list[OrderRequest]) -> list[OrderResponse]:
results: list[OrderResponse] = []
def _send(o: OrderRequest):
r = self.send_order(o)
if r:
results.append(r)
threads = [Thread(target=_send, args=(o,)) for o in orders]
for t in threads:
t.start()
for t in threads:
t.join()
return results
def cancel_order(self, order_id: str) -> None:
requests.delete(f"{self._cmi_url}/api/order/{order_id}", headers=self._auth_headers())
def cancel_all_orders(self) -> None:
orders = self.get_orders()
threads = [Thread(target=self.cancel_order, args=(o["id"],)) for o in orders]
for t in threads:
t.start()
for t in threads:
t.join()
def get_orders(self, product: str | None = None) -> list[dict]:
params = {"productsymbol": product} if product else {}
response = requests.get(
f"{self._cmi_url}/api/order/current-user",
params=params,
headers=self._auth_headers(),
)
return response.json() if response.ok else []
def get_products(self) -> list[Product]:
response = requests.get(f"{self._cmi_url}/api/product", headers=self._auth_headers())
response.raise_for_status()
return [Product(**p) for p in response.json()]
def get_positions(self) -> dict[str, int]:
response = requests.get(
f"{self._cmi_url}/api/position/current-user",
headers=self._auth_headers(),
)
if response.ok:
return {p["product"]: p["netPosition"] for p in response.json()}
return {}
def get_orderbook(self, product: str) -> OrderBook:
response = requests.get(
f"{self._cmi_url}/api/product/{product}/order-book/current-user",
headers=self._auth_headers(),
)
response.raise_for_status()
data = response.json()
buy_orders = sorted(
[Order(price=e["price"], volume=e["volume"], own_volume=e["userOrderVolume"]) for e in data.get("buy", [])],
key=lambda o: -o.price,
)
sell_orders = sorted(
[Order(price=e["price"], volume=e["volume"], own_volume=e["userOrderVolume"]) for e in data.get("sell", [])],
key=lambda o: o.price,
)
return OrderBook(data["product"], data["tickSize"], buy_orders, sell_orders)
def get_pnl(self) -> dict:
response = requests.get(
f"{self._cmi_url}/api/profit/current-user",
headers=self._auth_headers(),
)
return response.json() if response.ok else {}
# -- internals --
def _auth_headers(self) -> dict[str, str]:
return {**STANDARD_HEADERS, "Authorization": self.auth_token}