Add everything back for trading
This commit is contained in:
parent
75982f219d
commit
97deac7f20
1 changed files with 44 additions and 0 deletions
|
|
@ -79,6 +79,16 @@ Trader.prototype._consolidateTrades = function () {
|
||||||
return tradeRec;
|
return tradeRec;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Trader.prototype._purchase = function (trade) {
|
||||||
|
var self = this;
|
||||||
|
self.rate(trade.currency, function (err, rate) {
|
||||||
|
self.tradeExchange.purchase(trade.satoshis, rate, function (err) {
|
||||||
|
// XXX: don't ignore purchase errors
|
||||||
|
self.pollBalance();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
Trader.prototype.configure = function (config) {
|
Trader.prototype.configure = function (config) {
|
||||||
if (config.exchanges.settings.lowBalanceMargin < 1) {
|
if (config.exchanges.settings.lowBalanceMargin < 1) {
|
||||||
throw new Error('`settings.lowBalanceMargin` has to be >= 1');
|
throw new Error('`settings.lowBalanceMargin` has to be >= 1');
|
||||||
|
|
@ -201,12 +211,46 @@ Trader.prototype.trade = function (fiat, satoshis, currency, callback) {
|
||||||
callback(null);
|
callback(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Trader.prototype.executeTrades = function () {
|
||||||
|
if (!this.tradeExchange) return;
|
||||||
|
|
||||||
|
this.logger.info('checking for trades');
|
||||||
|
|
||||||
|
var trade = this._consolidateTrades();
|
||||||
|
this.logger.info('consolidated: ', JSON.stringify(trade));
|
||||||
|
|
||||||
|
if (trade.fiat === 0) {
|
||||||
|
this.logger.info('rejecting 0 trade');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (trade.fiat < this.config.exchanges.settings.minimumTradeFiat) {
|
||||||
|
// throw it back in the water
|
||||||
|
this.logger.info('reject fiat too small');
|
||||||
|
this._tradeQueue.unshift(trade);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.logger.info('making a trade: %d', trade.satoshis / Math.pow(10, 8));
|
||||||
|
this._purchase(trade);
|
||||||
|
};
|
||||||
|
|
||||||
Trader.prototype.startPolling = function () {
|
Trader.prototype.startPolling = function () {
|
||||||
this.pollBalance();
|
this.pollBalance();
|
||||||
this.pollRate();
|
this.pollRate();
|
||||||
|
this.executeTrades();
|
||||||
|
|
||||||
this.balanceInterval = setInterval(this.pollBalance.bind(this), 60 * 1000);
|
this.balanceInterval = setInterval(this.pollBalance.bind(this), 60 * 1000);
|
||||||
this.rateInterval = setInterval(this.pollRate.bind(this), 60 * 1000);
|
this.rateInterval = setInterval(this.pollRate.bind(this), 60 * 1000);
|
||||||
|
|
||||||
|
// Always start trading, even if we don't have a trade exchange configured,
|
||||||
|
// since configuration can always change in `Trader#configure`.
|
||||||
|
// `Trader#executeTrades` returns early if we don't have a trade exchange
|
||||||
|
// configured at the moment.
|
||||||
|
this.tradeInterval = setInterval(
|
||||||
|
this.executeTrades.bind(this),
|
||||||
|
this.config.exchanges.settings.tradeInterval
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
Trader.prototype.stopPolling = function () {
|
Trader.prototype.stopPolling = function () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue