UNPKG

4.26 kBJavaScriptView Raw
1var MtGoxClient = require("mtgox-apiv2");
2var util = require('../util.js');
3var _ = require('underscore');
4var log = require('../log.js');
5var moment = require('moment');
6
7var Trader = function(config) {
8 if(_.isObject(config)) {
9 this.key = config.key;
10 this.secret = config.secret;
11 this.currency = config.currency || 'USD';
12
13 this.pair = 'BTC' + this.currency;
14 }
15 this.name = 'Mt. Gox';
16
17 _.bindAll(this);
18
19 this.mtgox = new MtGoxClient(this.key, this.secret, this.pair);
20}
21
22Trader.prototype.buy = function(amount, price, callback) {
23 this.mtgox.add('bid', amount, price, function(err, result) {
24 // if Mt. Gox is down or lagging
25 if(err || result.result === 'error')
26 log.error('unable to buy (', err, result, ')');
27
28 callback(err, result.data);
29 });
30}
31
32Trader.prototype.sell = function(amount, price, callback) {
33 this.mtgox.add('ask', amount, price, function(err, result) {
34 // if Mt. Gox is down or lagging
35 if(err || result.result === 'error')
36 log.error('unable to sell (', err, result, ')');
37
38 callback(err, result.data);
39 });
40}
41
42Trader.prototype.getTrades = function(since, callback, descending) {
43 if(since && !_.isNumber(since))
44 since = util.toMicro(since);
45
46 var args = _.toArray(arguments);
47 this.mtgox.fetchTrades(since, _.bind(function(err, trades) {
48 if (err || !trades)
49 return this.retry(this.getTrades, args);
50
51 trades = trades.data;
52 if (trades.length === 0)
53 return this.retry(this.getTrades, args);
54
55 if(descending)
56 callback(trades.reverse());
57 else
58 callback(trades);
59 }, this));
60}
61
62// if the exchange errors we try the same call again after
63// waiting 10 seconds
64Trader.prototype.retry = function(method, args) {
65 var wait = +moment.duration(10, 'seconds');
66 log.debug(this.name, 'returned an error, retrying..');
67
68 var self = this;
69
70 // make sure the callback (and any other fn)
71 // is bound to Trader
72 _.each(args, function(arg, i) {
73 if(_.isFunction(arg))
74 args[i] = _.bind(arg, self);
75 });
76
77 // run the failed method again with the same
78 // arguments after wait.
79 setTimeout(
80 function() { method.apply(self, args) },
81 wait
82 );
83}
84
85// calls callback with the following data structure:
86//
87// [
88// { name: 'BTC', amount: 10.12413123},
89// { name: 'USD', amount: 500.0241},
90// { name: 'EUR', amount: 0},
91// ]
92Trader.prototype.getPortfolio = function(callback) {
93 var args = _.toArray(arguments);
94 var calculate = function(err, result) {
95 if(err)
96 return this.retry(this.getPortfolio, args);
97
98 var assets = [];
99 _.each(result.data.Wallets, function(wallet, name) {
100 var amount = parseFloat(wallet.Balance.value);
101 assets.push({name: name, amount: amount});
102 });
103 callback(null, assets);
104 };
105
106 this.mtgox.info(_.bind(calculate, this));
107}
108
109Trader.prototype.getFee = function(callback) {
110 var args = _.toArray(arguments);
111 var calculate = function(err, result) {
112 if(err)
113 return this.retry(this.getFee, args);
114
115 // convert the %
116 var fee = result.data.Trade_Fee / 100;
117 callback(null, fee);
118 };
119
120 this.mtgox.info(_.bind(calculate, this));
121}
122
123Trader.prototype.checkOrder = function(order, callback) {
124 var args = _.toArray(arguments);
125 // we can't just request the order id
126 // @link https://bitbucket.org/nitrous/mtgox-api/#markdown-header-moneyorderresult
127 var check = function(err, result) {
128 if(err)
129 return this.retry(this.checkOrder, args);
130
131 var stillThere = _.find(result.data, function(o) { return o.oid === order });
132 callback(null, !stillThere);
133 };
134
135 this.mtgox.orders(_.bind(check, this));
136}
137
138Trader.prototype.cancelOrder = function(order) {
139 var cancel = function(err, result) {
140 if(err || result.result !== 'succes')
141 log.error('unable to cancel order', order, '(', err, result, ')');
142 };
143
144 this.mtgox.cancel(order, _.bind(cancel, this));
145}
146
147Trader.prototype.getTicker = function(callback) {
148 var args = _.toArray(arguments);
149 var set = function(err, result) {
150 if(err)
151 return this.retry(this.getTicker, args);
152
153 var ticker = {
154 bid: result.data.buy.value,
155 ask: result.data.sell.value
156 }
157 callback(err, ticker);
158 };
159
160 this.mtgox.ticker(_.bind(set, this));
161}
162
163module.exports = Trader;
\No newline at end of file