This is an extension for Coinbase's Node.js library gdax-node.
- Synced level2 orderbook
Just change the modulename from gdax to gdax-ext and use it like you used gdax-node before.
const Gdax = require('gdax-ext');
const publicClient = new Gdax.PublicClient();
L2orderbook
is a data structure that can be used to store a local copy of the level2
orderbook.
const orderbook = new Gdax.L2orderbook();
The orderbook has the following methods:
state()
Gives the complete book
L2orderbook.state();
get(side, count)
Ask for the top (count) entries of a side. If count is not specified the top 5 entries where selected.
L2orderbook.get('buy',6);
getSpread()
Returns the spread on the book.
L2orderbook.getSpread();
processchanges(change)
Processes the messages (data) from the l2update websocket channel. This automaticaly checks, if this message has to be remove or adds an entry.
L2orderbook.processchanges(data.changes);
add(order)
Adds an order to the book.
const order = {
price: '6452.34',
size: '0.23245'
};
L2orderbook.add(order);
remove(price, side)
Removes an order from the book.
const price = '6452.34';
const side = 'sell'
L2orderbook.remove(price, side);
L2bookSync
creates a local mirror of the level2 orderbook on GDAX using
const orderbookSync = new Gdax.L2bookSync(productID);
productID
optional - defaults to 'BTC-USD' if not specified.
The following events can be emitted from the L2bookSync
:
synced
when a orderbook is syncedsynced productID
when the book of productID is syncedupdated
when a orderbook is updatedupdated productID
when the book of productID is updated
While productID has to be the name of the productID you specified.
const Gdax = require('gdax-ext');
const productIDs = ['ETH-BTC', 'BTC-USD'];
const orderbookSync = new Gdax.L2bookSync(productIDs);
//sync finished for every books
orderbookSync.on('synced', function(data) {
console.log('synced book:', data);
console.log(orderbookSync.books[data].state());
console.log(orderbookSync.books[data].getSpread());
});
//sync finished for one book
orderbookSync.on('synced BTC-USD', function(data) {
console.log('synced book:', data);
console.log(orderbookSync.books['BTC-USD'].state());
});
//update for every book
orderbookSync.on('updated', function(data) {
console.log('updated book:', data);
console.log(orderbookSync.books[data].getSpread());
});
//update for one book
orderbookSync.on('updated BTC-ETH', function(data) {
console.log('updated book:', data);
console.log(orderbookSync.books[data].get('buy',6));
console.log(orderbookSync.books[data].getSpread());
});
Data contains the productID.