backtest-kit 11.4.0 → 11.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +1996 -1995
- package/build/index.cjs +154 -33
- package/build/index.mjs +154 -33
- package/package.json +86 -86
package/build/index.mjs
CHANGED
|
@@ -23865,13 +23865,34 @@ let ReportStorage$a = class ReportStorage {
|
|
|
23865
23865
|
// per trade ("as-if 100% allocation"). Walks validSignals in chronological order
|
|
23866
23866
|
// (storage is newest-first, so iterate in reverse). Using validSignals (same set as
|
|
23867
23867
|
// tradesPerYear) keeps equityFinal consistent with the annualization exponent.
|
|
23868
|
-
//
|
|
23869
|
-
//
|
|
23868
|
+
//
|
|
23869
|
+
// MARK-TO-MARKET DD: each trade's worst intra-trade excursion (signal.maxDrawdown,
|
|
23870
|
+
// i.e. the `_fall` snapshot, ≤ 0) is applied as a trough BEFORE booking the realized
|
|
23871
|
+
// close. Without this the curve only steps at close, so a trade that dipped to -18%
|
|
23872
|
+
// and recovered to +2% would register zero drawdown — understating DD and inflating
|
|
23873
|
+
// Calmar/Recovery. The trough does not persist into equity (it's a transient
|
|
23874
|
+
// mark-to-market low); equity then moves to the realized close.
|
|
23875
|
+
// If equity (at trough or close) goes ≤ 0 (e.g. leveraged loss < -100%) — account
|
|
23876
|
+
// blown, fix DD at 100% and stop walking the curve.
|
|
23870
23877
|
let equity = 1;
|
|
23871
23878
|
let peak = 1;
|
|
23872
23879
|
let equityMaxDrawdown = 0;
|
|
23873
23880
|
let blown = false;
|
|
23874
23881
|
for (let i = validSignals.length - 1; i >= 0; i--) {
|
|
23882
|
+
// Intra-trade trough — mark-to-market low while the position was open.
|
|
23883
|
+
const fallPct = validSignals[i].signal.maxDrawdown?.pnlPercentage;
|
|
23884
|
+
if (typeof fallPct === "number" && fallPct < 0) {
|
|
23885
|
+
const trough = equity * (1 + fallPct / 100);
|
|
23886
|
+
if (trough <= 0) {
|
|
23887
|
+
equityMaxDrawdown = 100;
|
|
23888
|
+
blown = true;
|
|
23889
|
+
break;
|
|
23890
|
+
}
|
|
23891
|
+
const troughDd = (peak - trough) / peak * 100;
|
|
23892
|
+
if (troughDd > equityMaxDrawdown)
|
|
23893
|
+
equityMaxDrawdown = troughDd;
|
|
23894
|
+
}
|
|
23895
|
+
// Realized close — book the final per-trade result.
|
|
23875
23896
|
equity *= 1 + validSignals[i].pnl.pnlPercentage / 100;
|
|
23876
23897
|
if (equity <= 0) {
|
|
23877
23898
|
equityMaxDrawdown = 100;
|
|
@@ -24029,7 +24050,7 @@ let ReportStorage$a = class ReportStorage {
|
|
|
24029
24050
|
`**Win rate:** ${stats.winRate === null ? "N/A" : `${stats.winRate.toFixed(2)}% (${stats.winCount}W / ${stats.lossCount}L) (higher is better)`}`,
|
|
24030
24051
|
`**Average PNL:** ${stats.avgPnl === null ? "N/A" : `${stats.avgPnl > 0 ? "+" : ""}${stats.avgPnl.toFixed(2)}% (higher is better)`}`,
|
|
24031
24052
|
`**Total PNL:** ${stats.totalPnl === null ? "N/A" : `${stats.totalPnl > 0 ? "+" : ""}${stats.totalPnl.toFixed(2)}% (higher is better)`}`,
|
|
24032
|
-
`**Standard Deviation:** ${stats.stdDev === null ? "N/A" : `${stats.stdDev.toFixed(3)}% (lower is better)`}`,
|
|
24053
|
+
`**Standard Deviation Per Trade:** ${stats.stdDev === null ? "N/A" : `${stats.stdDev.toFixed(3)}% (lower is better)`}`,
|
|
24033
24054
|
`**Sharpe Ratio:** ${stats.sharpeRatio === null ? "N/A" : `${stats.sharpeRatio.toFixed(3)} (higher is better)`}`,
|
|
24034
24055
|
`**Annualized Sharpe Ratio:** ${stats.annualizedSharpeRatio === null ? "N/A" : `${stats.annualizedSharpeRatio.toFixed(3)} (higher is better)`}`,
|
|
24035
24056
|
`**Certainty Ratio:** ${stats.certaintyRatio === null ? "N/A" : `${stats.certaintyRatio.toFixed(3)} (higher is better)`}`,
|
|
@@ -24047,11 +24068,12 @@ let ReportStorage$a = class ReportStorage {
|
|
|
24047
24068
|
`*Sortino Ratio: below 1.0 is poor, 1.0-2.0 is acceptable, above 2.0 is strong. Requires 30+ signals. N/A when no losing trades — Sortino is mathematically undefined (infinite) and we cannot distinguish "truly flawless" from "lucky streak so far".*`,
|
|
24048
24069
|
`*Certainty Ratio: below 1.0 means average loss exceeds average win. Above 1.5 is considered good.*`,
|
|
24049
24070
|
`*Expected Yearly Returns: compounded geometric return from the equity curve, annualized by tradesPerYear. Same gating as Annualized Sharpe. Capped at ±${MAX_EXPECTED_YEARLY_RETURNS$2}% — values above the cap return N/A.*`,
|
|
24050
|
-
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is
|
|
24051
|
-
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator.*`,
|
|
24071
|
+
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is the mark-to-market max drawdown (see below). Capped at ±${MAX_CALMAR_RATIO$2}.*`,
|
|
24072
|
+
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator and the mark-to-market max drawdown as denominator.*`,
|
|
24073
|
+
`*Max Drawdown: mark-to-market — the compounded equity curve applies each trade's worst intra-trade excursion (the lowest unrealized point while the position was open) before booking its realized close, so deep round-trip dips count. It is NOT realized-only (close-to-close); a realized-only curve would understate drawdown and inflate Calmar/Recovery.*`,
|
|
24052
24074
|
`*Expectancy: per-trade expected value (winProb × avgWin + lossProb × avgLoss). Positive = profitable on average per trade. Break-even trades contribute 0.*`,
|
|
24053
24075
|
`*All metrics require 100+ signals to be statistically reliable. Annualized metrics assume the observed trading frequency and market conditions persist year-round.*`,
|
|
24054
|
-
`*IMPORTANT: Equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per
|
|
24076
|
+
`*IMPORTANT: Equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per position** (no portfolio fraction). These metrics ignore the position-sizing subsystem (PositionSize / Kelly / ATR): pnlPercentage is a return on the position's own invested capital, never scaled by account balance. With DCA (commitAverageBuy) the cost basis is the sum of all entries and the entry price is dollar-cost-weighted, so per-trade % is measured against the averaged position, not a fixed stake. If your strategy risks X% of capital per trade, the realized portfolio return / drawdown will be roughly X/100 of the reported figures — these metrics represent a theoretical upper bound under full allocation.*`,
|
|
24055
24077
|
`*Negative values for Sharpe / Sortino / Calmar / Recovery / Expected Yearly Returns indicate a losing strategy (avgPnl < 0 or totalPnl < 0). "Higher is better" still applies — closer to zero is less bad, positive is profitable.*`,
|
|
24056
24078
|
].join("\n");
|
|
24057
24079
|
}
|
|
@@ -24800,15 +24822,37 @@ let ReportStorage$9 = class ReportStorage {
|
|
|
24800
24822
|
// on cost basis — compounding assumes equal capital allocation per trade ("as-if 100%").
|
|
24801
24823
|
// If equity ≤ 0 (leveraged short with r < -100%) — account blown, fix DD at 100%.
|
|
24802
24824
|
// Built from validClosed (newest-first), iterated reverse for chronological order.
|
|
24803
|
-
|
|
24825
|
+
//
|
|
24826
|
+
// MARK-TO-MARKET DD: each trade's worst intra-trade excursion (fallPnl, the `_fall`
|
|
24827
|
+
// snapshot, ≤ 0) is applied as a trough BEFORE booking the realized close. Without it
|
|
24828
|
+
// the curve only steps at close, so a trade that dipped to -18% and recovered to +2%
|
|
24829
|
+
// would register zero drawdown — understating DD and inflating Calmar/Recovery.
|
|
24830
|
+
const chronological = [];
|
|
24804
24831
|
for (let i = validClosed.length - 1; i >= 0; i--) {
|
|
24805
|
-
|
|
24832
|
+
const fall = validClosed[i].fallPnl;
|
|
24833
|
+
chronological.push({
|
|
24834
|
+
r: validClosed[i].pnl,
|
|
24835
|
+
fall: typeof fall === "number" ? fall : null,
|
|
24836
|
+
});
|
|
24806
24837
|
}
|
|
24807
24838
|
let equity = 1;
|
|
24808
24839
|
let peak = 1;
|
|
24809
24840
|
let equityMaxDrawdown = 0;
|
|
24810
24841
|
let blown = false;
|
|
24811
|
-
for (const r of
|
|
24842
|
+
for (const { r, fall } of chronological) {
|
|
24843
|
+
// Intra-trade trough — mark-to-market low while the position was open.
|
|
24844
|
+
if (fall !== null && fall < 0) {
|
|
24845
|
+
const trough = equity * (1 + fall / 100);
|
|
24846
|
+
if (trough <= 0) {
|
|
24847
|
+
equityMaxDrawdown = 100;
|
|
24848
|
+
blown = true;
|
|
24849
|
+
break;
|
|
24850
|
+
}
|
|
24851
|
+
const troughDd = (peak - trough) / peak * 100;
|
|
24852
|
+
if (troughDd > equityMaxDrawdown)
|
|
24853
|
+
equityMaxDrawdown = troughDd;
|
|
24854
|
+
}
|
|
24855
|
+
// Realized close.
|
|
24812
24856
|
equity *= 1 + r / 100;
|
|
24813
24857
|
if (equity <= 0) {
|
|
24814
24858
|
equityMaxDrawdown = 100;
|
|
@@ -24907,7 +24951,7 @@ let ReportStorage$9 = class ReportStorage {
|
|
|
24907
24951
|
`**Win rate:** ${stats.winRate === null ? "N/A" : `${stats.winRate.toFixed(2)}% (${stats.winCount}W / ${stats.lossCount}L) (higher is better)`}`,
|
|
24908
24952
|
`**Average PNL:** ${stats.avgPnl === null ? "N/A" : `${stats.avgPnl > 0 ? "+" : ""}${stats.avgPnl.toFixed(2)}% (higher is better)`}`,
|
|
24909
24953
|
`**Total PNL:** ${stats.totalPnl === null ? "N/A" : `${stats.totalPnl > 0 ? "+" : ""}${stats.totalPnl.toFixed(2)}% (higher is better)`}`,
|
|
24910
|
-
`**Standard Deviation:** ${stats.stdDev === null ? "N/A" : `${stats.stdDev.toFixed(3)}% (lower is better)`}`,
|
|
24954
|
+
`**Standard Deviation Per Trade:** ${stats.stdDev === null ? "N/A" : `${stats.stdDev.toFixed(3)}% (lower is better)`}`,
|
|
24911
24955
|
`**Sharpe Ratio:** ${stats.sharpeRatio === null ? "N/A" : `${stats.sharpeRatio.toFixed(3)} (higher is better)`}`,
|
|
24912
24956
|
`**Annualized Sharpe Ratio:** ${stats.annualizedSharpeRatio === null ? "N/A" : `${stats.annualizedSharpeRatio.toFixed(3)} (higher is better)`}`,
|
|
24913
24957
|
`**Certainty Ratio:** ${stats.certaintyRatio === null ? "N/A" : `${stats.certaintyRatio.toFixed(3)} (higher is better)`}`,
|
|
@@ -24925,11 +24969,12 @@ let ReportStorage$9 = class ReportStorage {
|
|
|
24925
24969
|
`*Sortino Ratio: below 1.0 is poor, 1.0-2.0 is acceptable, above 2.0 is strong. Requires 30+ signals. N/A when no losing trades — Sortino is mathematically undefined (infinite) and we cannot distinguish "truly flawless" from "lucky streak so far".*`,
|
|
24926
24970
|
`*Certainty Ratio: below 1.0 means average loss exceeds average win. Above 1.5 is considered good.*`,
|
|
24927
24971
|
`*Expected Yearly Returns: compounded geometric return from the equity curve, annualized by tradesPerYear. Same gating as Annualized Sharpe. Capped at ±${MAX_EXPECTED_YEARLY_RETURNS$1}% — values above the cap return N/A.*`,
|
|
24928
|
-
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is
|
|
24929
|
-
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator.*`,
|
|
24972
|
+
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is the mark-to-market max drawdown (see below). Capped at ±${MAX_CALMAR_RATIO$1}.*`,
|
|
24973
|
+
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator and the mark-to-market max drawdown as denominator.*`,
|
|
24974
|
+
`*Max Drawdown: mark-to-market — the compounded equity curve applies each trade's worst intra-trade excursion (the lowest unrealized point while the position was open) before booking its realized close, so deep round-trip dips count. It is NOT realized-only (close-to-close); a realized-only curve would understate drawdown and inflate Calmar/Recovery.*`,
|
|
24930
24975
|
`*Expectancy: per-trade expected value (winProb × avgWin + lossProb × avgLoss). Positive = profitable on average per trade. Break-even trades contribute 0.*`,
|
|
24931
24976
|
`*All metrics require 100+ signals to be statistically reliable. Annualized metrics assume the observed trading frequency and market conditions persist year-round.*`,
|
|
24932
|
-
`*IMPORTANT: Equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per
|
|
24977
|
+
`*IMPORTANT: Equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per position** (no portfolio fraction). These metrics ignore the position-sizing subsystem (PositionSize / Kelly / ATR): pnlPercentage is a return on the position's own invested capital, never scaled by account balance. With DCA (commitAverageBuy) the cost basis is the sum of all entries and the entry price is dollar-cost-weighted, so per-trade % is measured against the averaged position, not a fixed stake. If your strategy risks X% of capital per trade, the realized portfolio return / drawdown will be roughly X/100 of the reported figures — these metrics represent a theoretical upper bound under full allocation.*`,
|
|
24933
24978
|
`*Negative values for Sharpe / Sortino / Calmar / Recovery / Expected Yearly Returns indicate a losing strategy (avgPnl < 0 or totalPnl < 0). "Higher is better" still applies — closer to zero is less bad, positive is profitable.*`,
|
|
24934
24979
|
].join("\n");
|
|
24935
24980
|
}
|
|
@@ -26841,6 +26886,12 @@ class HeatmapStorage {
|
|
|
26841
26886
|
// Equity-curve max drawdown via compounded equity ("as-if 100% allocation per trade").
|
|
26842
26887
|
// Signals are stored newest-first (unshift in addSignal), so iterate in reverse.
|
|
26843
26888
|
// If equity ≤ 0 — account blown, fix DD at 100%. equityFinal feeds expectedYearlyReturns.
|
|
26889
|
+
//
|
|
26890
|
+
// MARK-TO-MARKET DD: each trade's worst intra-trade excursion (signal.maxDrawdown,
|
|
26891
|
+
// the `_fall` snapshot, ≤ 0) is applied as a trough BEFORE booking the realized close.
|
|
26892
|
+
// Without it the curve only steps at close, so a trade that dipped to -18% and
|
|
26893
|
+
// recovered to +2% would register zero drawdown — understating DD and inflating
|
|
26894
|
+
// Calmar/Recovery.
|
|
26844
26895
|
let maxDrawdown = null;
|
|
26845
26896
|
let equityFinal = 1;
|
|
26846
26897
|
let blown = false;
|
|
@@ -26849,6 +26900,18 @@ class HeatmapStorage {
|
|
|
26849
26900
|
let peak = 1;
|
|
26850
26901
|
let maxDD = 0;
|
|
26851
26902
|
for (let i = signals.length - 1; i >= 0; i--) {
|
|
26903
|
+
const fallPct = signals[i].signal.maxDrawdown?.pnlPercentage;
|
|
26904
|
+
if (typeof fallPct === "number" && fallPct < 0) {
|
|
26905
|
+
const trough = equity * (1 + fallPct / 100);
|
|
26906
|
+
if (trough <= 0) {
|
|
26907
|
+
maxDD = 100;
|
|
26908
|
+
blown = true;
|
|
26909
|
+
break;
|
|
26910
|
+
}
|
|
26911
|
+
const troughDd = (peak - trough) / peak * 100;
|
|
26912
|
+
if (troughDd > maxDD)
|
|
26913
|
+
maxDD = troughDd;
|
|
26914
|
+
}
|
|
26852
26915
|
equity *= 1 + signals[i].pnl.pnlPercentage / 100;
|
|
26853
26916
|
if (equity <= 0) {
|
|
26854
26917
|
maxDD = 100;
|
|
@@ -27135,9 +27198,20 @@ class HeatmapStorage {
|
|
|
27135
27198
|
let portfolioCalmarRatio = null;
|
|
27136
27199
|
let portfolioRecoveryFactor = null;
|
|
27137
27200
|
const allReturns = [];
|
|
27201
|
+
// Parallel array of intra-trade troughs (≤ 0), aligned 1:1 with allReturns,
|
|
27202
|
+
// used for mark-to-market DD in the pooled equity curve below.
|
|
27203
|
+
const allFalls = [];
|
|
27204
|
+
let poolFirstPendingAt = Infinity;
|
|
27205
|
+
let poolLastCloseAt = -Infinity;
|
|
27138
27206
|
for (const signals of this.symbolData.values()) {
|
|
27139
27207
|
for (const s of signals) {
|
|
27140
27208
|
allReturns.push(s.pnl.pnlPercentage);
|
|
27209
|
+
const fall = s.signal.maxDrawdown?.pnlPercentage;
|
|
27210
|
+
allFalls.push(typeof fall === "number" ? fall : null);
|
|
27211
|
+
if (s.signal.pendingAt < poolFirstPendingAt)
|
|
27212
|
+
poolFirstPendingAt = s.signal.pendingAt;
|
|
27213
|
+
if (s.closeTimestamp > poolLastCloseAt)
|
|
27214
|
+
poolLastCloseAt = s.closeTimestamp;
|
|
27141
27215
|
}
|
|
27142
27216
|
}
|
|
27143
27217
|
if (allReturns.length >= MIN_SIGNALS_FOR_RATIOS) {
|
|
@@ -27169,13 +27243,27 @@ class HeatmapStorage {
|
|
|
27169
27243
|
if (wins.length > 0 || losses.length > 0) {
|
|
27170
27244
|
portfolioExpectancy = (wins.length / total) * avgWin + (losses.length / total) * avgLoss;
|
|
27171
27245
|
}
|
|
27172
|
-
// Pooled equity-curve max drawdown (compounded).
|
|
27246
|
+
// Pooled equity-curve max drawdown (compounded). MARK-TO-MARKET: each trade's
|
|
27247
|
+
// intra-trade trough (allFalls, ≤ 0) is applied before booking the realized close,
|
|
27248
|
+
// so deep round-trip dips are captured rather than understating DD.
|
|
27173
27249
|
let equity = 1;
|
|
27174
27250
|
let peak = 1;
|
|
27175
27251
|
let maxDD = 0;
|
|
27176
27252
|
let blown = false;
|
|
27177
|
-
for (
|
|
27178
|
-
|
|
27253
|
+
for (let i = 0; i < allReturns.length; i++) {
|
|
27254
|
+
const fall = allFalls[i];
|
|
27255
|
+
if (fall !== null && fall < 0) {
|
|
27256
|
+
const trough = equity * (1 + fall / 100);
|
|
27257
|
+
if (trough <= 0) {
|
|
27258
|
+
maxDD = 100;
|
|
27259
|
+
blown = true;
|
|
27260
|
+
break;
|
|
27261
|
+
}
|
|
27262
|
+
const troughDd = ((peak - trough) / peak) * 100;
|
|
27263
|
+
if (troughDd > maxDD)
|
|
27264
|
+
maxDD = troughDd;
|
|
27265
|
+
}
|
|
27266
|
+
equity *= 1 + allReturns[i] / 100;
|
|
27179
27267
|
if (equity <= 0) {
|
|
27180
27268
|
maxDD = 100;
|
|
27181
27269
|
blown = true;
|
|
@@ -27188,20 +27276,43 @@ class HeatmapStorage {
|
|
|
27188
27276
|
maxDD = dd;
|
|
27189
27277
|
}
|
|
27190
27278
|
const equityFinal = blown ? 0 : equity;
|
|
27191
|
-
// Pooled
|
|
27192
|
-
//
|
|
27193
|
-
|
|
27194
|
-
|
|
27195
|
-
|
|
27196
|
-
|
|
27197
|
-
|
|
27198
|
-
|
|
27199
|
-
|
|
27200
|
-
|
|
27201
|
-
|
|
27202
|
-
|
|
27279
|
+
// Pooled expected yearly returns — geometric annualization of the pooled
|
|
27280
|
+
// equity curve, gated exactly like the per-symbol path: requires a valid
|
|
27281
|
+
// calendar span (≥ MIN_CALENDAR_SPAN_DAYS) and a non-clustered trade
|
|
27282
|
+
// frequency (≤ MAX_TRADES_PER_YEAR). Above MAX_EXPECTED_YEARLY_RETURNS → null
|
|
27283
|
+
// (don't surface the cap as a real figure). This is the numerator for Calmar.
|
|
27284
|
+
let pooledExpectedYearlyReturns = null;
|
|
27285
|
+
const poolSpanDays = isFinite(poolFirstPendingAt) && isFinite(poolLastCloseAt)
|
|
27286
|
+
? (poolLastCloseAt - poolFirstPendingAt) / (1000 * 60 * 60 * 24)
|
|
27287
|
+
: 0;
|
|
27288
|
+
if (poolSpanDays >= MIN_CALENDAR_SPAN_DAYS) {
|
|
27289
|
+
const rawTradesPerYear = (allReturns.length / poolSpanDays) * 365;
|
|
27290
|
+
if (rawTradesPerYear <= MAX_TRADES_PER_YEAR) {
|
|
27291
|
+
if (blown) {
|
|
27292
|
+
pooledExpectedYearlyReturns = -100;
|
|
27293
|
+
}
|
|
27294
|
+
else {
|
|
27295
|
+
const raw = (Math.pow(equityFinal, rawTradesPerYear / allReturns.length) - 1) * 100;
|
|
27296
|
+
pooledExpectedYearlyReturns =
|
|
27297
|
+
Math.abs(raw) > MAX_EXPECTED_YEARLY_RETURNS ? null : raw;
|
|
27298
|
+
}
|
|
27203
27299
|
}
|
|
27204
27300
|
}
|
|
27301
|
+
// Pooled Calmar = annualized return / max drawdown — same formula and
|
|
27302
|
+
// gating as per-symbol Calmar. NULL when the annualized numerator is
|
|
27303
|
+
// unavailable (span/frequency gate, or over the yearly cap). This is what
|
|
27304
|
+
// distinguishes it from Recovery, which uses the compounded TOTAL return —
|
|
27305
|
+
// previously both used total return, making Calmar == Recovery (a bug).
|
|
27306
|
+
if (maxDD > 0 && pooledExpectedYearlyReturns !== null) {
|
|
27307
|
+
portfolioCalmarRatio = Math.max(-MAX_CALMAR_RATIO, Math.min(MAX_CALMAR_RATIO, pooledExpectedYearlyReturns / maxDD));
|
|
27308
|
+
}
|
|
27309
|
+
// Pooled Recovery Factor = compounded TOTAL return / max drawdown, clamped.
|
|
27310
|
+
// Time-independent (no annualization), so it needs no span gate — only a
|
|
27311
|
+
// valid DD and a non-blown account (ratio is meaningless after total loss).
|
|
27312
|
+
if (maxDD > 0 && !blown) {
|
|
27313
|
+
const rawRec = ((equityFinal - 1) * 100) / maxDD;
|
|
27314
|
+
portfolioRecoveryFactor = Math.max(-MAX_CALMAR_RATIO, Math.min(MAX_CALMAR_RATIO, rawRec));
|
|
27315
|
+
}
|
|
27205
27316
|
}
|
|
27206
27317
|
// Portfolio-wide weighted average peak/fall PNL. Denominator must include only
|
|
27207
27318
|
// symbols that contributed a value — otherwise trade-count-weighted mean is diluted
|
|
@@ -27298,8 +27409,17 @@ class HeatmapStorage {
|
|
|
27298
27409
|
return [
|
|
27299
27410
|
`# Portfolio Heatmap: ${strategyName}`,
|
|
27300
27411
|
"",
|
|
27301
|
-
`**Total Symbols:** ${data.totalSymbols}
|
|
27302
|
-
|
|
27412
|
+
`**Total Symbols:** ${data.totalSymbols}
|
|
27413
|
+
**Portfolio PNL:** ${data.portfolioTotalPnl !== null ? str(data.portfolioTotalPnl, "%") : "N/A"}
|
|
27414
|
+
**Pooled Sharpe:** ${data.portfolioSharpeRatio !== null ? str(data.portfolioSharpeRatio) : "N/A"}
|
|
27415
|
+
**Total Trades:** ${data.portfolioTotalTrades}
|
|
27416
|
+
**Avg Peak PNL:** ${data.portfolioAvgPeakPnl !== null ? str(data.portfolioAvgPeakPnl, "%") : "N/A"}
|
|
27417
|
+
**Avg Max Drawdown PNL:** ${data.portfolioAvgFallPnl !== null ? str(data.portfolioAvgFallPnl, "%") : "N/A"}`,
|
|
27418
|
+
`**Standard Deviation Per Trade:** ${data.portfolioStdDev !== null ? str(data.portfolioStdDev, "%") : "N/A"}
|
|
27419
|
+
**Sortino Ratio:** ${data.portfolioSortinoRatio !== null ? str(data.portfolioSortinoRatio) : "N/A"}
|
|
27420
|
+
**Calmar Ratio:** ${data.portfolioCalmarRatio !== null ? str(data.portfolioCalmarRatio) : "N/A"}
|
|
27421
|
+
**Recovery Factor:** ${data.portfolioRecoveryFactor !== null ? str(data.portfolioRecoveryFactor) : "N/A"}
|
|
27422
|
+
**Expectancy:** ${data.portfolioExpectancy !== null ? str(data.portfolioExpectancy, "%") : "N/A"}`,
|
|
27303
27423
|
"",
|
|
27304
27424
|
table,
|
|
27305
27425
|
"",
|
|
@@ -27309,10 +27429,11 @@ class HeatmapStorage {
|
|
|
27309
27429
|
`*Sortino Ratio: below 1.0 is poor, 1.0-2.0 is acceptable, above 2.0 is strong. Requires 30+ signals. N/A when no losing trades — Sortino is mathematically undefined (infinite) and we cannot distinguish "truly flawless" from "lucky streak so far".*`,
|
|
27310
27430
|
`*Certainty Ratio: below 1.0 means average loss exceeds average win. Above 1.5 is considered good.*`,
|
|
27311
27431
|
`*Profit Factor: below 1.0 means strategy is losing overall. Above 1.5 is considered good.*`,
|
|
27312
|
-
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is
|
|
27313
|
-
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator.*`,
|
|
27432
|
+
`*Calmar Ratio: below 0.5 is poor, 0.5-1.0 is acceptable, above 1.0 is strong. Denominator is the mark-to-market max drawdown (see below). N/A unless ≥${MIN_SIGNALS_FOR_ANNUALIZATION} signals per symbol and span ≥${MIN_CALENDAR_SPAN_DAYS} days. Capped at ±${MAX_CALMAR_RATIO}.*`,
|
|
27433
|
+
`*Recovery Factor: below 1.0 means total profit does not cover max drawdown. Above 3.0 is considered good. Uses compounded total return as numerator and the mark-to-market max drawdown as denominator.*`,
|
|
27434
|
+
`*Max Drawdown: mark-to-market — both the per-symbol and pooled equity curves apply each trade's worst intra-trade excursion (the lowest unrealized point while the position was open) before booking its realized close, so deep round-trip dips count. It is NOT realized-only (close-to-close); a realized-only curve would understate drawdown and inflate Calmar/Recovery. NOTE: the pooled curve orders trades by storage sequence, not wall-clock time, so simultaneous cross-symbol drawdowns are not modelled.*`,
|
|
27314
27435
|
`*All metrics require 100+ signals per symbol to be statistically reliable. Annualized metrics assume the observed trading frequency persists year-round.*`,
|
|
27315
|
-
`*IMPORTANT: Per-symbol equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per
|
|
27436
|
+
`*IMPORTANT: Per-symbol equity curve, Expected Yearly Returns, Calmar, Recovery and Max Drawdown all assume **100% capital allocation per position** (no portfolio fraction). These metrics ignore the position-sizing subsystem (PositionSize / Kelly / ATR): pnlPercentage is a return on the position's own invested capital, never scaled by account balance. With DCA (commitAverageBuy) the cost basis is the sum of all entries and the entry price is dollar-cost-weighted, so per-trade % is measured against the averaged position, not a fixed stake. If your strategy risks X% of capital per trade, the realized return / drawdown will be roughly X/100 of the reported figures — these metrics represent a theoretical upper bound under full allocation.*`,
|
|
27316
27437
|
`*Negative values for Sharpe / Sortino / Calmar / Recovery indicate a losing symbol (avgPnl < 0 or totalPnl < 0). "Higher is better" still applies — closer to zero is less bad, positive is profitable.*`,
|
|
27317
27438
|
].join("\n");
|
|
27318
27439
|
}
|
package/package.json
CHANGED
|
@@ -1,86 +1,86 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "backtest-kit",
|
|
3
|
-
"version": "11.
|
|
4
|
-
"description": "A TypeScript library for trading system backtest",
|
|
5
|
-
"author": {
|
|
6
|
-
"name": "Petr Tripolsky",
|
|
7
|
-
"email": "tripolskypetr@gmail.com",
|
|
8
|
-
"url": "https://github.com/tripolskypetr"
|
|
9
|
-
},
|
|
10
|
-
"funding": {
|
|
11
|
-
"type": "individual",
|
|
12
|
-
"url": "http://paypal.me/tripolskypetr"
|
|
13
|
-
},
|
|
14
|
-
"license": "MIT",
|
|
15
|
-
"homepage": "https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html",
|
|
16
|
-
"keywords": [
|
|
17
|
-
"backtesting",
|
|
18
|
-
"backtest",
|
|
19
|
-
"finance",
|
|
20
|
-
"trading",
|
|
21
|
-
"candles",
|
|
22
|
-
"indicators",
|
|
23
|
-
"multi value",
|
|
24
|
-
"multi symbol",
|
|
25
|
-
"framework"
|
|
26
|
-
],
|
|
27
|
-
"files": [
|
|
28
|
-
"build",
|
|
29
|
-
"types.d.ts",
|
|
30
|
-
"README.md"
|
|
31
|
-
],
|
|
32
|
-
"repository": {
|
|
33
|
-
"type": "git",
|
|
34
|
-
"url": "https://github.com/tripolskypetr/backtest-kit",
|
|
35
|
-
"documentation": "https://github.com/tripolskypetr/backtest-kit/tree/master/docs"
|
|
36
|
-
},
|
|
37
|
-
"bugs": {
|
|
38
|
-
"url": "https://github.com/tripolskypetr/backtest-kit/issues"
|
|
39
|
-
},
|
|
40
|
-
"scripts": {
|
|
41
|
-
"build": "rollup -c",
|
|
42
|
-
"test": "npm run build && node ./test/index.mjs",
|
|
43
|
-
"build:docs": "rimraf docs && mkdir docs && node ./scripts/dts-docs.cjs ./types.d.ts ./docs",
|
|
44
|
-
"docs:gpt": "npm run build && node ./tools/gpt-docs/index.mjs",
|
|
45
|
-
"docs:uml": "npm run build && node ./scripts/uml.mjs",
|
|
46
|
-
"docs:www": "rimraf docs/wwwroot && node ./tools/typedoc-packages-docs/index.mjs && typedoc && node ./tools/typedoc-yandex-metrica/index.mjs",
|
|
47
|
-
"repl": "dotenv -e .env -- npm run build && node -e \"import('./scripts/repl.mjs')\" --interactive"
|
|
48
|
-
},
|
|
49
|
-
"main": "build/index.cjs",
|
|
50
|
-
"module": "build/index.mjs",
|
|
51
|
-
"source": "src/index.ts",
|
|
52
|
-
"types": "./types.d.ts",
|
|
53
|
-
"exports": {
|
|
54
|
-
"require": "./build/index.cjs",
|
|
55
|
-
"types": "./types.d.ts",
|
|
56
|
-
"import": "./build/index.mjs",
|
|
57
|
-
"default": "./build/index.cjs"
|
|
58
|
-
},
|
|
59
|
-
"devDependencies": {
|
|
60
|
-
"@rollup/plugin-typescript": "11.1.6",
|
|
61
|
-
"@types/node": "22.9.0",
|
|
62
|
-
"glob": "11.0.1",
|
|
63
|
-
"plantuml": "0.0.2",
|
|
64
|
-
"rimraf": "6.0.1",
|
|
65
|
-
"rollup": "3.29.5",
|
|
66
|
-
"rollup-plugin-dts": "6.1.1",
|
|
67
|
-
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
68
|
-
"ts-morph": "27.0.2",
|
|
69
|
-
"tslib": "2.7.0",
|
|
70
|
-
"typedoc": "0.27.9",
|
|
71
|
-
"worker-testbed": "2.0.0"
|
|
72
|
-
},
|
|
73
|
-
"peerDependencies": {
|
|
74
|
-
"typescript": "^5.0.0"
|
|
75
|
-
},
|
|
76
|
-
"dependencies": {
|
|
77
|
-
"di-kit": "^1.1.1",
|
|
78
|
-
"di-scoped": "^1.0.21",
|
|
79
|
-
"di-singleton": "^1.0.5",
|
|
80
|
-
"functools-kit": "^2.3.0",
|
|
81
|
-
"get-moment-stamp": "^2.0.0"
|
|
82
|
-
},
|
|
83
|
-
"publishConfig": {
|
|
84
|
-
"access": "public"
|
|
85
|
-
}
|
|
86
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "backtest-kit",
|
|
3
|
+
"version": "11.5.2",
|
|
4
|
+
"description": "A TypeScript library for trading system backtest",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Petr Tripolsky",
|
|
7
|
+
"email": "tripolskypetr@gmail.com",
|
|
8
|
+
"url": "https://github.com/tripolskypetr"
|
|
9
|
+
},
|
|
10
|
+
"funding": {
|
|
11
|
+
"type": "individual",
|
|
12
|
+
"url": "http://paypal.me/tripolskypetr"
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"homepage": "https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"backtesting",
|
|
18
|
+
"backtest",
|
|
19
|
+
"finance",
|
|
20
|
+
"trading",
|
|
21
|
+
"candles",
|
|
22
|
+
"indicators",
|
|
23
|
+
"multi value",
|
|
24
|
+
"multi symbol",
|
|
25
|
+
"framework"
|
|
26
|
+
],
|
|
27
|
+
"files": [
|
|
28
|
+
"build",
|
|
29
|
+
"types.d.ts",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/tripolskypetr/backtest-kit",
|
|
35
|
+
"documentation": "https://github.com/tripolskypetr/backtest-kit/tree/master/docs"
|
|
36
|
+
},
|
|
37
|
+
"bugs": {
|
|
38
|
+
"url": "https://github.com/tripolskypetr/backtest-kit/issues"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "rollup -c",
|
|
42
|
+
"test": "npm run build && node ./test/index.mjs",
|
|
43
|
+
"build:docs": "rimraf docs && mkdir docs && node ./scripts/dts-docs.cjs ./types.d.ts ./docs",
|
|
44
|
+
"docs:gpt": "npm run build && node ./tools/gpt-docs/index.mjs",
|
|
45
|
+
"docs:uml": "npm run build && node ./scripts/uml.mjs",
|
|
46
|
+
"docs:www": "rimraf docs/wwwroot && node ./tools/typedoc-packages-docs/index.mjs && typedoc && node ./tools/typedoc-yandex-metrica/index.mjs",
|
|
47
|
+
"repl": "dotenv -e .env -- npm run build && node -e \"import('./scripts/repl.mjs')\" --interactive"
|
|
48
|
+
},
|
|
49
|
+
"main": "build/index.cjs",
|
|
50
|
+
"module": "build/index.mjs",
|
|
51
|
+
"source": "src/index.ts",
|
|
52
|
+
"types": "./types.d.ts",
|
|
53
|
+
"exports": {
|
|
54
|
+
"require": "./build/index.cjs",
|
|
55
|
+
"types": "./types.d.ts",
|
|
56
|
+
"import": "./build/index.mjs",
|
|
57
|
+
"default": "./build/index.cjs"
|
|
58
|
+
},
|
|
59
|
+
"devDependencies": {
|
|
60
|
+
"@rollup/plugin-typescript": "11.1.6",
|
|
61
|
+
"@types/node": "22.9.0",
|
|
62
|
+
"glob": "11.0.1",
|
|
63
|
+
"plantuml": "0.0.2",
|
|
64
|
+
"rimraf": "6.0.1",
|
|
65
|
+
"rollup": "3.29.5",
|
|
66
|
+
"rollup-plugin-dts": "6.1.1",
|
|
67
|
+
"rollup-plugin-peer-deps-external": "2.2.4",
|
|
68
|
+
"ts-morph": "27.0.2",
|
|
69
|
+
"tslib": "2.7.0",
|
|
70
|
+
"typedoc": "0.27.9",
|
|
71
|
+
"worker-testbed": "2.0.0"
|
|
72
|
+
},
|
|
73
|
+
"peerDependencies": {
|
|
74
|
+
"typescript": "^5.0.0"
|
|
75
|
+
},
|
|
76
|
+
"dependencies": {
|
|
77
|
+
"di-kit": "^1.1.1",
|
|
78
|
+
"di-scoped": "^1.0.21",
|
|
79
|
+
"di-singleton": "^1.0.5",
|
|
80
|
+
"functools-kit": "^2.3.0",
|
|
81
|
+
"get-moment-stamp": "^2.0.0"
|
|
82
|
+
},
|
|
83
|
+
"publishConfig": {
|
|
84
|
+
"access": "public"
|
|
85
|
+
}
|
|
86
|
+
}
|