Skip to content
This repository has been archived by the owner on Feb 16, 2020. It is now read-only.

Commit

Permalink
force decimal notation & cast to string when rounding, fix #2287
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 8, 2018
1 parent 228f679 commit 5215772
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions exchange/wrappers/binance.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,39 @@ Trader.prototype.round = function(amount, tickSize) {
amount *= precision;
amount = Math.floor(amount);
amount /= precision;

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
amount = this.scientificToDecimal(amount);

return amount;
};

// https://gist.github.com/jiggzson/b5f489af9ad931e3d186
Trader.prototype.scientificToDecimal = function(num) {
if(/\d+\.?\d*e[\+\-]*\d+/i.test(num)) {
const zero = '0';
const parts = String(num).toLowerCase().split('e'); // split into coeff and exponent
const e = parts.pop(); // store the exponential part
const l = Math.abs(e); // get the number of zeros
const sign = e/l;
const coeff_array = parts[0].split('.');
if(sign === -1) {
num = zero + '.' + new Array(l).join(zero) + coeff_array.join('');
} else {
const dec = coeff_array[1];
if(dec) {
l = l - dec.length;
}
num = coeff_array.join('') + new Array(l+1).join(zero);
}
} else {
// make sure we always cast to string
num = num + '';
}

return num;
}

Trader.prototype.roundAmount = function(amount) {
return this.round(amount, this.market.minimalOrder.amount);
}
Expand Down Expand Up @@ -354,8 +384,9 @@ Trader.prototype.cancelOrder = function(order, callback) {
// callback for cancelOrder should be true if the order was already filled, otherwise false
const cancel = (err, data) => {
if (err) {
if(data && data.msg === 'UNKNOWN_ORDER') { // this seems to be the response we get when an order was filled
return callback(undefined, true); // tell the thing the order was already filled
// when the order was filled
if(data && data.msg === 'UNKNOWN_ORDER') {
return callback(undefined, true);
}
return callback(err);
}
Expand All @@ -371,10 +402,6 @@ Trader.prototype.cancelOrder = function(order, callback) {
retry(retryForever, fetcher, cancel);
};

Trader.prototype.initMarkets = function(callback) {

}

Trader.getCapabilities = function() {
return {
name: 'Binance',
Expand Down

0 comments on commit 5215772

Please sign in to comment.