Skip to content

Commit

Permalink
properly relay portfolio from trader => performanceAnalyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
askmike committed Jul 24, 2017
1 parent c80c9a0 commit 93d9f03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
37 changes: 22 additions & 15 deletions plugins/trader/portfolioManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var Manager = function(conf) {
util.makeEventEmitter(Manager);

Manager.prototype.init = function(callback) {
log.debug('getting balance & fee from', this.exchange.name);
log.debug('getting ticker, balance & fee from', this.exchange.name);
var prepare = function() {
this.starting = false;

Expand All @@ -67,6 +67,7 @@ Manager.prototype.init = function(callback) {
};

async.series([
this.setTicker,
this.setPortfolio,
this.setFee
], _.bind(prepare, this));
Expand All @@ -91,7 +92,7 @@ Manager.prototype.setPortfolio = function(callback) {
});

if(_.isEmpty(this.portfolio))
this.emit('portfolioUpdate', _.clone(portfolio));
this.emit('portfolioUpdate', this.convertPortfolio(portfolio));

this.portfolio = portfolio;

Expand Down Expand Up @@ -139,10 +140,10 @@ Manager.prototype.getBalance = function(fund) {

// This function makes sure the limit order gets submitted
// to the exchange and initiates order registers watchers.
Manager.prototype.trade = function(what) {
Manager.prototype.trade = function(what, retry) {
// if we are still busy executing the last trade
// cancel that one (and ignore results = assume not filled)
if(_.size(this.orders))
if(!retry && _.size(this.orders))
return this.cancelLastOrder(() => this.trade(what));

this.action = what;
Expand Down Expand Up @@ -261,7 +262,7 @@ Manager.prototype.noteOrder = function(err, order) {
};


Manager.prototype.cancelLastOrder = function(done) {} {
Manager.prototype.cancelLastOrder = function(done) {
this.exchange.cancelOrder(_.last(this.orders), () => {
this.orders = [];
done();
Expand All @@ -285,12 +286,24 @@ Manager.prototype.checkOrder = function() {
}

var handleCancelResult = function() {
this.trade(this.action);
this.trade(this.action, true);
}

this.exchange.checkOrder(_.last(this.orders), _.bind(handleCheckResult, this));
}

// convert into the portfolio expected by the performanceAnalyzer
Manager.prototype.convertPortfolio = function(portfolio) {
var asset = _.find(portfolio, a => a.name === this.asset).amount;
var currency = _.find(portfolio, a => a.name === this.currency).amount;

return {
currency,
asset,
balance: currency + (asset * this.ticker.bid)
}
}

Manager.prototype.relayOrder = function() {
// look up all executed orders and relay average.
var process = (err, res) => {
Expand All @@ -309,23 +322,17 @@ Manager.prototype.relayOrder = function() {
this.setPortfolio,
this.setTicker
], () => {
var asset = _.find(this.portfolio, a => a.name === this.asset).amount;
var currency = _.find(this.portfolio, a => a.name === this.currency).amount;
const portfolio = this.convertPortfolio(this.portfolio);

this.emit('trade', {
date,
price,
action: this.action,
portfolio: {
currency,
asset,
balance: currency + (asset * this.ticker.bid)
},
balance: currency + (asset * this.ticker.bid)
portfolio: portfolio,
balance: portfolio.balance
});

this.orders = [];

});

}
Expand Down
20 changes: 15 additions & 5 deletions plugins/trader/trader.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ var Trader = function(next) {
this.manager = new Manager(_.extend(config.trader, config.watch));
this.manager.init(next);

this.mananager.on('trade', trade => {
this.emit(trade);
})
let sendPortfolio = false;

this.manager.on('trade', trade => {

if(!sendPortfolio && this.initialPortfolio) {
this.emit('portfolioUpdate', this.initialPortfolio);
sendPortfolio = true;
}

this.mananager.on('portfolioUpdate', trade => {
this.emit(trade);
this.emit('trade', trade);
});

this.manager.once('portfolioUpdate', portfolioUpdate => {
this.initialPortfolio = portfolioUpdate;
})
}

// teach our trader events
util.makeEventEmitter(Trader);

Trader.prototype.processCandle = (candle, done) => done();

Trader.prototype.processAdvice = function(advice) {
if(advice.recommendation == 'long') {
log.info(
Expand Down

0 comments on commit 93d9f03

Please sign in to comment.