Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge #5

Merged
merged 2 commits into from
Jul 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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