-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderbook.py
657 lines (567 loc) · 34.7 KB
/
orderbook.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
from config import * # import needed package
class EHKModel:
def __init__ (self, n_states=3, n_features=10):
self.n_states = n_states;
self.n_features = n_features;
self.hmm = hmm.GaussianHMM(n_components=n_states, covariance_type='full');
self.kf_models = [KalmanFilter(dim_x=n_features, dim_z=n_features) for _ in range(n_states)];
for kf in self.kf_models:
kf.x = np.zeros(n_features);
kf.P *= 1000.0;
kf.R = np.eye(n_features)*0.1;
kf.Q = np.eye(n_features)*0.01;
self.state_seq = [];
self.observations = [];
self.is_fit = False;
self.min_obvs = max(100, 10*n_states);
def em (self):
if len(self.observations) > self.n_states:
obvs_array = np.array(self.observations);
try:
print(f"FITTIN HMM {len(obvs_array)} obvs");
self.hmm.fit(obvs_array);
posteriors = self.hmm.predict_proba(obvs_array);
print("HMM FIT GOOD");
for i, kf in enumerate(self.kf_models):
obvs_weighted = obvs_array * posteriors[:, i:i+1];
kf.batch_filter(obvs_weighted);
self.is_fit = True;
print(f'FIT MODEL {len(self.observations)}');
except Exception as e:
print(f'FIT ERROR {str(e)}');
else:
print('LACKS OBVS, COUNT: {len(self.observations)}\nNEED COUNT: {self.n_states}');
def update (self, observation):
observation = np.array(observation).reshape(1,-1);
self.observations.append(observation[0]);
print(f'STATE SEQ UPDATE: {self.state_seq}');
if len(self.observations) >= self.min_obvs:
if not self.is_fit:
self.em();
print(f'TOTAL OBVS: {len(self.observations)}');
if self.is_fit:
state = self.hmm.predict(observation);
self.state_seq.append(state[0]);
self.kf_models[state[0]].update(observation[0]);
if len(self.observations) % 100 == 0:
self.em();
print(f'TOTAL OBVS: {len(self.observations)}');
def estimate_liq (self):
print(f'ESTIMATE LIQ: is_fit:{self.is_fit}\tstate_seq:{self.state_seq}');
if not self.is_fit:
return np.zeros(self.n_features);
if not self.state_seq:
return self.kf_models[0].x
current_state = self.state_seq[-1];
return self.kf_models[current_state].x;
def get_hidden_state (self):
if not self.state_seq:
return None;
return self.state_seq[-1];
def get_state_probs (self):
if not self.observations:
return None;
return self.hmm.predict_proba(self.observations[-1].reshape(1,-1))[0];
class OrderBook:
def __init__ (self):
self.bids = defaultdict(lambda: defaultdict(int));
self.asks = defaultdict(lambda: defaultdict(int));
self.last_update = None;
def update (self, quote):
bid_price, bid_size = float(quote['bid']), int(quote['bidsz']);
ask_price, ask_size = float(quote['ask']), int(quote['asksz']);
self.bids[bid_price][quote['bidexch']] = bid_size;
self.asks[ask_price][quote['askexch']] = ask_size;
# self.last_update = max(int(quote['biddate']), int(quote['askdate']));
self.last_update = max(
datetime.fromtimestamp(int(quote['biddate'])/1000).strftime('%Y-%m-%d %H:%M:%S'),
datetime.fromtimestamp(int(quote['askdate'])/1000).strftime('%Y-%m-%d %H:%M:%S')
);
def get_book_state (self):
return {
'bids': dict(self.bids),
'asks': dict(self.asks),
'last_update': self.last_update
};
def extract_features (book_state):
bids, asks = book_state['bids'], book_state['asks'];
bid_prices, ask_prices = sorted(bids.keys(), reverse=True), sorted(asks.keys());
features = {
'best_bid': bid_prices[0] if bid_prices else None,
'best_ask': ask_prices[0] if ask_prices else None,
'bid_ask_spread': ask_prices[0] - bid_prices[0] if bid_prices and ask_prices else None,
'total_bid_size': sum(sum(sizes.values()) for sizes in bids.values()),
'total_ask_size': sum(sum(sizes.values()) for sizes in asks.values()),
'bid_depth': len(bid_prices),
'ask_depth': len(ask_prices),
'bid_size_imbalance': sum(sum(sizes.values()) for sizes in bids.values()) / len(bid_prices) if bid_prices else None,
'ask_size_imbalance': sum(sum(sizes.values()) for sizes in asks.values()) / len(ask_prices) if ask_prices else None
# 'since_update': (int(datetime.now().strftime('%s'))-book_state['last_update'])
};
return features;
def ehk_init (n_states=3, n_features=10):
return EHKModel(n_states, n_features);
with open("./nvda_quotes724.json", "r") as f:
quote_events = json.load(f);
# >>> book.get_book_state()
# {'bids': {114.71: defaultdict(<class 'int'>, {'Z': 1, 'N': 7, 'Q': 1, 'H': 7, 'K': 9, 'P': 6, 'M': 3, 'J': 1}), 114.73: defaultdict(<class 'int'>, {'N': 8, 'Z': 9, 'K': 13, 'Q': 11, 'P': 8, 'H': 5, 'U': 3}), 114.72: defaultdict(<class 'int'>, {'N': 6, 'K': 7, 'Q': 9, 'P': 2, 'Z': 2, 'H': 6}), 114.75: defaultdict(<class 'int'>, {'H': 4, 'N': 7, 'P': 8, 'Q': 2, 'K': 6, 'Z': 1}), 114.74: defaultdict(<class 'int'>, {'H': 7, 'P': 5, 'N': 7, 'K': 7, 'U': 8, 'Q': 2, 'M': 3, 'Z': 2}), 114.76: defaultdict(<class 'int'>, {'H': 1, 'Q': 5, 'K': 7, 'N': 6, 'P': 7, 'Z': 2}), 114.77: defaultdict(<class 'int'>, {'Z': 2, 'H': 3, 'K': 8, 'N': 6, 'Q': 9, 'P': 1, 'U': 20}), 114.78: defaultdict(<class 'int'>, {'Q': 5, 'U': 1, 'N': 1, 'H': 5, 'P': 2, 'Z': 8, 'K': 8}), 114.7: defaultdict(<class 'int'>, {'Q': 3, 'K': 7, 'N': 25, 'P': 2, 'M': 3, 'H': 4, 'Z': 2, 'U': 1}), 114.69: defaultdict(<class 'int'>, {'Q': 8, 'A': 2, 'N': 7, 'H': 3, 'P': 1, 'K': 8, 'Z': 3}), 114.68: defaultdict(<class 'int'>, {'N': 7, 'Q': 9, 'Z': 9, 'H': 7, 'K': 15, 'P': 2, 'U': 6}), 114.67: defaultdict(<class 'int'>, {'Q': 3, 'Z': 18, 'N': 7, 'K': 8, 'H': 2, 'P': 1}), 114.66: defaultdict(<class 'int'>, {'Q': 1, 'H': 1, 'N': 9, 'K': 7, 'U': 6, 'Z': 2, 'J': 4, 'P': 7}), 114.65: defaultdict(<class 'int'>, {'K': 6, 'U': 1, 'Z': 2, 'Q': 9, 'N': 8, 'P': 1}), 114.79: defaultdict(<class 'int'>, {'Q': 4, 'N': 1, 'Z': 4, 'H': 6, 'K': 8}), 114.8: defaultdict(<class 'int'>, {'Q': 6, 'K': 7}), 114.81: defaultdict(<class 'int'>, {'Q': 3, 'H': 6, 'K': 9}), 114.64: defaultdict(<class 'int'>, {'Q': 6, 'K': 8, 'N': 1, 'H': 4, 'P': 1}), 114.63: defaultdict(<class 'int'>, {'K': 6, 'H': 5, 'Q': 13, 'M': 3, 'P': 13, 'N': 7, 'Z': 1, 'U': 1}), 114.61: defaultdict(<class 'int'>, {'U': 4, 'P': 1, 'K': 7, 'Q': 2, 'H': 5, 'N': 1, 'Z': 2, 'V': 1}), 114.59: defaultdict(<class 'int'>, {'Z': 2, 'N': 7, 'K': 6, 'Q': 9, 'P': 1, 'H': 5, 'U': 5}), 114.6: defaultdict(<class 'int'>, {'N': 6, 'K': 1, 'Z': 1, 'Q': 4, 'H': 6, 'M': 3, 'P': 1}), 114.58: defaultdict(<class 'int'>, {'N': 6, 'Q': 1, 'K': 18}), 114.57: defaultdict(<class 'int'>, {'Q': 9, 'N': 6, 'P': 8, 'K': 8}), 114.56: defaultdict(<class 'int'>, {'P': 10, 'N': 14}), 114.62: defaultdict(<class 'int'>, {'Q': 1, 'P': 2, 'H': 6, 'N': 6, 'K': 6, 'Z': 1})}, 'asks': {114.73: defaultdict(<class 'int'>, {'P': 8, 'M': 3, 'Q': 6, 'Z': 32, 'K': 6, 'N': 6, 'U': 1, 'H': 10}), 114.75: defaultdict(<class 'int'>, {'K': 6, 'P': 1, 'Q': 7, 'N': 6, 'M': 3, 'Z': 1, 'U': 53, 'H': 1}), 114.74: defaultdict(<class 'int'>, {'Q': 2, 'P': 1, 'K': 2, 'N': 6, 'H': 1, 'M': 3, 'Z': 1}), 114.77: defaultdict(<class 'int'>, {'K': 3, 'Q': 1, 'N': 1, 'X': 1, 'P': 1, 'Z': 1, 'U': 3, 'H': 1}), 114.76: defaultdict(<class 'int'>, {'Q': 35, 'H': 1, 'Z': 2, 'K': 6, 'P': 1, 'N': 7}), 114.79: defaultdict(<class 'int'>, {'P': 6, 'U': 12, 'Q': 9, 'K': 14, 'Z': 2, 'H': 1, 'N': 6}), 114.78: defaultdict(<class 'int'>, {'K': 6, 'Q': 7, 'P': 1, 'N': 7, 'U': 1, 'H': 1, 'Z': 2}), 114.8: defaultdict(<class 'int'>, {'Q': 6, 'K': 74, 'P': 2}), 114.71: defaultdict(<class 'int'>, {'Q': 2, 'H': 1, 'K': 7, 'Z': 1, 'U': 1, 'N': 6, 'X': 7, 'P': 1}), 114.72: defaultdict(<class 'int'>, {'Q': 12, 'U': 1, 'K': 7, 'N': 6, 'P': 1, 'M': 3, 'Z': 6, 'H': 1}), 114.7: defaultdict(<class 'int'>, {'Q': 8, 'K': 7, 'Z': 1, 'P': 2, 'N': 6, 'H': 1, 'M': 3}), 114.69: defaultdict(<class 'int'>, {'Q': 10, 'Z': 2, 'K': 8, 'P': 1, 'N': 7, 'H': 1}), 114.68: defaultdict(<class 'int'>, {'Q': 7, 'K': 1, 'N': 7, 'P': 10}), 114.67: defaultdict(<class 'int'>, {'P': 2, 'H': 10, 'X': 7, 'Q': 1, 'K': 2, 'Z': 1, 'N': 8}), 114.66: defaultdict(<class 'int'>, {'Q': 2, 'N': 6, 'K': 1, 'Z': 1, 'M': 3, 'P': 10, 'X': 6}), 114.81: defaultdict(<class 'int'>, {'K': 6, 'Q': 1, 'N': 6, 'H': 1, 'P': 10}), 114.82: defaultdict(<class 'int'>, {'Q': 15, 'Z': 8}), 114.83: defaultdict(<class 'int'>, {'K': 17}), 114.65: defaultdict(<class 'int'>, {'K': 7, 'Q': 2, 'N': 6, 'Z': 3, 'P': 8}), 114.64: defaultdict(<class 'int'>, {'K': 1, 'Q': 1, 'P': 3}), 114.62: defaultdict(<class 'int'>, {'Q': 2, 'K': 13, 'Z': 1, 'N': 7, 'H': 1}), 114.6: defaultdict(<class 'int'>, {'K': 1, 'Q': 1, 'H': 1, 'Z': 1, 'N': 6, 'U': 1}), 114.61: defaultdict(<class 'int'>, {'K': 8, 'Q': 3, 'N': 7, 'P': 9, 'Z': 1}), 114.63: defaultdict(<class 'int'>, {'N': 6, 'K': 6, 'P': 5, 'Q': 9, 'Z': 1, 'X': 2}), 114.59: defaultdict(<class 'int'>, {'K': 7, 'Q': 4}), 114.58: defaultdict(<class 'int'>, {'Q': 1, 'Z': 1})}, 'last_update': '2024-07-24 14:54:46'}
book = OrderBook();
model = ehk_init(n_states=3, n_features=9);
#
# Testing `for quote in quote_events` loop with first element of quote_events
#
# {'type': 'quote', 'symbol': 'NVDA', 'bid': 114.71, 'bidsz': 12, 'bidexch': 'Z', 'biddate': '1721847016000', 'ask': 114.73, 'asksz': 7, 'askexch': 'P', 'askdate': '1721847017000'}
# q_event = quote_events[0];
# >>> book.get_book_state()
# {'bids': {114.71: defaultdict(<class 'int'>, {'Z': 12})}, 'asks': {114.73: defaultdict(<class 'int'>, {'P': 7})}, 'last_update': '2024-07-24 14:50:17'}
# book.update(q_event);
# >>> book_features
# {'best_bid': 114.71, 'best_ask': 114.73, 'bid_ask_spread': 0.020000000000010232, 'total_bid_size': 12, 'total_ask_size': 7, 'bid_depth': 1, 'ask_depth': 1, 'bid_size_imbalance': 12.0, 'ask_size_imbalance': 7.0}
# book_features = extract_features(book.get_book_state());
# model.update(list(book_features.values()));
for i, q_event in enumerate(quote_events[1:]):
book.update(q_event);
book_features = extract_features(book.get_book_state());
model.update(list(book_features.values()));
#
# OUTPUT
#
# >>> model.state_seq
# [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
# >>> model
# <orderbook.EHKModel object at 0x15fa5fe00>
# >>> model.
# model.em() model.get_hidden_state() model.hmm model.kf_models model.n_features model.observations model.update(
# model.estimate_liq() model.get_state_probs() model.is_fit model.min_obvs model.n_states model.state_seq
# >>> model.kf_models
# [KalmanFilter object
# dim_x = 9
# dim_z = 9
# dim_u = 0
# x = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P = [[1172. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 1172. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 1172. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 1172. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 1172. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 1172. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 1172. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 1172. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 1172.]]
# x_prior = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P_prior = [[1172. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 1172. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 1172. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 1172. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 1172. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 1172. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 1172. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 1172. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 1172.]]
# x_post = [0. 0. 0. 0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 0. 0. 0. 0. 0. 0. 0.1 0. ]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.1]]
# SI = [[10. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 10. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 10. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 10. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 10. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 10. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 10. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 10. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 10.]]
# M = [[0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]]
# B = None
# z = [ 1.14810000e+02 1.14580000e+02 -2.30000000e-01 8.76000000e+02
# 7.83000000e+02 2.60000000e+01 2.60000000e+01 3.36923077e+01
# 3.01153846e+01]
# log-likelihood = -7050843.275754711
# likelihood = 2.2250738585072014e-308
# mahalanobis = 3755.2217955643655
# alpha = 1.0
# inv = <function inv at 0x1036920c0>, KalmanFilter object
# dim_x = 9
# dim_z = 9
# dim_u = 0
# x = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P = [[1172. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 1172. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 1172. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 1172. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 1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. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]]
# B = None
# z = [ 1.14810000e+02 1.14580000e+02 -2.30000000e-01 8.42000000e+02
# 7.92000000e+02 2.60000000e+01 2.60000000e+01 3.23846154e+01
# 3.04615385e+01]
# log-likelihood = -6829331.079009148
# likelihood = 2.2250738585072014e-308
# mahalanobis = 3695.7632960446117
# alpha = 1.0
# inv = <function inv at 0x1036920c0>, KalmanFilter object
# dim_x = 9
# dim_z = 9
# dim_u = 0
# x = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P = [[1172. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 1172. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 1172. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 1172. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 1172. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 1172. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 1172. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 1172. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 1172.]]
# x_prior = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P_prior = [[1172. 0. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 1172. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 1172. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 1172. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 1172. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 1172. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 1172. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 1172. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 1172.]]
# x_post = [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# P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. 0. 0. 0. 0.]
# [ 0. 10. 0. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 10. 0. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 10. 0. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 10. 0. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 10. 0. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 10. 0. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 10. 0.]
# [ 0. 0. 0. 0. 0. 0. 0. 0. 10.]]
# M = [[0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]
# [0. 0. 0. 0. 0. 0. 0. 0. 0.]]
# B = None
# z = [ 0. 0. -0. 0. 0. 0. 0. 0. 0.]
# log-likelihood = 2.0911861196311516
# likelihood = 8.094510530398724
# mahalanobis = 0.0
# alpha = 1.0
# inv = <function inv at 0x1036920c0>]
# >>> model.
# model.em() model.get_hidden_state() model.hmm model.kf_models model.n_features model.observations model.update(
# model.estimate_liq() model.get_state_probs() model.is_fit model.min_obvs model.n_states model.state_seq
# >>> model.estimate_liq()
# array([0., 0., 0., 0., 0., 0., 0., 0., 0.])
# class OrderBook:
# def __init__ (self):
# # self.bids = defaultdict(lambda: defaultdict(int));
# # self.asks = defaultdict(lambda: defaultdict(int));
# self.bids = [];
# self.asks = [];
# self.bid_map = defaultdict(dict);
# self.ask_map = defaultdict(dict);
# self.last_trade = None;
# def _add_bid (self, price, size, exchange):
# if exchange in self.bid_map[price]:
# self._remove_bid(price, exchange);
# heapq.heappush(self.bids, (-price, size, exchange));
# self.bid_map[price][exchange] = size;
# def _add_ask (self, price, size, exchange):
# if exchange in self.ask_map[price]:
# del self.bid_map[price][exchange];
# if not self.bid_map[price]:
# del self.bid_map[price];
# self.bids = [b for b in self.bids if b[2] != exchange or -b[0] != price];
# heapq.heapify(self.bids);
# def _remove_bid (self, price, exchange):
# if exchange in self.bid_map[price]:
# del self.bid_map[price][exchange];
# if not self.bid_map[price]:
# del self.bid_map[price];
# self.bids = [b for b in self.bids if b[2] != exchange or -b[0] != price];
# heapq.heapify(self.bids);
# def _remove_ask(self, price, exchange):
# if exchange in self.ask_map[price]:
# del self.ask_map[price][exchange];
# if not self.ask_map[price]:
# del self.ask_map[price];
# self.asks = [a for a in self.asks if a[2] != exchange or a[0] != price];
# heapq.heapify(self.asks);
# def update (self, data):
# event = json.loads(data);
# if event['type'] == 'quote':
# self.update_quote(event);
# elif event['type'] in ['trade', 'tradex']:
# self.update_trade(event);
# elif event['type'] == 'timesale':
# self.update_timesale(event);
# def update_quote (self, quote):
# self._add_bid(float(quote['bid']), int(quote['bidsz']), quote['bidexch']);
# self._add_ask(float(quote['ask']), int(quote['asksz']), quote['askexch']);
# def update_trade (self, trade):
# self.last_trade = {
# 'price': float(trade['price']),
# 'size': int(trade['size']),
# 'exchange': trade['exch']
# };
# def update_timesale (self, timesale):
# if not timesale['cancel'] and not timesale['correction']:
# self._add_bid(float(timesale['bid']), int(timesale['size']), timesale['exch']);
# self._add_ask(float(timesale['ask']), int(timesale['size']), timesale['exch']);
# self.last_trade = {
# 'price': float(timesale['last']),
# 'size': int(timesale['size']),
# 'exchange': timesale['exch']
# };
# def get_best_bid (self):
# return (-self.bids[0][0], self.bids[0][1], self.bids[0][2]) if self.bids else None;
# def get_best_ask (self):
# return self.asks[0] if self.asks else None;
# def get_book_for_exchange (self, exchange):
# bids = sorted(
# [(price,size) for price, exch_sizes in self.bid_map.items() for exch,size in exch_sizes.items() if exch==exchange],
# reverse=True
# );
# asks = sorted(
# [(price,size) for price, exch_sizes in self.ask_map.items() for exch,size in exch_sizes.items() if exch == exchange]
# );
# return {
# 'bids': bids,
# 'asks': asks,
# 'last_trade': self.last_trade
# };