0.3.0 • Published 5 years ago

hs-api-crypto-exchanges v0.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

hs-api-crypto-exchanges

A simple normalized API and WebSocket for crypto exchanges. This project is mainly focused on Public REST Endpoints and WebSocket channels.

Available Exchanges

  • Binance
  • Bitfinex
  • Bitstamp
  • Bittrex
  • CoinbasePro

Available Functions

Notice

  • Bitstamp - NodeJS version 12 will throw "Invalid header value char" error. Make sure to use the option --http-parser=legacy.
  • Coinbasepro - API request is slow becuase it is done in series to bypass request limit per/sec.

Install

npm install hs-api-crypto-exchanges --save

Getting started

const Exchange = require('hs-api-crypto-exchanges');

REST Endpoints

exchange.pairs()

Fetch all available pairs by request-promise. Depends on the exchange, this will include inactive pairs.

Exchange.binance.pairs().then(pairs => {
    console.log(pairs);
});

Exchange.bitfinex.pairs().then(pairs => {
    console.log(pairs);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.pairs().then(pairs => {
    console.log(pairs);
});

Exchange.bittrex.pairs().then(pairs => {
    console.log(pairs);
});

Exchange.coinbasepro.pairs().then(pairs => {
    console.log(pairs);
});

Pair

ParamTypeDescription
exchangeStringExchange ID. Always lower case with no space.
symbolStringThe symbol for the associated exchange. Exchange specific symbol.
baseStringThe base unit for the associated exchange symbol. Always upper case.
quoteStringThe quote unit for the associated exchange symbol. Always upper case.
activeBooleanDepends on the exchange API, this value can be all true and exclude inactive pairs.
[
    Pair { exchange: 'binance', symbol: 'ETHBTC', base: 'ETH', quote: 'BTC', active: true },
    Pair { exchange: 'binance', symbol: 'LTCBTC', base: 'LTC', quote: 'BTC', active: true },
    Pair { exchange: 'binance', symbol: 'BNBBTC', base: 'BNB', quote: 'BTC', active: true },
    // -- many more --
]

exchange.tickers()

Fetch all available tickers by request-promise. Depends on the exchange, this will include inactive pairs. Make sure to check ticker.pair.active to confirm.

Exchange.binance.tickers().then(tickers => {
    console.log(tickers);
});

Exchange.bitfinex.tickers().then(tickers => {
    console.log(tickers);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.tickers().then(tickers => {
    console.log(tickers);
});

Exchange.bittrex.tickers().then(tickers => {
    console.log(tickers);
});

Exchange.coinbasepro.tickers().then(tickers => {
    console.log(tickers);
});

Ticker

ParamTypeDescription
pairObjectPair.
lastNumberLast price.
lowestAskNumber / nullLowest ask price.
highestBidNumber / nullHighest bid price.
percentChangeNumber / nullDaily price percentage change.
baseVolumeNumber / nullDaily base volume.
quoteVolumeNumber / nullDaily quote volume.
highNumber / nullDaily highest price.
lowNumber / nullDaily lowest price.
vwapNumber / nullDaily volume weighted average price.
updatedAtDate / nullTicker updated at.
rawDataArray / Object / nullOriginal raw ticker data.
[
    Ticker {
        pair: Pair {
            exchange: 'binance',
            symbol: 'BTCUSDT',
            base: 'BTC',
            quote: 'USDT',
            active: true
        },
        last: 8051.07,
        lowestAsk: 8051.64,
        highestBid: 8050.11,
        percentChange: 0.9,
        baseVolume: 29969.890927,
        quoteVolume: 240744122.84976387,
        high: 8124.92,
        low: 7929.03,
        vwap: 8032.86616679,
        updatedAt: '2019-10-17T21:30:20.796Z',
        rawData: {
            symbol: 'BTCUSDT',
            priceChange: '71.81000000',
            priceChangePercent: '0.900',
            weightedAvgPrice: '8032.86616679',
            prevClosePrice: '7979.26000000',
            lastPrice: '8051.07000000',
            lastQty: '0.03216600',
            bidPrice: '8050.11000000',
            bidQty: '0.04719500',
            askPrice: '8051.64000000',
            askQty: '0.03329000',
            openPrice: '7979.26000000',
            highPrice: '8124.92000000',
            lowPrice: '7929.03000000',
            volume: '29969.89092700',
            quoteVolume: '240744122.84976388',
            openTime: 1571261420796,
            closeTime: 1571347820796,
            firstId: 190861699,
            lastId: 191221055,
            count: 359357
        }
    },
    // -- many more --
]

exchange.api(cache).getJson()

Fetch exchange specific endpoints by hs-cache-request-promise.

// no cache
Exchange.binance.api().getJson('/v1/ticker/24hr').then(rawTicker => {
    console.log(rawTicker);
});

// no cache
Exchange.coinbasepro.api().getJson('/products/BTC-USD/ticker').then(rawTicker => {
    console.log(rawTicker);
});

// no cache; NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.api().tradingPairsInfo().then(tradingPairsInfo => {
    console.log(tradingPairsInfo)
});

// cache ttl 30 sec to a folder
Exchange.binance.api({ttl: 30000, dir: './cache/folder'}).getJson('/v1/exchangeInfo').then(exchangeInfo => {
    console.log(exchangeInfo);
});

// cache ttl 60 sec to a folder
Exchange.bitfinex.api(ttl: 60000, dir: './cache/folder'}).pubListCurrency().then(pubListCurrency => {
    console.log(pubListCurrency)
});

WebSocket

By default, the web socket will automatically reconnect if there is any interruption. If unable to reconnect, it will throw an Error.

exchange.webSocket().ticker(symbols, callback)

Stream live ticker by callback.

ParamTypeDescription
symbolsArrayThe associated exchange symbols.
Exchange.binance.webSocket().ticker(['BTCUSDT', 'ETHUSDT'], ticker => {
    console.log(ticker);
});

Exchange.bitfinex.webSocket().ticker(['BTCUSD', 'ETHUSD'], ticker => {
    console.log(ticker);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().ticker(['btcusd', 'ethusd'], ticker => {
    console.log(ticker);    // bitstamp do not support ws ticker. Only last price & updatedAt available.
});

Exchange.bittrex.webSocket().ticker(['USD-BTC', 'USD-ETH'], ticker => {
    console.log(ticker);
});

Exchange.coinbasepro.webSocket().ticker(['BTC-USD', 'BTC-USD'], ticker => {
    console.log(ticker);
});

Ticker

ParamTypeDescription
pairObjectPair.
lastNumberLast price.
lowestAskNumber / nullLowest ask price.
highestBidNumber / nullHighest bid price.
percentChangeNumber / nullDaily price percentage change.
baseVolumeNumber / nullDaily base volume.
quoteVolumeNumber / nullDaily quote volume.
highNumber / nullDaily highest price.
lowNumber / nullDaily lowest price.
vwapNumber / nullDaily volume weighted average price.
updatedAtDate / nullTicker updated at.
rawDataArray / Object / nullOriginal raw ticker data.
{
    Ticker {
        pair: Pair {
            exchange: 'binance',
            symbol: 'BTCUSDT',
            base: 'BTC',
            quote: 'USDT',
            active: true
        },
        last: 8051.07,
        lowestAsk: 8051.64,
        highestBid: 8050.11,
        percentChange: 0.9,
        baseVolume: 29969.890927,
        quoteVolume: 240744122.84976387,
        high: 8124.92,
        low: 7929.03,
        vwap: 8032.86616679,
        updatedAt: '2019-10-17T21:30:20.796Z',
        rawData: {
            symbol: 'BTCUSDT',
            priceChange: '71.81000000',
            priceChangePercent: '0.900',
            weightedAvgPrice: '8032.86616679',
            prevClosePrice: '7979.26000000',
            lastPrice: '8051.07000000',
            lastQty: '0.03216600',
            bidPrice: '8050.11000000',
            bidQty: '0.04719500',
            askPrice: '8051.64000000',
            askQty: '0.03329000',
            openPrice: '7979.26000000',
            highPrice: '8124.92000000',
            lowPrice: '7929.03000000',
            volume: '29969.89092700',
            quoteVolume: '240744122.84976388',
            openTime: 1571261420796,
            closeTime: 1571347820796,
            firstId: 190861699,
            lastId: 191221055,
            count: 359357
        }
    }
}

exchange.webSocket().trade(symbols, callback)

Stream live trade by callback.

ParamTypeDescription
symbolsArrayThe associated exchange symbols.
Exchange.binance.webSocket().trade(['BTCUSDT', 'ETHUSDT'], trade => {
    console.log(trade);
});

Exchange.bitfinex.webSocket().trade(['BTCUSD', 'ETHUSD'], trade => {
    console.log(trade);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().trade(['btcusd', 'ethusd'], trade => {
    console.log(trade);
});

Exchange.bittrex.webSocket().trade(['USD-BTC', 'USD-ETH'], trade => {
    console.log(trade);
});

Exchange.coinbasepro.webSocket().trade(['BTC-USD', 'BTC-USD'], trade => {
    console.log(trade);
});

Trade

ParamTypeDescription
pairObjectPair from exchange.pairs()
typeStringThe only possible value is 'buy' or 'sell'.
priceNumber / nullTrade price.
baseAmountNumber / nullTrade base amount.
quoteAmountNumber / nullTrade quote amount.
tradeIDNumber / nullTrade ID.
updatedAtDate / nullTrade updated at.
rawDataArray / Object / nullOriginal raw trade data.
{
    Trade {
        pair: Pair { exchange: 'binance', symbol: 'BTCUSDT', base: 'BTC', quote: 'USDT', active: true },
        type: 'buy',
        price: 8087.93,
        baseAmount: 0.034148,
        quoteAmount: 276.18663364,
        tradeID: 173090135,
        updatedAt: '2019-10-18T00:52:04.500Z',
        rawData: {
            eventType: 'aggTrade',
            eventTime: 1571359924500,
            symbol: 'BTCUSDT',
            price: '8087.93000000',
            quantity: '0.03414800',
            maker: false,
            isBuyerMaker: true,
            tradeId: 173090135
        }
    }
}

exchange.webSocket().allTicker(callback)

Stream all available live ticker by callback. It will automatically subscribe to new symbols.

Exchange.binance.webSocket().allTicker(ticker => {
    console.log(ticker);
});

Exchange.bitfinex.webSocket().allTicker(ticker => {
    console.log(ticker);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().allTicker(ticker => {
    console.log(ticker);
});

Exchange.bittrex.webSocket().allTicker(ticker => {
    console.log(ticker);
});

Exchange.coinbasepro.webSocket().allTicker(ticker => {
    console.log(ticker);
});

exchange.webSocket().allTrade(callback)

Stream all available live trade by callback. It will automatically subscribe to new symbols. This is resource-intensive function. Unless you really need all available trades, use exchange.webSocket().trade().

Exchange.binance.webSocket().allTrade(trade => {
    console.log(trade);
});

Exchange.bitfinex.webSocket().allTrade(trade => {
    console.log(trade);
});

// NodeJS v12: make sure to use the option --http-parser=legacy
Exchange.bitstamp.webSocket().allTrade(trade => {
    console.log(trade);
});

Exchange.bittrex.webSocket().allTrade(trade => {
    console.log(trade);
});

Exchange.coinbasepro.webSocket().allTrade(trade => {
    console.log(trade);
});

License

This project is licensed under the MIT License - see the LICENSE.md for details.