Skip to content

Commit

Permalink
Fixed issue mentioned by olalonde. Factored console formatting out to…
Browse files Browse the repository at this point in the history
… separate file. Little cleanup on console output. Put back 'connect' method for better backwards compatibility.
  • Loading branch information
johnwarden committed Aug 23, 2013
1 parent 7d0ef8f commit 929b974
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 107 deletions.
108 changes: 13 additions & 95 deletions demo.js
@@ -1,12 +1,20 @@
var util = require('util'); var mtgox = require('./mtgox');
var colors = require('colors'); var consoleFormatting = require('./console-formatting')
var datetime = require('datetime');
var MtGoxClient = require('./mtgox').MtGoxClient var getTradeFormat = consoleFormatting.getTradeFormat;
var getPriceFormat = consoleFormatting.getPriceFormat;
var getTickerFormat = consoleFormatting.getTickerFormat;
var getDepthFormat = consoleFormatting.getDepthFormat;
var getTimeFormat = consoleFormatting.getTimeFormat;

// var MtGoxClient = require('./mtgox').MtGoxClient


var lastTradePrice = -1; var lastTradePrice = -1;
var lastTickerPrice = -1; var lastTickerPrice = -1;
var lastTickerVolume = -1; var lastTickerVolume = -1;
var client = new MtGoxClient();
// var client = new MtGoxClient();
var client = mtgox.connect();


client.on('connect', function() { client.on('connect', function() {
// Good place to unsubscribe from unwanted channels // Good place to unsubscribe from unwanted channels
Expand Down Expand Up @@ -65,97 +73,7 @@ var renderDepthMessage = function(depth) {
console.log(getTimeFormat(), getDepthFormat(depth)); console.log(getTimeFormat(), getDepthFormat(depth));
}; };


var getDepthFormat = function(depth) {
var format = '';

if (depth.volume < 0) {
format += '+ '.grey;
}
else {
format += '- '.grey;
}

if (depth.type_str == 'ask') {
format += 'Ask: '.grey.bold;
}
else if (depth.type_str == 'bid') {
format += 'Bid: '.grey.bold;
}

var amount = Math.abs(depth.volume);
var price = Math.abs(depth.price);

format += (amount + ' ' + depth.item).yellow + ' @ ';
format += getPriceFormat(price, price, depth.currency);

return format;
};

var getTickerFormat = function(ticker, lastPrice) {


var format = '> ';

var last = 'Last: '.bold;
var high = 'High: '.bold;
var low = 'Low: '.bold;
var vol = 'Vol: '.bold;
var avg = 'Avg: '.bold;

last += getPriceFormat(ticker.last.display, lastPrice);
high += ticker.high.display;
low += ticker.low.display;
vol += ticker.vol.display;
avg += ticker.vwap.display;

return format + [vol, high, low, avg, last].join(' ');
};

var getTradeFormat = function(trade, lastPrice) {
var format = '$ ';

if (trade.trade_type == 'ask') {
format += 'Ask: '.bold;
}
else if (trade.trade_type == 'bid') {
format += 'Bid: '.bold;
}

format += (trade.amount + ' ' + trade.item).yellow + ' @ ';
format += getPriceFormat(trade.price, lastPrice, trade.price_currency);

return format;
};

var getChannelFormat = function(message) { var getChannelFormat = function(message) {
var channel = mtgox.getChannel(message.channel)||message.channel; var channel = mtgox.getChannel(message.channel)||message.channel;
return channel.name.magenta; return channel.name.magenta;
}; };

var getTimeFormat = function() {
var now = new Date();
var time = '[' + datetime.format(now, '%T') + ']';
return time.blue;
};

var getPriceFormat = function(currentPrice, lastPrice, currency) {
var format = currentPrice + (currency ? ' ' + currency : '');
if (lastPrice < 0) {
return format;
}

var delta = lastPrice - currentPrice;
var percent = (lastPrice > 0) ? (delta / lastPrice) * 100 : 100;
var round = function(n) {
return Math.round(Math.abs(n) * 100) / 100;
};

if (delta > 0) {
format += (' \u25b2 +' + round(delta) + ' +' + round(percent) + '%').green;
}
else if (delta < 0) {
format += (' \u25bc -' + round(delta) + ' -' +round( percent) + '%').red;
}

return format;
};
21 changes: 9 additions & 12 deletions mtgox.js
Expand Up @@ -20,7 +20,7 @@ var MTGOX_CHANNELS = [


var getChannel = function(key) { var getChannel = function(key) {
return MTGOX_CHANNELS.filter(function(channel) { return MTGOX_CHANNELS.filter(function(channel) {
return (channel.key == key || channel.private == key || channel.key == key); return (channel.key == key || channel.private == key);
})[0]; })[0];
}; };


Expand Down Expand Up @@ -55,16 +55,12 @@ var MtGoxClient = function(options) {
socket.on('message', function(raw) { socket.on('message', function(raw) {
lastPulse = new Date() / 1E3; lastPulse = new Date() / 1E3;


// Emit raw data
var data = raw; var data = raw;


// Emit messages
var message = data; var message = data;



if (message.op == 'remark') { if (message.op == 'remark') {
console.info("Remark",message); console.info("Remark",message);
console.info("Debug",message.debug);
} }
else if (message.op == 'subscribe') { else if (message.op == 'subscribe') {
console.info("Got subscribe confirmation",getChannel(message.channel).name); console.info("Got subscribe confirmation",getChannel(message.channel).name);
Expand Down Expand Up @@ -109,11 +105,11 @@ var MtGoxClient = function(options) {
}); });


socket.on('connect', function() { socket.on('connect', function() {
console.info("Got connect"); console.info("Successfully connected");


if(options.channels) { if(options.channels) {
if(options.channels.client) { if(options.channels.client) {
console.info("Subscribing to client"); console.info("subscribing to private client data");
self.subscribePrivate(); self.subscribePrivate();
} }
if(!options.channels.trades) { if(!options.channels.trades) {
Expand All @@ -126,7 +122,6 @@ var MtGoxClient = function(options) {
} }
} }



}); });


socket.on('disconnected', function() { socket.on('disconnected', function() {
Expand All @@ -140,7 +135,7 @@ var MtGoxClient = function(options) {




socket.on('close', function() { socket.on('close', function() {
console.info("Got close"); console.info("Got socket close");
}); });


function reSubscribe() { function reSubscribe() {
Expand Down Expand Up @@ -215,10 +210,10 @@ var MtGoxClient = function(options) {
function checkPulse() { function checkPulse() {
var now = new Date() / 1E3; var now = new Date() / 1E3;
if((now - lastPulse) > 30) { if((now - lastPulse) > 30) {
console.info("checking pulse: " + Math.round((now - lastPulse)*100)/100); console.info("No activity on socket for " + (Math.round((now - lastPulse)*100)/100) + "s");
} }
if (now - lastPulse > threshhold) { if (now - lastPulse > threshhold) {
console.info("More than " + threshhold + "s since last data. Reopening socket.") console.info("More than " + threshhold + "s since last activity. Destroying and re-creating socket.")
reopenSocket(); reopenSocket();
} }
} }
Expand All @@ -228,4 +223,6 @@ util.inherits(MtGoxClient, EventEmitter);


exports.MtGoxClient = MtGoxClient; exports.MtGoxClient = MtGoxClient;



exports.connect = function() {
return new MtGoxClient();
}

0 comments on commit 929b974

Please sign in to comment.