-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
159 lines (126 loc) · 3.93 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var util = require('util');
var colors = require('colors');
var datetime = require('datetime');
var mtgox = require('./mtgox');
var lastTradePrice = -1;
var lastTickerPrice = -1;
var lastTickerVolume = -1;
var client = mtgox.connect();
client.on('open', function() {
// Good place to unsubscribe from unwanted channels
// client.unsubscribe(mtgox.getChannel('trade').key);
// client.unsubscribe(mtgox.getChannel('depth').key);
// client.unsubscribe(mtgox.getChannel('ticker').key);
});
client.on('subscribe', function(message) {
renderSubscribeMessage(message);
});
client.on('unsubscribe', function(message) {
renderUnsubscribeMessage(message);
});
client.on('trade', function(message) {
renderTradeMessage(message, lastTradePrice);
lastTradePrice = message.trade.price;
});
client.on('depth', function(message) {
renderDepthMessage(message);
});
client.on('ticker', function(message) {
renderTickerMessage(message, lastTickerPrice);
lastTickerPrice = message.ticker.last;
lastTickerVolume = message.ticker.vol;
});
process.on('exit', function() {
console.log('Goodbye!'.bold);
client.close();
});
var renderSubscribeMessage = function(message) {
var format = 'Subscribed to channel:'.green;
console.log(getTimeFormat(), format, getChannelFormat(message));
};
var renderUnsubscribeMessage = function(message) {
var format = 'Unsubscribed from channel:'.red;
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 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, lastPrice);
high += ticker.high;
low += ticker.low;
vol += ticker.vol;
avg += ticker.vwap;
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 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;
};