Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create reconcileOrder f'n to pass all tests #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,36 @@ Working in a branch called answer, implement your function to get your tests to
## Requirements
An order book tracks all buy and sell requests from traders for Bitcoin assets at various price levels. When a trader submits a trade it is either fulfilled immediately, if there is a matching order in the book, or it is added to the book to be fulfilled later.

orderbook is an array of objects
// orderbook = [{
type: '',
quantity: '',
price: '',
},{
type: '',
quantity: '',
price: '',
}
]

Assume starting with an empty order book, let's walkthrough several scenarios.
// declare orderbook as an array
const existingBook = []

// check if existingBook is empty
// if book is empty
// push order into existingBook

// interate over objects in array (existing book)
// check price
// if there is not an existing price 'if(!price)', add new order
// check type of each object if they match the current object (if we have a buy and there are no sells in stack)
// if there is a buy for the sell
// check the quantity
// if the buy > sell; change quantity to the difference and keep the type as Buy
// if the buy < sell; change quantity to the difference and keep the type as Sell
// if buy === sell, remove the order from the list
// if there is no sell to match the buy add in new object (order)

**Buy 15 BTC at 5800:** The order book is empty so this order cannot be fulfilled immediately. This order is added to the book.

Expand Down Expand Up @@ -38,3 +67,4 @@ For example, assume the book contains an order to buy 15 BTC at 6000. When we re
The inverse however would not be true. Given a book with a buy 15 BTC at 5900, when we receive a sell order for 15 BTC at 6000 we should not fulfill this order since it would result in the seller getting less (5900 vs 6000).

There are two tests associated with this extra created that are currently being skipped. Remove the `.skip` on these tests so they run when beginning this work.

57 changes: 57 additions & 0 deletions orderBook.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
function reconcileOrder(existingBook, incomingOrder) {
let updatedBook = []
let changedOrders = []

// checks to see if the existingOrderBook is empty; if so, add incomingOrder
if (!existingBook.length) {
updatedBook.push(incomingOrder)

return updatedBook
}

// loop through the existingBook and check for conditions
for (let i = 0; i < existingBook.length; i++) {
// exact match condition (buy for a sell):
if (existingBook[i].type !== incomingOrder.type &&
existingBook[i].price === incomingOrder.price &&
existingBook[i].quantity === incomingOrder.quantity) {
incomingOrder.price = -1
// transaction type match, but quantities are different
} else if (existingBook[i].type !== incomingOrder.type &&
existingBook[i].price === incomingOrder.price &&
existingBook[i].quantity !== incomingOrder.quantity) {
// two paths to follow depending on quantity differences:
if (existingBook[i].quantity > incomingOrder.quantity) {
existingBook[i].quantity = existingBook[i].quantity - incomingOrder.quantity
incomingOrder.quantity = 0
changedOrders.push(existingBook[i])
} else {
incomingOrder.quantity = incomingOrder.quantity - existingBook[i].quantity
}
} else {
updatedBook.push(existingBook[i])
}
}
if (incomingOrder.price !== -1 && incomingOrder.quantity !== 0) {
updatedBook.push(incomingOrder)
}

return updatedBook.concat(changedOrders)
}

module.exports = {
reconcileOrder
}

/* (order) => {
if (order.type === incomingOrder.type) {
updatedBook.push(incomingOrder)
}
else {
if (order.price !== incomingOrder.price) {
updatedBook.push(incomingOrder)
}

} */

// })
Loading