This package allow you to keep update your Orderbook snapshot from a given Websocket source of Orders.
Features:
- Orderbook size limit available to avoid overgrow
- Lightweight / No dependency
- Handle Bid/Ask overlaps, best Ask always higher or equal with best Bids
Orderbook output sample(.getOrderBook()
):
{
asks: [
[8000, 1.9869372781826433],
[8001, 0.6590219188159661],
[8002, 1.0430790809329253],
[8003, 0.1279416255016304],
[8004, 1.3397780002928084],
[8005, 1.8250517469118535],
],
bids: [
[6020, 1.5764351751676466],
[6019, 1.9678679870588733],
[6018, 1.7349295266546774],
[6017, 1.0087743314824977],
[6016, 0.0942339070977258],
]
}
Imports:
import {OrderBookStore} from 'orderbook-synchronizer'; // Multiple OrderBooks in a single class
import {Orderbook} from 'orderbook-synchronizer'; // Single OrderBook
const OrderBooks = new OrderBookStore(1000); // Set memory limit / maximum length of Ask/Bid array
OrderBooks.updateOrderBook('BTC/USD', asks: Order[], bids: Order[]); // Create/Update Orderbook
OrderBooks.hasOrderBook('BTC/USD'); // return boolean if Orderbook exist
OrderBooks.getOrderBook('BTC/USD'); // return Orderbook | undefined depends on Orderbook exist
OrderBooks.getSymbolList(); // return all Orderbook keys in array ['BTC/USD']
const SingleOrderBook = new Orderbook(1000); // Set memory limit / maximum length of Ask/Bid array
SingleOrderBook.getOrderBook(); // return Orderbook
SingleOrderBook.updateOrderBook(asks: Order[], bids: Order[]); // Update Orderbook
Original inspiration source: