forked from askmike/gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cexio.js
187 lines (153 loc) · 4.78 KB
/
cexio.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
var CEXio = require('cexio'),
moment = require('moment'),
async = require('async'),
_ = require('lodash'),
util = require('../core/util'),
log = require('../core/log');
var Trader = function(config) {
this.user = config.username;
this.key = config.key;
this.secret = config.secret;
this.currency = config.currency.toUpperCase();
this.asset = config.asset.toUpperCase();
this.pair = this.asset + '_' + this.currency;
this.name = 'cex.io';
this.cexio = new CEXio(
this.pair,
this.user,
this.key,
this.secret
);
_.bindAll(this);
}
Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var process = function(err, trades) {
if(err || !trades || trades.length === 0)
return this.retry(this.getTrades, args, err);
if(descending)
callback(null, trades);
else
callback(null, trades.reverse());
}
this.cexio.trades({}, _.bind(process, this));
}
Trader.prototype.buy = function(amount, price, callback) {
// Prevent "You incorrectly entered one of fields."
// because of more than 8 decimals.
amount *= 100000000;
amount = Math.floor(amount);
amount /= 100000000;
log.debug('BUY', amount, this.asset, '@', price, this.currency);
var set = function(err, data) {
if(err)
return log.error('unable to buy:', err);
if(data.error)
return log.error('unable to buy:', data.error);
log.debug('BUY order placed. Order ID', data.id);
callback(null, data.id);
};
this.cexio.place_order('buy', amount, price, _.bind(set, this));
}
Trader.prototype.sell = function(amount, price, callback) {
// Prevent "You incorrectly entered one of fields."
// because of more than 8 decimals.
amount *= 100000000;
amount = Math.floor(amount);
amount /= 100000000;
// test placing orders which will not be filled
//price *= 10; price = price.toFixed(8);
log.debug('SELL', amount, this.asset, '@', price, this.currency);
var set = function(err, data) {
if(err)
return log.error('unable to sell:', err);
if(data.error)
return log.error('unable to sell:', data.error);
log.debug('SELL order placed. Order ID', data.id);
callback(null, data.id);
};
this.cexio.place_order('sell', amount, price, _.bind(set, this));
}
Trader.prototype.retry = function(method, args, err) {
var wait = +moment.duration(10, 'seconds');
log.debug(this.name, 'returned an error, retrying..', err, 'waiting for', wait, 'ms');
if (!_.isFunction(method)) {
log.error(this.name, 'failed to retry, no method supplied.');
return;
}
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);
var calculate = function(err, data) {
if(err)
return this.retry(this.getPortfolio, args, err);
currency = parseFloat(data[this.currency].available)
if(parseFloat(data[this.currency].orders)){
currency -= parseFloat(data[this.currency].orders)
}
assets = parseFloat(data[this.asset].available);
if( parseFloat(data[this.asset].orders)){
assets -= parseFloat(data[this.asset].orders);
}
var portfolio = [];
portfolio.push({name: this.currency, amount: currency});
portfolio.push({name: this.asset, amount: assets});
callback(err, portfolio);
}
this.cexio.balance(calculate.bind(this));
}
Trader.prototype.getTicker = function(callback) {
var set = function(err, data) {
var ticker = {
ask: data.ask,
bid: data.bid
};
callback(err, ticker);
}
this.cexio.ticker(_.bind(set, this));
}
Trader.prototype.getFee = function(callback) {
// cexio does currently don't take a fee on trades
// TODO: isn't there an API call for this?
callback(false, 0.002);
}
Trader.prototype.checkOrder = function(order, callback) {
var check = function(err, result) {
if(err)
return callback(false, true);
if(result.error)
return callback(false, true);
var exists = false;
_.each(result, function(entry) {
if(entry.id === order) {
exists = true;
return;
}
});
callback(err, !exists);
};
this.cexio.open_orders(_.bind(check, this));
}
Trader.prototype.cancelOrder = function(order) {
var check= function(err, result) {
if(err)
log.error('cancel order failed:', err);
if(typeof(result) !== 'undefined' && result.error)
log.error('cancel order failed:', result.error);
}
this.cexio.cancel_order(order, check);
}
module.exports = Trader;