forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtgox.js
178 lines (144 loc) · 4.68 KB
/
mtgox.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
var MtGoxClient = require("mtgox-apiv2");
var _ = require('lodash');
var moment = require('moment');
var util = require('../core/util.js');
var log = require('../core/log');
var Trader = function(config) {
if(_.isObject(config)) {
this.key = config.key;
this.secret = config.secret;
this.currency = config.currency || 'USD';
this.pair = 'BTC' + this.currency;
}
this.name = 'Mt. Gox';
_.bindAll(this);
this.mtgox = new MtGoxClient(this.key, this.secret, this.pair);
}
Trader.prototype.buy = function(amount, price, callback) {
var process = function(err, result) {
this.checkUnauthorized(err);
// if Mt. Gox is down or lagging
if(err || result.result === 'error')
log.error('unable to buy (', err, result, ')');
callback(null, result.data);
};
this.mtgox.add('bid', amount, price, _.bind(process, this));
}
Trader.prototype.sell = function(amount, price, callback) {
var process = function(err, result) {
this.checkUnauthorized(err);
// if Mt. Gox is down or lagging
if(err || result.result === 'error')
log.error('unable to sell (', err, result, ')');
callback(null, result.data);
};
this.mtgox.add('ask', amount, price, _.bind(process, this));
}
Trader.prototype.getTrades = function(since, callback, descending) {
if(since && !_.isNumber(since))
since = util.toMicro(since);
var args = _.toArray(arguments);
this.mtgox.fetchTrades(since, _.bind(function(err, trades) {
if (err || !trades)
return this.retry(this.getTrades, args);
trades = trades.data;
if (trades.length === 0)
return this.retry(this.getTrades, args);
if(descending)
callback(false, trades.reverse());
else
callback(false, trades);
}, this));
}
// if the exchange errors we try the same call again after
// waiting 10 seconds
Trader.prototype.retry = function(method, args) {
var wait = +moment.duration(10, 'seconds');
log.debug(this.name, 'returned an error, retrying..');
var self = this;
// make sure the callback (and any other fn)
// is bound to Trader
_.each(args, function(arg, i) {
if(_.isFunction(arg))
args[i] = _.bind(arg, self);
});
// run the failed method again with the same
// arguments after wait.
setTimeout(
function() { method.apply(self, args) },
wait
);
}
Trader.prototype.checkUnauthorized = function(err) {
if(err && err.message === 'Request failed with 403')
util.die('It appears your ' + this.name + ' API key and secret are incorrect');
}
// calls callback with the following data structure:
//
// [
// { name: 'BTC', amount: 10.12413123},
// { name: 'USD', amount: 500.0241},
// { name: 'EUR', amount: 0},
// ]
Trader.prototype.getPortfolio = function(callback) {
var args = _.toArray(arguments);
var calculate = function(err, result) {
this.checkUnauthorized(err);
if(err)
return this.retry(this.getPortfolio, args);
if(!('Wallets' in result.data))
log.error('unable to get portfolio, do I have get_info rights?');
var assets = [];
_.each(result.data.Wallets, function(wallet, name) {
var amount = parseFloat(wallet.Balance.value);
assets.push({name: name, amount: amount});
});
callback(null, assets);
};
this.mtgox.info(_.bind(calculate, this));
}
Trader.prototype.getFee = function(callback) {
var args = _.toArray(arguments);
var calculate = function(err, result) {
this.checkUnauthorized(err);
if(err)
return this.retry(this.getFee, args);
// convert the %
var fee = result.data.Trade_Fee / 100;
callback(null, fee);
};
this.mtgox.info(_.bind(calculate, this));
}
Trader.prototype.checkOrder = function(order, callback) {
var args = _.toArray(arguments);
// we can't just request the order id
// @link https://bitbucket.org/nitrous/mtgox-api/#markdown-header-moneyorderresult
var check = function(err, result) {
if(err)
return this.retry(this.checkOrder, args);
var stillThere = _.find(result.data, function(o) { return o.oid === order });
callback(null, !stillThere);
};
this.mtgox.orders(_.bind(check, this));
}
Trader.prototype.cancelOrder = function(order) {
var cancel = function(err, result) {
if(err || result.result !== 'succes')
log.error('unable to cancel order', order, '(', err, result, ')');
};
this.mtgox.cancel(order, _.bind(cancel, this));
}
Trader.prototype.getTicker = function(callback) {
var args = _.toArray(arguments);
var set = function(err, result) {
if(err)
return this.retry(this.getTicker, args);
var ticker = {
bid: result.data.buy.value,
ask: result.data.sell.value
}
callback(err, ticker);
};
this.mtgox.ticker(_.bind(set, this));
}
module.exports = Trader;