"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(tearsheet): keep month-boundary bars in their own calendar month
Haozhe Wu7925bd071750
diff --git a/frontend/src/lib/__tests__/tearsheet.test.ts b/frontend/src/lib/__tests__/tearsheet.test.tsindex 5598214eb..ef82dd844 100644--- a/frontend/src/lib/__tests__/tearsheet.test.ts+++ b/frontend/src/lib/__tests__/tearsheet.test.ts@@ -252,3 +252,56 @@ describe("toDrawdownZones", () => {expect(toDrawdownZones([], "2024-01-01")).toEqual([]);});});++// Daily backtests write date-only timestamps — pandas drops `00:00:00` when+// every bar is midnight — and a bare "YYYY-MM-DD" parses as UTC while the+// getters read local time. West of UTC that moved every month-boundary bar+// into the previous month, so the heatmap was wrong for the Americas and right+// in CI (UTC) and in UTC+8. These run under a pinned negative-offset zone.+describe("calendar attribution west of UTC", () => {+ const originalTz = process.env.TZ;+ beforeAll(() => {+ process.env.TZ = "America/New_York";+ });+ afterAll(() => {+ process.env.TZ = originalTz;+ });++ it("confirms the pinned zone is actually in effect", () => {+ expect(new Date(2024, 1, 1).getTimezoneOffset()).toBe(300);+ });++ const daily = [+ pt("2024-01-30", 100),+ pt("2024-01-31", 110), // true January close+ pt("2024-02-01", 111),+ pt("2024-02-29", 120), // true February close+ pt("2024-03-01", 121),+ pt("2024-03-28", 130),+ ];++ it("keeps a month-boundary bar in its own month", () => {+ const monthly = computeMonthlyReturns(daily);+ expect(monthly.map((m) => `${m.year}-${m.month}`)).toEqual(["2024-1", "2024-2", "2024-3"]);+ // January: 110/100 - 1, from the January close — not February 1st's 111.+ expect(monthly[0].ret).toBeCloseTo(110 / 100 - 1, 12);+ // February: 120/110 - 1, both true month closes.+ expect(monthly[1].ret).toBeCloseTo(120 / 110 - 1, 12);+ expect(monthly[2].ret).toBeCloseTo(130 / 120 - 1, 12);+ });++ it("keeps a year-boundary bar in its own year", () => {+ const across = [pt("2023-12-29", 100), pt("2023-12-31", 120), pt("2024-01-01", 121), pt("2024-12-31", 150)];+ const annual = computeAnnualReturns(across);+ expect(annual.map((a) => a.year)).toEqual([2023, 2024]);+ expect(annual[0].ret).toBeCloseTo(120 / 100 - 1, 12);+ expect(annual[1].ret).toBeCloseTo(150 / 120 - 1, 12);+ });++ it("measures drawdown episode spans in whole days", () => {+ const episodes = computeTopDrawdowns([pt("2024-03-01", 100), pt("2024-03-11", 80), pt("2024-03-21", 100)], 1);+ expect(episodes).toHaveLength(1);+ expect(episodes[0].peakToTroughDays).toBe(10);+ expect(episodes[0].troughToRecoveryDays).toBe(10);+ });+});diff --git a/frontend/src/lib/tearsheet.ts b/frontend/src/lib/tearsheet.tsindex 11a2d46ab..ab3537a8a 100644--- a/frontend/src/lib/tearsheet.ts+++ b/frontend/src/lib/tearsheet.ts@@ -38,9 +38,21 @@ export interface DrawdownZone {rank: number;}-/** Safari-safe date parse: "YYYY-MM-DD HH:MM:SS" -> "YYYY-MM-DDTHH:MM:SS". */+const DATE_ONLY = /^\d{4}-\d{2}-\d{2}$/;++/**+ * Safari-safe date parse: "YYYY-MM-DD HH:MM:SS" -> "YYYY-MM-DDTHH:MM:SS".+ *+ * A bare "YYYY-MM-DD" is specified to parse as UTC midnight while every getter+ * below (`getFullYear`, `getMonth`) reads local time, so west of UTC the whole+ * series shifts back a day and each month-boundary bar lands in the previous+ * month. Daily backtests write exactly that shape — pandas drops `00:00:00`+ * when every bar is midnight — so pin date-only input to local midnight, which+ * is also how the "YYYY-MM-DD HH:MM:SS" form already parses.+ */function parseTime(time: string): Date | null {- const d = new Date(time.replace(" ", "T"));+ const normalized = DATE_ONLY.test(time) ? `${time}T00:00:00` : time.replace(" ", "T");+ const d = new Date(normalized);return Number.isNaN(d.getTime()) ? null : d;}