Skip to content

Instantly share code, notes, and snippets.

@southbite
Last active February 17, 2022 06:49
Show Gist options
  • Save southbite/9c9ce932d5dca577ca6daed5a9c99acd to your computer and use it in GitHub Desktop.
Save southbite/9c9ce932d5dca577ca6daed5a9c99acd to your computer and use it in GitHub Desktop.
// fetched from binance here: https://api.binance.com/api/v3/depth?limit=10&symbol=BTCUSDT
// top 10 bids on binance
const orderbook = {
"lastUpdateId":17087314354,
"bids":[
["44127.87000000","0.20199000"],
["44127.55000000","0.00401000"],
["44127.53000000","0.10323000"],
["44127.17000000","0.04000000"],
["44126.73000000","0.04000000"],
["44126.15000000","0.04000000"],
["44125.58000000","0.04661000"],
["44124.80000000","0.08000000"],
["44124.03000000","0.00900000"],
["44122.55000000","0.10000000"]
],
"asks":[
["44127.88000000","1.11588000"],
["44129.30000000","0.36500000"],
["44129.99000000","0.00226000"],
["44130.00000000","0.00293000"],
["44130.73000000","0.19185000"],
["44130.74000000","0.47963000"],
["44133.67000000","0.00843000"],
["44133.86000000","0.00066000"],
["44133.87000000","0.00086000"],
["44133.88000000","0.14600000"]
]
}
//using slice to make a shallow clone of the arrays
const bidsArray = orderbook.bids.slice();
const asksArray = orderbook.asks.slice();
// use map to transform our array items
const bidsHumanReadableArray = bidsArray.map(item => {
return {
volume: parseFloat(item[0]),
price: parseFloat(item[1])
}
});
const asksHumanReadableArray = asksArray.map(item => {
return {
volume: parseFloat(item[0]),
price: parseFloat(item[1])
}
})
//sort by the highest bid, lowest ask
//sorting happens in situ - this means the same array is being changed
bidsHumanReadableArray.sort((a, b) => {
return b.price - a.price
});
asksHumanReadableArray.sort((a, b) => {
return a.price - b.price
});
console.log('bids human readable: ', JSON.stringify(bidsHumanReadableArray, null, 2));
console.log('asks human readable: ', JSON.stringify(asksHumanReadableArray, null, 2));
//add a high bid to the bottom of bids:
bidsHumanReadableArray.push({
volume: 1000,
price: 1000000
});
console.log('bid with bad price is now at the bottom: ', bidsHumanReadableArray.at(-1));
// re-sort so the highest bid is at the top
bidsHumanReadableArray.sort((a, b) => {
return b.price - a.price
});
console.log('sorted again, bid with bad price is now at the top: ', bidsHumanReadableArray[0]);
// a price of a million is ridiculous, remove that top item using shift
let removed = bidsHumanReadableArray.shift()
console.log('removed bid with bad price: ', removed.price);
console.log(JSON.stringify(bidsHumanReadableArray, null, 2));
// pop the crappiest bid from the bottom
let crappiest = bidsHumanReadableArray.pop();
console.log('the crappiest bid was popped off:', crappiest);
console.log('should be 9 now:', bidsHumanReadableArray.length);
//wrap up all the volume using reduce
let totalBidVolume = bidsHumanReadableArray.reduce((total, item) => {
return total + item.volume;
}, 0);
console.log('total bid volume: ', totalBidVolume);
let totalAskVolume = asksHumanReadableArray.reduce((total, item) => {
return total + item.volume;
}, 0);
console.log('total ask volume: ', totalAskVolume);
//filter out all bids smaller than 0.1
const filteredBids = bidsHumanReadableArray.filter(item => {
return item.price >= 0.1;
});
console.log('bids priced at greater than 0.1', filteredBids);
// add a bad ask to the top of the asks, using unshift
asksHumanReadableArray.unshift({
volume: 1000,
price: 0.00001
});
console.log('added bad ask', asksHumanReadableArray.at(0));
// an ask of 0.00001 is terrible - remove using shift
console.log('removed bad ask', asksHumanReadableArray.shift());
// and we are back to the original sorted pricing
console.log('asks are back to normal', asksHumanReadableArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment