hi is it possible to keep an up to date copy of the order book in an array from the level 2 web socket feed similar to how it is on the website or in the first message returned as snapshot
snapshot returned from level 2 update as type="snapshot"
this is what i am trying to recreate using the type="l2update"
the question is how do i use the l2updade to keep an accurate copy of the order book or at least get the top level ask and bid from the web socket instead of poling the "getProductOrderBook"
const websocket = new CoinbasePro.WebsocketClient(
["BTC-EUR"],
"wss://ws-feed.pro.coinbase.com",
null,
{ channels: [
{
"name": "level2"
}
]
});
websocket.on('message', data => {
var bids = [];
var asks = [];
var x= 0;
var x2 = 0;
if(data.changes[0][0] == "buy"){
if(x <= 100){
bids[x] = parseFloat(data.changes[0][1]);
x++;
}else{
x = 0;
bids[x] = parseFloat(data.changes[0][1]);
x++;
}
}
if(data.changes[0][0] == "sell"){
if(x2 <= 100){
asks[x2] = parseFloat(data.changes[0][1]);
x2++;
}else{
x2 = 0;
asks[x2] = parseFloat(data.changes[0][1]);
x2++;
}
}
console.log(data);
});