Unify analytics calculations and fail closed on unavailable data
This commit is contained in:
parent
783b619846
commit
ccce401f72
31 changed files with 1978 additions and 1553 deletions
|
|
@ -311,7 +311,11 @@ describe('detached, repeatable backtest reporting', () => {
|
|||
expect(result.metrics.totalReturn).toBeCloseTo(-110);
|
||||
expect(result.metrics.maxDrawdown).toBe(1.1);
|
||||
expect(result.analytics.drawdownSeries.at(-1)?.value).toBeCloseTo(-110);
|
||||
expect(Object.values(result.analytics.riskMetrics).every(Number.isFinite)).toBe(true);
|
||||
expect(
|
||||
Object.values(result.analytics.riskMetrics).every(
|
||||
value => value === null || Number.isFinite(value)
|
||||
)
|
||||
).toBe(true);
|
||||
expect(result.analytics.dailyReturns.every(Number.isFinite)).toBe(true);
|
||||
});
|
||||
|
||||
|
|
@ -323,8 +327,15 @@ describe('detached, repeatable backtest reporting', () => {
|
|||
expect(result.equity).toEqual([{ date: new Date(start).toISOString(), value: 998 }]);
|
||||
expect(result.metrics.totalReturn).toBeCloseTo(-0.2);
|
||||
expect(result.metrics.maxDrawdown).toBeCloseTo(0.002);
|
||||
expect(result.analytics.dailyReturns).toEqual([]);
|
||||
expect(Object.values(result.analytics.riskMetrics).every(Number.isFinite)).toBe(true);
|
||||
expect(result.analytics.dailyReturns).toHaveLength(1);
|
||||
expect(result.analytics.dailyReturns[0]).toBeCloseTo(-0.2);
|
||||
expect(result.metrics.sharpeRatio).toBeNull();
|
||||
expect(result.metrics.annualizedReturn).toBeNull();
|
||||
expect(
|
||||
Object.values(result.analytics.riskMetrics).every(
|
||||
value => value === null || Number.isFinite(value)
|
||||
)
|
||||
).toBe(true);
|
||||
expect(Object.keys(result.ohlcData)).toEqual(config.symbols);
|
||||
expect(result.ohlcData['A.US']?.map(bar => bar.timestamp)).toEqual([
|
||||
start,
|
||||
|
|
@ -364,6 +375,27 @@ describe('detached, repeatable backtest reporting', () => {
|
|||
expect(losses.analytics.monthlyReturns).toEqual({ '2024-01': -100 });
|
||||
});
|
||||
|
||||
test('risk analytics and summary share fraction-based math without 100x percentage scaling', () => {
|
||||
const report = new BacktestReport(config, 'risk');
|
||||
let value = config.initialCapital;
|
||||
for (let i = 1; i <= 30; i++) {
|
||||
value *= 1 + (i % 2 ? -0.01 : 0.02);
|
||||
report.record(start + i * day, value);
|
||||
}
|
||||
const result = report.complete(dataset, [], [], {});
|
||||
const returns = result.analytics.dailyReturns.map(value => value / 100);
|
||||
const average = returns.reduce((sum, value) => sum + value, 0) / returns.length;
|
||||
const expected =
|
||||
Math.sqrt(
|
||||
returns.reduce((sum, value) => sum + (value - average) ** 2, 0) / (returns.length - 1)
|
||||
) * Math.sqrt(252);
|
||||
expect(result.analytics.riskMetrics.volatility).toBeCloseTo(expected, 10);
|
||||
const summary = JSON.parse(report.export()).summary;
|
||||
expect(summary.volatility / 100).toBeCloseTo(expected, 10);
|
||||
expect(summary.var95 / 100).toBeCloseTo(result.analytics.riskMetrics.var95!, 10);
|
||||
expect(summary.cvar95 / 100).toBeCloseTo(result.analytics.riskMetrics.cvar95!, 10);
|
||||
});
|
||||
|
||||
test('quality inspection groups symbols once without changing frozen inputs or retaining old warnings', () => {
|
||||
const input = structuredClone(dataset);
|
||||
for (const bar of input.bars.filter(bar => bar.timestamp === start + day)) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue