/
mtgox_rest.js
85 lines (68 loc) · 2.06 KB
/
mtgox_rest.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
/// mtgox rest order book
var https = require('https');
var events = require('events');
// emits level_changed events
var L1RestBook = function(options) {
var self = this;
var host = options.host;
var path = '/api/0/data/getDepth.php?Currency=USD' + options.product;
var bid = {};
var ask = {};
function maybe_update(side, old_value, new_value) {
var new_level = {
price: new_value[0] - 0,
size: new_value[1] - 0,
};
function emit(event) {
old_value.price = new_level.price;
old_value.size = new_level.size;
self.emit(event, {
side: side,
price: old_value.price,
size: old_value.size
});
};
if (!old_value.price || !old_value.size) {
if (new_value) {
emit('changed');
}
return;
}
if (old_value.price !== new_level.price || old_value.size !== new_level.size) {
emit('changed');
}
}
function process_new_book(book) {
maybe_update(0, bid, book.bids[book.bids.length - 1]);
maybe_update(1, ask, book.asks[0]);
}
function refresh() {
var options = {
host: host,
path: path,
headers: {
accept: 'application/json',
'user-agent': 'nodejs',
}
};
https.get(options, function(res) {
var buff = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
buff += chunk;
});
res.on('end', function() {
var book = JSON.parse(buff);
process_new_book(book);
});
res.on('error', function(err) {
self.emit('error', err);
});
});
};
setInterval(refresh, options.refresh_interval || 1000);
};
L1RestBook.prototype = new events.EventEmitter();
module.exports.l1_book = function(options) {
return new L1RestBook(options);
};