From 23b72c7e586b93f1909d1baa9e6e5a592746aa09 Mon Sep 17 00:00:00 2001 From: Kevin Mershon Date: Tue, 6 Mar 2018 15:25:35 -0800 Subject: [PATCH] Fix precision for BTC pairs --- exchanges/bitstamp.js | 62 ++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/exchanges/bitstamp.js b/exchanges/bitstamp.js index 1bce41d3d..6c5717306 100644 --- a/exchanges/bitstamp.js +++ b/exchanges/bitstamp.js @@ -87,6 +87,9 @@ Trader.prototype.getFee = function(callback) { } Trader.prototype.buy = function(amount, price, callback) { + var findMarket = function(pair) { + return pair.pair[0].toLowerCase() === this.currency && pair.pair[1].toLowerCase() === this.asset + } var args = _.toArray(arguments); var set = function(err, result) { if(err || result.status === "error") { @@ -98,22 +101,19 @@ Trader.prototype.buy = function(amount, price, callback) { }.bind(this); //Decrease amount by 1% to avoid trying to buy more than balance allows. - amount -= amount / 100; - - amount *= 100000000; - amount = Math.floor(amount); - amount /= 100000000; + amount = Number(amount * 0.99).toFixed(8); - // prevent: - // 'Ensure that there are no more than 2 decimal places.' - price *= 100; - price = Math.floor(price); - price /= 100; + // Use proper precision by currency pair + var pair = _.find(Trader.getCapabilities().markets, _.bind(findMarket, this)); + price = Number(Number.parseFloat(price).toFixed(pair.precision)); this.bitstamp.buy(this.market, amount, price, undefined, set); } Trader.prototype.sell = function(amount, price, callback) { + var findMarket = function(pair) { + return pair.pair[0].toLowerCase() === this.currency && pair.pair[1].toLowerCase() === this.asset + } var args = _.toArray(arguments); var set = function(err, result) { if(err || result.status === "error") { @@ -126,15 +126,11 @@ Trader.prototype.sell = function(amount, price, callback) { // prevent: // 'Ensure that there are no more than 8 decimal places.' - amount *= 100000000; - amount = Math.floor(amount); - amount /= 100000000; + amount = Number(Number.parseFloat(amount)).toFixed(8); - // prevent: - // 'Ensure that there are no more than 2 decimal places.' - price *= 100; - price = Math.ceil(price); - price /= 100; + // Use proper precision by currency pair + var pair = _.find(Trader.getCapabilities().markets, _.bind(findMarket, this)); + price = Number(Number.parseFloat(price).toFixed(pair.precision)); this.bitstamp.sell(this.market, amount, price, undefined, set); } @@ -234,26 +230,26 @@ Trader.getCapabilities = function () { maxTradesAge: 60, maxHistoryFetch: null, markets: [ - { pair: ['USD', 'EUR'], minimalOrder: { amount: 5, unit: 'currency' } }, + { pair: ['USD', 'EUR'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, - { pair: ['USD', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['EUR', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' } }, + { pair: ['USD', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['EUR', 'BTC'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, - { pair: ['USD', 'BCH'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['EUR', 'BCH'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['BTC', 'BCH'], minimalOrder: { amount: 0.001, unit: 'currency' } }, + { pair: ['USD', 'BCH'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['EUR', 'BCH'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['BTC', 'BCH'], minimalOrder: { amount: 0.001, unit: 'currency'}, precision: 8 }, - { pair: ['USD', 'XRP'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['EUR', 'XRP'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['BTC', 'XRP'], minimalOrder: { amount: 0.001, unit: 'currency' } }, + { pair: ['USD', 'XRP'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 5 }, + { pair: ['EUR', 'XRP'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 5 }, + { pair: ['BTC', 'XRP'], minimalOrder: { amount: 0.001, unit: 'currency'}, precision: 8 }, - { pair: ['USD', 'LTC'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['EUR', 'LTC'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['BTC', 'LTC'], minimalOrder: { amount: 0.001, unit: 'currency' } }, + { pair: ['USD', 'LTC'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['EUR', 'LTC'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['BTC', 'LTC'], minimalOrder: { amount: 0.001, unit: 'currency'}, precision: 8 }, - { pair: ['USD', 'ETH'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['EUR', 'ETH'], minimalOrder: { amount: 5, unit: 'currency' } }, - { pair: ['BTC', 'ETH'], minimalOrder: { amount: 0.001, unit: 'currency' } }, + { pair: ['USD', 'ETH'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['EUR', 'ETH'], minimalOrder: { amount: 5, unit: 'currency' }, precision: 2 }, + { pair: ['BTC', 'ETH'], minimalOrder: { amount: 0.001, unit: 'currency'}, precision: 8 }, ], requires: ['key', 'secret', 'username'], fetchTimespan: 60,