Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
},
"homepage": "https://github.com/fugle-dev/fugle-backtest-node#readme",
"dependencies": {
"html-minifier": "^4.0.0",
"html-minifier-terser": "^7.2.0",
"lodash": "^4.17.21",
"luxon": "^3.2.1",
"open": "^8.4.1"
Expand All @@ -41,6 +41,7 @@
"@commitlint/cli": "^16.2.1",
"@commitlint/config-conventional": "^16.2.1",
"@types/html-minifier": "^4.0.2",
"@types/html-minifier-terser": "^7.0.2",
"@types/jest": "^27.0.1",
"@types/lodash": "^4.14.191",
"@types/luxon": "^3.2.0",
Expand Down
21 changes: 9 additions & 12 deletions src/plotting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as assert from 'assert';
import * as open from 'open';
import { minify } from 'html-minifier';
import { minify } from 'html-minifier-terser';
import { Stats } from './stats';
import { PlottingOptions } from './interfaces';

Expand All @@ -16,17 +16,14 @@ export class Plotting {
this.filename = options?.filename?.toLowerCase() ?? 'output.html';
}

public plot() {
const html = minify(this.createHTML(), {
collapseWhitespace: true,
removeComments: true,
collapseBooleanAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeOptionalTags: true,
minifyJS: true
});
const outputFile = this.filename.endsWith('.html') ? `./${this.filename}` : `./${this.filename}.html`;
public async getHTML() {
return await minify(this.createHTML());
}

public async plot() {
const html = await minify(this.createHTML());
let outputFile = this.filename.startsWith('/') || this.filename.startsWith('./') ? this.filename : `./${this.filename}`;
if (! outputFile.endsWith('.html')) outputFile = outputFile + '.html';
fs.writeFileSync(outputFile, html);

if (this.openBrowser) open(outputFile);
Expand Down
11 changes: 9 additions & 2 deletions src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,18 @@ export class Stats {
this.results.print();
}

public plot() {
public async plot() {
if (!this.results) {
throw new Error('No stats results');
}
new Plotting(this, { openBrowser: this.options.openBrowser, filename: this.options.filename }).plot();
await new Plotting(this, { openBrowser: this.options.openBrowser, filename: this.options.filename }).plot();
}

public async toHTML() {
if (!this.results) {
throw new Error('No stats results');
}
return await new Plotting(this).getHTML();
}

private computeExposureTime(index: string[], tradeLog: DataFrame) {
Expand Down
16 changes: 8 additions & 8 deletions test/plotting.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as open from 'open';
import { minify } from 'html-minifier';
import { minify } from 'html-minifier-terser';
import { Plotting } from '../src/plotting';
import { Backtest } from '../src/backtest';
import { Stats } from '../src/stats';
import { SmaCross } from './sma-cross.strategy';

jest.mock('fs');
jest.mock('open');
jest.mock('html-minifier');
jest.mock('html-minifier-terser');

describe('Plotting', () => {
let backtest: Backtest;
Expand Down Expand Up @@ -47,18 +47,18 @@ describe('Plotting', () => {
});

describe('.plot()', () => {
it('should create the HTML file with minified content', () => {
const options = { openBrowser: false, filename: 'test.html' };
it('should create the HTML file with minified content', async () => {
const options = {openBrowser: false, filename: 'test.html'};
const plotting = new Plotting(stats, options);
plotting.plot();
await plotting.plot();
expect(minify).toHaveBeenCalled();
expect(open).not.toBeCalled();
});

it('should create the HTML file with minified content and open it in the browser', () => {
const options = { openBrowser: true, filename: 'test.html' };
it('should create the HTML file with minified content and open it in the browser', async () => {
const options = {openBrowser: true, filename: 'test.html'};
const plotting = new Plotting(stats, options);
plotting.plot();
await plotting.plot();
expect(minify).toHaveBeenCalled();
expect(open).toHaveBeenCalledWith(`./${options.filename}`);
});
Expand Down
16 changes: 8 additions & 8 deletions test/stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ describe('Stats', () => {
});

describe('.plot()', () => {
it('should plot the equity curve', () => {
const stats = new Stats(data, strategy, equity, trades, { riskFreeRate: 0 });
it('should plot the equity curve', async () => {
const stats = new Stats(data, strategy, equity, trades, {riskFreeRate: 0});
stats.compute();
Plotting.prototype.plot = jest.fn();
stats.plot();
await stats.plot();
expect(Plotting.prototype.plot).toBeCalled();
});

it('should throw error when missing results', () => {
expect(() => {
const stats = new Stats(data, strategy, equity, trades, { riskFreeRate: 0 });
stats.plot();
}).toThrow(Error);
it('should throw error when missing results', async () => {
await expect(async () => {
const stats = new Stats(data, strategy, equity, trades, {riskFreeRate: 0});
await stats.plot();
}).rejects.toThrow(Error);
});
});
});
Loading