Skip to content
Draft
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-07-19 - Fast Time-Series Lookups via Binary Search
**Learning:** The `backtestRunner` loops repeatedly over historical time-series data. Previous logic used `.filter((b) => b.time <= asOf)` to get the price of a stock at a specific turn. This caused an $O(N)$ operation inside an already heavy simulation loop, leading to $O(T \times N)$ time complexity for $T$ turns.
**Action:** Since time-series bar data (like `OHLCV`) is naturally sorted chronologically, use `findLastBarIndex` (binary search) to retrieve the required index in $O(\log N)$ time. I have added this reusable utility to `src/data/sources/dnsePublic.ts`. Prefer this utility over full array slicing/filtering for performance-critical lookups on time-sorted arrays.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"vitest": "^2.1.3"
},
"pnpm": {
"overrides": {
"undici": ">=7.28.0 <8.0.0",
"ws": ">=8.21.0"
},
"onlyBuiltDependencies": [
"better-sqlite3"
]
Expand Down
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.

12 changes: 7 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 !== -1 ? 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,10 @@ 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];
if (!series || series.length === 0) return null;
const idx = findLastBarIndex(series, asOf);
return idx !== -1 ? series[idx]!.close : null;
};
broker.setPriceOverride(priceOverride);
cb.onTurnStart?.({ asOf, dateIso });
Expand Down
25 changes: 24 additions & 1 deletion src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ export interface Bar {
volume: number;
}

/**
* Finds the index of the last bar with time <= targetTime.
* Requires the bars array to be sorted chronologically by time.
* Returns -1 if no such bar exists.
*/
export function findLastBarIndex(bars: Bar[], targetTime: number): number {
let left = 0;
let right = bars.length - 1;
let ans = -1;
while (left <= right) {
const mid = Math.floor((left + right) / 2);
if (bars[mid]!.time <= targetTime) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return ans;
}

async function fetchOhlcs(
kind: "stock" | "index",
symbol: string,
Expand Down Expand Up @@ -68,7 +89,9 @@ function clipBars(bars: Bar[]): Bar[] {
asOfClock.getStore()?.asOfSec != null || isAsOfOverridden();
if (!hasOverride) return bars;
const asOf = nowSec();
return bars.filter((b) => b.time <= asOf);
const idx = findLastBarIndex(bars, asOf);
if (idx === -1) return [];
return bars.slice(0, idx + 1);
}

export async function getStockOhlcv(
Expand Down
Loading