Skip to content

Commit

Permalink
Update btc-markets.js
Browse files Browse the repository at this point in the history
  • Loading branch information
danKHUU authored and askmike committed Oct 12, 2017
1 parent 867c0b2 commit 50d71ba
Showing 1 changed file with 94 additions and 41 deletions.
135 changes: 94 additions & 41 deletions exchanges/btc-markets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ var Trader = function(config) {
}
this.name = 'BTC Markets';
this.priceDivider = 100000000; // one hundred million

this.btcmakets = new BTCMarkets(this.key, this.secret);
}

Expand Down Expand Up @@ -44,8 +43,8 @@ Trader.prototype.retry = function(method, args) {
Trader.prototype.getPortfolio = function(callback) {
var set = function(err, data) {

if(!_.isEmpty(data.error))
return callback('BTC-MARKET API ERROR: ' + data.error);
if(!_.isEmpty(data.errorMessage))
return callback('BTC-MARKET API ERROR: ' + data.errorMessage);

var portfolio = _.map(data, function(balance) {
return {
Expand Down Expand Up @@ -75,24 +74,37 @@ Trader.prototype.getTicker = function(callback) {
}

Trader.prototype.getFee = function(callback) {
// TODO, not 100% correct.
// However there is no API call to retrieve real fee
callback(false, 0.00085)
}
var args = _.toArray(arguments);
var set = function(err, data) {

Trader.prototype.buy = function(amount, price, callback) {
var invFee = 0.9915;
price *= this.priceDivider;
amount = Math.floor(amount * this.priceDivider * invFee);
var id = Math.random() + '';
if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err){
log.error('unable to retrieve fee', err, ' retrying...');
return this.retry(this.getFee, args);
}
data.tradingFeeRate /= this.priceDivider;
callback(false, data.tradingFeeRate);
}.bind(this);
this.btcmakets.getTradingFee(this.asset, this.currency, set);
};

var set = function(err, result) {
if(err || result.error)
return log.error('unable to buy:', err, result);
Trader.prototype.buy = function(amount, price, callback) {

callback(null, id);
price *= this.priceDivider;
amount = Math.floor(amount * this.priceDivider);
var id = Math.random() + '';
var set = function(err, data) {
if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err)
return log.error('unable to buy', err);
callback(null, data.id);
}.bind(this);

this.btcmakets.createOrder(
this.asset,
this.currency,
Expand All @@ -108,15 +120,16 @@ Trader.prototype.buy = function(amount, price, callback) {
Trader.prototype.sell = function(amount, price, callback) {
price *= this.priceDivider;
amount = Math.floor(amount * this.priceDivider);
var id = Math.random() + '';

var set = function(err, result) {
if(err || result.error)
return log.error('unable to buy:', err, result);

callback(null, id);
var id = Math.random() + ''
var set = function(err, data) {
if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err)
return log.error('unable to sell', err)
callback(null, data.id);
}.bind(this);

this.btcmakets.createOrder(
this.asset,
this.currency,
Expand All @@ -130,22 +143,56 @@ Trader.prototype.sell = function(amount, price, callback) {
}

Trader.prototype.checkOrder = function(order, callback) {
var check = function(err, result) {
callback(err, result && result.success);
}.bind(this);

var check = function(err, data) {
if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err)
return log.error('unable to check order: ', order, '(', err ,'), retrying...');
var placed = data.orders[0].status === 'placed';
callback(err, placed);
};
this.btcmakets.getOpenOrders(this.asset, this.currency, 10, null, check);
}
};

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

if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err){
return log.error('unable to get order detail: ', order, '(', err ,'), retrying...');
this.retry(this.getOrder, args);
}
var price = parseFloat(data.orders[0].price);
var amount = parseFloat(data.orders[0].volumn);
var date = moment.unix(data.orders[0].creationDate);
callback(undefined, {price, amount, date});
}.bind(this);

this.btcmakets.cancelOrder([order], cancel);
this.btcmakets.getOrderDetail([order], callback);
}

Trader.prototype.cancelOrder = function(order, callback) {
var args = _.toArray(arguments);
var get = function(err, data) {

if(!err && _.isEmpty(data))
err = 'no data';
else if(!err && !_.isEmpty(data.errorMessage))
err = data.errorMessage;
if(err){
return log.error('unable to cancel order: ',order, '(', err, '), retrying...');
this.retry(this.cancelOrder, args);
}
callback();
};
this.btcmakets.cancelOrders([order], callback);
};

Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var process = function(err, result) {
Expand All @@ -165,20 +212,26 @@ Trader.getCapabilities = function () {
slug: 'btc-markets',
currencies: ['AUD', 'BTC'],
assets: [
'BTC', 'LTC', 'ETH', 'ETC'
'BTC', 'LTC', 'ETH', 'ETC', 'BCH', 'XRP'
],
markets: [
{ pair: ['AUD', 'BTC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'LTC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'ETH'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'LTC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'ETC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'BCH'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['AUD', 'XRP'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['BTC', 'LTC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['BTC', 'ETH'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['BTC', 'ETC'], minimalOrder: { amount: 0.001, unit: 'asset' } }
{ pair: ['BTC', 'ETC'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['BTC', 'BCH'], minimalOrder: { amount: 0.001, unit: 'asset' } },
{ pair: ['BTC', 'XRP'], minimalOrder: { amount: 0.001, unit: 'asset' } }
],
requires: ['key', 'secret'],
providesHistory: false,
tid: 'tid'
tid: 'tid',
providesHistory: 'scan',
providesFullHistory: false,
tradable: true
};
}

Expand Down

0 comments on commit 50d71ba

Please sign in to comment.