# ctrlcq/zerobt architecture

> Architecture diagram of the GitHub repository ctrlcq/zerobt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-29). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ctrlcq/zerobt
- Repository: https://github.com/ctrlcq/zerobt

## Overview

zerobt is a C-based event-driven trading backtester. The main workflow loads market data and configuration, runs a strategy through a language-specific frontend, simulates orders and positions, then computes and prints or exports results. The README also documents parameter sweeps and JSON/binary data support. Some implementation files were not excerpted; those connections are omitted or marked unverified rather than assumed.

Main entry points: [`src/main.c`](https://github.com/ctrlcq/zerobt/blob/main/src/main.c) (Command runner), [`src/sweep.c`](https://github.com/ctrlcq/zerobt/blob/main/src/sweep.c) (Parameter sweeps), [`src/backtest.c`](https://github.com/ctrlcq/zerobt/blob/main/src/backtest.c) (JavaScript runner), [`src/broker.c`](https://github.com/ctrlcq/zerobt/blob/main/src/broker.c) (Account state)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["CLI orchestration"]
  node_cli["Command runner<br/>[main.c]"]
end

subgraph group_inputs["Market inputs"]
  node_config["Account config<br/>[config.c]"]
  node_marketdata["Market data loader<br/>[data.c]"]
end

subgraph group_strategies["Strategy runtimes"]
  node_jsruntime["JavaScript runner<br/>[backtest.c]"]
  node_luaruntime["Lua runner<br/>[lua_run.c]"]
  node_nativeruntime["Native runner<br/>[native.c]"]
  node_capi["C strategy API<br/>[zb.c]"]
  node_jsapi["JavaScript trading API<br/>[api.c]"]
  node_indicators["Technical indicators<br/>[ta.c]"]
end

subgraph group_trading["Trading engine"]
  node_account["Account state<br/>[broker.c]"]
  node_backtest["Bar backtest loop<br/>[backtest.c]"]
  node_matching["Order matching engine<br/>[matching_engine.c]"]
end

subgraph group_analysis["Results and sweeps"]
  node_results["Stats and exports<br/>[results.c]"]
  node_sweeps["Parameter sweeps<br/>[sweep.c]"]
end

node_user(("CLI user"))
node_strategy["Strategy script or library"]

node_user -->|"runs"| node_cli
node_cli -->|"loads"| node_config
node_cli -->|"loads"| node_marketdata
node_cli -->|"initializes"| node_account
node_cli -->|"runs strategy"| node_backtest
node_cli -->|"runs sweep"| node_sweeps
node_cli -->|"calculates and writes"| node_results
node_backtest -.->|"uses per bar"| node_matching
node_jsruntime -.->|"loads JS"| node_strategy
node_luaruntime -.->|"loads Lua"| node_strategy
node_nativeruntime -.->|"loads native code"| node_strategy
node_sweeps -->|"dispatches JS"| node_jsruntime
node_sweeps -->|"dispatches Lua"| node_luaruntime
node_sweeps -->|"dispatches native"| node_nativeruntime
node_sweeps -->|"calculates stats"| node_results
node_capi -.->|"exposes trading"| node_account
node_jsapi -.->|"exposes trading"| node_account
node_jsruntime -.->|"registers API"| node_jsapi
node_jsruntime -.->|"uses indicators"| node_indicators

click node_cli "https://github.com/ctrlcq/zerobt/blob/main/src/main.c"
click node_config "https://github.com/ctrlcq/zerobt/blob/main/src/config.c"
click node_marketdata "https://github.com/ctrlcq/zerobt/blob/main/src/data.c"
click node_account "https://github.com/ctrlcq/zerobt/blob/main/src/broker.c"
click node_jsruntime "https://github.com/ctrlcq/zerobt/blob/main/src/backtest.c"
click node_luaruntime "https://github.com/ctrlcq/zerobt/blob/main/src/lua_run.c"
click node_nativeruntime "https://github.com/ctrlcq/zerobt/blob/main/src/native.c"
click node_capi "https://github.com/ctrlcq/zerobt/blob/main/src/zb.c"
click node_jsapi "https://github.com/ctrlcq/zerobt/blob/main/src/api.c"
click node_indicators "https://github.com/ctrlcq/zerobt/blob/main/src/ta.c"
click node_backtest "https://github.com/ctrlcq/zerobt/blob/main/src/backtest.c"
click node_matching "https://github.com/ctrlcq/zerobt/blob/main/src/matching_engine.c"
click node_results "https://github.com/ctrlcq/zerobt/blob/main/src/results.c"
click node_sweeps "https://github.com/ctrlcq/zerobt/blob/main/src/sweep.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_user toneBlue
class node_config,node_marketdata toneAmber
class node_jsruntime,node_luaruntime,node_nativeruntime,node_capi,node_jsapi,node_indicators toneMint
class node_account,node_backtest,node_matching toneRose
class node_results,node_sweeps,node_strategy toneIndigo
```

## Components

### CLI orchestration

- [Command runner](https://github.com/ctrlcq/zerobt/blob/main/src/main.c): `src/main.c`

### Market inputs

- [Account config](https://github.com/ctrlcq/zerobt/blob/main/src/config.c): `src/config.c`
- [Market data loader](https://github.com/ctrlcq/zerobt/blob/main/src/data.c): `src/data.c`

### Strategy runtimes

- [JavaScript runner](https://github.com/ctrlcq/zerobt/blob/main/src/backtest.c): `src/backtest.c`
- [Lua runner](https://github.com/ctrlcq/zerobt/blob/main/src/lua_run.c): `src/lua_run.c`
- [Native runner](https://github.com/ctrlcq/zerobt/blob/main/src/native.c): `src/native.c`
- [C strategy API](https://github.com/ctrlcq/zerobt/blob/main/src/zb.c): `src/zb.c`
- [JavaScript trading API](https://github.com/ctrlcq/zerobt/blob/main/src/api.c): `src/api.c`
- [Technical indicators](https://github.com/ctrlcq/zerobt/blob/main/src/ta.c): `src/ta.c`

### Trading engine

- [Account state](https://github.com/ctrlcq/zerobt/blob/main/src/broker.c): `src/broker.c`
- [Bar backtest loop](https://github.com/ctrlcq/zerobt/blob/main/src/backtest.c): `src/backtest.c`
- [Order matching engine](https://github.com/ctrlcq/zerobt/blob/main/src/matching_engine.c): `src/matching_engine.c`

### Results and sweeps

- [Stats and exports](https://github.com/ctrlcq/zerobt/blob/main/src/results.c): `src/results.c`
- [Parameter sweeps](https://github.com/ctrlcq/zerobt/blob/main/src/sweep.c): `src/sweep.c`

### Other components

- **CLI user**
- **Strategy script or library**

## Connections

- CLI user → Command runner: runs (evidence: `README.md`)
- Command runner → Account config: loads (evidence: `src/main.c`)
- Command runner → Market data loader: loads (evidence: `src/main.c`)
- Command runner → Account state: initializes (evidence: `src/main.c`)
- Command runner → Bar backtest loop: runs strategy (evidence: `src/main.c`)
- Command runner → Parameter sweeps: runs sweep (evidence: `src/main.c`)
- Command runner → Stats and exports: calculates and writes (evidence: `src/main.c`)
- Bar backtest loop → Order matching engine: uses per bar
- JavaScript runner → Strategy script or library: loads JS
- Lua runner → Strategy script or library: loads Lua
- Native runner → Strategy script or library: loads native code
- Parameter sweeps → JavaScript runner: dispatches JS (evidence: `src/sweep.c`)
- Parameter sweeps → Lua runner: dispatches Lua (evidence: `src/sweep.c`)
- Parameter sweeps → Native runner: dispatches native (evidence: `src/sweep.c`)
- Parameter sweeps → Stats and exports: calculates stats (evidence: `src/sweep.c`)
- C strategy API → Account state: exposes trading
- JavaScript trading API → Account state: exposes trading
- JavaScript runner → JavaScript trading API: registers API
- JavaScript runner → Technical indicators: uses indicators
