Skip to main content

How Backtests Work

Entry Point

Run:

npx @tradejs/cli backtest

Local infra prerequisite:

npx @tradejs/cli infra-init
npx @tradejs/cli infra-up

infra-init creates docker-compose.dev.yml once and keeps user changes if file already exists. infra-up starts Redis + PostgreSQL/Timescale using that file. Stop infra after work with npx @tradejs/cli infra-down.

Main files:

  • script: @tradejs/cli
  • worker runtime: @tradejs/node

Real CLI Flags (from code)

args.option(['c', 'config'], 'Backtest config', 'breakout');
args.option(['n', 'tests'], 'Tests limit', TESTS_LIMIT);
args.option(['p', 'parallel'], 'Parallel tasks', MAX_PARALLEL);
args.option('connector', 'Connector/provider', 'bybit');
args.option(['m', 'ml'], 'Write ML dataset rows', false);
args.option(['A', 'ai'], 'Write AI prompt rows', false);

Example run for TrendLine-like setup:

npx @tradejs/cli backtest --config trendline --connector bybit --tests 500 --parallel 4 --ml --ai

Pipeline

  1. Resolve backtest config from Redis (users:<user>:backtests:configs:<config>).
  2. Load symbols from selected connector.
  3. Refresh candle cache (unless --cacheOnly).
  4. Build test-suite grid.
  5. Split suite into chunks and run worker processes.
  6. Aggregate stats and store top results.

Real Worker Processing Pattern

import { testing } from '@tradejs/node/backtest';

for await (const test of testSuite) {
const testResult = await testing(test);
process.send?.({
stat: testResult.stat,
orderLogId: testResult.orderLogId,
test,
});
}

ML Dataset During Backtest

Enable ML rows writing:

npx @tradejs/cli backtest --ml

Workers write chunk files:

  • ml-dataset-<strategy>-chunk-<chunkId>.jsonl

Later, merge with:

npx @tradejs/cli ml-export

AI Prompt Dataset During Backtest

Enable AI prompt dataset writing:

npx @tradejs/cli backtest --ai

Workers write chunk files:

  • ai-dataset-<strategy>-chunk-<chunkId>.jsonl

Later, merge and replay with:

npx @tradejs/cli ai-export
npx @tradejs/cli ai-train -n 50 --minQuality 4

UI Note

The built-in Next.js app is not yet distributed as a public external package. For external package users, the supported backtest flow is CLI-first.