Skip to content

Commit

Permalink
update GDAX dep, minor fixes, implment getOrder, fix askmike#628
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 4, 2017
1 parent fe647cf commit 1acb4d6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
58 changes: 46 additions & 12 deletions exchanges/gdax.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var batchSize = 100;
var Trader = function(config) {
_.bindAll(this);

this.post_only = false; // orders can be rejected because of this
this.post_only = false;
this.use_sandbox = false;
this.name = 'GDAX';
this.import = false;
Expand All @@ -23,7 +23,6 @@ var Trader = function(config) {
this.passphrase = config.passphrase;

this.pair = [config.asset, config.currency].join('-').toUpperCase();
this.use_sandbox = config.sandbox ? config.sandbox : false;
this.post_only = config.post_only ? config.post_only : false;
}

Expand Down Expand Up @@ -56,9 +55,14 @@ Trader.prototype.retry = function(method, args) {

Trader.prototype.getPortfolio = function(callback) {
var result = function(err, response, data) {
if (data.hasOwnProperty('message')) {

if(_.has(data, 'message')) {
if(data.message === 'Invalid API Key' || data.message === 'Invalid Passphrase')
util.die('GDAX said: ' + data.message);

return callback(data.message, []);
}

var portfolio = data.map(function (account) {
return {
name: account.currency.toUpperCase(),
Expand Down Expand Up @@ -88,16 +92,19 @@ Trader.prototype.getFee = function(callback) {
}

Trader.prototype.buy = function(amount, price, callback) {
var args = _.toArray(arguments);
var buyParams = {
'price': price,
'size': amount,
'product_id': this.pair,
'post_only': this.post_only
};
var result = function(err, response, data) {
if (data.hasOwnProperty('message')) {
return callback(data.message, null);
if (err || data.hasOwnProperty('message')) {
log.error('Error buying at GDAX:', data.message, error);
return this.retry(this.buy, args);
}

callback(err, data.id);
};

Expand All @@ -112,8 +119,9 @@ Trader.prototype.sell = function(amount, price, callback) {
'post_only': this.post_only
};
var result = function(err, response, data) {
if (data.hasOwnProperty('message')) {
return callback(data.message, null);
if (err || data.hasOwnProperty('message')) {
log.error('Error selling at GDAX:', data.message, error);
return this.retry(this.sell, args);
}
callback(err, data.id);
};
Expand All @@ -123,13 +131,15 @@ Trader.prototype.sell = function(amount, price, callback) {

Trader.prototype.checkOrder = function(order, callback) {

var args = _.toArray(arguments);
if (order == null) {
return callback('EMPTY ORDER_ID', false);
}

var result = function(err, response, data) {
if (data.hasOwnProperty('message')) {
return callback(data.message, null);
if(err || (data && data.message)) {
log.error('GDAX ERROR:', err, data.message);
return this.retry(this.checkOrder, args);
}

var status = data.status;
Expand All @@ -141,18 +151,42 @@ Trader.prototype.checkOrder = function(order, callback) {
return callback(err, false);
}
callback(err, false);
};
}.bind(this);

this.gdax.getOrder(order, result);
}

Trader.prototype.getOrder = function(order, callback) {

var args = _.toArray(arguments);
if (order == null) {
return callback('EMPTY ORDER_ID', false);
}

var result = function(err, response, data) {
if(err || (data && data.message)) {
log.error('GDAX ERROR:', err, data.message);
return this.retry(this.checkOrder, args);
}

var price = parseFloat( data.price );
var amount = parseFloat( data.filled_size );
var date = moment( data.done_at );

callback(undefined, {price, amount, date});
}.bind(this);

this.gdax.getOrder(order, result);
}

Trader.prototype.cancelOrder = function(order) {
Trader.prototype.cancelOrder = function(order, callback) {
if (order == null) {
return;
}

var result = function(err, response, data) {
//
// todo..
callback();
};

this.gdax.cancelOrder(order, result);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"cexio": "0.0.x",
"co-fs": "^1.2.0",
"commander": "^2.9.0",
"gdax": "0.3.0",
"gdax": "^0.4.2",
"gekko": "0.0.9",
"humanize-duration": "^3.10.0",
"koa": "^1.2.0",
Expand Down
4 changes: 4 additions & 0 deletions plugins/trader/portfolioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ Manager.prototype.buy = function(amount, price) {
);
}

amount = minimum;

log.info(
'Attempting to BUY',
amount,
Expand Down Expand Up @@ -238,6 +240,8 @@ Manager.prototype.sell = function(amount, price) {
);
}

amount = minimum;

log.info(
'Attempting to SELL',
amount,
Expand Down

0 comments on commit 1acb4d6

Please sign in to comment.