Last active
April 29, 2016 03:57
-
-
Save tarcisio/2b7d7a5730962b9d208d791593ce89b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var BitfinexWS = require ('bitfinex-api-node').WS; | |
var moment = require('moment'); | |
function printReg(reg) | |
{ | |
if(1 != reg.Info.version) { | |
process.exit(1); | |
} | |
if(-1 != reg.Info.code) { | |
process.exit(1); | |
} | |
var message = []; | |
message.push(reg.Time); | |
message.push(moment(reg.Time).format()); | |
for (var t in reg.Trades) { | |
for(var k in reg.Trades[t]) { | |
message.push(reg.Trades[t][k]); | |
} | |
} | |
for (var t in reg.Ticker) { | |
for(var k in reg.Ticker[t]) { | |
message.push(reg.Ticker[t][k]); | |
} | |
} | |
console.log(message.join(';')); | |
} | |
var reg = { | |
Time: 0, | |
Info: { | |
version: 0, | |
code: -1 | |
}, | |
Trades: { | |
BTCUSD: {seq: '-', id: 0, timestamp: 0, price: 0, amount: 0}, | |
LTCUSD: {seq: '-', id: 0, timestamp: 0, price: 0, amount: 0}, | |
LTCBTC: {seq: '-', id: 0, timestamp: 0, price: 0, amount: 0}, | |
ETHUSD: {seq: '-', id: 0, timestamp: 0, price: 0, amount: 0}, | |
ETHBTC: {seq: '-', id: 0, timestamp: 0, price: 0, amount: 0} | |
}, | |
Ticker: { | |
BTCUSD: {bid: 0, bidSize: 3, ask: 0, askSize: 0, dailyChange: 0, dailyChangePerc: 0, lastPrice: 0, volume: 0, high: 0, low: 0}, | |
LTCUSD: {bid: 0, bidSize: 3, ask: 0, askSize: 0, dailyChange: 0, dailyChangePerc: 0, lastPrice: 0, volume: 0, high: 0, low: 0}, | |
LTCBTC: {bid: 0, bidSize: 3, ask: 0, askSize: 0, dailyChange: 0, dailyChangePerc: 0, lastPrice: 0, volume: 0, high: 0, low: 0}, | |
ETHUSD: {bid: 0, bidSize: 3, ask: 0, askSize: 0, dailyChange: 0, dailyChangePerc: 0, lastPrice: 0, volume: 0, high: 0, low: 0}, | |
ETHBTC: {bid: 0, bidSize: 3, ask: 0, askSize: 0, dailyChange: 0, dailyChangePerc: 0, lastPrice: 0, volume: 0, high: 0, low: 0} | |
} | |
}; | |
var bws = new BitfinexWS(); | |
bws.on('open', function () | |
{ | |
bws.subscribeTrades('BTCUSD'); | |
bws.subscribeTicker('BTCUSD'); | |
bws.subscribeTrades('LTCUSD'); | |
bws.subscribeTicker('LTCUSD'); | |
bws.subscribeTrades('LTCBTC'); | |
bws.subscribeTicker('LTCBTC'); | |
bws.subscribeTrades('ETHUSD'); | |
bws.subscribeTicker('ETHUSD'); | |
bws.subscribeTrades('ETHBTC'); | |
bws.subscribeTicker('ETHBTC'); | |
}); | |
bws.on('info', function (info) | |
{ | |
reg.Time = moment().valueOf(); | |
if("version" in info) { | |
reg.Info.version = info.version; | |
} | |
if("code" in info) { | |
reg.Info.code = info.code; | |
} | |
printReg(reg); | |
}); | |
bws.on('ticker', function (pair, ticker) | |
{ | |
reg.Time = moment().valueOf(); | |
reg.Ticker[pair] = ticker; | |
printReg(reg); | |
}); | |
bws.on('trade', function (pair, trade) | |
{ | |
reg.Time = moment().valueOf(); | |
if("id" in trade) { | |
reg.Trades[pair] = trade; | |
} | |
printReg(reg); | |
}); | |
// ----------------------------------------------------------------------------- | |
bws.on('error', function (){process.exit(1)}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment