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
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-14 - Time-Series O(log n) Search Pattern
**Learning:** `src/agent/backtestRunner.ts` makes heavy use of `.filter((b) => b.time <= asOf)` to get the latest close price inside loop bounds, changing potentially O(1) loop lookups or O(log n) lookups into repetitive O(n) filtering over large arrays.
**Action:** Expose `findLastBarIndex` in `src/data/sources/dnsePublic.ts` and use binary search or track indices explicitly in tight loops.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
"pnpm": {
"onlyBuiltDependencies": [
"better-sqlite3"
]
],
"overrides": {
"undici": ">=7.28.0 <8.0.0",
"ws": ">=8.21.0"
}
},
"packageManager": "pnpm@10.33.2+sha512.a90faf6feeab71ad6c6e57f94e0fe1a12f5dcc22cd754db40ae9593eb6a3e0b6b12e3540218bb37ae083404b1f2ce6db2a4121e979829b4aff94b99f49da1cf8"
}
30 changes: 14 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions src/agent/backtestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto";
import { loadConfig } from "../config/loader.js";
import { getDb } from "../storage/db.js";
import { getBacktestBroker } from "../broker/index.js";
import { getStockOhlcv, getIndexOhlcv, type Bar } from "../data/sources/dnsePublic.js";
import { getStockOhlcv, getIndexOhlcv, findLastBarIndex, type Bar } from "../data/sources/dnsePublic.js";
import { DISCOVERY_UNIVERSE, discoverTickers } from "../tools/discover.js";
import { setActiveAsOf } from "./clock.js";
import { runTeamAnalysis } from "./team/index.js";
Expand Down Expand Up @@ -298,8 +298,8 @@ export async function runBacktestSession(
);

const vnindexAt = (asOf: number): number | null => {
const series = vnindex.filter((b) => b.time <= asOf);
return series.length ? series[series.length - 1]!.close : null;
const idx = findLastBarIndex(vnindex, asOf);
return idx >= 0 ? vnindex[idx]!.close : null;
};
const vnindexBaseline = vnindexAt(intervalTurns[0]!);
if (vnindexBaseline == null) throw new Error(`no VNINDEX data at first ${interval.label} turn`);
Expand All @@ -312,8 +312,9 @@ export async function runBacktestSession(
throwIfAborted(cb.signal);
const dateIso = ictLabel(asOf);
const priceOverride = (sym: string): number | null => {
const series = bars[sym]?.filter((b) => b.time <= asOf) ?? [];
return series.length ? series[series.length - 1]!.close : null;
const series = bars[sym] ?? [];
const idx = findLastBarIndex(series, asOf);
return idx >= 0 ? series[idx]!.close : null;
};
broker.setPriceOverride(priceOverride);
cb.onTurnStart?.({ asOf, dateIso });
Expand Down
20 changes: 20 additions & 0 deletions src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ export function seriesToBars(s: OhlcvSeries): Bar[] {
return out;
}

/**
* O(log n) binary search to find the last bar index with time <= timeSec.
* Returns -1 if no such bar exists.
*/
export function findLastBarIndex(bars: Bar[], timeSec: number): number {
let low = 0;
let high = bars.length - 1;
let ans = -1;
while (low <= high) {
const mid = (low + high) >>> 1;
if (bars[mid]!.time <= timeSec) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}

function clipBars(bars: Bar[]): Bar[] {
// Clip when an as-of clock is active (ALS or module override). When neither
// is set, fall through unchanged β€” DNSE only returns historical data anyway.
Expand Down
Loading