Skip to content

osnwt/poloniex-orderbook

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Poloniex Orderbook

Module for creating and maintaining Poloniex's orderbook on server side. I use Push API and binary search so it must be as fast as possible. After inspection Poloniex's code on the poloniex.com can say that the module must be more robust and faster.

Installation

$ npm install poloniex-orderbook

Usage

Require library first

const PoloBook = require('poloniex-orderbook');

create orderbook instance and provide currency pair you want to subscribe:

// currency title is case-insensetive
const polobook = new PoloBook(['btc', 'eth']);
// or
const polobook = new PoloBook('btc_eth');

and just start listening orderbook udpdate. start returns ES6 Promise, so you can do your work in then

polobook.start()
  .then(() => { /* polobook is synced and ready */ });

now you can watch updates:

console.log(polobook.asks)
console.log(polobook.bids)

if you need to stop listening for updates from poloniex server, just use stop method:

polobook.stop()
  .then(() => { /* polobook has data but it stopped syncing it with server */ });

for closing WAMP connection use static method PoloBook.close

const PoloBook = require('poloniex-orderbook');
const polobook = new PoloBook(['btc', 'eth']);

polobook.start()
  .then(() => { 
        // you've done all stuff and want to finish
        PoloBook.close()
   });

One connection is shared with all orderbook instances, so if you call PoloBook.close() you'll loose connection for all instances. If you want to stop listening only for current orderbook use stop.

From version 2.2 you cun listen on polobook's event update to get notified when push event comes

polobook.on('update', res => { console.log(res, polobook.asks.slice(0,1)});
polobook.start().catch(err => console.log(err));

#####Note:

  • You can create different pairs orderbooks, they will work ok simultaneously (see examples )
  • This module written with ES6 syntax. Check your nodejs version if you get some errors first.

Example

"use strict";

const PoloBook = require('poloniex-orderbook');
const polobook = new PoloBook("btc_xmr");

polobook.start().then(() => {
  console.log(polobook.asks.slice(0, 10));
  console.log('-------------------------');
  console.log(polobook.bids.slice(0, 10));
})
  .then(PoloBook.close)
  .catch(error => console.log(error));

you should get output like this:

[ [ '0.01431999', 0.77974162 ],
  [ '0.01432000', 115.71500042 ],
  [ '0.01432001', 47.99559466 ],
  [ '0.01432215', 5.06983173 ],
  [ '0.01434657', 2.61260333 ],
  [ '0.01434658', 4 ],
  [ '0.01435000', 13.92148 ],
  [ '0.01435385', 23.29175115 ],
  [ '0.01436000', 0.5 ],
  [ '0.01436017', 87.95229339 ] ]
-------------------------
[ [ '0.01420000', 558.54511746 ],
  [ '0.01418879', 4.639175 ],
  [ '0.01418368', 0.65352199 ],
  [ '0.01416569', 5.60436236 ],
  [ '0.01416450', 32.03058816 ],
  [ '0.01416000', 0.12412218 ],
  [ '0.01415001', 504.724 ],
  [ '0.01415000', 407.53385512 ],
  [ '0.01413966', 0.84143041 ],
  [ '0.01413935', 191.98591077 ] ]

Contacts

If you have some suggestions please email me: evilive3000@gmail.com

About

Poloniex Orderbook manager

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%