1 | var MtGoxClient = require("mtgox-apiv2");
|
2 | var util = require('../util.js');
|
3 | var _ = require('underscore');
|
4 | var log = require('../log.js');
|
5 | var moment = require('moment');
|
6 |
|
7 | var 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 |
|
22 | Trader.prototype.buy = function(amount, price, callback) {
|
23 | this.mtgox.add('bid', amount, price, function(err, result) {
|
24 |
|
25 | if(err || result.result === 'error')
|
26 | log.error('unable to buy (', err, result, ')');
|
27 |
|
28 | callback(err, result.data);
|
29 | });
|
30 | }
|
31 |
|
32 | Trader.prototype.sell = function(amount, price, callback) {
|
33 | this.mtgox.add('ask', amount, price, function(err, result) {
|
34 |
|
35 | if(err || result.result === 'error')
|
36 | log.error('unable to sell (', err, result, ')');
|
37 |
|
38 | callback(err, result.data);
|
39 | });
|
40 | }
|
41 |
|
42 | Trader.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 |
|
63 |
|
64 | Trader.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 |
|
71 |
|
72 | _.each(args, function(arg, i) {
|
73 | if(_.isFunction(arg))
|
74 | args[i] = _.bind(arg, self);
|
75 | });
|
76 |
|
77 |
|
78 |
|
79 | setTimeout(
|
80 | function() { method.apply(self, args) },
|
81 | wait
|
82 | );
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | Trader.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 |
|
109 | Trader.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 |
|
116 | var fee = result.data.Trade_Fee / 100;
|
117 | callback(null, fee);
|
118 | };
|
119 |
|
120 | this.mtgox.info(_.bind(calculate, this));
|
121 | }
|
122 |
|
123 | Trader.prototype.checkOrder = function(order, callback) {
|
124 | var args = _.toArray(arguments);
|
125 |
|
126 |
|
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 |
|
138 | Trader.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 |
|
147 | Trader.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 |
|
163 | module.exports = Trader; |
\ | No newline at end of file |