-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathorderBook.js
More file actions
63 lines (53 loc) · 2.04 KB
/
Copy pathorderBook.js
File metadata and controls
63 lines (53 loc) · 2.04 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
function reconcileOrder(existingBook, incomingOrder) {
let updatedBook = []
let newOrders = []
// If the existingOrderBook is empty, add incomingOrder
if (!existingBook.length) {
let updatedBook = existingBook.concat({ ...incomingOrder })
return updatedBook
}
// loop through the existingBook objects
for (let i = 0; i < existingBook.length; i++) {
// set variables to simplify reading
const bookType = existingBook[i].type
const bookPrice = existingBook[i].price
const orderType = incomingOrder.type
let orderPrice = incomingOrder.price
let bookQuantity = existingBook[i].quantity
let orderQuantity = incomingOrder.quantity
// if buy and sell match (same price and quantity)
if (bookType !== orderType &&
bookPrice === orderPrice &&
bookQuantity === orderQuantity) {
// if all are true set incoming price to true
incomingOrder.price = true
// if buy and sell work, but quantities are different
} else if (bookType !== orderType &&
bookPrice === orderPrice &&
bookQuantity !== orderQuantity) {
// if existing book quantity is greater than incoming order quantity
if (bookQuantity > orderQuantity) {
//book quantity is equal to book quantity minus order quantity
existingBook[i].quantity -= orderQuantity
// set order quantity to false
incomingOrder.quantity = false
newOrders.push(existingBook[i])
} else {
//if existing book quantity is less than incoming order quantity
incomingOrder.quantity -= bookQuantity
}
} else {
// for other conditions push existingBook to the updatedBook
updatedBook.push(existingBook[i])
}
}
// and incoming order quantity is not 0 (also see above)
// if the price of the incomingOrder is not true (see above)
// push incoming order to updatedBook
if (incomingOrder.quantity !== false &&
incomingOrder.price !== true) {
updatedBook.push(incomingOrder)
}
return updatedBook.concat(newOrders)
}
module.exports = reconcileOrder