Skip to content

Instantly share code, notes, and snippets.

@memeyou
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save memeyou/9068360 to your computer and use it in GitHub Desktop.

Select an option

Save memeyou/9068360 to your computer and use it in GitHub Desktop.
var mcxnow = require('mcxnow');
var moment = require('moment');
var util = require('../core/util');
var _ = require('lodash');
var log = require('../core/log')
var Trader = function(config) {
this.key = config.key;
this.secret = config.secret;
this.asset = config.asset;
this.currency = config.currency;
this.name = 'mcxnow';
_.bindAll(this);
this.mcxPrivate = new mcxnow(this.key, this.secret);
}
Trader.prototype.round = function(amount) {
// Prevent "You incorrectly entered one of fields."
// because of more than 8 decimals.
amount *= 100000000;
amount = Math.floor(amount);
amount /= 100000000;
return amount;
}
Trader.prototype.buy = function(amount, price, callback) {
amount = this.round(amount);
var args = _.toArray(arguments);
var set = function(err, data) {
if(err) {
log.error('unable to buy:', err);
return this.retry(this.buy, args);
}
callback(null, 1);
};
setTimeout(_.bind(function() {
this.mcxPrivate.trade(this.asset, amount, price, '1', _.bind(set, this));
}, this), 1000);
}
Trader.prototype.sell = function(amount, price, callback) {
amount = this.round(amount);
var args = _.toArray(arguments);
var set = function(err, data) {
if(err) {
log.error('unable to sell:', err);
return this.retry(this.sell, args);
}
callback(null, 1);
};
setTimeout(_.bind(function() {
this.mcxPrivate.trade(this.asset, amount, price, '0', _.bind(set, this));
}, this), 1000);
}
// if the exchange errors we try the same call again after
// waiting 10 seconds
Trader.prototype.retry = function(method, args) {
var wait = +moment.duration(10, 'seconds');
log.debug(this.name, 'returned an error, retrying...');
var self = this;
// make sure the callback (and any other fn)
// is bound to Trader
_.each(args, function(arg, i) {
if(_.isFunction(arg))
args[i] = _.bind(arg, self);
});
// run the failed method again with the same
// arguments after wait
setTimeout(
function() { method.apply(self, args) },
wait
);
}
Trader.prototype.getPortfolio = function(callback) {
var args = _.toArray(arguments);
var calculate = function(data) {
if(!data || !data.base_bal) {
return this.retry(this.getPortfolio, args);
}
var portfolio = [];
portfolio.push({name: this.currency, amount: data.base_bal[0]});
portfolio.push({name: this.asset, amount: data.cur_bal[0]});
callback(null, portfolio);
}
setTimeout(_.bind(function() {
this.mcxPrivate.info(this.asset, _.bind(calculate, this))
}, this), 1000);
}
Trader.prototype.getTicker = function(callback) {
var args = _.toArray(arguments);
var set = function(err, data) {
if (!data) {
return this.retry(this.getTicker, args);
}
callback(err, data);
}
this.mcxPrivate.getTicker(this.asset, _.bind(set, this));
}
Trader.prototype.getFee = function(callback) {
callback(false, 0.0025);
}
Trader.prototype.checkOrder = function(order, callback) {
var check = function(data) {
try {
if(data.orders[0].o[0].id) return callback(null, false);
} catch(e) {
return callback(null, true);
}
return callback(null, true);
};
setTimeout(_.bind(function() {
this.mcxPrivate.info(this.asset, _.bind(check, this));
}, this), 1000);
}
Trader.prototype.cancelOrder = function(order, callback) {
var args = _.toArray(arguments);
var check = function(err, data) {
if(err) {
log.error('unable to cancel order', order, '(', err, data, ')');
return this.retry(this.cancelOrder, args);
}
}
this.mcxPrivate.cancelOrder(this.asset, _.bind(check, this));
}
Trader.prototype.getTrades = function(since, callback, descending) {
var args = _.toArray(arguments);
var process = function(err, trades) {
if (err)
return this.retry(this.getTrades, args);
callback(err, trades);
}
this.mcxPrivate.getHistory(this.asset, _.bind(process, this));
}
module.exports = Trader;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment