Permalink
Browse files

Merge pull request #2 from johnwarden/master

Various Modifications
  • Loading branch information...
2 parents c9a312a + 929b974 commit 4bfa5699888b81bbace9faf8cbcf8fd4999eddc9 @olalonde committed Sep 5, 2013
Showing with 394 additions and 206 deletions.
  1. +26 −1 README.md
  2. +102 −0 console-formatting.js
  3. +26 −106 demo.js
  4. +1 −0 keys.js
  5. +210 −80 mtgox.js
  6. +29 −19 package.json
View
@@ -8,17 +8,42 @@ Installation
$ npm install mtgox-socket-client
+$ node demo.js
+
+You may also need to put your API keys inside of 'keys.js' if you want to access the 'client' channel (e.g. your personal trades).
+
+Example Use
+------------
+
+```javascript
+var MtGoxClient = require('mtgox-client').MtGoxClient;
+
+var client = new MtGoxClient({channels: {client: true, trade: true}});
+
+/* Show the trade message every time someone makes a trade */
+client.on("trade",function(message) {
+ console.log("Got trade message from mtgox!",message);
+});
+
+/* Show the trade message every there is a client-specific action (buy, sell, etc) */
+client.on("other",function(message) {
+ console.log("Got client-specific message from mtgox!",message);
+});
+```
+
+
Contributors
------------
+* [olalonde](https://github.com/olalonde)
* [dlanod](https://github.com/dlanod)
* [cronopio](https://github.com/cronopio)
* [marak](https://github.com/marak)
License
-------
-Copyright 2011 Donald Ness, Olivier Lalonde
+Copyright 2011 Donald Ness, Olivier Lalonde, Jonathan Warden
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
View
@@ -0,0 +1,102 @@
+var colors = require('colors');
+
+
+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 = currentPrice - lastPrice;
+ 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;
+};
+
+
+var getTradeFormat = function(trade, lastPrice) {
+
+ var format = getTimeFormat(trade.dateObject) + ' $ ';
+
+ 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 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(' ');
+};
+
+
+exports.getTradeFormat = getTradeFormat;
+exports.getPriceFormat = getPriceFormat;
+exports.getTickerFormat = getTickerFormat;
+exports.getDepthFormat = getDepthFormat;
+exports.getTimeFormat = getTimeFormat;
View
132 demo.js
@@ -1,11 +1,19 @@
-var util = require('util');
-var colors = require('colors');
-var datetime = require('datetime');
var mtgox = require('./mtgox');
+var consoleFormatting = require('./console-formatting')
+
+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 lastTickerPrice = -1;
var lastTickerVolume = -1;
+
+// var client = new MtGoxClient();
var client = mtgox.connect();
client.on('connect', function() {
@@ -23,19 +31,19 @@ client.on('unsubscribe', function(message) {
renderUnsubscribeMessage(message);
});
-client.on('trade', function(message) {
- renderTradeMessage(message, lastTradePrice);
- lastTradePrice = message.trade.price;
+client.on('trade', function(trade) {
+ renderTradeMessage(trade, lastTradePrice);
+ lastTradePrice = trade.price;
});
-client.on('depth', function(message) {
- renderDepthMessage(message);
+client.on('depth', function(depth) {
+ renderDepthMessage(depth);
});
-client.on('ticker', function(message) {
- renderTickerMessage(message, lastTickerPrice);
- lastTickerPrice = message.ticker.last;
- lastTickerVolume = message.ticker.vol;
+client.on('ticker', function(ticker) {
+ renderTickerMessage(ticker, lastTickerPrice);
+ lastTickerPrice = ticker.last;
+ lastTickerVolume = ticker.vol;
});
process.on('exit', function() {
@@ -53,107 +61,19 @@ var renderUnsubscribeMessage = function(message) {
console.log(getTimeFormat(), format, getChannelFormat(message));
};
-var renderTradeMessage = function(message, lastPrice) {
- console.log(getTimeFormat(), getTradeFormat(message.trade, lastPrice));
-};
-
-var renderTickerMessage = function(message, lastPrice) {
- console.log(getTimeFormat(), getTickerFormat(message.ticker, lastPrice));
-};
-
-var renderDepthMessage = function(message) {
- console.log(getTimeFormat(), getDepthFormat(message.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 renderTradeMessage = function(trade, lastPrice) {
+ console.log(getTimeFormat(), getTradeFormat(trade, lastPrice));
};
-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 renderTickerMessage = function(ticker, lastPrice) {
+ console.log(getTimeFormat(), getTickerFormat(ticker, lastPrice));
};
-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 renderDepthMessage = function(depth) {
+ console.log(getTimeFormat(), getDepthFormat(depth));
};
var getChannelFormat = function(message) {
var channel = mtgox.getChannel(message.channel)||message.channel;
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;
-};
View
@@ -0,0 +1 @@
+exports.keys = ['your keys here', 'your keys here'];
Oops, something went wrong.

0 comments on commit 4bfa569

Please sign in to comment.