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
29 changes: 28 additions & 1 deletion components/day-v3/DayV3Backtest.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
import assert from "node:assert/strict";

import { formatDayV3MonthlyReturn } from "@/components/day-v3/DayV3Backtest";
import {
buildDayV3BacktestWindows,
formatDayV3MonthlyReturn,
} from "@/components/day-v3/DayV3Backtest";

assert.equal(formatDayV3MonthlyReturn(0.01234), "+1.23%");
assert.equal(formatDayV3MonthlyReturn(-0.01234), "-1.23%");
assert.equal(formatDayV3MonthlyReturn(Number.POSITIVE_INFINITY), "N/A");
assert.equal(formatDayV3MonthlyReturn(Number.NEGATIVE_INFINITY), "N/A");
assert.equal(formatDayV3MonthlyReturn(Number.NaN), "N/A");

const windows = buildDayV3BacktestWindows(
[
{ date: "2022-12-30" },
{ date: "2023-01-03" },
{ date: "2023-06-30" },
{ date: "2024-01-02" },
{ date: "2024-12-31" },
{ date: "2025-01-02" },
],
{
id: "2023-2024",
label: "2023–2024",
from: "2023-01-01",
to: "2024-12-31",
},
);
assert.deepEqual(windows[0], {
id: "2023-2024",
label: "2023–2024",
from: 1,
to: 5,
});
assert.equal(windows[1].id, "full");

console.log("Day V3 non-finite monthly return presentation: PASS");
88 changes: 59 additions & 29 deletions components/day-v3/DayV3Backtest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,60 @@ export const formatDayV3MonthlyReturn = (value: number) =>
? `${value < 0 ? "-" : "+"}${Math.abs(value * 100).toFixed(2)}%`
: "N/A";

export type DayV3BacktestWindowPreset = {
id: string;
label: string;
from: string;
to: string;
};

type DayV3BacktestWindow = {
id: string;
label: string;
from: number;
to: number;
};

/** Resolve display windows against dated source observations only. */
export function buildDayV3BacktestWindows(
series: readonly { date: string }[],
preset?: DayV3BacktestWindowPreset | null,
): DayV3BacktestWindow[] {
if (series.length < 3) return [];
const lastTime = Date.parse(series[series.length - 1].date);
const rolling = [
{ id: "full", label: "Full history", months: 0 },
{ id: "24m", label: "Last 24 months", months: 24 },
{ id: "12m", label: "Last 12 months", months: 12 },
{ id: "6m", label: "Last 6 months", months: 6 },
{ id: "3m", label: "Last 3 months", months: 3 },
]
.map((span) => {
if (span.months === 0) {
return { id: span.id, label: span.label, from: 0, to: series.length };
}
const cutoff = lastTime - span.months * 30.44 * 86_400_000;
const from = series.findIndex(
(point) => Date.parse(point.date) >= cutoff,
);
return { id: span.id, label: span.label, from, to: series.length };
})
// A window needs enough points to be a run rather than a line between two
// dots, and one that covers the whole series is just "full history" twice.
.filter((span) => span.from >= 0 && span.to - span.from >= 3)
.filter((span, index, all) => index === 0 || span.from !== all[0].from);

if (!preset) return rolling;
const from = series.findIndex((point) => point.date >= preset.from);
const firstAfter = series.findIndex((point) => point.date > preset.to);
const to = firstAfter < 0 ? series.length : firstAfter;
if (from < 0 || to - from < 3) return rolling;
return [{ id: preset.id, label: preset.label, from, to }, ...rolling];
}

function DayV3Backtest({
bandPct: bandInput,
windowOption,
coveragePct: coverageInput,
customSource,
liqY0Pct: liqY0Input,
Expand All @@ -80,6 +132,7 @@ function DayV3Backtest({
sourceApyPct: sourceInput,
}: {
bandPct: number;
windowOption?: DayV3BacktestWindowPreset | null;
coveragePct: number;
customSource: boolean;
liqY0Pct: number;
Expand Down Expand Up @@ -159,37 +212,14 @@ function DayV3Backtest({
[market.series, sourceApyPct],
);

const windows = useMemo(() => {
if (series.length < 3) return [];
const last = series[series.length - 1];
const lastTime = Date.parse(last.date);
const spans: { id: string; label: string; months: number }[] = [
{ id: "full", label: "Full history", months: 0 },
{ id: "24m", label: "Last 24 months", months: 24 },
{ id: "12m", label: "Last 12 months", months: 12 },
{ id: "6m", label: "Last 6 months", months: 6 },
{ id: "3m", label: "Last 3 months", months: 3 },
];
return (
spans
.map((span) => {
if (span.months === 0) return { ...span, from: 0 };
const cutoff = lastTime - span.months * 30.44 * 86_400_000;
const from = series.findIndex(
(point) => Date.parse(point.date) >= cutoff,
);
return { ...span, from: from < 0 ? -1 : from };
})
// A window needs enough points to be a run rather than a line between two
// dots, and one that covers the whole series is just "full history" twice.
.filter((span) => span.from >= 0 && series.length - span.from >= 3)
.filter((span, index, all) => index === 0 || span.from !== all[0].from)
);
}, [series]);
const windows = useMemo(
() => buildDayV3BacktestWindows(series, windowOption),
[series, windowOption],
);

const active = windows.find((span) => span.id === windowId) ?? windows[0];
const view = useMemo(
() => (active ? series.slice(active.from) : series),
() => (active ? series.slice(active.from, active.to) : series),
[active, series],
);

Expand Down Expand Up @@ -555,7 +585,7 @@ function DayV3Backtest({
{windows.map((span) => (
<option key={span.id} value={span.id}>
{span.label} ({series[span.from].date} to{" "}
{series[series.length - 1].date})
{series[span.to - 1].date})
</option>
))}
</select>
Expand Down
88 changes: 55 additions & 33 deletions components/day-v3/DayV3PremiumCurveEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const markup = renderToStaticMarkup(
liqY100Pct={15}
liqYtPct={5}
juniorModeledApy={0.085}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={80}
riskY0Pct={2}
riskY100Pct={16}
Expand All @@ -26,13 +28,22 @@ const markup = renderToStaticMarkup(

assert.match(markup, /Yield split/);
assert.match(markup, /aria-expanded="false"/);
assert.match(markup, /Jr 13\.0% · SLP 5\.0% at 90\.0% utilization/);
assert.match(
markup,
/Jr 2\.0% \/ 16\.0% · SLP 1\.0% \/ 15\.0% · Y0 \/ Y100/,
);
assert.match(markup, /Section status: Set/);
assert.doesNotMatch(markup, /See .*impact/i);
assert.match(markup, /Junior yield share/);
assert.match(markup, /SLP yield share/);
assert.match(markup, /Jr share at 90% utilization/);
assert.match(markup, /SLP share at 90% utilization/);
assert.match(markup, /Junior risk yield curve/);
assert.match(markup, /SLP liquidity yield curve/);
for (const label of [
"Jr Y0 · 0% utilization",
"Jr Y100 · 100% utilization",
"SLP Y0 · 0% utilization",
"SLP Y100 · 100% utilization",
]) {
assert.match(markup, new RegExp(label));
}
assert.match(markup, /Projected Jr APY/);
assert.match(markup, /Projected SLP APY/);
assert.doesNotMatch(markup, /Exact E-CLP sizing/);
Expand All @@ -45,18 +56,16 @@ assert.match(markup, /8\.5%/);
assert.match(markup, /2\.4%/);
assert.match(markup, /Updates from the shared accountant/);
assert.match(markup, /Reset yield split/);
assert.equal(markup.match(/type="range"/g)?.length, 2);
assert.equal(markup.match(/aria-valuetext=/g)?.length, 2);
assert.equal(markup.match(/step="0.1"/g)?.length, 2);
assert.equal(markup.match(/type="range"/g)?.length, 4);
assert.equal(markup.match(/aria-valuetext=/g)?.length, 4);
assert.equal(markup.match(/step="0.1"/g)?.length, 4);
assert.doesNotMatch(markup, / YT /);

// The unified editor exposes only the operating-target shares. The parent
// still supplies the derived zero/full anchors so they can bound each target
// slider without moving any curve or accountant math into this component.
// The unified editor exposes only the two endpoints for each curve. The parent
// retains the fixed YT contract anchors and accountant routing.
assert.doesNotMatch(markup, /Simple/);
assert.doesNotMatch(markup, /Advanced/);
assert.doesNotMatch(markup, /Curve shape anchors/);
assert.doesNotMatch(markup, /No coverage used \(Y0\)/);
assert.doesNotMatch(markup, /All liquidity used \(Y100\)/);
assert.doesNotMatch(markup, /Confirm suggested curves/);
assert.doesNotMatch(markup, /Reset to suggested curves/);
assert.doesNotMatch(markup, /Live pool validation/);
Expand All @@ -70,9 +79,11 @@ const invalidMarkup = renderToStaticMarkup(
liqY100Pct={15}
liqYtPct={5}
juniorModeledApy={0.085}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={80}
riskY0Pct={2}
riskY100Pct={16}
Expand All @@ -95,9 +106,11 @@ const slpOnlyMarkup = renderToStaticMarkup(
liqY100Pct={15}
liqYtPct={5}
juniorModeledApy={0}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={0}
riskY0Pct={0}
riskY100Pct={0}
Expand All @@ -107,9 +120,9 @@ const slpOnlyMarkup = renderToStaticMarkup(
/>,
);
assert.match(slpOnlyMarkup, /Compare how source yield is shared with SLP/);
assert.match(slpOnlyMarkup, /SLP yield share/);
assert.doesNotMatch(slpOnlyMarkup, /Junior yield share/);
assert.equal(slpOnlyMarkup.match(/type="range"/g)?.length, 1);
assert.match(slpOnlyMarkup, /SLP liquidity yield curve/);
assert.doesNotMatch(slpOnlyMarkup, /Junior risk yield curve/);
assert.equal(slpOnlyMarkup.match(/type="range"/g)?.length, 2);

const juniorOnlyMarkup = renderToStaticMarkup(
<DayV3PremiumCurveEditor
Expand All @@ -119,9 +132,11 @@ const juniorOnlyMarkup = renderToStaticMarkup(
liqY100Pct={0}
liqYtPct={0}
juniorModeledApy={0.085}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={80}
riskY0Pct={2}
riskY100Pct={16}
Expand All @@ -132,9 +147,9 @@ const juniorOnlyMarkup = renderToStaticMarkup(
/>,
);
assert.match(juniorOnlyMarkup, /Compare how source yield is shared with Junior/);
assert.match(juniorOnlyMarkup, /Junior yield share/);
assert.doesNotMatch(juniorOnlyMarkup, /SLP yield share/);
assert.equal(juniorOnlyMarkup.match(/type="range"/g)?.length, 1);
assert.match(juniorOnlyMarkup, /Junior risk yield curve/);
assert.doesNotMatch(juniorOnlyMarkup, /SLP liquidity yield curve/);
assert.equal(juniorOnlyMarkup.match(/type="range"/g)?.length, 2);

const bothOffMarkup = renderToStaticMarkup(
<DayV3PremiumCurveEditor
Expand All @@ -145,9 +160,11 @@ const bothOffMarkup = renderToStaticMarkup(
liqY100Pct={0}
liqYtPct={0}
juniorModeledApy={0}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={0}
riskY0Pct={0}
riskY100Pct={0}
Expand All @@ -167,9 +184,11 @@ const unresolvedPoolSlpMarkup = renderToStaticMarkup(
liqY100Pct={15}
liqYtPct={5}
juniorModeledApy={0.085}
onLiqYtPct={ignore}
onLiqY0Pct={ignore}
onLiqY100Pct={ignore}
onResetCurve={ignore}
onRiskYtPct={ignore}
onRiskY0Pct={ignore}
onRiskY100Pct={ignore}
riskCapPct={80}
riskY0Pct={2}
riskY100Pct={16}
Expand All @@ -178,9 +197,12 @@ const unresolvedPoolSlpMarkup = renderToStaticMarkup(
targetUtilization={0.9}
/>,
);
assert.match(unresolvedPoolSlpMarkup, /Jr 13\.0% · SLP 5\.0%/);
assert.match(
unresolvedPoolSlpMarkup,
/Jr 2\.0% \/ 16\.0% · SLP 1\.0% \/ 15\.0%/,
);
assert.match(unresolvedPoolSlpMarkup, /Section status: Set/);
assert.doesNotMatch(unresolvedPoolSlpMarkup, /awaiting exit validation/);
assert.equal(unresolvedPoolSlpMarkup.match(/type="range"/g)?.length, 2);
assert.equal(unresolvedPoolSlpMarkup.match(/type="range"/g)?.length, 4);

console.log("Day V3 unified premium-curve editor: PASS");
Loading
Loading