如何过滤Bitfinex API ws2流数据?

时间:2017-07-14 13:23:17

标签: javascript json node.js websocket lodash

我尝试使用以下代码从WS流中过滤特定数据:

bws.on('open', () => {
  console.log('WS open')
bws.subscribeTrades('BTCUSD')

bws.on('trade', (pair, msg) => {
    var trades = msg.map(function (msg) {
        return {
            trade_id: msg.ID,
            time: msg.MTS,
            size: Math.abs(msg.AMOUNT),
            price: msg.PRICE,
            side: msg.AMOUNT > 0 ? 'buy' : 'sell'
        }
    })
    console.log('Trades:\n', trades)
})

我得到未定义的数据:

WS open
Trades:
[ {
<..> 
{ 
     trade_id: 43477863,
     time: 1500037112000,
     size: 0.01,
     price: 2259.7,
     side: 'buy' 
} ]
Trades:
 [ { trade_id: undefined,
     time: undefined,
      size: NaN,
      price: undefined,
      side: 'sell' 
 }, { 
      trade_id: 43478179,
      time: 1500037170000,
      size: 0.35702096,
      price: 2259.9,
      side: 'sell' 
} ]
Trades:
[ { trade_id: undefined,
    time: undefined,
    size: NaN,
    price: undefined,
    side: 'sell' 
}, { 
    trade_id: 43478179,
    time: 1500037170000,
    size: 0.35702096,
    price: 2259.9,
    side: 'sell' 
} ]

正在流媒体

[{ID:,MTS:,AMOUNT:,PRICE},{ID:,MTS:,AMOUNT:,PRICE:}]

在第一个块中,然后用

更新
['te',{ID:,MTS:,AMOUNT:,PRICE:}]

['tu', {ID:,MTS:,AMOUNT:,PRICE:}]

我想我得到了未定义的&#39;何时发生这些更新。我该如何管理? 它已经通过API解析了JSON。

1 个答案:

答案 0 :(得分:0)

我有解决方案:

var wstrades = []
bws.on('open', () => {
  console.log('WS open')
bws.subscribeTrades('BTCUSD')
bws.on('close', () => {
  console.log('WS close')
})
bws.on('trade', (pair, data) => {
  wstrades.push( {
    trade_id: data[1].ID,
    time: data[1].MTS,
    size: Math.abs(data[1].AMOUNT),
    price: data[1].PRICE,
    side: data[1].AMOUNT > 0 ? 'buy' : 'sell'
  })
  var t = wstrades
  var trades = t.map(function (trade) {
    return (trade)
  })
  console.log('Trades:\n', trades)
})

虽然输出有重复,因为&#39; te&#39;和&#39; tu&#39;更新:

Trades:
 [ { trade_id: 43683909,
    time: 1500097172000,
    size: 1,
    price: 2109.8,
    side: 'buy' },
  { trade_id: 43683913,
    time: 1500097178000,
    size: 0.0075,
    price: 2109.8,
    side: 'buy' },
  { trade_id: 43683914,
    time: 1500097178000,
    size: 0.9925,
    price: 2110,
    side: 'buy' },
  { trade_id: 43683913,
    time: 1500097178000,
    size: 0.0075,
    price: 2109.8,
    side: 'buy' } ]

否&#39;未定义&#39;。但无法过滤特定阵列......仍然打开。