-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathOrderBook.cs
535 lines (474 loc) · 17.2 KB
/
OrderBook.cs
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
using VisualHFT.Commons.Model;
using VisualHFT.Commons.Pools;
using VisualHFT.Helpers;
using VisualHFT.Studies;
using VisualHFT.Enums;
using VisualHFT.Model;
using System.Linq;
namespace VisualHFT.Model
{
public partial class OrderBook : ICloneable, IResettable, IDisposable
{
private bool _disposed = false; // to track whether the object has been disposed
private OrderFlowAnalysis lobMetrics = new OrderFlowAnalysis();
protected OrderBookData _data;
protected static readonly log4net.ILog log =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected CustomObjectPool<BookItem> _poolBookItems
= new CustomObjectPool<BookItem>(2000);
// Add counters for level changes
private long _addedLevels = 0;
private long _deletedLevels = 0;
private long _updatedLevels = 0;
// Properties to expose counters
public long AddedLevels => _addedLevels;
public long DeletedLevels => _deletedLevels;
public long UpdatedLevels => _updatedLevels;
public OrderBook()
{
_data = new OrderBookData();
FilterBidAskByMaxDepth = true;
}
public OrderBook(string symbol, int priceDecimalPlaces, int maxDepth)
{
if (maxDepth <= 0)
throw new ArgumentOutOfRangeException(nameof(maxDepth), "maxDepth must be greater than zero.");
_data = new OrderBookData(symbol, priceDecimalPlaces, maxDepth);
FilterBidAskByMaxDepth = true;
}
~OrderBook()
{
Dispose(false);
}
public string Symbol
{
get => _data.Symbol;
set => _data.Symbol = value;
}
public int MaxDepth
{
get => _data.MaxDepth;
set => _data.MaxDepth = value;
}
public int PriceDecimalPlaces
{
get => _data.PriceDecimalPlaces;
set => _data.PriceDecimalPlaces = value;
}
public int SizeDecimalPlaces
{
get => _data.SizeDecimalPlaces;
set => _data.SizeDecimalPlaces = value;
}
public double SymbolMultiplier => _data.SymbolMultiplier;
public int ProviderID
{
get => _data.ProviderID;
set => _data.ProviderID = value;
}
public string ProviderName
{
get => _data.ProviderName;
set => _data.ProviderName = value;
}
public eSESSIONSTATUS ProviderStatus
{
get => _data.ProviderStatus;
set => _data.ProviderStatus = value;
}
public double MaximumCummulativeSize
{
get => _data.MaximumCummulativeSize;
set => _data.MaximumCummulativeSize = value;
}
public CachedCollection<BookItem> Asks
{
get
{
lock (_data.Lock)
{
if (_data.Asks == null)
return null;
if (MaxDepth > 0 && FilterBidAskByMaxDepth)
return _data.Asks.Take(MaxDepth);
else
return _data.Asks;
}
}
set => _data.Asks.Update(value); //do not remove setter: it is used to auto parse json
}
public CachedCollection<BookItem> Bids
{
get
{
lock (_data.Lock)
{
if (_data.Bids == null)
return null;
if (MaxDepth >0 && FilterBidAskByMaxDepth)
return _data.Bids.Take(MaxDepth);
else
return _data.Bids;
}
}
set => _data.Bids.Update(value); //do not remove setter: it is used to auto parse json
}
public BookItem GetTOB(bool isBid)
{
lock (_data.Lock)
{
return _data.GetTOB(isBid);
}
}
public double MidPrice
{
get
{
return _data.MidPrice;
}
}
public double Spread
{
get
{
return _data.Spread;
}
}
public bool FilterBidAskByMaxDepth
{
get
{
return _data.FilterBidAskByMaxDepth;
}
set
{
_data.FilterBidAskByMaxDepth = value;
}
}
public void GetAddDeleteUpdate(ref CachedCollection<BookItem> inputExisting, bool matchAgainsBids)
{
if (inputExisting == null)
return;
lock (_data.Lock)
{
IEnumerable<BookItem> listToMatch = (matchAgainsBids ? _data.Bids : _data.Asks);
if (listToMatch.Count() == 0)
return;
if (inputExisting.Count() == 0)
{
foreach (var item in listToMatch)
{
inputExisting.Add(item);
}
return;
}
IEnumerable<BookItem> inputNew = listToMatch;
List<BookItem> outAdds;
List<BookItem> outUpdates;
List<BookItem> outRemoves;
var existingSet = inputExisting;
var newSet = inputNew;
outRemoves = inputExisting.Where(e => !newSet.Contains(e)).ToList();
outUpdates = inputNew.Where(e =>
existingSet.Contains(e) && e.Size != existingSet.FirstOrDefault(i => i.Equals(e)).Size).ToList();
outAdds = inputNew.Where(e => !existingSet.Contains(e)).ToList();
foreach (var b in outRemoves)
inputExisting.Remove(b);
foreach (var b in outUpdates)
{
var itemToUpd = inputExisting.Where(x => x.Price == b.Price).FirstOrDefault();
if (itemToUpd != null)
{
itemToUpd.Size = b.Size;
itemToUpd.ActiveSize = b.ActiveSize;
itemToUpd.CummulativeSize = b.CummulativeSize;
itemToUpd.LocalTimeStamp = b.LocalTimeStamp;
itemToUpd.ServerTimeStamp = b.ServerTimeStamp;
}
}
foreach (var b in outAdds)
inputExisting.Add(b);
}
}
public void CalculateMetrics()
{
lock (_data.Lock)
{
lobMetrics.LoadData(_data.Asks, _data.Bids, MaxDepth);
}
_data.ImbalanceValue = lobMetrics.Calculate_OrderImbalance();
}
public bool LoadData(IEnumerable<BookItem> asks, IEnumerable<BookItem> bids)
{
bool ret = true;
lock (_data.Lock)
{
#region Bids
if (bids != null)
{
_data.Bids.Update(bids
.Where(x => x != null && x.Price.HasValue && x.Size.HasValue)
.OrderByDescending(x => x.Price.Value)
);
}
#endregion
#region Asks
if (asks != null)
{
_data.Asks.Update(asks
.Where(x => x != null && x.Price.HasValue && x.Size.HasValue)
.OrderBy(x => x.Price.Value)
);
}
#endregion
_data.CalculateAccummulated();
}
CalculateMetrics();
return ret;
}
public double GetMaxOrderSize()
{
double _maxOrderSize = 0;
lock (_data.Lock)
{
if (_data.Bids != null)
_maxOrderSize = _data.Bids.Where(x => x.Size.HasValue).DefaultIfEmpty(new BookItem()).Max(x => x.Size.Value);
if (_data.Asks != null)
_maxOrderSize = Math.Max(_maxOrderSize, _data.Asks.Where(x => x.Size.HasValue).DefaultIfEmpty(new BookItem()).Max(x => x.Size.Value));
}
return _maxOrderSize;
}
public Tuple<double, double> GetMinMaxSizes()
{
lock (_data.Lock)
{
return _data.GetMinMaxSizes();
}
}
public virtual object Clone()
{
var clone = new OrderBook(_data.Symbol, _data.PriceDecimalPlaces, _data.MaxDepth);
clone.ProviderID = _data.ProviderID;
clone.ProviderName = _data.ProviderName;
clone.SizeDecimalPlaces = _data.SizeDecimalPlaces;
clone._data.ImbalanceValue = _data.ImbalanceValue;
clone.ProviderStatus = _data.ProviderStatus;
clone.MaxDepth = _data.MaxDepth;
clone.LoadData(Asks, Bids);
return clone;
}
public void PrintLOB(bool isBid)
{
lock (_data.Lock)
{
int _level = 0;
foreach (var item in isBid ? _data.Bids : _data.Asks)
{
Console.WriteLine($"{_level} - {item.FormattedPrice} [{item.Size}]");
_level++;
}
}
}
public double ImbalanceValue
{
get => _data.ImbalanceValue;
set => _data.ImbalanceValue = value;
}
public long Sequence { get; set; }
public void ShallowCopyFrom(OrderBook e, CustomObjectPool<BookItem> pool)
{
if (e == null)
return;
_data.ShallowCopyFrom(e, pool);
}
/// <summary>
/// ShallowUpdateFrom
/// Will update the existing data.
/// This is very useful when keeping a Collection locally and want to avoid swapping and allocating
/// </summary>
/// <param name="sourceList">The source list.</param>
public void ShallowUpdateFrom(OrderBook e)
{
if (e == null)
return;
_data.ShallowUpdateFrom(e);
}
private void InternalClear()
{
for (int i = 0; i < _data.Asks.Count();)
{
var ask = _data.Asks[i];
if (ask.Price != 0)
{
DeleteLevel(new DeltaBookItem() { IsBid = false, Price = ask.Price });
}
else
{
i++;
}
}
for (int i = 0; i < _data.Bids.Count();)
{
var bid = _data.Bids[i];
if (bid.Price != 0)
{
DeleteLevel(new DeltaBookItem() { IsBid = true, Price = bid.Price });
}
else
{
i++;
}
}
GetAndResetChangeCounts();
}
public void Clear()
{
lock (_data.Lock)
{
InternalClear();
_data.Clear();
}
}
public void Reset()
{
lock (_data.Lock)
{
InternalClear();
_data?.Reset();
}
}
public virtual void AddOrUpdateLevel(DeltaBookItem item)
{
if (!item.IsBid.HasValue)
return;
eMDUpdateAction eAction = eMDUpdateAction.None;
lock (_data.Lock)
{
var _list = (item.IsBid.HasValue && item.IsBid.Value ? _data.Bids : _data.Asks);
var itemFound = _list.FirstOrDefault(x => x.Price == item.Price);
if (itemFound == null)
eAction = eMDUpdateAction.New;
else
eAction = eMDUpdateAction.Change;
}
if (eAction == eMDUpdateAction.Change)
UpdateLevel(item);
else
AddLevel(item);
}
public virtual void AddLevel(DeltaBookItem item)
{
if (!item.IsBid.HasValue)
return;
lock (_data.Lock)
{
// Check if it is appropriate to add a new item to the Limit Order Book (LOB).
// If the item exceeds the depth scope defined by MaxDepth, it should not be added.
// If the item is within the acceptable depth, truncate the LOB to ensure it adheres to the MaxDepth limit.
bool willNewItemFallOut = false;
var list = item.IsBid.Value ? _data.Bids : _data.Asks;
var listCount = list.Count();
if (item.IsBid.Value)
{
willNewItemFallOut = listCount > this.MaxDepth && item.Price < list.Min(x => x.Price);
}
else
{
willNewItemFallOut = listCount > this.MaxDepth && item.Price > list.Max(x => x.Price);
}
if (!willNewItemFallOut)
{
if (string.IsNullOrEmpty(_poolBookItems.ProviderName))
_poolBookItems.ProviderName = _data.ProviderName;
var _level = _poolBookItems.Get();
_level.EntryID = item.EntryID;
_level.Price = item.Price;
_level.IsBid = item.IsBid.Value;
_level.LocalTimeStamp = item.LocalTimeStamp;
_level.ProviderID = _data.ProviderID;
_level.ServerTimeStamp = item.ServerTimeStamp;
_level.Size = item.Size;
_level.Symbol = _data.Symbol;
_level.PriceDecimalPlaces = this.PriceDecimalPlaces;
_level.SizeDecimalPlaces = this.SizeDecimalPlaces;
list.Add(_level);
listCount++;
Interlocked.Increment(ref _addedLevels);
//truncate last item if we exceeded the MaxDepth
if (listCount > this.MaxDepth)
{
_poolBookItems.Return(list.TakeLast(listCount - this.MaxDepth));
list.TruncateItemsAfterPosition(MaxDepth-1);
}
}
}
}
public virtual void UpdateLevel(DeltaBookItem item)
{
lock (_data.Lock)
{
(item.IsBid.HasValue && item.IsBid.Value ? _data.Bids : _data.Asks).Update(x => x.Price == item.Price,
existingItem =>
{
if (existingItem.Size > item.Size) //added
Interlocked.Increment(ref _addedLevels);
else if (existingItem.Size < item.Size) //deleted
Interlocked.Increment(ref _deletedLevels);
existingItem.Price = item.Price;
existingItem.Size = item.Size;
existingItem.LocalTimeStamp = item.LocalTimeStamp;
existingItem.ServerTimeStamp = item.ServerTimeStamp;
});
}
}
public virtual void DeleteLevel(DeltaBookItem item)
{
if (string.IsNullOrEmpty(item.EntryID) && (!item.Price.HasValue || item.Price.Value == 0))
throw new Exception("DeltaBookItem cannot be deleted since has no price or no EntryID.");
lock (_data.Lock)
{
BookItem _itemToDelete = null;
if (!string.IsNullOrEmpty(item.EntryID))
{
_itemToDelete = (item.IsBid.HasValue && item.IsBid.Value ? _data.Bids : _data.Asks)
.FirstOrDefault(x => x.EntryID == item.EntryID);
}
else if (item.Price.HasValue && item.Price > 0)
{
_itemToDelete = (item.IsBid.HasValue && item.IsBid.Value ? _data.Bids : _data.Asks)
.FirstOrDefault(x => x.Price == item.Price);
}
if (_itemToDelete != null)
{
(item.IsBid.HasValue && item.IsBid.Value ? _data.Bids : _data.Asks).Remove(_itemToDelete);
_poolBookItems.Return(_itemToDelete);
Interlocked.Increment(ref _deletedLevels);
}
}
}
public (long added, long deleted, long updated) GetAndResetChangeCounts()
{
var result = (_addedLevels, _deletedLevels, _updatedLevels);
_addedLevels = 0;
_deletedLevels = 0;
_updatedLevels = 0;
return result;
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_data?.Dispose();
}
_disposed = true;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}