TSB — Backtest an AI/LLM Trading Strategy | Free, Open Source

$0.00

Category:

Description

Free · Open source (MIT) · Python CLI · Bring your own LLM

Your strategy is a paragraph, not a formula. Backtest it anyway.

DTSB backtests discretionary strategies — the kind that are judgment written in English rather than fixed rules — by asking the model bar by bar what it would have done, then scoring the trades against real price. No-lookahead point-in-time walking, anti-contamination controls, deterministic outcome scoring. Not an MCP server, not a live-trading bot: it generates and scores historical signals only.

What you actually write

“Buy pullbacks in an uptrend.”

a persona, in plain English

“Fade RSI extremes in a range.”

needs: [rsi14, adx14]

“Don't even convene unless ADX is above 25.”

triggers: adx_min

“One persona, always convenes — keep it simple.”

strategies/solo-swing

“Three personas, each gated by its own triggers, weighted consensus at 0.6.”

strategies/mini-council

“Only take it when conviction clears my floor.”

min_conviction

A strategy is a directory: one strategy.yaml and one markdown file per persona. The persona's front matter names the indicators it needs and the scout triggers that decide when it convenes; the body is your method, written the way you'd explain it to a trader. That body becomes the prompt the model reasons from — there is no Python to write, and no equation to reduce your read to. Two working examples ship with the repo.

Why this is more than looping an LLM over some bars

Failure modeWhat goes wrongDTSB's control
Training-data contaminationThe model may have seen the test period during training and “predicts” the future by remembering itPackets are anonymized (symbol → alias, prices scaled, dates shifted) before the model ever sees them, plus an explicit contamination probe
LookaheadUsing bar data that wouldn't have existed yet at decision timeA point-in-time walker builds each packet from only bars closed by that instant — unit-tested
Self-gradingThe LLM decides both the trade AND whether it wonThe LLM only generates ballots; a deterministic walker scores outcomes against real bars
Non-determinismThe same prompt can return a different verdict on repeatA variance probe measures the flip rate and reports it as a noise band
Overfitting / small samplesMonths of LLM-scale data ≠ statistical powerChronological train/test split; bootstrap confidence intervals on the test split only

These are the ways an LLM backtest goes wrong that a mechanical one never fails on — a Strategy Tester can't be contaminated by its own training data, because it doesn't have any. Full rationale in docs/LLM-VS-MECHANICAL.md; architecture in docs/DESIGN.md.

A run, end to end

  1. Sanity checkdriver --config dtsb.yaml --check-deps confirms the data provider and the model endpoint are both actually reachable before you spend anything.
  2. A small first rundriver --config dtsb.yaml --limit 20 walks 20 decision points, so you find out your prompt returns valid ballots on twenty calls, not two thousand.
  3. Export the signalsbridge <run_id> writes a signal CSV, if you want the raw trades elsewhere. Optional.
  4. Score itscore <run_id> computes expectancy, profit factor and bootstrap confidence intervals.
  5. Read the verdictreport <run_id> writes data/runs/<run_id>/report.md.

Every report carries an honesty certificate

Alongside the expectancy and profit-factor numbers, report.md states how much the run itself can be trusted: scout recall (what the triggers filtered out), gate miss rate, ballot validity, and — once you run the probes — vote variance and contamination.

A result without its certificate isn't a result. The numbers and the reasons to doubt them are printed in the same file, on purpose — you can't quote one without seeing the other.

Data in, model in

Price data providers

ProviderWhat it needs
csvYour own OHLCV files — header time,open,high,low,close,volume. No key, no account, no network.
tradingviewA TVR_API_KEY in .env, via tvremix.xyz
mt5A MetaTrader 5 terminal — pip install MetaTrader5, Windows only

LLM engines

EngineWhat it talks to
openai_compatAny OpenAI-compatible endpoint — Ollama, LM Studio, vLLM, OpenAI itself, a paid model. Your key, your bill, your choice.
openrouterA pool of free OpenRouter models, health-checked automatically

The openrouter engine probes candidate models with a real ballot-shaped prompt rather than a trivial ping — some models answer a hello fine and still fail to follow a “respond with only JSON” instruction. It remembers which ones came back healthy, and learns your actual daily free-request cap from OpenRouter's own 429 response instead of assuming a number.

Setting it up

Quickstart A — zero API keys

Your own CSV files, a model running locally on your own machine. Nothing to sign up for, nothing to pay, no rate limit but your GPU:

pip install -r requirements.txtcp .env.example .env (nothing to fill in for this path) → cp config.example.yaml dtsb.yaml

Drop your OHLCV data in ./data/csv/, point the engine at your local Ollama base_url, and run the driver. That's the whole setup.

Quickstart B — TradingView data + a free hosted model: set provider: tradingview with a TVR_API_KEY, and kind: openrouter with an OPENROUTER_API_KEY, both in .env. Still no paid model required.

Requirements

  • Python, and one dependency — pyyaml
  • Price data: your own CSVs, a tvremix key, or MetaTrader 5
  • An LLM endpoint — local, free-tier or paid
  • MetaTrader5 only if you use the mt5 provider (Windows)

Source & support

MIT licensed, fully open on GitHub — nothing compiled, nothing to trust blindly. The test suite runs without network access, with all HTTP mocked. DTSB is signals-only: it never places a trade and never touches a live account.

View source, issues & updates on GitHub

Honest warnings

  • Results are pinned to one model version — a model upgrade invalidates the estimate until you re-run.
  • Confidence intervals from a few months of history are wide. If the CI spans zero R, the report says so plainly rather than overclaiming an edge.
  • Anonymization reduces contamination risk. It does not prove zero.
  • A backtest — mechanical or LLM — is still a backtest. Regime change and execution reality aren't in the numbers.
These are the repo's own warnings, carried over word for word. A tool that measures whether your judgment has an edge is only worth having if it's willing to tell you it doesn't.

If your strategy is mechanical instead

MBT — MT5 Backtest Toolkit

Free · Open source

DTSB exists for strategies with no equation to test — judgment, written out. If yours is already an indicator or an EA, it has one, and MBT is the better tool: it backtests through Claude by replaying the indicator's own logged decisions against real broker price data. Same principle, opposite kind of strategy.

Get MBT

Reviews

There are no reviews yet.

Be the first to review “TSB — Backtest an AI/LLM Trading Strategy | Free, Open Source”

Your email address will not be published. Required fields are marked *