1
votes

please apologize the unprecise title for this question, I am not an experienced programmer and even less so in node.js

My intent is a simple one: I want to use the bitfinex-api-node package (a node.js wrapper for bitfinex cryptocurrency exchange) that I installed via npm to read price data of various currency-pairs from the exchange to calculate better trading strategies.

The example code provided in the readme.md works fine, this is a stripped down version that creates a BFX-object which subscribes to a ticker of a given currency-pair and constantly outputs ticker-data:

const BFX = require('bitfinex-api-node')

const API_KEY = 'secret'
const API_SECRET = 'secret'

const opts = {
  version: 2,
  transform: true
}

const bws = new BFX(API_KEY, API_SECRET, opts).ws


bws.on('open', () => {
  bws.subscribeTicker('BTCUSD') 
})

bws.on('ticker', (pair, ticker) => {
  console.log('Ticker:', ticker)
})

bws.on('error', console.error)

so far so good. Now for the sake of a simple example let's say I want to get the current price of two currency pairs (BTC/USD, ETH/USD) and add them an display the result. My obviously naive approach is like this:

const BFX = require('bitfinex-api-node')

const API_KEY = 'secret'
const API_SECRET = 'secret'

const opts = {
  version: 2,
  transform: true
}

const bws1 = new BFX(API_KEY, API_SECRET, opts).ws
const bws2 = new BFX(API_KEY, API_SECRET, opts).ws

var priceBTCUSD;
var priceETHBTC;

bws1.on('open', () => {
  bws1.subscribeTicker('BTCUSD') 
})
bws2.on('open', () => {
  bws2.subscribeTicker('ETHUSD') 
})

bws1.on('ticker', (pair, ticker) => {
  //console.log('Ticker1:', ticker.LAST_PRICE)
  priceBTCUSD = ticker.LAST_PRICE
})
bws2.on('ticker', (pair, ticker) => {
  //console.log('Ticker2:', ticker.LAST_PRICE)
  priceETHBTC = ticker.LAST_PRICE
})

bws1.on('error', console.error)
bws2.on('error', console.error)
//HERE IT COMES:
console.log(priceBTCUSD+priceETHBTC)

where the resulting output of the last line is "NaN". It seems the last line that logs the desired result to the console is executed before the BFX-objects establish a connection and receive any data.

How do I set this up properly? How can I retrieve data from the received data-stream? Do I really need a BFX-websocket object per currency pair? How would I read the price-data once, close down the websocket connection (which is not needed after reading the price once) and reconnect to read the price for a different currency pair?

Thank you! Feel free to request more data if my question isn't clear enough.

Kind regards, s

1

1 Answers

1
votes

Oh, your console.log is too soon there. Try this (I skipped a few lines):

bws1.on('ticker', (pair, ticker) => {
  //console.log('Ticker1:', ticker.LAST_PRICE)
  priceBTCUSD = ticker.LAST_PRICE;
  printResults();
})
bws2.on('ticker', (pair, ticker) => {
  //console.log('Ticker2:', ticker.LAST_PRICE)
  priceETHBTC = ticker.LAST_PRICE
  printResults();
})

bws1.on('error', console.error)
bws2.on('error', console.error)
//HERE IT COMES:
function printResults() {
  if (priceBTCUSD && priceETHBTC)
    console.log(priceBTCUSD+priceETHBTC)  
}

Now, this is not the best approach, but it gets you of the ground. A better way is to have both prices asked on the same websocket, so when you get both prices back, call this function to calculate your results.