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 @@
## 2023-10-24 - [O(N) to O(log N) for Chronological Lookups]
**Learning:** Found a major bottleneck in `src/agent/backtestRunner.ts` where large OHLCV market data arrays were being filtered iteratively with `array.filter((b) => b.time <= asOf)` to find the latest available bar. Given these arrays are natively sorted chronologically, doing an O(N) filter on thousands of bars in the inner loop (e.g. interval turns for all backtest symbols) scales poorly.
**Action:** Created and used `findLastBarIndex`, a reusable O(log N) binary search utility, when extracting subset views or current elements of OHLCV bars. Applied this specifically to `clipBars`, `vnindexAt`, and `priceOverride` to improve backtesting engine performance.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@
"dependencies": {
"@anthropic-ai/claude-agent-sdk": "^0.1.0",
"better-sqlite3": "^11.5.0",
"cheerio": "^1.0.0",
"ink": "^5.0.1",
"cheerio": "^1.2.0",
"ink": "^5.2.1",
"ink-spinner": "^5.0.0",
"ink-text-input": "^6.0.0",
"react": "^18.3.1",
"technicalindicators": "^3.1.0",
"undici": "^6.20.0",
"undici": "^7.28.0",
"yaml": "^2.6.0",
"zod": "^3.23.8"
},
Expand Down
30 changes: 12 additions & 18 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, type Bar, findLastBarIndex } 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 symBars = bars[sym];
if (!symBars || symBars.length === 0) return null;
const idx = findLastBarIndex(symBars, asOf);
return idx !== -1 ? symBars[idx]!.close : null;
};
broker.setPriceOverride(priceOverride);
cb.onTurnStart?.({ asOf, dateIso });
Expand Down
24 changes: 23 additions & 1 deletion src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,36 @@ export function seriesToBars(s: OhlcvSeries): Bar[] {
return out;
}

/**
* Binary search to find the last bar index with time <= targetTime.
* Assumes the bars array is chronologically sorted.
*/
export function findLastBarIndex(bars: Bar[], targetTime: 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 <= targetTime) {
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.
const hasOverride =
asOfClock.getStore()?.asOfSec != null || isAsOfOverridden();
if (!hasOverride) return bars;
const asOf = nowSec();
return bars.filter((b) => b.time <= asOf);
const index = findLastBarIndex(bars, asOf);
if (index === -1) return [];
return bars.slice(0, index + 1);
}

export async function getStockOhlcv(
Expand Down
Loading