Skip to content

Commit

Permalink
ratio roundtrips
Browse files Browse the repository at this point in the history
  • Loading branch information
julesGoullee authored and askmike committed Dec 4, 2018
1 parent e8a3bf6 commit 9c86127
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
5 changes: 3 additions & 2 deletions plugins/performanceAnalyzer/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ if(mode === 'backtest') {
`(PROFIT REPORT) simulated yearly profit:\t ${report.yearlyProfit}`,
`${this.currency} (${report.relativeYearlyProfit}%)`
);

log.info(`(PROFIT REPORT) sharpe ratio:\t\t\t ${report.sharpe}`);
log.info(`(PROFIT REPORT) expected downside:\t\t ${report.downside}`);
log.info(`(PROFIT REPORT) ratio roundtrips:\t\t ${report.ratioRoundTrips}%`);
}

Logger.prototype.handleRoundtrip = function(rt) {
this.roundtrips.push(rt);
}
Expand Down
17 changes: 10 additions & 7 deletions plugins/performanceAnalyzer/performanceAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const PerformanceAnalyzer = function() {
this.trades = 0;

this.exposure = 0;

this.roundTrips = [];
this.losses = [];
this.roundTrip = {
Expand Down Expand Up @@ -176,7 +176,7 @@ PerformanceAnalyzer.prototype.handleCompletedRoundtrip = function() {
// track losses separately for downside report
if (roundtrip.exitBalance < roundtrip.entryBalance)
this.losses.push(roundtrip);

}

PerformanceAnalyzer.prototype.calculateReportStatistics = function() {
Expand All @@ -194,16 +194,18 @@ PerformanceAnalyzer.prototype.calculateReportStatistics = function() {
);
const relativeProfit = this.balance / this.start.balance * 100 - 100;
const relativeYearlyProfit = relativeProfit / timespan.asYears();

const percentExposure = this.exposure / (Date.parse(this.dates.end) - Date.parse(this.dates.start));

const sharpe = (relativeYearlyProfit - perfConfig.riskFreeReturn)
/ statslite.stdev(this.roundTrips.map(r => r.profit))
const sharpe = (relativeYearlyProfit - perfConfig.riskFreeReturn)
/ statslite.stdev(this.roundTrips.map(r => r.profit))
/ Math.sqrt(this.trades / (this.trades - 2));

const downside = statslite.percentile(this.losses.map(r => r.profit), 0.25)
* Math.sqrt(this.trades / (this.trades - 2));

const ratioRoundTrips = this.roundTrips.length > 0 ? (this.roundTrips.filter(roundTrip => roundTrip.pnl > 0 ).length / this.roundTrips.length * 100).toFixed(4) : 100;

const report = {
startTime: this.dates.start.utc().format('YYYY-MM-DD HH:mm:ss'),
endTime: this.dates.end.utc().format('YYYY-MM-DD HH:mm:ss'),
Expand All @@ -223,7 +225,8 @@ PerformanceAnalyzer.prototype.calculateReportStatistics = function() {
startBalance: this.start.balance,
exposure: percentExposure,
sharpe,
downside
downside,
ratioRoundTrips
}

report.alpha = report.profit - report.market;
Expand Down

0 comments on commit 9c86127

Please sign in to comment.