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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: pnpm

- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: pnpm
registry-url: https://registry.npmjs.org

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
node-version: 22
cache: pnpm

- name: Install dependencies
Expand Down
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-06-25 - Time-Series Market Data Filtering Bottleneck
**Learning:** Chronologically sorted market data arrays (`Bar[]`) were being repeatedly traversed with O(n) `.filter()` calls inside the backtest runner. For intervals over long periods, this creates a massive performance bottleneck.
**Action:** Replace O(n) array `.filter()` on sorted time-series arrays with O(log n) binary search (`findLastBarIndex`) and `.slice()` or direct array indexing where only the latest elements are needed. This provides a dramatic performance improvement especially during backtesting.
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@
"yaml": "^2.6.0",
"zod": "^3.23.8"
},
"pnpm": {
"overrides": {
"ws": "^8.21.0"
},
"onlyBuiltDependencies": [
"better-sqlite3"
]
},
"devDependencies": {
"@changesets/cli": "^2.31.0",
"@types/better-sqlite3": "^7.6.11",
Expand All @@ -75,10 +83,5 @@
"typescript": "^5.6.3",
"vitest": "^2.1.3"
},
"pnpm": {
"onlyBuiltDependencies": [
"better-sqlite3"
]
},
"packageManager": "pnpm@10.33.2+sha512.a90faf6feeab71ad6c6e57f94e0fe1a12f5dcc22cd754db40ae9593eb6a3e0b6b12e3540218bb37ae083404b1f2ce6db2a4121e979829b4aff94b99f49da1cf8"
}
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

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

15 changes: 9 additions & 6 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 @@ -101,8 +101,10 @@ function parseBacktestInterval(value: string | undefined): { label: string; minu
}

function intervalCloses(vnindexBars: Bar[], startSec: number, endSec: number, intervalMinutes: number): number[] {
const startIdx = findLastBarIndex(vnindexBars, startSec - 1) + 1;
const endIdx = findLastBarIndex(vnindexBars, endSec);
const base = vnindexBars
.filter((b) => b.time >= startSec && b.time <= endSec)
.slice(startIdx, endIdx + 1)
.map((b) => b.time)
.sort((a, b) => a - b);
const step = Math.max(1, Math.round(intervalMinutes / 30));
Expand Down Expand Up @@ -298,8 +300,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 lastIdx = findLastBarIndex(vnindex, asOf);
return lastIdx >= 0 ? vnindex[lastIdx]!.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 +314,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 symBars = bars[sym] ?? [];
const lastIdx = findLastBarIndex(symBars, asOf);
return lastIdx >= 0 ? symBars[lastIdx]!.close : null;
};
broker.setPriceOverride(priceOverride);
cb.onTurnStart?.({ asOf, dateIso });
Expand Down
20 changes: 19 additions & 1 deletion src/data/sources/dnsePublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ async function fetchOhlcs(
return (await body.json()) as OhlcvSeries;
}

export function findLastBarIndex(bars: Bar[], maxTime: number): number {
let low = 0;
let high = bars.length - 1;
let ans = -1;
while (low <= high) {
const mid = Math.floor((low + high) / 2);
if (bars[mid]!.time <= maxTime) {
ans = mid;
low = mid + 1;
} else {
high = mid - 1;
}
}
return ans;
}

export function seriesToBars(s: OhlcvSeries): Bar[] {
const out: Bar[] = [];
for (let i = 0; i < s.t.length; i++) {
Expand All @@ -68,7 +84,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