forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini.js
228 lines (184 loc) · 6.27 KB
/
gemini.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var Gemini = require('gemini-exchange-coffee-api/lib/gemini');
var util = require('../core/util.js');
var _ = require('lodash');
var moment = require('moment');
var log = require('../core/log');
var Trader = function(config) {
_.bindAll(this);
if(_.isObject(config)) {
this.key = config.key;
this.secret = config.secret;
}
this.name = 'Gemini';
this.balance;
this.price;
this.asset = config.asset;
this.currency = config.currency;
this.pair = this.asset + this.currency;
this.gemini = new Gemini(this.key, this.secret);
}
// 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.getPortfolio = function(callback) {
var args = _.toArray(arguments);
this.gemini.wallet_balances(function (err, data, body) {
if(err && err.message === '401') {
let e = 'Gemini replied with an unauthorized error. ';
e += 'Double check whether your API key is correct.';
util.die(e);
}
if(err || !data)
return this.retry(this.getPortfolio, args);
// We are only interested in funds in the "exchange" wallet
data = data.filter(c => c.type === 'exchange');
const asset = _.find(data, c => c.currency.toUpperCase() === this.asset);
const currency = _.find(data, c => c.currency.toUpperCase() === this.currency);
let assetAmount, currencyAmount;
if(_.isObject(asset) && _.isNumber(+asset.available) && !_.isNaN(+asset.available))
assetAmount = +asset.available;
else {
log.error(`Gemini did not provide ${this.asset} amount, assuming 0`);
assetAmount = 0;
}
if(_.isObject(currency) && _.isNumber(+currency.available) && !_.isNaN(+currency.available))
currencyAmount = +currency.available;
else {
log.error(`Gemini did not provide ${this.currency} amount, assuming 0`);
currencyAmount = 0;
}
const portfolio = [
{ name: this.asset, amount: assetAmount },
{ name: this.currency, amount: currencyAmount },
];
callback(err, portfolio);
}.bind(this));
}
Trader.prototype.getTicker = function(callback) {
var args = _.toArray(arguments);
// the function that will handle the API callback
var process = function(err, data, body) {
if (err)
return this.retry(this.getTicker(args));
// whenever we reach this point we have valid
// data, the callback is still the same since
// we are inside the same javascript scope.
callback(err, {bid: +data.bid, ask: +data.ask})
}.bind(this);
this.gemini.ticker(this.pair, process);
}
// This assumes that only limit orders are being placed, so fees are the
// "maker fee" of 0.1%. It does not take into account volume discounts.
Trader.prototype.getFee = function(callback) {
var makerFee = 0.25;
callback(false, makerFee / 100);
}
Trader.prototype.submit_order = function(type, amount, price, callback) {
var args = _.toArray(arguments);
amount = Math.floor(amount*100000000)/100000000;
this.gemini.new_order(
this.pair,
amount + '',
price + '',
this.name.toLowerCase(),
type,
'exchange limit',
function (err, data, body) {
if (err) {
log.error('unable to ' + type, err, body);
return this.retry(this.submit_order, args);
}
callback(err, data.order_id);
}.bind(this));
}
Trader.prototype.buy = function(amount, price, callback) {
this.submit_order('buy', amount, price, callback);
}
Trader.prototype.sell = function(amount, price, callback) {
this.submit_order('sell', amount, price, callback);
}
Trader.prototype.checkOrder = function(order_id, callback) {
var args = _.toArray(arguments);
this.gemini.order_status(order_id, function (err, data, body) {
if(err || !data)
return this.retry(this.checkOrder, arguments);
callback(err, !data.is_live);
}.bind(this));
}
Trader.prototype.getOrder = function(order, callback) {
var args = _.toArray(arguments);
var get = function(err, data) {
if(err || !data)
return this.retry(this.getOrder, arguments);
var price = parseFloat(data.avg_execution_price);
var amount = parseFloat(data.executed_amount);
var date = moment.unix(data.timestamp);
callback(undefined, {price, amount, date});
}.bind(this);
this.gemini.order_status(order, get);
}
Trader.prototype.cancelOrder = function(order_id, callback) {
var args = _.toArray(arguments);
this.gemini.cancel_order(order_id, function (err, data, body) {
if (err || !data) {
// gemini way of telling it was already cancelled..
if(err.message === 'Order could not be cancelled.')
return callback();
log.error('unable to cancel order', order_id, '(', err, data, '), retrying...');
return this.retry(this.cancelOrder, args);
}
return callback();
}.bind(this));
}
Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var path = this.pair;
if(since)
path += '?limit_trades=2000';
this.gemini.trades(path, function(err, data) {
if (err)
return this.retry(this.getTrades, args);
var trades = _.map(data, function(trade) {
return {
tid: trade.tid,
date: trade.timestamp,
price: +trade.price,
amount: +trade.amount
}
});
callback(null, descending ? trades : trades.reverse());
}.bind(this));
}
Trader.getCapabilities = function () {
return {
name: 'Gemini',
slug: 'gemini',
currencies: ['USD', 'BTC'],
assets: ['BTC', 'ETH'],
markets: [
{ pair: ['USD', 'BTC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['USD', 'ETC'], minimalOrder: { amount: 0.01, unit: 'asset' } },
{ pair: ['BTC', 'ETH'], minimalOrder: { amount: 0.01, unit: 'asset' } },
],
requires: ['key', 'secret'],
tid: 'tid',
tradable: true
};
}
module.exports = Trader;