Skip to content

Commit

Permalink
prepare bitstamp trader for the UI (implement getOrder)
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 24, 2017
1 parent ec4c4f2 commit 5d59d3c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
74 changes: 63 additions & 11 deletions exchanges/bitstamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ var Trader = function(config) {
this.key = config.key;
this.secret = config.secret;
this.clientID = config.username;
this.market = (config.asset + config.currency).toLowerCase();
this.asset = config.asset.toLowerCase();
this.currency = config.currency.toLowerCase();
this.market = this.asset + this.currency;
}
this.name = 'Bitstamp';

Expand Down Expand Up @@ -40,10 +42,13 @@ Trader.prototype.retry = function(method, args) {
}

Trader.prototype.getPortfolio = function(callback) {
var args = _.toArray(arguments);
var set = function(err, data) {

if(!_.isEmpty(data.error))
return callback('BITSTAMP API ERROR: ' + data.error);
if(!_.isEmpty(data.error)) {
log.error('BITSTAMP API ERROR: ' + data.error);
return this.retry(this.getPortfolio, args);
}

var portfolio = [];
_.each(data, function(amount, asset) {
Expand Down Expand Up @@ -74,6 +79,7 @@ Trader.prototype.getFee = function(callback) {
}

Trader.prototype.buy = function(amount, price, callback) {
var args = _.toArray(arguments);
var set = function(err, result) {
if(err || result.status === "error") {
log.error('unable to buy:', err, result.reason, 'retrying...');
Expand Down Expand Up @@ -110,6 +116,12 @@ Trader.prototype.sell = function(amount, price, callback) {
callback(null, result.id);
}.bind(this);

// prevent:
// 'Ensure that there are no more than 8 decimal places.'
amount *= 100000000;
amount = Math.floor(amount);
amount /= 100000000;

// prevent:
// 'Ensure that there are no more than 2 decimal places.'
price *= 100;
Expand All @@ -119,6 +131,40 @@ Trader.prototype.sell = function(amount, price, callback) {
this.bitstamp.sell(this.market, amount, price, undefined, set);
}


Trader.prototype.getOrder = function(id, callback) {
var args = _.toArray(arguments);
var get = function(err, data) {
if(!err && _.isEmpty(data) && _.isEmpty(data.result))
err = 'no data';

else if(!err && !_.isEmpty(data.error))
err = data.error;

if(err) {
log.error('Unable to get order', order, JSON.stringify(err));
return this.retry(this.getOrder, args);
}

var order = _.find(data, o => o.order_id === +id);

if(!order) {
var price = 0;
var amount = 0;
var date = moment(0);
return callback(err, {price, amount, date});
}

var price = parseFloat( order[this.market] );
var amount = Math.abs(parseFloat( order[this.asset] ));
var date = moment( order.datetime );

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

this.bitstamp.user_transactions(this.market, {}, get);
}

Trader.prototype.checkOrder = function(order, callback) {
var check = function(err, result) {
var stillThere = _.find(result, function(o) { return o.id === order });
Expand All @@ -129,9 +175,14 @@ Trader.prototype.checkOrder = function(order, callback) {
}

Trader.prototype.cancelOrder = function(order, callback) {
var args = _.toArray(arguments);
var cancel = function(err, result) {
if(err || !result)
if(err || !result) {
log.error('unable to cancel order', order, '(', err, result, ')');
return this.retry(this.cancelOrder, args);
}

callback();
}.bind(this);

this.bitstamp.cancel_order(order, cancel);
Expand All @@ -155,10 +206,11 @@ Trader.prototype.getTrades = function(since, callback, descending) {
callback(null, result.reverse());
}.bind(this);

if(since)
this.bitstamp.transactions(this.market, {time: 'day'}, process);
else
this.bitstamp.transactions(this.market, process);
// NOTE: temporary disabled, see https://github.com/askmike/gekko/issues/794
// if(since)
// this.bitstamp.transactions(this.market, {time: 'day'}, process);
// else
this.bitstamp.transactions(this.market, process);
}

Trader.getCapabilities = function () {
Expand All @@ -170,9 +222,9 @@ Trader.getCapabilities = function () {
maxTradesAge: 60,
maxHistoryFetch: null,
markets: [
{ pair: ['USD', 'BTC'], minimalOrder: { amount: 1, unit: 'currency' } },
{ pair: ['EUR', 'BTC'], minimalOrder: { amount: 1, unit: 'currency' } },
{ pair: ['USD', 'EUR'], minimalOrder: { amount: 1, unit: 'currency' } }
{ pair: ['USD', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' } },
{ pair: ['EUR', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' } },
{ pair: ['USD', 'EUR'], minimalOrder: { amount: 5, unit: 'currency' } }
],
requires: ['key', 'secret', 'username'],
fetchTimespan: 60,
Expand Down
4 changes: 2 additions & 2 deletions plugins/trader/portfolioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ Manager.prototype.buy = function(amount, price) {
);
}

amount = minimum;
amount = minimum * 1.1;

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

amount = minimum;
amount = minimum * 1.1;

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

0 comments on commit 5d59d3c

Please sign in to comment.