From 5d59d3c4bd6134ad2e56e39627734bff510c3e8b Mon Sep 17 00:00:00 2001 From: Mike van Rossum Date: Tue, 27 Jun 2017 18:56:31 +0100 Subject: [PATCH] prepare bitstamp trader for the UI (implement getOrder) --- exchanges/bitstamp.js | 74 +++++++++++++++++++++++++----- plugins/trader/portfolioManager.js | 4 +- 2 files changed, 65 insertions(+), 13 deletions(-) diff --git a/exchanges/bitstamp.js b/exchanges/bitstamp.js index 80d8570b2..d0f84f0e2 100644 --- a/exchanges/bitstamp.js +++ b/exchanges/bitstamp.js @@ -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'; @@ -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) { @@ -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...'); @@ -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; @@ -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 }); @@ -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); @@ -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 () { @@ -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, diff --git a/plugins/trader/portfolioManager.js b/plugins/trader/portfolioManager.js index 6d98e3121..41f6d3b12 100644 --- a/plugins/trader/portfolioManager.js +++ b/plugins/trader/portfolioManager.js @@ -207,7 +207,7 @@ Manager.prototype.buy = function(amount, price) { ); } - amount = minimum; + amount = minimum * 1.1; log.info( 'Attempting to BUY', @@ -240,7 +240,7 @@ Manager.prototype.sell = function(amount, price) { ); } - amount = minimum; + amount = minimum * 1.1; log.info( 'Attempting to SELL',