Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions orderBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

function reconcileOrder(existingBook, incomingOrder) {
// default switch case key
let key = 'a'
// matching order index variable
let i = -1
// callback function for findIndex method which sets conditions for matching orders in existing book

const buyEqualOrder = order => order.type === 'buy' && order.price >= incomingOrder.price

// Finds matching order index in existing book

if (existingBook.length) { i = existingBook.findIndex(buyEqualOrder) }

// switches handling of orders in special scenarios

if (i >= 0 && incomingOrder.quantity <= existingBook[i].quantity) { key = 'b' }
if (i >= 0 && incomingOrder.quantity > existingBook[i].quantity) { key = 'c' }

switch (key) {
// adds an order to the book when the book is empty, or no matching order
case 'a':
existingBook.push(incomingOrder)

return existingBook
// fulfills an order and removes matching order with same quantity, or reduces when larger quantity
case 'b':
existingBook[i].quantity -= incomingOrder.quantity
if (existingBook[i].quantity > 0) { existingBook.push(existingBook.splice((i), 1)[0]) }
existingBook = existingBook.filter(orders => orders.quantity > 0)

return existingBook
// all the rest of the special cases (uses recursion to fullfill order with to existing orders)
case 'c':
incomingOrder.quantity -= existingBook[i].quantity
existingBook[i].quantity = 0
existingBook = existingBook.filter(orders => orders.quantity > 0)
if (existingBook.findIndex(buyEqualOrder) >= 0) {
existingBook = reconcileOrder(existingBook, incomingOrder)
}
else {
existingBook.push(incomingOrder)
}

return existingBook
}
}
module.exports = reconcileOrder
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ describe('Order Book', () => {
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }, { type: 'sell', quantity: 5, price: 6150 }])
})

it.skip('Extra Credit: it fulfills a mismatched order when both parties benefit', () => {
it('Extra Credit: it fulfills a mismatched order when both parties benefit', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6000 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 5900 }

Expand All @@ -100,7 +100,7 @@ describe('Order Book', () => {
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }])
})

it.skip('Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties', () => {
it('Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 5900 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6000 }

Expand Down