简体   繁体   English

Node.js、bitfinex-api-node:如何正确设置以处理来自 websocket-connection 的数据

[英]Node.js, bitfinex-api-node: How-to set-up correctly to process data from the websocket-connection

please apologize the unprecise title for this question, I am not an experienced programmer and even less so in node.js请为这个问题的不准确标题道歉,我不是一个有经验的程序员,在 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.我的意图很简单:我想使用通过 npm 安装的bitfinex-api-node包(用于 bitfinex 加密货币交换的 node.js 包装器)从交易所读取各种货币对的价格数据以计算更好的交易策略。

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: readme.md 中提供的示例代码工作正常,这是一个精简版本,它创建一个 BFX 对象,该对象订阅给定货币对的股票代码并不断输出股票代码数据:

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.现在为了一个简单的例子,假设我想获得两个货币对(BTC/USD、ETH/USD)的当前价格并将它们添加并显示结果。 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".其中最后一行的结果输出是“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.似乎在 BFX 对象建立连接并接收任何数据之前执行将所需结果记录到控制台的最后一行。

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?每个货币对我真的需要一个 BFX-websocket 对象吗? 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?我将如何读取价格数据一次,关闭 websocket 连接(读取一次价格后不需要)并重新连接以读取不同货币对的价格?

Thank you!谢谢! Feel free to request more data if my question isn't clear enough.如果我的问题不够清楚,请随时请求更多数据。

Kind regards, s亲切的问候,s

Oh, your console.log is too soon there.哦,你的 console.log 太早了。 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.更好的方法是在同一个 websocket 上询问两个价格,所以当你得到两个价格时,调用这个函数来计算你的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM