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
158 changes: 158 additions & 0 deletions orderbook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
describe('15', () => {
describe('reconcileOrder', () => {
it('adds an order to the book when the book is empty and thus cannot fulfill the order', () => {
const existingBook = []
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 10, price: 6150 }])
})

it('adds an order to the book when the book has orders of the corresponding type (i.e. a sell with no buys)', () => {
const existingBook = [{ type: 'sell', quantity: 10, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 12, price: 6000 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([
{ type: 'sell', quantity: 10, price: 6150 },
{ type: 'sell', quantity: 12, price: 6000 }
])
})

it('adds an order to the book when the book has a corresponding order type but it does not match', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6000 }]
const incomingOrder = { type: 'sell', quantity: 12, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([
{ type: 'buy', quantity: 10, price: 6000 },
{ type: 'sell', quantity: 12, price: 6150 }
])
})

it('fulfills an order and removes the matching order when the book contains a matching order of the same quantity', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 6250 }]
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6250 }])
})

it('fulfills an order and reduces the matching order when the book contains a matching order of a larger quantity', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6150 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }, { type: 'buy', quantity: 5, price: 6150 }])
})

it('partially fulfills an order, removes the matching order and adds the remainder of the order to the book when the book contains a matching order of a smaller quantity', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }, { type: 'sell', quantity: 5, price: 6150 }])
})

it('uses two existing orders to completely fulfill an order, removing the matching orders from the book', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 5, price: 6150 }, { type: 'sell', quantity: 12, price: 5950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 5950 }])
})

it('uses two existing orders to completely fulfill an order, removing the first matching order from the book and reducing the second', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 12, price: 6950 }, { type: 'buy', quantity: 5, price: 6150 }])
})

it('uses two existing orders to partially fulfill an order, removing the matching orders from the book and reducing the incoming order before adding it to the book', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }, { type: 'buy', quantity: 10, price: 6150 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 25, price: 6150 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

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', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6000 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 5900 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

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', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 5900 }, { type: 'sell', quantity: 12, price: 6950 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6000 }

const updatedBook = reconcileOrder(existingBook, incomingOrder)

expect(updatedBook).to.deep.equal([
{ type: 'buy', quantity: 15, price: 5900 },
{ type: 'sell', quantity: 12, price: 6950 },
{ type: 'sell', quantity: 15, price: 6000 },
])
})
})
})

function typeFilter(exisitingBook, type) {
return existingBook.filter(item => item.type === type);
}
function reconcileOrder(existingBook, incomingOrder) {


let match = false;
let amount;
let i = 0;
for (; i < existingBook.length && !match; i++) {
if (existingBook[i].type != incomingOrder.type) {
if (existingBook[i].price === incomingOrder.price) {
match = true;
amount = (existingBook[i].quantity - incomingOrder.quantity);
}
}
if (match) {
break;
}
}

if (!match) {
existingBook.push(incomingOrder);
} else {
const matched = existingBook[i];
console.log(matched)
matched.quantity -= amount;
console.log(matched)

//remove the order from it's location
existingBook.splice(i, 1);

if (matched.quantity > 0) {
existingBook.push(matched);
}
}




return existingBook;
}

module.exports = reconcileOrder;

74 changes: 26 additions & 48 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@
"devDependencies": {
"chai": "^4.2.0",
"eslint": "^6.8.0",
"mocha": "^7.1.2"
"mocha": "^7.2.0"
}
}