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

Commit

Permalink
Merge pull request #4 from askmike/develop
Browse files Browse the repository at this point in the history
check portfolio data before calculating report (#2369)
  • Loading branch information
hiyan authored Jul 27, 2018
2 parents 4b6846b + 81cdfad commit f576abe
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions plugins/performanceAnalyzer/performanceAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const moment = require('moment');

const statslite = require('stats-lite');
const util = require('../../core/util');
const log = require(util.dirs().core + 'log')
const ENV = util.gekkoEnv();

const config = util.getConfig();
Expand Down Expand Up @@ -48,13 +49,15 @@ const PerformanceAnalyzer = function() {
}

PerformanceAnalyzer.prototype.processPortfolioValueChange = function(event) {
if(!this.start.balance)
if(!this.start.balance) {
this.start.balance = event.balance;
}
}

PerformanceAnalyzer.prototype.processPortfolioChange = function(event) {
if(!this.start.portfolio)
if(!this.start.portfolio) {
this.start.portfolio = event;
}
}

PerformanceAnalyzer.prototype.processCandle = function(candle, done) {
Expand Down Expand Up @@ -91,13 +94,13 @@ PerformanceAnalyzer.prototype.processTradeCompleted = function(trade) {
this.portfolio = trade.portfolio;
this.balance = trade.balance;

const report = this.calculateReportStatistics();

this.logger.handleTrade(trade, report);

this.registerRoundtripPart(trade);

this.deferredEmit('performanceReport', report);
const report = this.calculateReportStatistics();
if(report) {
this.logger.handleTrade(trade, report);
this.deferredEmit('performanceReport', report);
}
}

PerformanceAnalyzer.prototype.registerRoundtripPart = function(trade) {
Expand Down Expand Up @@ -163,6 +166,12 @@ PerformanceAnalyzer.prototype.handleCompletedRoundtrip = function() {
}

PerformanceAnalyzer.prototype.calculateReportStatistics = function() {
if(!this.start.balance || !this.start.portfolio) {
log.error('Cannot calculate a profit report without having received portfolio data.');
log.error('Skipping performanceReport..');
return false;
}

// the portfolio's balance is measured in {currency}
const profit = this.balance - this.start.balance;

Expand Down Expand Up @@ -214,7 +223,9 @@ PerformanceAnalyzer.prototype.finalize = function(done) {
}

const report = this.calculateReportStatistics();
this.logger.finalize(report);
if(report) {
this.logger.finalize(report);
}
done();
}

Expand Down

0 comments on commit f576abe

Please sign in to comment.