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
4 changes: 3 additions & 1 deletion src/backtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export class Backtest {
tradeOnClose: false,
hedging: false,
exclusiveOrders: false,
outputFile: '',
openBrowser: true,
...this.options,
});
const strategy = new this.Strategy(data, broker);
Expand All @@ -85,7 +87,7 @@ export class Backtest {
strategy,
new Series(broker.equities),
broker.closedTrades as Trade[],
{ riskFreeRate: 0 },
{ riskFreeRate: 0, filename: this.options?.outputFile, openBrowser: this.options?.openBrowser },
).compute();

this._stats = stats;
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/backtest-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export interface BacktestOptions {
tradeOnClose?: boolean;
hedging?: boolean;
exclusiveOrders?: boolean;
outputFile?: string;
openBrowser?: boolean;
}
2 changes: 1 addition & 1 deletion src/interfaces/broker-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { BacktestOptions } from './backtest-options.interface';

export interface BrokerOptions extends Required<BacktestOptions> {}
export interface BrokerOptions extends Required<Omit<BacktestOptions, 'outputFile' | 'openBrowser'>> {}
2 changes: 2 additions & 0 deletions src/interfaces/stats-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ export interface StatsOptions {
riskFreeRate?: number;
precision?: number;
digits?: number;
filename?: string;
openBrowser?: boolean;
}
4 changes: 2 additions & 2 deletions src/plotting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class Plotting {

constructor(private readonly stats: Stats, private readonly options?: PlottingOptions) {
this.openBrowser = options?.openBrowser ?? true;
this.filename = options?.filename ?? 'output.html';
this.filename = options?.filename?.toLowerCase() ?? 'output.html';
}

public plot() {
Expand All @@ -26,7 +26,7 @@ export class Plotting {
removeOptionalTags: true,
minifyJS: true
});
const outputFile = `./${this.filename}`;
const outputFile = this.filename.endsWith('.html') ? `./${this.filename}` : `./${this.filename}.html`;
fs.writeFileSync(outputFile, html);

if (this.openBrowser) open(outputFile);
Expand Down
3 changes: 2 additions & 1 deletion src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class Stats {
if (!this.results) {
throw new Error('No stats results');
}
new Plotting(this).plot();
new Plotting(this, { openBrowser: this.options.openBrowser, filename: this.options.filename }).plot();
}

private computeExposureTime(index: string[], tradeLog: DataFrame) {
Expand Down Expand Up @@ -267,6 +267,7 @@ export class Stats {
private computeGeometricMean(returns: Series) {
returns = returns.fillNa(0).add(1) as Series;
/* istanbul ignore next */
// @ts-ignore
if (returns.values.some(v => v <= 0)) return 0;
/* istanbul ignore next */
return Math.exp(returns.apply(v => Math.log(v)).sum() / (returns.values.length || 0)) - 1;
Expand Down
Loading