Permalink
Browse files

[refactor] Move ticker logic into bin/ticker

  • Loading branch information...
1 parent 999caaa commit d39341f29155168eb7dfe9212017590f2e37d073 @Marak Marak committed Sep 16, 2011
Showing with 123 additions and 113 deletions.
  1. +1 −113 bin/mtgox
  2. +122 −0 bin/ticker
View
114 bin/mtgox
@@ -7,116 +7,4 @@ var hook = new MtgoxHook({
debug: false
});
-
-hook.on('children::ready', function(){
-
- hook.lastPrice = 0;
-
- hook.on('subscribe', function(data){
- console.log('Subscribed to channel: '.green, getChannel(data.channel).name.magenta);
- });
-
- hook.on('private', function(data){
- console.log('private'.magenta, data);
- });
-
- hook.on('trade', function(data){
- renderTrade(data, hook.lastPrice);
- hook.lastPrice = data.trade.price;
- });
-
- hook.on('depth', function(data){
- if (hook.v || hook.verbose)
- renderChange(data);
- });
-
- hook.on('ticker', function(data){
- renderTicker(data,hook.lastPrice);
- });
-
-});
-
-hook.start();
-
-
-var getChannel = function(key){
- var selected = hook.channels.filter(function(c){
- if (c.key === key)
- return true;
- return false;
- });
- return selected[0];
-};
-
-var getTime = function(){
- var now = new Date();
- var time = '['+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds()+'] ';
- return time.blue;
-}
-
-var renderChange = function(depth){
- var vol,
- data = depth.depth,
- msg = getTime();
- if (data.volume < 0){
- // An order has been deleted
- msg += 'Removed '.grey;
- vol = (data.volume * -1).toString();
- } else {
- // An order has been created
- msg += 'Created '.grey;
- vol = data.volume;
- }
- msg += data.type_str.bold;
- msg += ' '+vol.yellow + ' @ '.italic + data.price.green;
- console.log(msg);
-};
-
-var renderTrade = function(trade,last){
- var data = trade.trade,
- msg = getTime(),
- btc = data.amount + ' ' + data.item,
- money = data.price + ' ' + data.price_currency;
-
- if (data.trade_type === 'ask'){
- msg += 'Sell ';
- }
-
- if (data.trade_type === 'bid'){
- msg += 'Buy ';
- }
-
- msg += btc.yellow + ' @ ';
-
- if (data.price > last){
- msg += money.green;
- msg += ''.green.bold;
- }
-
- if (data.price < last){
- msg += money.red;
- msg += ''.red.bold;
- }
-
- if (data.price == last){
- msg += money;
- }
-
- console.log(msg.bold);
-};
-
-var renderTicker = function(tick,last){
- var data = tick.ticker;
-
- var lineOne = ' Volume: ' + data.vol.toString().red;
- lineOne += ' Last Price: ' + last.toString().green.bold;
-
- var lineTwo = ' Buy: ' + data.buy.toString().bold;
- lineTwo += ' Sell: ' + data.sell.toString().bold;
-
- var lineThree = ' | Max: ' + data.high.toString().italic;
- lineThree += ' Min: ' + data.low.toString().italic;
-
- console.log(lineOne);
- console.log(lineTwo,lineThree);
-};
+hook.start();
View
@@ -0,0 +1,122 @@
+#!/usr/bin/env node
+
+var MtgoxHook = require('../lib/mtgox').MtgoxHook;
+
+var hook = new MtgoxHook({
+ name: 'mtgox',
+ debug: false
+});
+
+
+hook.on('children::ready', function(){
+
+ hook.lastPrice = 0;
+
+ hook.on('subscribe', function(data){
+ console.log('Subscribed to channel: '.green, getChannel(data.channel).name.magenta);
+ });
+
+ hook.on('private', function(data){
+ console.log('private'.magenta, data);
+ });
+
+ hook.on('trade', function(data){
+ renderTrade(data, hook.lastPrice);
+ hook.lastPrice = data.trade.price;
+ });
+
+ hook.on('depth', function(data){
+ if (hook.v || hook.verbose)
+ renderChange(data);
+ });
+
+ hook.on('ticker', function(data){
+ renderTicker(data,hook.lastPrice);
+ });
+
+});
+
+hook.start();
+
+
+var getChannel = function(key){
+ var selected = hook.channels.filter(function(c){
+ if (c.key === key)
+ return true;
+ return false;
+ });
+ return selected[0];
+};
+
+var getTime = function(){
+ var now = new Date();
+ var time = '['+now.getHours()+':'+now.getMinutes()+':'+now.getSeconds()+'] ';
+ return time.blue;
+}
+
+var renderChange = function(depth){
+ var vol,
+ data = depth.depth,
+ msg = getTime();
+ if (data.volume < 0){
+ // An order has been deleted
+ msg += 'Removed '.grey;
+ vol = (data.volume * -1).toString();
+ } else {
+ // An order has been created
+ msg += 'Created '.grey;
+ vol = data.volume;
+ }
+ msg += data.type_str.bold;
+ msg += ' '+vol.yellow + ' @ '.italic + data.price.green;
+ console.log(msg);
+};
+
+var renderTrade = function(trade,last){
+ var data = trade.trade,
+ msg = getTime(),
+ btc = data.amount + ' ' + data.item,
+ money = data.price + ' ' + data.price_currency;
+
+ if (data.trade_type === 'ask'){
+ msg += 'Sell ';
+ }
+
+ if (data.trade_type === 'bid'){
+ msg += 'Buy ';
+ }
+
+ msg += btc.yellow + ' @ ';
+
+ if (data.price > last){
+ msg += money.green;
+ msg += ''.green.bold;
+ }
+
+ if (data.price < last){
+ msg += money.red;
+ msg += ''.red.bold;
+ }
+
+ if (data.price == last){
+ msg += money;
+ }
+
+ console.log(msg.bold);
+};
+
+var renderTicker = function(tick,last){
+ var data = tick.ticker;
+
+ var lineOne = ' Volume: ' + data.vol.toString().red;
+ lineOne += ' Last Price: ' + last.toString().green.bold;
+
+ var lineTwo = ' Buy: ' + data.buy.toString().bold;
+ lineTwo += ' Sell: ' + data.sell.toString().bold;
+
+ var lineThree = ' | Max: ' + data.high.toString().italic;
+ lineThree += ' Min: ' + data.low.toString().italic;
+
+ console.log(lineOne);
+ console.log(lineTwo,lineThree);
+};

0 comments on commit d39341f

Please sign in to comment.