This repository has been archived by the owner on Jan 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 240
/
BookBuilderTest.ts
152 lines (139 loc) · 6.93 KB
/
BookBuilderTest.ts
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
/***************************************************************************************************************************
* @license *
* Copyright 2017 Coinbase, Inc. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on *
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/
import { AggregatedLevelWithOrders, BookBuilder } from '../../src/lib/BookBuilder';
import * as assert from 'assert';
import { CumulativePriceLevel, Level3Order, OrderbookState } from '../../src/lib/Orderbook';
import { Big, BigJS, ZERO } from '../../src/lib/types';
import { NullLogger } from '../../src/utils/Logger';
describe('BookBuilder:', () => {
describe('Empty instance', () => {
let book: BookBuilder = null;
beforeEach(() => {
book = new BookBuilder(NullLogger);
});
it('has zero total size', () => {
assert.equal(+book.asksTotal, 0);
assert.equal(+book.bidsTotal, 0);
assert.equal(+book.asksValueTotal, 0);
assert.equal(+book.bidsValueTotal, 0);
assert.equal(book.numAsks, 0);
assert.equal(book.numBids, 0);
});
it('has a zero state', () => {
const state: OrderbookState = book.state();
assert.ok(state.sequence <= 0);
assert.deepEqual(state.bids, []);
assert.deepEqual(state.asks, []);
assert.deepEqual(state.orderPool, {});
});
it('returns null for best bid/ask', () => {
assert.equal(book.highestBid, null);
assert.equal(book.lowestAsk, null);
});
it('ordersForValue returns empty list', () => {
let orders: CumulativePriceLevel[] = book.ordersForValue('buy', Big(1), false);
assert.deepEqual(orders, []);
orders = book.ordersForValue('sell', Big(1), false);
assert.deepEqual(orders, []);
orders = book.ordersForValue('buy', Big(1), true);
assert.deepEqual(orders, []);
orders = book.ordersForValue('sell', Big(1), true);
assert.deepEqual(orders, []);
});
it('hasOrder returns false', () => {
assert.equal(book.hasOrder('111'), false);
});
});
describe('maintains an orderbook', () => {
let book: BookBuilder = null;
let randomBook: RandomBook = null;
const BOOK_SIZE = 1000;
before(() => {
book = new BookBuilder(NullLogger);
randomBook = generateRandomBook(BOOK_SIZE);
randomBook.orders.forEach((order: Level3Order) => {
book.add(order);
});
});
it('adds orders', () => {
assert.ok(book.bidsTotal.eq(randomBook.totalBids), 'totalBids');
assert.ok(book.asksTotal.eq(randomBook.totalAsks), 'totalAsks');
assert.ok(book.bidsValueTotal.eq(randomBook.totalBidValue), 'totalBidsValue');
assert.ok(book.asksValueTotal.eq(randomBook.totalAsksValue), 'totalAsksValue');
assert.equal(Object.keys(book.orderPool).length, BOOK_SIZE);
});
it('Every order in the levels is linked on the order pool', () => {
let count = 0;
['buy', 'sell'].forEach((side: string) => {
const tree = book.getTree(side);
tree.each((level: AggregatedLevelWithOrders) => {
level.orders.forEach((order: Level3Order) => {
const pooledOrder = book.getOrder(order.id);
// Check that it's the same object, not a copy
assert.equal(order, pooledOrder);
count++;
});
});
});
assert.equal(count, BOOK_SIZE);
});
it('can remove an order', () => {
const deconstedOrder = book.remove('00005');
assert.ok(deconstedOrder, 'Order was not removed');
assert.ok(!book.hasOrder('00005'));
if (deconstedOrder.side === 'buy') {
assert.ok(book.bidsTotal.eq(randomBook.totalBids.minus(deconstedOrder.size)));
assert.ok(book.bidsValueTotal.eq(randomBook.totalBidValue.minus(deconstedOrder.size.times(deconstedOrder.price))));
} else {
assert.ok(book.asksTotal.eq(randomBook.totalAsks.minus(deconstedOrder.size)));
assert.ok(book.asksValueTotal.eq(randomBook.totalAsksValue.minus(deconstedOrder.size.times(deconstedOrder.price))));
}
});
});
});
interface RandomBook {
orders: Level3Order[];
totalBids: BigJS;
totalAsks: BigJS;
totalBidValue: BigJS;
totalAsksValue: BigJS;
}
function generateRandomBook(n: number): RandomBook {
const result = {
orders: [] as Level3Order[],
totalBids: ZERO,
totalAsks: ZERO,
totalBidValue: ZERO,
totalAsksValue: ZERO
};
for (let i = 0; i < n; i++) {
const side = Math.random() >= 0.5 ? 'buy' : 'sell';
const size = Big(String(Math.random() * 10)).round(1);
const price = side === 'buy' ? Big(String(100 - Math.random() * 20)).round(2) : Big(String(101 + Math.random() * 20)).round(2);
result.orders.push({
id: `0000${i}`,
side: side,
size: size,
price: price
});
if (side === 'buy') {
result.totalBids = result.totalBids.plus(size);
result.totalBidValue = result.totalBidValue.plus(size.times(price));
} else {
result.totalAsks = result.totalAsks.plus(size);
result.totalAsksValue = result.totalAsksValue.plus(size.times(price));
}
}
return result;
}