@backtest-kit/cli 9.5.0 → 9.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/README.md +1854 -1738
  2. package/build/index.cjs +302 -207
  3. package/build/index.mjs +302 -207
  4. package/config/notification.config.mjs +13 -13
  5. package/config/symbol.config.mjs +460 -460
  6. package/docker/.env.example +2 -2
  7. package/docker/README.md +1 -0
  8. package/docker/content/feb_2026/feb_2026.strategy.ts +11 -11
  9. package/docker/content/feb_2026/modules/backtest.module.ts +83 -83
  10. package/docker/docker-compose.yaml +46 -46
  11. package/docker/package.json +38 -38
  12. package/docker/tsconfig.json +36 -36
  13. package/package.json +126 -126
  14. package/template/average-buy.mustache +22 -22
  15. package/template/breakeven.mustache +21 -21
  16. package/template/cancel-scheduled.mustache +14 -14
  17. package/template/cancelled.mustache +21 -21
  18. package/template/close-pending.mustache +16 -16
  19. package/template/closed.mustache +23 -23
  20. package/template/opened.mustache +22 -22
  21. package/template/partial-loss.mustache +22 -22
  22. package/template/partial-profit.mustache +22 -22
  23. package/template/project/README.md +1 -0
  24. package/template/project/config/symbol.config.ts +460 -460
  25. package/template/project/package.mustache +28 -28
  26. package/template/risk.mustache +19 -19
  27. package/template/scheduled.mustache +22 -22
  28. package/template/signal-close.mustache +22 -22
  29. package/template/signal-info.mustache +20 -20
  30. package/template/signal-open.mustache +22 -22
  31. package/template/source/CLAUDE.md +160 -160
  32. package/template/trailing-stop.mustache +21 -21
  33. package/template/trailing-take.mustache +21 -21
  34. package/types.d.ts +1 -6
package/README.md CHANGED
@@ -1,1738 +1,1854 @@
1
- <img src="https://github.com/tripolskypetr/backtest-kit/raw/refs/heads/master/assets/square_compasses.svg" height="45px" align="right">
2
-
3
- # 📟 @backtest-kit/cli
4
-
5
- > Zero-boilerplate CLI for launching backtests, paper trading, and live trading. Run any backtest-kit strategy from the command line — no setup code required.
6
-
7
- ![screenshot](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot16.png)
8
-
9
- [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tripolskypetr/backtest-kit)
10
- [![npm](https://img.shields.io/npm/v/@backtest-kit/cli.svg?style=flat-square)](https://npmjs.org/package/@backtest-kit/cli)
11
- [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue)]()
12
-
13
- Point the CLI at your strategy file, choose a mode, and it handles exchange connectivity, candle caching, UI dashboard, and Telegram notifications for you.
14
-
15
- 📚 **[Backtest Kit Docs](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html)** | 🌟 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
16
-
17
- > **New to backtest-kit?** The fastest way to get a real, production-ready setup is to clone the [reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example) — a fully working news-sentiment AI trading system with LLM forecasting, multi-timeframe data, and a documented February 2026 backtest. Start there instead of from scratch.
18
-
19
- ## Features
20
-
21
- - 🚀 **Zero Config**: Run `npx @backtest-kit/cli --backtest ./strategy.mjs` — no boilerplate needed
22
- - 🔄 **Four Modes**: Backtest on historical data, walker A/B comparison, paper trade on live prices, or deploy live bots
23
- - 💾 **Auto Candle Cache**: Warms OHLCV cache for all required intervals before backtest starts
24
- - 🌐 **Web Dashboard**: Launch `@backtest-kit/ui` with a single `--ui` flag
25
- - 📬 **Telegram Alerts**: Send formatted trade notifications with charts via `--telegram`
26
- - 🔌 **Default Binance**: CCXT Binance exchange schema registered automatically when none is provided
27
- - 🧩 **Module Hooks**: Drop a `live.module.mjs`, `paper.module.mjs`, or `backtest.module.mjs` to register a `Broker` adapter. No manual wiring needed.
28
- - 🗃️ **Transactional Live Orders**: Broker adapter intercepts every trade mutation before internal state changes — exchange rejection rolls back the operation atomically.
29
- - 🔑 **Pluggable Logger**: Override the built-in logger with `setLogger()` from your strategy module
30
- - 🛑 **Graceful Shutdown**: SIGINT stops the active run and cleans up all subscriptions safely
31
-
32
- ## 📋 What It Does
33
-
34
- `@backtest-kit/cli` wraps the `backtest-kit` engine and resolves all scaffolding automatically:
35
-
36
- | Mode | Command Line Args | Description |
37
- |------------------|----------------------------|----------------------------------------------|
38
- | **Backtest** | `--backtest` | Run strategy on historical candle data |
39
- | **Walker** | `--walker` | A/B compare multiple strategies on the same historical data |
40
- | **Paper** | `--paper` | Live prices, no real orders |
41
- | **Live** | `--live` | Real trades via exchange API |
42
- | **UI Dashboard** | `--ui` | Web dashboard at `http://localhost:60050` |
43
- | **Telegram** | `--telegram` | Trade notifications with price charts |
44
- | **PineScript** | `--pine` | Run a local `.pine` indicator against exchange data |
45
- | **Pine Editor** | `--editor` | Open the visual Pine Script editor in the browser |
46
- | **Candle Dump** | `--dump` | Fetch and save raw OHLCV candles to a file |
47
- | **PnL Debug** | `--pnldebug` | Simulate per-minute PnL for a given entry price and direction |
48
- | **Broker Debug** | `--brokerdebug` | Fire a single broker commit against the live broker adapter |
49
- | **Flush** | `--flush` | Delete report/log/markdown/agent folders from strategy dump dir |
50
- | **Init Project** | `--init` | Scaffold a new backtest-kit project |
51
-
52
- ## 🚀 Installation
53
-
54
- Add `@backtest-kit/cli` to your project and wire it up in `package.json` scripts:
55
-
56
- ```bash
57
- npm install @backtest-kit/cli
58
- ```
59
-
60
- ```json
61
- {
62
- "scripts": {
63
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs",
64
- "paper": "npx @backtest-kit/cli --paper ./src/index.mjs",
65
- "start": "npx @backtest-kit/cli --live ./src/index.mjs"
66
- },
67
- "dependencies": {
68
- "@backtest-kit/cli": "latest",
69
- "backtest-kit": "latest",
70
- "ccxt": "latest"
71
- }
72
- }
73
- ```
74
-
75
- Or run once without installing:
76
-
77
- ```bash
78
- npx @backtest-kit/cli --backtest ./src/index.mjs
79
- ```
80
-
81
- ## 📖 Quick Start
82
-
83
- Create your strategy entry point (`src/index.mjs`). The file registers schemas via `backtest-kit` `@backtest-kit/cli` is only the runner:
84
-
85
- ```javascript
86
- // src/index.mjs
87
- import { addStrategySchema, addExchangeSchema, addFrameSchema } from 'backtest-kit';
88
- import ccxt from 'ccxt';
89
-
90
- // Register exchange
91
- addExchangeSchema({
92
- exchangeName: 'binance',
93
- getCandles: async (symbol, interval, since, limit) => {
94
- const exchange = new ccxt.binance();
95
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
96
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
97
- timestamp, open, high, low, close, volume,
98
- }));
99
- },
100
- formatPrice: (symbol, price) => price.toFixed(2),
101
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
102
- });
103
-
104
- // Register frame (backtest only)
105
- addFrameSchema({
106
- frameName: 'feb-2024',
107
- interval: '1m',
108
- startDate: new Date('2024-02-01'),
109
- endDate: new Date('2024-02-29'),
110
- });
111
-
112
- // Register strategy
113
- addStrategySchema({
114
- strategyName: 'my-strategy',
115
- interval: '15m',
116
- getSignal: async (symbol) => {
117
- // return signal or null
118
- return null;
119
- },
120
- });
121
- ```
122
-
123
- Run a backtest:
124
-
125
- ```bash
126
- npm run backtest -- --symbol BTCUSDT
127
- ```
128
-
129
- Run with UI dashboard and Telegram:
130
-
131
- ```bash
132
- npm run backtest -- --symbol BTCUSDT --ui --telegram
133
- ```
134
-
135
- Run live trading:
136
-
137
- ```bash
138
- npm start -- --symbol BTCUSDT --ui
139
- ```
140
-
141
- ## 🎛️ CLI Flags
142
-
143
- | Command Line Args | Type | Description |
144
- |---------------------------|---------|--------------------------------------------------------------------|
145
- | `--backtest` | boolean | Run historical backtest (default: `false`) |
146
- | `--walker` | boolean | Run Walker A/B strategy comparison (default: `false`) |
147
- | `--paper` | boolean | Paper trading (live prices, no orders) (default: `false`) |
148
- | `--live` | boolean | Run live trading (default: `false`) |
149
- | `--ui` | boolean | Start web UI dashboard (default: `false`) |
150
- | `--telegram` | boolean | Enable Telegram notifications (default: `false`) |
151
- | `--verbose` | boolean | Log each candle fetch (default: `false`) |
152
- | `--noCache` | boolean | Skip candle cache warming before backtest (default: `false`) |
153
- | `--noFlush` | boolean | Skip removing report/log/markdown/agent folders before backtest run (default: `false`) |
154
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
155
- | `--strategy` | string | Strategy name (default: first registered) |
156
- | `--exchange` | string | Exchange name (default: first registered) |
157
- | `--frame` | string | Backtest frame name (default: first registered) |
158
- | `--cacheInterval` | string | Intervals to pre-cache before backtest (default: `"1m, 15m, 30m, 4h"`) |
159
- | `--brokerdebug` | boolean | Fire a single broker commit against the live broker adapter (default: `false`) |
160
- | `--commit` | string | Commit type for `--brokerdebug` (default: `"signal-open"`) |
161
-
162
- **Positional argument (required):** path to your strategy entry point file (set once in `package.json` scripts).
163
-
164
- ```json
165
- {
166
- "scripts": {
167
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
168
- }
169
- }
170
- ```
171
-
172
- ## 🏃 Execution Modes
173
-
174
- ### Backtest
175
-
176
- Runs the strategy against historical candle data using a registered `FrameSchema`.
177
-
178
- ```json
179
- {
180
- "scripts": {
181
- "backtest": "npx @backtest-kit/cli --backtest --symbol ETHUSDT --strategy my-strategy --exchange binance --frame feb-2024 --cacheInterval \"1m, 15m, 1h, 4h\" ./src/index.mjs"
182
- }
183
- }
184
- ```
185
-
186
- ```bash
187
- npm run backtest
188
- ```
189
-
190
- Before running, the CLI removes the `report`, `log`, `markdown`, and `agent` folders from the strategy's `dump/` directory, then warms the candle cache for every interval in `--cacheInterval`. On the next run, cached data is used directly — no API calls needed. Pass `--noCache` to skip cache warming, `--noFlush` to keep existing output folders.
191
-
192
- ### Paper Trading
193
-
194
- Connects to the live exchange but does not place real orders. Identical code path to live — safe for strategy validation.
195
-
196
- ```json
197
- {
198
- "scripts": {
199
- "paper": "npx @backtest-kit/cli --paper --symbol BTCUSDT ./src/index.mjs"
200
- }
201
- }
202
- ```
203
-
204
- ```bash
205
- npm run paper
206
- ```
207
-
208
- ### Live Trading
209
-
210
- Deploys a real trading bot. Requires exchange API keys configured in your `.env` or environment.
211
-
212
- ```json
213
- {
214
- "scripts": {
215
- "start": "npx @backtest-kit/cli --live --ui --telegram --symbol BTCUSDT ./src/index.mjs"
216
- }
217
- }
218
- ```
219
-
220
- ```bash
221
- npm start
222
- ```
223
-
224
- ### Walker — A/B Strategy Comparison
225
-
226
- Runs the same historical period against multiple strategy files and prints a ranked comparison report. Use it to pick the best variant before deploying to backtest or live.
227
-
228
- ```json
229
- {
230
- "scripts": {
231
- "walker": "npx @backtest-kit/cli --walker --symbol BTCUSDT --noCache ./content/feb_2026_v1.strategy.ts ./content/feb_2026_v2.strategy.ts ./content/feb_2026_v3.strategy.ts"
232
- }
233
- }
234
- ```
235
-
236
- ```bash
237
- npm run walker
238
- ```
239
-
240
- Each positional argument is a separate strategy entry point. Before loading them, the CLI removes the `report`, `log`, `markdown`, and `agent` folders from each entry point's `dump/` directory. Pass `--noFlush` to keep existing output. All files are loaded without changing `process.cwd()` — `.env` is read from the working directory only. After loading, `addWalkerSchema` is called automatically using the exchange and frame registered by the strategy files.
241
-
242
- If no frame is registered, the CLI falls back to the last 31 days from `Date.now()` with a console warning.
243
-
244
- **Walker-specific flags:**
245
-
246
- | Flag | Type | Description |
247
- |------|------|-------------|
248
- | `--walker` | boolean | Enable Walker comparison mode |
249
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
250
- | `--cacheInterval` | string | Intervals to pre-cache (default: `"1m, 15m, 30m, 4h"`) |
251
- | `--noCache` | boolean | Skip candle cache warming (default: `false`) |
252
- | `--noFlush` | boolean | Skip removing report/log/markdown/agent folders before walker run (default: `false`) |
253
- | `--verbose` | boolean | Log each candle fetch and strategy progress (default: `false`) |
254
- | `--output` | string | Output file base name (default: `walker_{SYMBOL}_{TIMESTAMP}`) |
255
- | `--json` | boolean | Save results as JSON to `./dump/<output>.json` |
256
- | `--markdown` | boolean | Save report as Markdown to `./dump/<output>.md` |
257
-
258
- **Output modes:**
259
-
260
- - No flag — print Markdown report to stdout
261
- - `--json` — save `Walker.getData()` result as JSON and exit
262
- - `--markdown` — save `Walker.getReport()` as `.md` file and exit
263
-
264
- **Module hook:** `./modules/walker.module` is loaded automatically before the comparison starts (same rules as other modes — `.ts`, `.mjs`, `.cjs` tried in order).
265
-
266
- **Example — compare three variants and save the report:**
267
-
268
- ```bash
269
- npx @backtest-kit/cli --walker \
270
- --symbol BTCUSDT \
271
- --noCache \
272
- --markdown \
273
- --output feb_2026_comparison \
274
- ./content/feb_2026_v1.strategy.ts \
275
- ./content/feb_2026_v2.strategy.ts \
276
- ./content/feb_2026_v3.strategy.ts
277
- # → ./dump/feb_2026_comparison.md
278
- ```
279
-
280
- ## 🐙 Multiple Symbol Parallel
281
-
282
- > **For Poweruser — skip unless needed.** The standard flow runs one symbol from `--symbol`. Use `--entry` only to fan out one strategy across many symbols at once, or to drive `*.background()` from a UI / DB / API.
283
-
284
- With `--entry`, the CLI does only the boilerplate `Setup`, providers (`--ui` / `--telegram`), the matching `./modules/<mode>.module`, SIGINT that stops every active run via `*.list()`, and `shutdown()` once `listenDone*` reports all your runs complete. Picking the symbol set, warming cache, and calling `*.background()` is on you.
285
-
286
- The `--entry` flag is a modifier combine it with exactly one of `--backtest` / `--live` / `--paper` / `--walker`. One positional: the path to your entry file.
287
-
288
- ```bash
289
- npx @backtest-kit/cli --backtest --entry ./src/multi-symbol.mjs
290
- ```
291
-
292
- ### Example: backtest a strategy on five symbols at once
293
-
294
- ```javascript
295
- // src/multi-symbol.mjs
296
- import {
297
- addExchangeSchema,
298
- addFrameSchema,
299
- addStrategySchema,
300
- Backtest,
301
- warmCandles,
302
- } from "backtest-kit";
303
- import ccxt from "ccxt";
304
-
305
- addExchangeSchema({
306
- exchangeName: "binance",
307
- getCandles: async (symbol, interval, since, limit) => {
308
- const exchange = new ccxt.binance();
309
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
310
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
311
- timestamp, open, high, low, close, volume,
312
- }));
313
- },
314
- formatPrice: (symbol, price) => price.toFixed(2),
315
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
316
- });
317
-
318
- addFrameSchema({
319
- frameName: "feb-2026",
320
- interval: "1m",
321
- startDate: new Date("2026-02-01"),
322
- endDate: new Date("2026-02-28"),
323
- });
324
-
325
- addStrategySchema({
326
- strategyName: "my-strategy",
327
- interval: "15m",
328
- getSignal: async (symbol) => null,
329
- });
330
-
331
- // Decide the symbol set yourself — from a UI, database, API, or just a list.
332
- const symbols = ["BTCUSDT", "ETHUSDT", "SOLUSDT", "BNBUSDT", "XRPUSDT"];
333
-
334
- for (const symbol of symbols) {
335
-
336
- //
337
- // Optional
338
- //
339
- // await warmCandles({
340
- // exchangeName: "binance,
341
- // from: new Date("2026-01-01T00:00:00Z"),
342
- // to: new Date("2026-01-31T23:59:59Z"),
343
- // interval: "1m",
344
- // symbol,
345
- // })
346
-
347
-
348
- Backtest.background(symbol, {
349
- strategyName: "my-strategy",
350
- exchangeName: "binance",
351
- frameName: "feb-2026",
352
- });
353
- }
354
- ```
355
-
356
- The same shape works for `--live --entry` / `--paper --entry` (call `Live.background()` per symbol with your broker adapter)
357
-
358
- ## 🗂️ Monorepo Usage
359
-
360
- `@backtest-kit/cli` works out of the box in a monorepo where each strategy lives in its own subdirectory. When the CLI loads your entry point file, it automatically changes the working directory to the file's location — so all relative paths (`dump/`, `modules/`, `template/`) resolve inside that strategy's folder, not the project root.
361
-
362
- ### How It Works
363
-
364
- Internally, `ResolveService` does the following before executing your entry point:
365
-
366
- ```
367
- process.chdir(path.dirname(entryPoint)) // cwd → strategy directory
368
- dotenv.config({ path: rootDir + '/.env' }) // load root .env first
369
- dotenv.config({ path: strategyDir + '/.env', override: true }) // strategy .env overrides
370
- ```
371
-
372
- Everything that follows candle cache warming, report generation, module loading, template resolution — uses the new cwd automatically.
373
-
374
- ### Project Structure
375
-
376
- ```
377
- monorepo/
378
- ├── package.json # root scripts (one per strategy)
379
- ├── .env # shared API keys (exchange, Telegram, etc.)
380
- └── strategies/
381
- ├── oct_2025/
382
- │ ├── index.mjs # entry point — registers exchange/frame/strategy schemas
383
- │ ├── .env # overrides root .env for this strategy
384
- ├── modules (optional)
385
- | ├── live.module.mjs # broker adapter for --live mode (optional)
386
- | ├── paper.module.mjs # broker adapter for --paper mode (optional)
387
- | ├── backtest.module.mjs # broker adapter for --backtest mode (optional)
388
- ├── template/ # custom Mustache templates (optional)
389
- │ └── dump/ # auto-created: candle cache + backtest reports
390
- └── dec_2025/
391
- ├── index.mjs
392
- ├── .env
393
- └── dump/
394
- ```
395
-
396
- ### Root `package.json`
397
-
398
- ```json
399
- {
400
- "scripts": {
401
- "backtest:oct": "npx @backtest-kit/cli --backtest ./strategies/oct_2025/index.mjs",
402
- "backtest:dec": "npx @backtest-kit/cli --backtest ./strategies/dec_2025/index.mjs"
403
- },
404
- "dependencies": {
405
- "@backtest-kit/cli": "latest",
406
- "backtest-kit": "latest",
407
- "ccxt": "latest"
408
- }
409
- }
410
- ```
411
-
412
- ```bash
413
- npm run backtest:oct
414
- npm run backtest:dec
415
- ```
416
-
417
- ### Isolated Resources Per Strategy
418
-
419
- | Resource | Path (relative to strategy dir) | Isolated |
420
- |--------------------------|-----------------------------------|------------------|
421
- | Candle cache | `./dump/data/candle/` | ✅ per-strategy |
422
- | Backtest reports | `./dump/` | per-strategy |
423
- | Broker module (live) | `./modules/live.module.mjs` | ✅ per-strategy |
424
- | Broker module (paper) | `./modules/paper.module.mjs` | ✅ per-strategy |
425
- | Broker module (backtest) | `./modules/backtest.module.mjs` | ✅ per-strategy |
426
- | Config module (walker) | `./modules/walker.module.mjs` | loaded once |
427
- | Telegram templates | `./template/*.mustache` | per-strategy |
428
- | Environment variables | `./.env` (overrides root) | ✅ per-strategy |
429
-
430
- Each strategy run produces its own `dump/` directory, making it straightforward to compare results across time periods — both by inspection and by pointing an AI agent at a specific strategy folder.
431
-
432
- ## 🔗 Shared Import Aliases
433
-
434
- `@backtest-kit/cli` automatically turns every **top-level folder** in `process.cwd()` into a bare import alias available inside any strategy file. No configuration needed — just create the folder.
435
-
436
- ### How It Works
437
-
438
- When the CLI loads a strategy file, it scans the current working directory for subdirectories and registers each one as an import alias. The alias name is the folder name. Both barrel imports and deep subpath imports are supported:
439
-
440
- | Import | Resolves to |
441
- |--------|-------------|
442
- | `import { fn } from "utils"` | `<cwd>/utils/index.ts` (or `.js`, `.mjs`, `.cjs`) |
443
- | `import { calcRSI } from "math/rsi"` | `<cwd>/math/rsi.ts` |
444
- | `import { research } from "logic"` | `<cwd>/logic/index.ts` |
445
- | `import { ResearchResponseContract } from "logic/contract/ResearchResponse.contract"` | `<cwd>/logic/contract/ResearchResponse.contract.ts` |
446
-
447
- ### Project Structure
448
-
449
- ```
450
- my-project/
451
- ├── utils/ ← import { formatDate } from "utils"
452
- │ └── index.ts
453
- ├── math/ ← import { calcRSI } from "math/rsi"
454
- │ └── rsi.ts
455
- ├── logic/ ← import { research } from "logic"
456
- │ ├── index.ts ← barrel
457
- │ └── contract/
458
- │ └── ResearchResponse.contract.ts ← import { ... } from "logic/contract/ResearchResponse.contract"
459
- └── content/
460
- ├── feb_2026.strategy.ts ← uses all three aliases freely
461
- └── mar_2026.strategy.ts ← same aliases, no duplication
462
- ```
463
-
464
- This lets you extract shared utilities, math helpers, or AI agent logic (e.g. `agent-swarm-kit` workflows) into named folders and reuse them across every strategy in the project without relative path hell.
465
-
466
- ### TypeScript Support
467
-
468
- Add a matching `paths` entry to your `tsconfig.json` so the editor resolves the aliases:
469
-
470
- ```json
471
- {
472
- "compilerOptions": {
473
- "moduleResolution": "bundler",
474
- "paths": {
475
- "logic": ["./logic/index.ts"],
476
- "logic/*": ["./logic/*"],
477
- "math": ["./math/index.ts"],
478
- "math/*": ["./math/*"],
479
- "utils": ["./utils/index.ts"],
480
- "utils/*": ["./utils/*"]
481
- }
482
- },
483
- "include": [
484
- "./logic",
485
- "./math",
486
- "./utils",
487
- "./content",
488
- "./modules",
489
- ],
490
- }
491
- ```
492
-
493
- ## 🔔 Integrations
494
-
495
- ### Web Dashboard (`--ui`)
496
-
497
- Starts `@backtest-kit/ui` server. Access the interactive dashboard at:
498
-
499
- ```
500
- http://localhost:60050
501
- ```
502
-
503
- Customize host/port via environment variables `CC_WWWROOT_HOST` and `CC_WWWROOT_PORT`.
504
-
505
- #### Symbol List (`symbol.config`)
506
-
507
- By default the UI shows all symbols from the exchange. To restrict or reorder the list, create a `config/symbol.config` file in your strategy directory (next to the entry point).
508
-
509
- **Resolution order — first match wins:**
510
-
511
- | Priority | Path | Notes |
512
- |----------|------|-------|
513
- | 1 | `{strategyDir}/config/symbol.config` | per-strategy override (cwd after `chdir`) |
514
- | 2 | `{projectRoot}/config/symbol.config` | project-root override (cwd where `npx` was invoked) |
515
- | 3 | `@backtest-kit/cli/config/symbol.config` | built-in default shipped with the package |
516
-
517
- Supported file formats (`.ts`, `.cjs`, `.mjs`, `.js` tried automatically):
518
-
519
- ```ts
520
- // config/symbol.config.ts
521
- export const symbol_list = [
522
- {
523
- icon: "/icon/btc.png",
524
- logo: "/icon/128/btc.png",
525
- symbol: "BTCUSDT",
526
- displayName: "Bitcoin",
527
- color: "#F7931A",
528
- priority: 50,
529
- description: "Bitcoin - the first and most popular cryptocurrency",
530
- },
531
- {
532
- icon: "/icon/eth.png",
533
- logo: "/icon/128/eth.png",
534
- symbol: "ETHUSDT",
535
- displayName: "Ethereum",
536
- color: "#6F42C1",
537
- priority: 50,
538
- description: "Ethereum - a blockchain platform for smart contracts",
539
- },
540
- ];
541
- ```
542
-
543
- #### Notification Filter (`notification.config`)
544
-
545
- Controls which notification categories are shown in the UI dashboard. Create a `config/notification.config` file in your strategy directory to override the defaults.
546
-
547
- **Resolution order first match wins:**
548
-
549
- | Priority | Path | Notes |
550
- |----------|------|-------|
551
- | 1 | `{strategyDir}/config/notification.config` | per-strategy override (cwd after `chdir`) |
552
- | 2 | `{projectRoot}/config/notification.config` | project-root override (cwd where `npx` was invoked) |
553
- | 3 | `@backtest-kit/cli/config/notification.config` | built-in default shipped with the package |
554
-
555
- **Default values (built-in):**
556
-
557
- | Key | Default | Description |
558
- |-----|---------|-------------|
559
- | `signal` | `true` | Signal lifecycle: opened, scheduled, closed, cancelled |
560
- | `risk` | `true` | Risk manager rejection notifications |
561
- | `info` | `true` | Informational messages attached to an active signal |
562
- | `breakeven` | `true` | Breakeven level reached |
563
- | `common_error` | `true` | Non-fatal runtime errors |
564
- | `critical_error` | `true` | Fatal errors that terminate the session |
565
- | `validation_error` | `true` | Strategy config / input validation errors |
566
- | `strategy_commit` | `true` | All committed actions (partial close, DCA, trailing, etc.) |
567
- | `partial_loss` | `false` | Partial loss level reached (before commit) |
568
- | `partial_profit` | `false` | Partial profit level reached (before commit) |
569
- | `signal_sync` | `false` | Live order fill / exit confirmations from exchange sync |
570
-
571
- ```js
572
- // config/notification.config.ts
573
- export default {
574
- signal: true,
575
- risk: true,
576
- info: true,
577
- breakeven: true,
578
- common_error: true,
579
- critical_error: true,
580
- validation_error: true,
581
- strategy_commit: true,
582
- partial_loss: false,
583
- partial_profit: false,
584
- signal_sync: false,
585
- };
586
- ```
587
-
588
- ### Telegram Notifications (`--telegram`)
589
-
590
- Sends formatted HTML messages with 1m / 15m / 1h price charts to your Telegram channel for every position event: opened, closed, scheduled, cancelled, risk rejection, partial profit/loss, trailing stop/take, and breakeven.
591
-
592
- Requires `CC_TELEGRAM_TOKEN` and `CC_TELEGRAM_CHANNEL` in your environment.
593
-
594
- #### Telegram Message Adapter (`telegram.config`)
595
-
596
- By default messages are rendered from Mustache templates (`template/*.mustache`). To override rendering programmatically, create a `config/telegram.config` file and export an object with any subset of `get*Markdown` methods. Each method receives the event payload and must return a `Promise<string>` with the Markdown message body.
597
-
598
- Resolution order is the same as other configs (strategy dir → project root → package default).
599
-
600
- ```ts
601
- // config/telegram.config.ts
602
- import {
603
- IStrategyTickResultOpened,
604
- IStrategyTickResultClosed,
605
- RiskContract,
606
- } from "backtest-kit";
607
-
608
- export default {
609
- async getOpenedMarkdown(event: IStrategyTickResultOpened): Promise<string> {
610
- return `**Opened** ${event.symbol} at ${event.priceOpen}`;
611
- },
612
- async getClosedMarkdown(event: IStrategyTickResultClosed): Promise<string> {
613
- return `**Closed** ${event.symbol} at ${event.priceClosed}`;
614
- },
615
- async getRiskMarkdown(event: RiskContract): Promise<string> {
616
- return `**Risk rejected** ${event.symbol}`;
617
- },
618
- };
619
- ```
620
-
621
- All methods are optional — unimplemented ones fall back to the Mustache template.
622
-
623
- | Method | Event type |
624
- |--------|------------|
625
- | `getOpenedMarkdown` | `IStrategyTickResultOpened` |
626
- | `getClosedMarkdown` | `IStrategyTickResultClosed` |
627
- | `getScheduledMarkdown` | `IStrategyTickResultScheduled` |
628
- | `getCancelledMarkdown` | `IStrategyTickResultCancelled` |
629
- | `getRiskMarkdown` | `RiskContract` |
630
- | `getPartialProfitMarkdown` | `PartialProfitCommit` |
631
- | `getPartialLossMarkdown` | `PartialLossCommit` |
632
- | `getBreakevenMarkdown` | `BreakevenCommit` |
633
- | `getTrailingTakeMarkdown` | `TrailingTakeCommit` |
634
- | `getTrailingStopMarkdown` | `TrailingStopCommit` |
635
- | `getAverageBuyMarkdown` | `AverageBuyCommit` |
636
- | `getSignalOpenMarkdown` | `SignalOpenContract` |
637
- | `getSignalCloseMarkdown` | `SignalCloseContract` |
638
- | `getCancelScheduledMarkdown` | `CancelScheduledCommit` |
639
- | `getClosePendingMarkdown` | `ClosePendingCommit` |
640
- | `getSignalInfoMarkdown` | `SignalInfoContract` |
641
-
642
- ## 🧩 Module Hooks (Broker Adapter)
643
-
644
- The CLI supports **mode-specific module files** that are loaded as side-effect imports before the strategy starts. Each file is expected to call `Broker.useBrokerAdapter()` from `backtest-kit` to register a broker adapter.
645
-
646
- | Command Line Args | Module file | Loaded before |
647
- |-------------------|---------------------------------|-----------------------------|
648
- | `--live` | `./modules/live.module.mjs` | `Live.background()` |
649
- | `--paper` | `./modules/paper.module.mjs` | `Live.background()` (paper) |
650
- | `--backtest` | `./modules/backtest.module.mjs` | `Backtest.background()` |
651
- | `--walker` | `./modules/walker.module.mjs` | `Walker.background()` |
652
- | `--brokerdebug` | `./modules/brokerdebug.module.mjs` | broker commit test |
653
-
654
- > File is resolved relative to `cwd` (the strategy directory). All of `.mjs`, `.cjs`, `.ts` extensions are tried automatically. Missing module is a soft warning — not an error.
655
-
656
- ### How It Works
657
-
658
- The module file is a side-effect import. When the CLI loads it, your code runs and registers the adapter. From that point on, `backtest-kit` intercepts every trade-mutating call through the adapter **before** updating internal state — if the adapter throws, the position state is never changed.
659
-
660
- ```javascript
661
- // live.module.mjs
662
- import { Broker } from 'backtest-kit';
663
- import { myExchange } from './exchange.mjs';
664
-
665
- class MyBroker {
666
- async onSignalOpenCommit({ symbol, priceOpen, direction }) {
667
- await myExchange.openPosition(symbol, direction, priceOpen);
668
- }
669
-
670
- async onSignalCloseCommit({ symbol, priceClosed }) {
671
- await myExchange.closePosition(symbol, priceClosed);
672
- }
673
-
674
- async onPartialProfitCommit({ symbol, cost, currentPrice }) {
675
- await myExchange.createOrder({
676
- symbol,
677
- side: 'sell',
678
- quantity: cost / currentPrice,
679
- });
680
- }
681
-
682
- async onAverageBuyCommit({ symbol, cost, currentPrice }) {
683
- await myExchange.createOrder({
684
- symbol,
685
- side: 'buy',
686
- quantity: cost / currentPrice,
687
- });
688
- }
689
- }
690
-
691
- Broker.useBrokerAdapter(MyBroker);
692
-
693
- Broker.enable();
694
- ```
695
-
696
- ### Available Broker Hooks
697
-
698
- | Method | Payload type | Triggered on |
699
- |--------------------------|------------------------------|---------------------------|
700
- | `onSignalOpenCommit` | `BrokerSignalOpenPayload` | Position activation |
701
- | `onSignalCloseCommit` | `BrokerSignalClosePayload` | SL / TP / manual close |
702
- | `onPartialProfitCommit` | `BrokerPartialProfitPayload` | PP |
703
- | `onPartialLossCommit` | `BrokerPartialLossPayload` | PL |
704
- | `onTrailingStopCommit` | `BrokerTrailingStopPayload` | SL adjustment |
705
- | `onTrailingTakeCommit` | `BrokerTrailingTakePayload` | TP adjustment |
706
- | `onBreakevenCommit` | `BrokerBreakevenPayload` | SL moved to entry |
707
- | `onAverageBuyCommit` | `BrokerAverageBuyPayload` | DCA entry |
708
-
709
- All methods are optional. Unimplemented hooks are silently skipped. In backtest mode all broker calls are skipped automatically — no adapter code runs during backtests.
710
-
711
- ### TypeScript
712
-
713
- ```typescript
714
- import { Broker, IBroker, BrokerSignalOpenPayload, BrokerSignalClosePayload } from 'backtest-kit';
715
-
716
- class MyBroker implements Partial<IBroker> {
717
- async onSignalOpenCommit(payload: BrokerSignalOpenPayload) {
718
- // place open order on exchange
719
- }
720
-
721
- async onSignalCloseCommit(payload: BrokerSignalClosePayload) {
722
- // place close order on exchange
723
- }
724
- }
725
-
726
- Broker.useBrokerAdapter(MyBroker);
727
-
728
- Broker.enable();
729
- ```
730
-
731
- ## ⚙️ Setup Hook (`config/setup.config`)
732
-
733
- `@backtest-kit/cli` loads a `{projectRoot}/config/setup.config` file once before any module hooks or strategy code run. Use it to perform one-time initialization that must happen before the first persistence call — registering a custom storage backend, configuring a logger, seeding global state, or anything else the process needs before `backtest-kit` starts.
734
-
735
- **Important:** When `setup.config` is present, the CLI skips its own default adapter registration — it does **not** call `usePersist()` / `useLocal()` / `useMemory()` for any of the persistence slots. This means your config takes full ownership of the persistence layer: whatever adapters you register in `{projectRoot}/config/setup.config` are the ones `backtest-kit` uses, with no interference from the CLI defaults.
736
-
737
- ### Example: MongoDB + Redis persistence via `@backtest-kit/mongo`
738
-
739
- The most common use-case is swapping the default file-based persistence for a production-grade backend. Install `@backtest-kit/mongo` and call `setup()` — it registers all 15 persistence adapters in one call and reads connection parameters from environment variables:
740
-
741
- ```bash
742
- npm install @backtest-kit/mongo
743
- ```
744
-
745
- ```ts
746
- // config/setup.config.ts
747
- import { setup } from '@backtest-kit/mongo';
748
-
749
- setup();
750
- ```
751
-
752
- ```env
753
- # .env
754
- CC_MONGO_CONNECTION_STRING=mongodb://localhost:27017/backtest-kit
755
- CC_REDIS_HOST=127.0.0.1
756
- CC_REDIS_PORT=6379
757
- ```
758
-
759
- Or pass connection parameters explicitly:
760
-
761
- ```ts
762
- // config/setup.config.ts
763
- import { setup } from '@backtest-kit/mongo';
764
-
765
- setup({
766
- CC_MONGO_CONNECTION_STRING: 'mongodb://mongo:27017/mydb',
767
- CC_REDIS_HOST: 'redis',
768
- CC_REDIS_PORT: 6379,
769
- CC_REDIS_PASSWORD: 'secret',
770
- });
771
- ```
772
-
773
- No changes to strategy code are needed — `setup()` wires up the adapters transparently before `backtest-kit` makes its first persistence call.
774
-
775
- ## 🔀 Import Aliases (`config/alias.config`)
776
-
777
- `@backtest-kit/cli` lets you override any nodejs module import without touching the strategy code. Drop a `config/alias.config` file in your project root and export a mapping from module name to replacement module.
778
-
779
- The alias table is loaded once (on the first `import` call) from `{projectRoot}/config/alias.config` and applied globally to every subsequent module load via `require`/ `import`.
780
-
781
- **Use cases:**
782
-
783
- - Replace a heavy dependency with a lighter stub for backtesting
784
- - Swap any external api for a mock during CI runs
785
-
786
- ```ts
787
- // config/alias.config.ts — named export
788
- export const ccxt = require("./stubs/ccxt.stub.cjs");
789
- ```
790
-
791
- ```js
792
- // config/alias.config.cjs — default export
793
- module.exports = {
794
- ccxt: require("./stubs/ccxt.stub.cjs"),
795
- };
796
- ```
797
-
798
- ```js
799
- // config/alias.config.mjs — default export
800
- import ccxtStub from "./stubs/ccxt.stub.mjs";
801
-
802
- export default {
803
- ccxt: ccxtStub,
804
- };
805
- ```
806
-
807
- **How it works:** when strategy code calls `require("ccxt")`, the loader checks `IMPORT_ALIAS` first. If a key matches, the mapped value is returned instead of the real module — no monkey-patching of `node_modules` needed.
808
-
809
- **Important:** It is **not** per-strategy — it applies to all modules loaded in the current process.
810
-
811
- ## 📦 Supported Entry Point Formats
812
-
813
- `@backtest-kit/cli` automatically detects the format of your strategy file and loads it with the appropriate runtime — no flags or configuration required.
814
-
815
- | Format | Extension | Runtime | Use Case |
816
- |--------|-----------|---------|----------|
817
- | **TypeScript** | `.ts` | [`tsx`](https://tsx.is/) via `tsImport()` | TypeScript strategies with cross-imports (ESM CJS) |
818
- | **ES Module** | `.mjs` | Native `import()` | Modern JavaScript with top-level `await` and ESM syntax |
819
- | **CommonJS** | `.cjs` | Native `require()` | Legacy or dual-package strategies |
820
-
821
- ### TypeScript (`.ts`)
822
-
823
- Run TypeScript strategy files directly — no `tsc` compilation step needed. Powered by `tsx`, which handles cross-format imports transparently:
824
-
825
- ```json
826
- {
827
- "scripts": {
828
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.ts"
829
- },
830
- "dependencies": {
831
- "@backtest-kit/cli": "latest",
832
- "backtest-kit": "latest",
833
- "tsx": "latest"
834
- }
835
- }
836
- ```
837
-
838
- ### ES Module (`.mjs`)
839
-
840
- Standard ESM format. Supports top-level `await`, named exports, and `import` syntax:
841
-
842
- ```json
843
- {
844
- "scripts": {
845
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
846
- }
847
- }
848
- ```
849
-
850
- ### CommonJS (`.cjs`)
851
-
852
- For projects that compile to or use CommonJS. Loaded via `require()`:
853
-
854
- ```json
855
- {
856
- "scripts": {
857
- "backtest": "npx @backtest-kit/cli --backtest ./dist/index.cjs"
858
- }
859
- }
860
- ```
861
-
862
- ## 🌲 Running Local PineScript Indicators
863
-
864
- `@backtest-kit/cli` can execute any local `.pine` file against a real exchange and print the results as a Markdown table — no TradingView account required.
865
-
866
- ### CLI Flags
867
-
868
- | Flag | Type | Description |
869
- |------|------|-------------|
870
- | `--pine` | boolean | Enable PineScript execution mode |
871
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
872
- | `--timeframe` | string | Candle interval (default: `"15m"`) |
873
- | `--limit` | string | Number of candles to fetch (default: `250`) |
874
- | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
875
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
876
- | `--output` | string | Output file base name without extension (default: `.pine` file name) |
877
- | `--json` | boolean | Write plots as a JSON array to `<pine-dir>/dump/{output}.json` |
878
- | `--jsonl` | boolean | Write plots as JSONL (one row per line) to `<pine-dir>/dump/{output}.jsonl` |
879
- | `--markdown` | boolean | Write Markdown table to `<pine-dir>/dump/{output}.md` |
880
-
881
- **Important:** `limit` must cover indicator warmup bars rows before warmup completes will show `N/A`
882
-
883
- **Positional argument:** path to the `.pine` file.
884
-
885
- ### Exchange via `pine.module`
886
-
887
- By default the CLI registers CCXT Binance automatically. To use a different exchange — or to configure API keys, custom rate limits, or a non-spot market — create a `modules/pine.module.ts` file. The CLI loads it automatically before running the script.
888
-
889
- The CLI looks for `modules/pine.module` in two locations (first match wins):
890
-
891
- 1. **Next to the `.pine` file** — `<pine-file-dir>/modules/pine.module.ts`
892
- 2. **Project root** — `<cwd>/modules/pine.module.ts`
893
-
894
- ```
895
- my-project/
896
- ├── math/
897
- │ ├── impulse_trend_15m.pine ← indicator
898
- │ └── modules/
899
- │ └── pine.module.ts ← loaded first (next to .pine file)
900
- ├── modules/
901
- │ └── pine.module.ts ← fallback (project root)
902
- └── package.json
903
- ```
904
-
905
- Inside `pine.module.ts` call `addExchangeSchema` from `backtest-kit` and give the exchange a name:
906
-
907
- ```typescript
908
- // modules/pine.module.ts
909
- import { addExchangeSchema } from "backtest-kit";
910
- import ccxt from "ccxt";
911
-
912
- addExchangeSchema({
913
- exchangeName: "my-exchange",
914
- getCandles: async (symbol, interval, since, limit) => {
915
- const exchange = new ccxt.bybit({ enableRateLimit: true });
916
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
917
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
918
- timestamp, open, high, low, close, volume,
919
- }));
920
- },
921
- formatPrice: (symbol, price) => price.toFixed(2),
922
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
923
- });
924
- ```
925
-
926
- ### Environment variables (`.env`)
927
-
928
- Before loading `pine.module`, the CLI loads `.env` files in the same order as for strategy modules — project root first, then the `.pine` file directory (overrides root):
929
-
930
- ```
931
- my-project/
932
- ├── math/
933
- │ ├── .env ← loaded second (overrides root)
934
- │ └── impulse_trend_15m.pine
935
- ├── .env ← loaded first
936
- └── package.json
937
- ```
938
-
939
- Use this to store API keys without hardcoding them:
940
-
941
- ```env
942
- # .env
943
- BYBIT_API_KEY=xxx
944
- BYBIT_API_SECRET=yyy
945
- ```
946
-
947
- ```typescript
948
- // modules/pine.module.ts
949
- addExchangeSchema({
950
- exchangeName: "my-exchange",
951
- getCandles: async (symbol, interval, since, limit) => {
952
- const exchange = new ccxt.bybit({
953
- apiKey: process.env.BYBIT_API_KEY,
954
- secret: process.env.BYBIT_API_SECRET,
955
- enableRateLimit: true,
956
- });
957
- // ...
958
- },
959
- });
960
- ```
961
-
962
- Then run:
963
-
964
- ```bash
965
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine \
966
- --exchange my-exchange \
967
- --symbol BTCUSDT \
968
- --timeframe 15m \
969
- --limit 180 \
970
- --when "2025-09-24T12:00:00.000Z"
971
- ```
972
-
973
- Or add it to `package.json`:
974
-
975
- ```json
976
- {
977
- "scripts": {
978
- "pine": "npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --symbol BTCUSDT --timeframe 15m --limit 180"
979
- }
980
- }
981
- ```
982
-
983
- ```bash
984
- npm run pine
985
- ```
986
-
987
- ### PineScript Requirements
988
-
989
- The CLI reads all `plot()` calls that use `display=display.data_window` as output columns. Every other `plot()` is ignored. Name each output plot explicitly:
990
-
991
- ```pine
992
- //@version=5
993
- indicator("MyIndicator", overlay=true)
994
-
995
- // ... computation ...
996
-
997
- plot(close, "Close", display=display.data_window)
998
- plot(position, "Position", display=display.data_window)
999
- ```
1000
-
1001
- The column names in the output Markdown table are taken directly from those plot names — no manual schema definition needed.
1002
-
1003
- ### Output
1004
-
1005
- The CLI prints a Markdown table to stdout:
1006
-
1007
- ```
1008
- # PineScript Technical Analysis Dump
1009
-
1010
- **Signal ID**: CLI execution 2025-09-24T12:00:00.000Z
1011
-
1012
- | Close | Position | timestamp |
1013
- | --- | --- | --- |
1014
- | 112871.28 | -1.0000 | 2025-09-22T15:00:00.000Z |
1015
- | 112666.69 | -1.0000 | 2025-09-22T15:15:00.000Z |
1016
- | 112736.00 | 0.0000 | 2025-09-22T18:30:00.000Z |
1017
- | 112653.90 | 1.0000 | 2025-09-22T22:15:00.000Z |
1018
- ```
1019
-
1020
- Save to `./math/dump/impulse_trend_15m.md` (uses `.pine` file name automatically, dump is created next to the `.pine` file):
1021
-
1022
- ```bash
1023
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --markdown
1024
- ```
1025
-
1026
- Override the output name with `--output`:
1027
-
1028
- ```bash
1029
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --jsonl --output feb2026_bb
1030
- # ./math/dump/feb2026_bb.jsonl
1031
- ```
1032
-
1033
- Print to stdout (no flag):
1034
-
1035
- ```bash
1036
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine
1037
- ```
1038
-
1039
- ## 🎨 Visual Pine Script Editor
1040
-
1041
- ![pine](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot32.png)
1042
-
1043
- `@backtest-kit/cli` ships a browser-based Pine Script editor powered by `@backtest-kit/ui`. It lets you write, run, and iterate on indicators interactively — with a live chart that updates as you hit **▶ Run**
1044
-
1045
- ### Usage
1046
-
1047
- ```bash
1048
- npx @backtest-kit/cli --editor
1049
- ```
1050
-
1051
- The CLI will:
1052
-
1053
- 1. Load `./modules/editor.module` (if it exists) — use it to register your exchange schema, identical to `pine.module`
1054
- 2. Start the `@backtest-kit/ui` server on `http://localhost:60050` (or `CC_WWWROOT_PORT`)
1055
- 3. Open `http://localhost:{CC_WWWROOT_PORT}?pine=1` automatically in your default browser
1056
-
1057
- Press **Ctrl+C** to stop the server.
1058
-
1059
- ### Exchange via `editor.module`
1060
-
1061
- Drop a `modules/editor.module.ts` next to your project to register the exchange that the editor's candle provider will use:
1062
-
1063
- ```typescript
1064
- // modules/editor.module.ts
1065
- import { addExchangeSchema } from "backtest-kit";
1066
- import ccxt from "ccxt";
1067
-
1068
- addExchangeSchema({
1069
- exchangeName: "my-exchange",
1070
- getCandles: async (symbol, interval, since, limit) => {
1071
- const exchange = new ccxt.bybit({ enableRateLimit: true });
1072
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1073
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1074
- timestamp, open, high, low, close, volume,
1075
- }));
1076
- },
1077
- formatPrice: (symbol, price) => price.toFixed(2),
1078
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1079
- });
1080
- ```
1081
-
1082
- ### Environment Variables
1083
-
1084
- | Variable | Default | Description |
1085
- |-------------------|-----------|----------------------------------|
1086
- | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1087
- | `CC_WWWROOT_PORT` | `60050` | UI server port |
1088
-
1089
- ### `package.json` script
1090
-
1091
- ```json
1092
- {
1093
- "scripts": {
1094
- "editor": "npx @backtest-kit/cli --editor"
1095
- }
1096
- }
1097
- ```
1098
-
1099
- ```bash
1100
- npm run editor
1101
- ```
1102
-
1103
- ## 💾 Dumping Raw Candles
1104
-
1105
- `@backtest-kit/cli` can fetch raw OHLCV candles from any registered exchange and save them to a file no strategy file required.
1106
-
1107
- ### CLI Flags
1108
-
1109
- | Flag | Type | Description |
1110
- |------|------|-------------|
1111
- | `--dump` | boolean | Enable candle dump mode |
1112
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1113
- | `--timeframe` | string | Candle interval (default: `"15m"`) |
1114
- | `--limit` | string | Number of candles to fetch (default: `250`) |
1115
- | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
1116
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1117
- | `--output` | string | Output file base name without extension (default: `{SYMBOL}_{LIMIT}_{TIMEFRAME}_{TIMESTAMP}`) |
1118
- | `--json` | boolean | Write candles as a JSON array to `./dump/{output}.json` |
1119
- | `--jsonl` | boolean | Write candles as JSONL (one row per line) to `./dump/{output}.jsonl` |
1120
-
1121
- The `dump/` directory is created in the current working directory (where the CLI is invoked from).
1122
-
1123
- ### Exchange via `dump.module`
1124
-
1125
- By default the CLI registers CCXT Binance automatically. To use a different exchange — or to configure API keys, custom rate limits, or a non-spot market — create a `modules/dump.module.ts` file. The CLI loads it automatically before fetching candles.
1126
-
1127
- The CLI looks for `modules/dump.module` in the current working directory
1128
-
1129
- ```
1130
- my-project/
1131
- ├── modules/
1132
- │ └── dump.module.ts ← exchange registration
1133
- ├── dump/ ← auto-created: candle output files
1134
- └── package.json
1135
- ```
1136
-
1137
- Inside `dump.module.ts` call `addExchangeSchema` from `backtest-kit`:
1138
-
1139
- ```typescript
1140
- // modules/dump.module.ts
1141
- import { addExchangeSchema } from "backtest-kit";
1142
- import ccxt from "ccxt";
1143
-
1144
- addExchangeSchema({
1145
- exchangeName: "my-exchange",
1146
- getCandles: async (symbol, interval, since, limit) => {
1147
- const exchange = new ccxt.bybit({ enableRateLimit: true });
1148
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1149
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1150
- timestamp, open, high, low, close, volume,
1151
- }));
1152
- },
1153
- formatPrice: (symbol, price) => price.toFixed(2),
1154
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1155
- });
1156
- ```
1157
-
1158
- ### Output
1159
-
1160
- Each candle row contains OHLCV fields. Print to stdout:
1161
-
1162
- ```bash
1163
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100
1164
- ```
1165
-
1166
- Save to `./dump/BTCUSDT_100_15m_{timestamp}.jsonl`:
1167
-
1168
- ```bash
1169
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100 --jsonl
1170
- ```
1171
-
1172
- Fetch candles up to a specific date with `--when` and override the file name with `--output`:
1173
-
1174
- ```bash
1175
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 \
1176
- --when "2026-02-28T00:00:00.000Z" \
1177
- --jsonl --output feb2026_btc
1178
- # → ./dump/feb2026_btc.jsonl
1179
- ```
1180
-
1181
- Or add it to `package.json`:
1182
-
1183
- ```json
1184
- {
1185
- "scripts": {
1186
- "dump": "npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl"
1187
- }
1188
- }
1189
- ```
1190
-
1191
- ```bash
1192
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl
1193
- ```
1194
-
1195
- ## 🐞 PnL Debug (`--pnldebug`)
1196
-
1197
- `@backtest-kit/cli` can simulate a hypothetical position minute by minute and print running PnL, peak profit, and maximum drawdown for each candle — without placing any trades or loading a strategy file.
1198
-
1199
- ### CLI Flags
1200
-
1201
- | Flag | Type | Description |
1202
- |------|------|-------------|
1203
- | `--pnldebug` | boolean | Enable PnL debug mode |
1204
- | `--priceopen` | number | Entry price (required) |
1205
- | `--direction` | string | `long` or `short` (default: `long`) |
1206
- | `--when` | string | Start timestamp — ISO 8601 or Unix ms (default: now) |
1207
- | `--minutes` | string | Number of 1m candles to simulate (default: `60`) |
1208
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1209
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1210
- | `--output` | string | Output file base name (default: `{SYMBOL}_{DIRECTION}_{PRICEOPEN}_{TIMESTAMP}`) |
1211
- | `--json` | boolean | Save results as JSON array to `./dump/<output>.json` |
1212
- | `--jsonl` | boolean | Save results as JSONL to `./dump/<output>.jsonl` |
1213
- | `--markdown` | boolean | Save results as Markdown table to `./dump/<output>.md` |
1214
-
1215
- ### Output columns
1216
-
1217
- | Column | Description |
1218
- |--------|-------------|
1219
- | `min` | Minute offset from start (1-based) |
1220
- | `timestamp` | Candle timestamp (ISO 8601) |
1221
- | `close` | Candle close price |
1222
- | `pnl%` | Running PnL vs entry price (signed %) |
1223
- | `peak%` | Highest PnL reached so far (always ≥ 0) |
1224
- | `drawdown%` | Lowest PnL reached so far (always ≤ 0) |
1225
-
1226
- ### Exchange via `pnldebug.module`
1227
-
1228
- By default the CLI registers CCXT Binance automatically. To use a different exchange, create a `modules/pnldebug.module.ts` file in the current working directory — the CLI loads it automatically before fetching candles.
1229
-
1230
- ```typescript
1231
- // modules/pnldebug.module.ts
1232
- import { addExchangeSchema } from "backtest-kit";
1233
- import ccxt from "ccxt";
1234
-
1235
- addExchangeSchema({
1236
- exchangeName: "my-exchange",
1237
- getCandles: async (symbol, interval, since, limit) => {
1238
- const exchange = new ccxt.bybit({ enableRateLimit: true });
1239
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1240
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1241
- timestamp, open, high, low, close, volume,
1242
- }));
1243
- },
1244
- formatPrice: (symbol, price) => price.toFixed(2),
1245
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1246
- });
1247
- ```
1248
-
1249
- ### Usage
1250
-
1251
- Print to stdout (default table format):
1252
-
1253
- ```bash
1254
- npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120
1255
- ```
1256
-
1257
- Save as Markdown:
1258
-
1259
- ```bash
1260
- npx @backtest-kit/cli --pnldebug --priceopen 67956.73 --direction long --when 1772064000000 --minutes 60 --markdown
1261
- # → ./dump/BTCUSDT_long_67956.73_{timestamp}.md
1262
- ```
1263
-
1264
- Override the output file name with `--output`:
1265
-
1266
- ```bash
1267
- npx @backtest-kit/cli --pnldebug --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120 \
1268
- --jsonl --output feb25_short_debug
1269
- # ./dump/feb25_short_debug.jsonl
1270
- ```
1271
-
1272
- Or add it to `package.json`:
1273
-
1274
- ```json
1275
- {
1276
- "scripts": {
1277
- "pnldebug": "npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when \"2025-02-25\" --minutes 120"
1278
- }
1279
- }
1280
- ```
1281
-
1282
- ```bash
1283
- npm run pnldebug
1284
- ```
1285
-
1286
- ### Example stdout output
1287
-
1288
- ```
1289
- Symbol: BTCUSDT | Direction: short | PriceOpen: 64069.50 | From: 2025-02-25T00:00:00.000Z | Minutes: 120
1290
-
1291
- min | timestamp | close | pnl% | peak% | drawdown%
1292
- -----------------------------------------------------------------------------------
1293
- 1 | 2025-02-25T00:01:00.000Z | 64020.10 | +0.08% | +0.08% | 0.00%
1294
- 2 | 2025-02-25T00:02:00.000Z | 64105.30 | -0.06% | +0.08% | -0.06%
1295
- ...
1296
- 120 | 2025-02-25T02:00:00.000Z | 63200.00 | +1.36% | +1.36% | -0.06%
1297
- ```
1298
-
1299
- ## 🐛 Broker Debug (`--brokerdebug`)
1300
-
1301
- `@backtest-kit/cli` can fire a single broker commit against your live broker adapter without running a full strategy — useful for verifying that your `brokerdebug.module` correctly wires up exchange calls.
1302
-
1303
- ### CLI Flags
1304
-
1305
- | Flag | Type | Description |
1306
- |------|------|-------------|
1307
- | `--brokerdebug` | boolean | Enable broker debug mode |
1308
- | `--commit` | string | Commit type to fire (default: `"signal-open"`) |
1309
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1310
- | `--exchange` | string | Exchange name (default: first registered) |
1311
-
1312
- **Available `--commit` values:**
1313
-
1314
- | Value | Broker hook |
1315
- |-------|-------------|
1316
- | `signal-open` | `onSignalOpenCommit` |
1317
- | `signal-close` | `onSignalCloseCommit` |
1318
- | `partial-profit` | `onPartialProfitCommit` |
1319
- | `partial-loss` | `onPartialLossCommit` |
1320
- | `average-buy` | `onAverageBuyCommit` |
1321
- | `trailing-stop` | `onTrailingStopCommit` |
1322
- | `trailing-take` | `onTrailingTakeCommit` |
1323
- | `breakeven` | `onBreakevenCommit` |
1324
-
1325
- ### How It Works
1326
-
1327
- The CLI loads `./modules/brokerdebug.module`, fetches the last candle for `--symbol` / `--timeframe`, derives synthetic payload values from `currentPrice` (TP = +2%, SL = -2%), and calls the selected broker hook once. Exits with code `0` on success.
1328
-
1329
- ### Broker via `brokerdebug.module`
1330
-
1331
- Create a `modules/brokerdebug.module.ts` file and register your broker adapter:
1332
-
1333
- ```typescript
1334
- // modules/brokerdebug.module.ts
1335
- import { Broker } from 'backtest-kit';
1336
- import { myExchange } from './exchange.mjs';
1337
-
1338
- class MyBroker {
1339
- async onSignalOpenCommit({ symbol, priceOpen, position }) {
1340
- await myExchange.openPosition(symbol, position, priceOpen);
1341
- }
1342
- // ... other hooks
1343
- }
1344
-
1345
- Broker.useBrokerAdapter(MyBroker);
1346
- Broker.enable();
1347
- ```
1348
-
1349
- ### Usage
1350
-
1351
- ```bash
1352
- npx @backtest-kit/cli --brokerdebug --commit signal-open --symbol BTCUSDT
1353
- npx @backtest-kit/cli --brokerdebug --commit partial-profit --symbol ETHUSDT --timeframe 1h
1354
- ```
1355
-
1356
- ## 🗑️ Flushing Strategy Output (`--flush`)
1357
-
1358
- `@backtest-kit/cli` can delete generated output folders from one or more strategy dump directories without touching cached candle data.
1359
-
1360
- ### CLI Flags
1361
-
1362
- | Flag | Type | Description |
1363
- |------|------|-------------|
1364
- | `--flush` | boolean | Enable flush mode |
1365
-
1366
- **Positional arguments (required):** one or more strategy entry point files. For each entry point the CLI resolves its directory and removes the following subdirectories from `<entry-dir>/dump/`:
1367
-
1368
- | Folder | Contents |
1369
- |--------|----------|
1370
- | `report` | Backtest report files (`.jsonl`) |
1371
- | `log` | Run logs (`log.jsonl`) |
1372
- | `markdown` | Exported Markdown reports |
1373
- | `agent` | Agent outline files |
1374
-
1375
- Candle cache (`dump/data/`) and AI forecast outlines (`dump/outline/`) are **not** removed.
1376
-
1377
- ### Usage
1378
-
1379
- Flush a single strategy:
1380
-
1381
- ```bash
1382
- npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts
1383
- ```
1384
-
1385
- Flush multiple strategies at once:
1386
-
1387
- ```bash
1388
- npx @backtest-kit/cli --flush \
1389
- ./content/feb_2026.strategy/modules/backtest.module.ts \
1390
- ./content/mar_2026.strategy/modules/backtest.module.ts
1391
- ```
1392
-
1393
- Or add it to `package.json`:
1394
-
1395
- ```json
1396
- {
1397
- "scripts": {
1398
- "flush": "npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts"
1399
- }
1400
- }
1401
- ```
1402
-
1403
- ```bash
1404
- npm run flush
1405
- ```
1406
-
1407
- ## 🗂️ Scaffolding a New Project (`--init`)
1408
-
1409
- `@backtest-kit/cli` can bootstrap a ready-to-use project directory with a pre-configured layout, example strategy files, and all documentation fetched automatically.
1410
-
1411
- ### CLI Flags
1412
-
1413
- | Flag | Type | Description |
1414
- |------|------|-------------|
1415
- | `--init` | boolean | Scaffold a new project |
1416
- | `--output` | string | Target directory name (default: `backtest-kit-project`) |
1417
-
1418
- ### Usage
1419
-
1420
- ```bash
1421
- npx @backtest-kit/cli --init
1422
- ```
1423
-
1424
- Creates `./backtest-kit-project/` in the current working directory.
1425
-
1426
- Override the directory name with `--output`:
1427
-
1428
- ```bash
1429
- npx @backtest-kit/cli --init --output my-trading-bot
1430
- ```
1431
-
1432
- Creates `./my-trading-bot/`.
1433
-
1434
- The target directory must not exist or must be empty — the command aborts if it contains any files.
1435
-
1436
- ### Generated Project Structure
1437
-
1438
- ```
1439
- backtest-kit-project/
1440
- ├── package.json # pre-configured with all backtest-kit dependencies
1441
- ├── .gitignore
1442
- ├── CLAUDE.md # AI-agent guide for writing strategies
1443
- ├── content/
1444
- │ └── feb_2026.strategy.ts # example strategy entry point
1445
- ├── docs/
1446
- │ ├── lib/ # fetched automatically (see below)
1447
- │ ├── backtest_actions.md
1448
- │ ├── backtest_graph_pattern.md
1449
- │ ├── backtest_logging_jsonl.md
1450
- │ ├── backtest_pinets_usage.md
1451
- │ ├── backtest_risk_async.md
1452
- │ ├── backtest_strategy_structure.md
1453
- │ ├── pine_debug.md
1454
- │ └── pine_indicator_warmup.md
1455
- ├── math/
1456
- │ └── feb_2026.pine # example PineScript indicator
1457
- ├── modules/
1458
- │ ├── dump.module.ts # exchange schema for --dump mode
1459
- │ └── pine.module.ts # exchange schema for --pine mode
1460
- ├── report/
1461
- │ └── feb_2026.md # example strategy research report
1462
- └── scripts/
1463
- └── fetch_docs.mjs # utility: downloads library READMEs into docs/lib/
1464
- ```
1465
-
1466
- ### Automatic Documentation Fetch
1467
-
1468
- After scaffolding, the CLI immediately runs `scripts/fetch_docs.mjs` inside the new project, which downloads the latest README files for all bundled libraries into `docs/lib/`:
1469
-
1470
- | File | Source |
1471
- |------|--------|
1472
- | `backtest-kit.md` | `backtest-kit` README |
1473
- | `backtest-kit__graph.md` | `@backtest-kit/graph` README |
1474
- | `backtest-kit__pinets.md` | `@backtest-kit/pinets` README |
1475
- | `backtest-kit__cli.md` | `@backtest-kit/cli` README |
1476
- | `garch.md` | `garch` README |
1477
- | `volume-anomaly.md` | `volume-anomaly` README |
1478
- | `agent-swarm-kit.md` | `agent-swarm-kit` README |
1479
- | `functools-kit.md` | `functools-kit` README |
1480
-
1481
- You can re-run this script at any time to refresh the docs:
1482
-
1483
- ```bash
1484
- cd backtest-kit-project
1485
- node ./scripts/fetch_docs.mjs
1486
- ```
1487
-
1488
- Or via the pre-configured npm script:
1489
-
1490
- ```bash
1491
- npm run sync:lib
1492
- ```
1493
-
1494
- ## 🐳 Running in Docker (`--docker`)
1495
-
1496
- CLI can create a ready-to-use Docker workspace: self-contained directory with `docker-compose.yaml` and a strategy entry point.
1497
-
1498
- ### CLI Flags
1499
-
1500
- | Flag | Type | Description |
1501
- |------|------|-------------|
1502
- | `--docker` | boolean | Scaffold a Docker workspace |
1503
- | `--output` | string | Target directory name (default: `backtest-kit-docker`) |
1504
-
1505
- ### Usage
1506
-
1507
- ```bash
1508
- npx @backtest-kit/cli --docker
1509
- ```
1510
-
1511
- Creates `./backtest-kit-docker/` in the current working directory.
1512
-
1513
- Override the directory name with `--output`:
1514
-
1515
- ```bash
1516
- npx @backtest-kit/cli --docker --output my-docker-workspace
1517
- ```
1518
-
1519
- The target directory must not exist or must be empty — the command aborts if it contains any files.
1520
-
1521
- ### Two Launch Modes
1522
-
1523
- The Docker image entrypoint supports two ways to run a strategy:
1524
-
1525
- #### 1. `command:` in `docker-compose.yaml`
1526
-
1527
- Pin mode and flags directly in the compose file. The entrypoint forwards all arguments to the CLI unchanged:
1528
-
1529
- ```yaml
1530
- command:
1531
- - --live
1532
- - --symbol
1533
- - TRXUSDT
1534
- - --strategy
1535
- - feb_2026_strategy
1536
- - --exchange
1537
- - ccxt-exchange
1538
- - ./content/feb_2026/feb_2026.strategy.ts
1539
- - --ui
1540
- ```
1541
-
1542
- #### 2. Inline environment variables
1543
-
1544
- Pass `MODE` and `STRATEGY_FILE` on the command line — no file edits needed:
1545
-
1546
- ```bash
1547
- MODE=live SYMBOL=TRXUSDT STRATEGY_FILE=./content/feb_2026/feb_2026.strategy.ts docker-compose up -d
1548
- ```
1549
-
1550
- | Variable | Required | Default | Description |
1551
- |----------|----------|---------|-------------|
1552
- | `MODE` | yes | — | `backtest` \| `live` \| `paper` \| `walker` |
1553
- | `STRATEGY_FILE` | yes | — | Path to strategy entry point (relative to `working_dir`) |
1554
- | `SYMBOL` | no | `BTCUSDT` | Trading pair |
1555
- | `STRATEGY` | no | first registered | Strategy name |
1556
- | `EXCHANGE` | no | first registered | Exchange name |
1557
- | `FRAME` | no | first registered | Frame name (backtest only) |
1558
- | `UI` | no | — | Any non-empty value enables `--ui` |
1559
- | `TELEGRAM` | no | — | Any non-empty value enables `--telegram` |
1560
- | `VERBOSE` | no | — | Any non-empty value enables `--verbose` |
1561
- | `NO_CACHE` | no | — | Any non-empty value enables `--noCache` |
1562
- | `NO_FLUSH` | no | — | Any non-empty value enables `--noFlush` |
1563
- | `ENTRY` | no | — | Any non-empty value enables multiple symbols from userspace |
1564
-
1565
- ## 🌍 Environment Variables
1566
-
1567
- Create a `.env` file in your project root:
1568
-
1569
- ```env
1570
- # Telegram notifications (required for --telegram)
1571
- CC_TELEGRAM_TOKEN=your_bot_token_here
1572
- CC_TELEGRAM_CHANNEL=-100123456789
1573
-
1574
- # Web UI server (optional, defaults shown)
1575
- CC_WWWROOT_HOST=0.0.0.0
1576
- CC_WWWROOT_PORT=60050
1577
-
1578
- # Custom QuickChart service URL (optional)
1579
- CC_QUICKCHART_HOST=
1580
- ```
1581
-
1582
- | Variable | Default | Description |
1583
- |------------------------|-------------|---------------------------------------|
1584
- | `CC_TELEGRAM_TOKEN` | — | Telegram bot token (from @BotFather) |
1585
- | `CC_TELEGRAM_CHANNEL` | — | Telegram channel or chat ID |
1586
- | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1587
- | `CC_WWWROOT_PORT` | `60050` | UI server port |
1588
- | `CC_QUICKCHART_HOST` | | Self-hosted QuickChart instance URL |
1589
-
1590
- ## ⚙️ Default Behaviors
1591
-
1592
- When your strategy module does not register an exchange, frame, or strategy name, the CLI falls back to built-in defaults and prints a console warning:
1593
-
1594
- | Component | Default | Warning |
1595
- |--------------|--------------------------------|---------------------------------------------------------------------------|
1596
- | **Exchange** | CCXT Binance (`default_exchange`) | `Warning: The default exchange schema is set to CCXT Binance...` |
1597
- | **Frame** | February 2024 (`default_frame`) | `Warning: The default frame schema is set to February 2024...` |
1598
- | **Symbol** | `BTCUSDT` | — |
1599
- | **Cache intervals** | `1m, 15m, 30m, 4h` | Used if `--cacheInterval` not provided; skip entirely with `--noCache` |
1600
-
1601
- > **Note:** The default exchange schema **does not support order book fetching in backtest mode**. If your strategy calls `getOrderBook()` during backtest, you must register a custom exchange schema with your own snapshot storage.
1602
-
1603
- ## 🔧 Programmatic API
1604
-
1605
- In addition to the CLI, `@backtest-kit/cli` can be used as a library — call `run()` directly from your own script without spawning a child process or parsing CLI flags.
1606
-
1607
- ### `run(mode, args)`
1608
-
1609
- ```typescript
1610
- import { run } from '@backtest-kit/cli';
1611
-
1612
- await run(mode, args);
1613
- ```
1614
-
1615
- | Parameter | Description |
1616
- |-----------|-------------|
1617
- | `mode` | `"backtest" \| "paper" \| "live"` — Execution mode |
1618
- | `args` | Mode-specific options (all optional same defaults as CLI) |
1619
-
1620
- `run()` can be called **only once per process**. A second call throws `"Should be called only once"`.
1621
-
1622
- ### Payload fields
1623
-
1624
- **Backtest** (`mode: "backtest"`):
1625
-
1626
- | Field | Type | Description |
1627
- |-------|------|-------------|
1628
- | `entryPoint` | `string` | Path to strategy entry point file |
1629
- | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1630
- | `strategy` | `string` | Strategy name (default: first registered) |
1631
- | `exchange` | `string` | Exchange name (default: first registered) |
1632
- | `frame` | `string` | Frame name (default: first registered) |
1633
- | `cacheInterval` | `CandleInterval[]` | Intervals to pre-cache (default: `["1m","15m","30m","1h","4h"]`) |
1634
- | `noCache` | `boolean` | Skip candle cache warming (default: `false`) |
1635
- | `noFlush` | `boolean` | Skip removing report/log/markdown/agent folders before the run (default: `false`) |
1636
- | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1637
-
1638
- **Paper** and **Live** (`mode: "paper"` / `mode: "live"`):
1639
-
1640
- | Field | Type | Description |
1641
- |-------|------|-------------|
1642
- | `entryPoint` | `string` | Path to strategy entry point file |
1643
- | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1644
- | `strategy` | `string` | Strategy name (default: first registered) |
1645
- | `exchange` | `string` | Exchange name (default: first registered) |
1646
- | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1647
-
1648
- ### Examples
1649
-
1650
- **Backtest:**
1651
-
1652
- ```typescript
1653
- import { run } from '@backtest-kit/cli';
1654
-
1655
- await run('backtest', {
1656
- entryPoint: './src/index.mjs',
1657
- symbol: 'ETHUSDT',
1658
- frame: 'feb-2024',
1659
- cacheInterval: ['1m', '15m', '1h'],
1660
- verbose: true,
1661
- });
1662
- ```
1663
-
1664
- **Paper trading:**
1665
-
1666
- ```typescript
1667
- import { run } from '@backtest-kit/cli';
1668
-
1669
- await run('paper', {
1670
- entryPoint: './src/index.mjs',
1671
- symbol: 'BTCUSDT',
1672
- });
1673
- ```
1674
-
1675
- **Live trading:**
1676
-
1677
- ```typescript
1678
- import { run } from '@backtest-kit/cli';
1679
-
1680
- await run('live', {
1681
- entryPoint: './src/index.mjs',
1682
- symbol: 'BTCUSDT',
1683
- verbose: true,
1684
- });
1685
- ```
1686
-
1687
- ## 💡 Why Use @backtest-kit/cli?
1688
-
1689
- Instead of writing infrastructure code for every project:
1690
-
1691
- **❌ Without @backtest-kit/cli (manual setup)**
1692
-
1693
- ```typescript
1694
- // index.ts
1695
- import { setLogger, setConfig, Storage, Notification, Report, Markdown } from 'backtest-kit';
1696
- import { serve } from '@backtest-kit/ui';
1697
-
1698
- setLogger({ log: console.log, ... });
1699
- Storage.enable();
1700
- Notification.enable();
1701
- Report.enable();
1702
- Markdown.disable();
1703
-
1704
- // ... parse CLI args manually
1705
- // ... register exchange schema
1706
- // ... warm candle cache
1707
- // ... set up Telegram bot
1708
- // ... handle SIGINT gracefully
1709
- // ... load and run backtest
1710
- ```
1711
-
1712
- **✅ With @backtest-kit/cli (one script)**
1713
-
1714
- ```json
1715
- { "scripts": { "backtest": "npx @backtest-kit/cli --backtest --ui --telegram ./src/index.mjs" } }
1716
- ```
1717
-
1718
- ```bash
1719
- npm run backtest
1720
- ```
1721
-
1722
- **Benefits:**
1723
-
1724
- - 🚀 From zero to running backtest in seconds
1725
- - 💾 Automatic candle cache warming with retry logic
1726
- - 🌐 Production-ready web dashboard out of the box
1727
- - 📬 Telegram notifications with price charts — no chart code needed
1728
- - 🛑 Graceful shutdown on SIGINT — no hanging processes
1729
- - 🔌 Works with any `backtest-kit` strategy file as-is
1730
- - 🧩 Broker adapter hooks via side-effect module files — no CLI internals to touch
1731
-
1732
- ## 🤝 Contribute
1733
-
1734
- Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
1735
-
1736
- ## 📜 License
1737
-
1738
- MIT © [tripolskypetr](https://github.com/tripolskypetr)
1
+ <img src="https://github.com/tripolskypetr/backtest-kit/raw/refs/heads/master/assets/square_compasses.svg" height="45px" align="right">
2
+
3
+ # 📟 @backtest-kit/cli
4
+
5
+ > Zero-boilerplate CLI for launching backtests, paper trading, and live trading. Run any backtest-kit strategy from the command line — no setup code required.
6
+
7
+ ![screenshot](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot16.png)
8
+
9
+ [![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/tripolskypetr/backtest-kit)
10
+ [![npm](https://img.shields.io/npm/v/@backtest-kit/cli.svg?style=flat-square)](https://npmjs.org/package/@backtest-kit/cli)
11
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.0+-blue)]()
12
+
13
+ Point the CLI at your strategy file, choose a mode, and it handles exchange connectivity, candle caching, UI dashboard, and Telegram notifications for you.
14
+
15
+ 📚 **[Backtest Kit Docs](https://backtest-kit.github.io/documents/article_07_ai_news_trading_signals.html)** | 🌟 **[GitHub](https://github.com/tripolskypetr/backtest-kit)**
16
+
17
+ > **New to backtest-kit?** The fastest way to get a real, production-ready setup is to clone the [reference implementation](https://github.com/tripolskypetr/backtest-kit/tree/master/example) — a fully working news-sentiment AI trading system with LLM forecasting, multi-timeframe data, and a documented February 2026 backtest. Start there instead of from scratch.
18
+
19
+ ## 🏎️ CLI Init
20
+
21
+ Minimal scaffold all boilerplate stays inside @backtest-kit/cli:
22
+
23
+ ```bash
24
+ npx @backtest-kit/cli --init --output backtest-kit-project
25
+ cd backtest-kit-project
26
+ npm install
27
+ npm start -- --help
28
+ ```
29
+
30
+ ## 🤔 Philosophy
31
+
32
+ `@backtest-kit/cli` is designed to do **two things well** — and the same tool covers both.
33
+
34
+ **1. The lightest possible runner for a solo quant on day one.**
35
+
36
+ You write a strategy file, point the CLI at it, and you're trading. No DI container to learn, no project scaffold to fight, no infrastructure code to copy-paste. One developer, one strategy, one command:
37
+
38
+ ```bash
39
+ npx @backtest-kit/cli --init
40
+ npx @backtest-kit/cli --backtest ./content/feb_2026.strategy/index.ts
41
+ ```
42
+
43
+ That's the whole onboarding. The first day you have an idea, you can backtest it. The first week you have an edge, you can paper-trade it. The first month you have a P&L, you can run it live — same CLI, different flag.
44
+
45
+ **2. Built-in monorepo tooling for when the business takes off.**
46
+
47
+ The moment you start making money is the worst possible moment to rewrite your stack in another language. So the CLI is also a monorepo-grade runner from day one — even if you don't use it that way at first.
48
+
49
+ ```
50
+ monorepo/
51
+ ├── content/
52
+ │ ├── feb_2026.strategy/
53
+ │ ├── feb_2026.strategy.ts # strategy production code
54
+ │ ├── feb_2026.test.ts # developer playground
55
+ ├── packages/
56
+ │ ├── shared-broker/ # shared broker code
57
+ │ ├── shared-signals/ # common indicators (RSI, MACD)
58
+ ```
59
+
60
+
61
+ As a result: you used to backtest your first idea is the same tool you use to run a desk of strategies in production. No rewrite, no language switch, no framework migration when the business scales — only more files in the monorepo.
62
+
63
+ ## Features
64
+
65
+ - 🚀 **Zero Config**: Run `npx @backtest-kit/cli --backtest ./strategy.mjs` — no boilerplate needed
66
+ - 🔄 **Four Modes**: Backtest on historical data, walker A/B comparison, paper trade on live prices, or deploy live bots
67
+ - 💾 **Auto Candle Cache**: Warms OHLCV cache for all required intervals before backtest starts
68
+ - 🌐 **Web Dashboard**: Launch `@backtest-kit/ui` with a single `--ui` flag
69
+ - 📬 **Telegram Alerts**: Send formatted trade notifications with charts via `--telegram`
70
+ - 🔌 **Default Binance**: CCXT Binance exchange schema registered automatically when none is provided
71
+ - 🧩 **Module Hooks**: Drop a `live.module.mjs`, `paper.module.mjs`, or `backtest.module.mjs` to register a `Broker` adapter. No manual wiring needed.
72
+ - 🗃️ **Transactional Live Orders**: Broker adapter intercepts every trade mutation before internal state changes — exchange rejection rolls back the operation atomically.
73
+ - 🔑 **Pluggable Logger**: Override the built-in logger with `setLogger()` from your strategy module
74
+ - 🛑 **Graceful Shutdown**: SIGINT stops the active run and cleans up all subscriptions safely
75
+
76
+ ## 📋 What It Does
77
+
78
+ `@backtest-kit/cli` wraps the `backtest-kit` engine and resolves all scaffolding automatically:
79
+
80
+ | Mode | Command Line Args | Description |
81
+ |------------------|----------------------------|----------------------------------------------|
82
+ | **Backtest** | `--backtest` | Run strategy on historical candle data |
83
+ | **Walker** | `--walker` | A/B compare multiple strategies on the same historical data |
84
+ | **Paper** | `--paper` | Live prices, no real orders |
85
+ | **Live** | `--live` | Real trades via exchange API |
86
+ | **UI Dashboard** | `--ui` | Web dashboard at `http://localhost:60050` |
87
+ | **Telegram** | `--telegram` | Trade notifications with price charts |
88
+ | **PineScript** | `--pine` | Run a local `.pine` indicator against exchange data |
89
+ | **Pine Editor** | `--editor` | Open the visual Pine Script editor in the browser |
90
+ | **Candle Dump** | `--dump` | Fetch and save raw OHLCV candles to a file |
91
+ | **PnL Debug** | `--pnldebug` | Simulate per-minute PnL for a given entry price and direction |
92
+ | **Broker Debug** | `--brokerdebug` | Fire a single broker commit against the live broker adapter |
93
+ | **Flush** | `--flush` | Delete report/log/markdown/agent folders from strategy dump dir |
94
+ | **Init Project** | `--init` | Scaffold a new backtest-kit project |
95
+
96
+ ## 🚀 Installation
97
+
98
+ Add `@backtest-kit/cli` to your project and wire it up in `package.json` scripts:
99
+
100
+ ```bash
101
+ npm install @backtest-kit/cli
102
+ ```
103
+
104
+ ```json
105
+ {
106
+ "scripts": {
107
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs",
108
+ "paper": "npx @backtest-kit/cli --paper ./src/index.mjs",
109
+ "start": "npx @backtest-kit/cli --live ./src/index.mjs"
110
+ },
111
+ "dependencies": {
112
+ "@backtest-kit/cli": "latest",
113
+ "backtest-kit": "latest",
114
+ "ccxt": "latest"
115
+ }
116
+ }
117
+ ```
118
+
119
+ Or run once without installing:
120
+
121
+ ```bash
122
+ npx @backtest-kit/cli --backtest ./src/index.mjs
123
+ ```
124
+
125
+ ## 📖 Quick Start
126
+
127
+ Create your strategy entry point (`src/index.mjs`). The file registers schemas via `backtest-kit` — `@backtest-kit/cli` is only the runner:
128
+
129
+ ```javascript
130
+ // src/index.mjs
131
+ import { addStrategySchema, addExchangeSchema, addFrameSchema } from 'backtest-kit';
132
+ import ccxt from 'ccxt';
133
+
134
+ // Register exchange
135
+ addExchangeSchema({
136
+ exchangeName: 'binance',
137
+ getCandles: async (symbol, interval, since, limit) => {
138
+ const exchange = new ccxt.binance();
139
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
140
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
141
+ timestamp, open, high, low, close, volume,
142
+ }));
143
+ },
144
+ formatPrice: (symbol, price) => price.toFixed(2),
145
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
146
+ });
147
+
148
+ // Register frame (backtest only)
149
+ addFrameSchema({
150
+ frameName: 'feb-2024',
151
+ interval: '1m',
152
+ startDate: new Date('2024-02-01'),
153
+ endDate: new Date('2024-02-29'),
154
+ });
155
+
156
+ // Register strategy
157
+ addStrategySchema({
158
+ strategyName: 'my-strategy',
159
+ interval: '15m',
160
+ getSignal: async (symbol) => {
161
+ // return signal or null
162
+ return null;
163
+ },
164
+ });
165
+ ```
166
+
167
+ Run a backtest:
168
+
169
+ ```bash
170
+ npm run backtest -- --symbol BTCUSDT
171
+ ```
172
+
173
+ Run with UI dashboard and Telegram:
174
+
175
+ ```bash
176
+ npm run backtest -- --symbol BTCUSDT --ui --telegram
177
+ ```
178
+
179
+ Run live trading:
180
+
181
+ ```bash
182
+ npm start -- --symbol BTCUSDT --ui
183
+ ```
184
+
185
+ ## 🎛️ CLI Flags
186
+
187
+ | Command Line Args | Type | Description |
188
+ |---------------------------|---------|--------------------------------------------------------------------|
189
+ | `--backtest` | boolean | Run historical backtest (default: `false`) |
190
+ | `--walker` | boolean | Run Walker A/B strategy comparison (default: `false`) |
191
+ | `--paper` | boolean | Paper trading (live prices, no orders) (default: `false`) |
192
+ | `--live` | boolean | Run live trading (default: `false`) |
193
+ | `--ui` | boolean | Start web UI dashboard (default: `false`) |
194
+ | `--telegram` | boolean | Enable Telegram notifications (default: `false`) |
195
+ | `--verbose` | boolean | Log each candle fetch (default: `false`) |
196
+ | `--noCache` | boolean | Skip candle cache warming before backtest (default: `false`) |
197
+ | `--noFlush` | boolean | Skip removing report/log/markdown/agent folders before backtest run (default: `false`) |
198
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
199
+ | `--strategy` | string | Strategy name (default: first registered) |
200
+ | `--exchange` | string | Exchange name (default: first registered) |
201
+ | `--frame` | string | Backtest frame name (default: first registered) |
202
+ | `--cacheInterval` | string | Intervals to pre-cache before backtest (default: `"1m, 15m, 30m, 4h"`) |
203
+ | `--brokerdebug` | boolean | Fire a single broker commit against the live broker adapter (default: `false`) |
204
+ | `--commit` | string | Commit type for `--brokerdebug` (default: `"signal-open"`) |
205
+
206
+ **Positional argument (required):** path to your strategy entry point file (set once in `package.json` scripts).
207
+
208
+ ```json
209
+ {
210
+ "scripts": {
211
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
212
+ }
213
+ }
214
+ ```
215
+
216
+ ## 🏃 Execution Modes
217
+
218
+ ### Backtest
219
+
220
+ Runs the strategy against historical candle data using a registered `FrameSchema`.
221
+
222
+ ```json
223
+ {
224
+ "scripts": {
225
+ "backtest": "npx @backtest-kit/cli --backtest --symbol ETHUSDT --strategy my-strategy --exchange binance --frame feb-2024 --cacheInterval \"1m, 15m, 1h, 4h\" ./src/index.mjs"
226
+ }
227
+ }
228
+ ```
229
+
230
+ ```bash
231
+ npm run backtest
232
+ ```
233
+
234
+ Before running, the CLI removes the `report`, `log`, `markdown`, and `agent` folders from the strategy's `dump/` directory, then warms the candle cache for every interval in `--cacheInterval`. On the next run, cached data is used directly — no API calls needed. Pass `--noCache` to skip cache warming, `--noFlush` to keep existing output folders.
235
+
236
+ ### Paper Trading
237
+
238
+ Connects to the live exchange but does not place real orders. Identical code path to live — safe for strategy validation.
239
+
240
+ ```json
241
+ {
242
+ "scripts": {
243
+ "paper": "npx @backtest-kit/cli --paper --symbol BTCUSDT ./src/index.mjs"
244
+ }
245
+ }
246
+ ```
247
+
248
+ ```bash
249
+ npm run paper
250
+ ```
251
+
252
+ ### Live Trading
253
+
254
+ Deploys a real trading bot. Requires exchange API keys configured in your `.env` or environment.
255
+
256
+ ```json
257
+ {
258
+ "scripts": {
259
+ "start": "npx @backtest-kit/cli --live --ui --telegram --symbol BTCUSDT ./src/index.mjs"
260
+ }
261
+ }
262
+ ```
263
+
264
+ ```bash
265
+ npm start
266
+ ```
267
+
268
+ ### Walker — A/B Strategy Comparison
269
+
270
+ Runs the same historical period against multiple strategy files and prints a ranked comparison report. Use it to pick the best variant before deploying to backtest or live.
271
+
272
+ ```json
273
+ {
274
+ "scripts": {
275
+ "walker": "npx @backtest-kit/cli --walker --symbol BTCUSDT --noCache ./content/feb_2026_v1.strategy.ts ./content/feb_2026_v2.strategy.ts ./content/feb_2026_v3.strategy.ts"
276
+ }
277
+ }
278
+ ```
279
+
280
+ ```bash
281
+ npm run walker
282
+ ```
283
+
284
+ Each positional argument is a separate strategy entry point. Before loading them, the CLI removes the `report`, `log`, `markdown`, and `agent` folders from each entry point's `dump/` directory. Pass `--noFlush` to keep existing output. All files are loaded without changing `process.cwd()` `.env` is read from the working directory only. After loading, `addWalkerSchema` is called automatically using the exchange and frame registered by the strategy files.
285
+
286
+ If no frame is registered, the CLI falls back to the last 31 days from `Date.now()` with a console warning.
287
+
288
+ **Walker-specific flags:**
289
+
290
+ | Flag | Type | Description |
291
+ |------|------|-------------|
292
+ | `--walker` | boolean | Enable Walker comparison mode |
293
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
294
+ | `--cacheInterval` | string | Intervals to pre-cache (default: `"1m, 15m, 30m, 4h"`) |
295
+ | `--noCache` | boolean | Skip candle cache warming (default: `false`) |
296
+ | `--noFlush` | boolean | Skip removing report/log/markdown/agent folders before walker run (default: `false`) |
297
+ | `--verbose` | boolean | Log each candle fetch and strategy progress (default: `false`) |
298
+ | `--output` | string | Output file base name (default: `walker_{SYMBOL}_{TIMESTAMP}`) |
299
+ | `--json` | boolean | Save results as JSON to `./dump/<output>.json` |
300
+ | `--markdown` | boolean | Save report as Markdown to `./dump/<output>.md` |
301
+
302
+ **Output modes:**
303
+
304
+ - No flag — print Markdown report to stdout
305
+ - `--json` — save `Walker.getData()` result as JSON and exit
306
+ - `--markdown` — save `Walker.getReport()` as `.md` file and exit
307
+
308
+ **Module hook:** `./modules/walker.module` is loaded automatically before the comparison starts (same rules as other modes — `.ts`, `.mjs`, `.cjs` tried in order).
309
+
310
+ **Example compare three variants and save the report:**
311
+
312
+ ```bash
313
+ npx @backtest-kit/cli --walker \
314
+ --symbol BTCUSDT \
315
+ --noCache \
316
+ --markdown \
317
+ --output feb_2026_comparison \
318
+ ./content/feb_2026_v1.strategy.ts \
319
+ ./content/feb_2026_v2.strategy.ts \
320
+ ./content/feb_2026_v3.strategy.ts
321
+ # ./dump/feb_2026_comparison.md
322
+ ```
323
+
324
+ ## 🐙 Multiple Symbol Parallel
325
+
326
+ > **For Poweruser — skip unless needed.** The standard flow runs one symbol from `--symbol`. Use `--entry` only to fan out one strategy across many symbols at once, or to drive `*.background()` from a UI / DB / API.
327
+
328
+ With `--entry`, the CLI does only the boilerplate — `Setup`, providers (`--ui` / `--telegram`), the matching `./modules/<mode>.module`, SIGINT that stops every active run via `*.list()`, and `shutdown()` once `listenDone*` reports all your runs complete. Picking the symbol set, warming cache, and calling `*.background()` is on you.
329
+
330
+ The `--entry` flag is a modifier — combine it with exactly one of `--backtest` / `--live` / `--paper` / `--walker`. One positional: the path to your entry file.
331
+
332
+ ```bash
333
+ npx @backtest-kit/cli --backtest --entry ./src/multi-symbol.mjs
334
+ ```
335
+
336
+ ### Example: backtest a strategy on five symbols at once
337
+
338
+ ```javascript
339
+ // src/multi-symbol.mjs
340
+ import {
341
+ addExchangeSchema,
342
+ addFrameSchema,
343
+ addStrategySchema,
344
+ Backtest,
345
+ warmCandles,
346
+ } from "backtest-kit";
347
+ import ccxt from "ccxt";
348
+
349
+ addExchangeSchema({
350
+ exchangeName: "binance",
351
+ getCandles: async (symbol, interval, since, limit) => {
352
+ const exchange = new ccxt.binance();
353
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
354
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
355
+ timestamp, open, high, low, close, volume,
356
+ }));
357
+ },
358
+ formatPrice: (symbol, price) => price.toFixed(2),
359
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
360
+ });
361
+
362
+ addFrameSchema({
363
+ frameName: "feb-2026",
364
+ interval: "1m",
365
+ startDate: new Date("2026-02-01"),
366
+ endDate: new Date("2026-02-28"),
367
+ });
368
+
369
+ addStrategySchema({
370
+ strategyName: "my-strategy",
371
+ interval: "15m",
372
+ getSignal: async (symbol) => null,
373
+ });
374
+
375
+ // Decide the symbol set yourself — from a UI, database, API, or just a list.
376
+ const symbols = ["BTCUSDT", "ETHUSDT", "SOLUSDT", "BNBUSDT", "XRPUSDT"];
377
+
378
+ for (const symbol of symbols) {
379
+
380
+ //
381
+ // Optional
382
+ //
383
+ // await warmCandles({
384
+ // exchangeName: "binance,
385
+ // from: new Date("2026-01-01T00:00:00Z"),
386
+ // to: new Date("2026-01-31T23:59:59Z"),
387
+ // interval: "1m",
388
+ // symbol,
389
+ // })
390
+
391
+
392
+ Backtest.background(symbol, {
393
+ strategyName: "my-strategy",
394
+ exchangeName: "binance",
395
+ frameName: "feb-2026",
396
+ });
397
+ }
398
+ ```
399
+
400
+ The same shape works for `--live --entry` / `--paper --entry` (call `Live.background()` per symbol with your broker adapter)
401
+
402
+ ## 🗂️ Monorepo Usage
403
+
404
+ `@backtest-kit/cli` works out of the box in a monorepo where each strategy lives in its own subdirectory. When the CLI loads your entry point file, it automatically changes the working directory to the file's location — so all relative paths (`dump/`, `modules/`, `template/`) resolve inside that strategy's folder, not the project root.
405
+
406
+ ### How It Works
407
+
408
+ Internally, `ResolveService` does the following before executing your entry point:
409
+
410
+ ```
411
+ process.chdir(path.dirname(entryPoint)) // cwd → strategy directory
412
+ dotenv.config({ path: rootDir + '/.env' }) // load root .env first
413
+ dotenv.config({ path: strategyDir + '/.env', override: true }) // strategy .env overrides
414
+ ```
415
+
416
+ Everything that follows — candle cache warming, report generation, module loading, template resolution — uses the new cwd automatically.
417
+
418
+ ### Project Structure
419
+
420
+ ```
421
+ monorepo/
422
+ ├── package.json # root scripts (one per strategy)
423
+ ├── .env # shared API keys (exchange, Telegram, etc.)
424
+ └── strategies/
425
+ ├── oct_2025/
426
+ ├── index.mjs # entry point — registers exchange/frame/strategy schemas
427
+ │ ├── .env # overrides root .env for this strategy
428
+ │ ├── modules (optional)
429
+ │ | ├── live.module.mjs # broker adapter for --live mode (optional)
430
+ │ | ├── paper.module.mjs # broker adapter for --paper mode (optional)
431
+ │ | ├── backtest.module.mjs # broker adapter for --backtest mode (optional)
432
+ │ ├── template/ # custom Mustache templates (optional)
433
+ │ └── dump/ # auto-created: candle cache + backtest reports
434
+ └── dec_2025/
435
+ ├── index.mjs
436
+ ├── .env
437
+ └── dump/
438
+ ```
439
+
440
+ ### Root `package.json`
441
+
442
+ ```json
443
+ {
444
+ "scripts": {
445
+ "backtest:oct": "npx @backtest-kit/cli --backtest ./strategies/oct_2025/index.mjs",
446
+ "backtest:dec": "npx @backtest-kit/cli --backtest ./strategies/dec_2025/index.mjs"
447
+ },
448
+ "dependencies": {
449
+ "@backtest-kit/cli": "latest",
450
+ "backtest-kit": "latest",
451
+ "ccxt": "latest"
452
+ }
453
+ }
454
+ ```
455
+
456
+ ```bash
457
+ npm run backtest:oct
458
+ npm run backtest:dec
459
+ ```
460
+
461
+ ### Isolated Resources Per Strategy
462
+
463
+ | Resource | Path (relative to strategy dir) | Isolated |
464
+ |--------------------------|-----------------------------------|------------------|
465
+ | Candle cache | `./dump/data/candle/` | ✅ per-strategy |
466
+ | Backtest reports | `./dump/` | ✅ per-strategy |
467
+ | Broker module (live) | `./modules/live.module.mjs` | ✅ per-strategy |
468
+ | Broker module (paper) | `./modules/paper.module.mjs` | per-strategy |
469
+ | Broker module (backtest) | `./modules/backtest.module.mjs` | ✅ per-strategy |
470
+ | Config module (walker) | `./modules/walker.module.mjs` | ✅ loaded once |
471
+ | Telegram templates | `./template/*.mustache` | ✅ per-strategy |
472
+ | Environment variables | `./.env` (overrides root) | ✅ per-strategy |
473
+
474
+ Each strategy run produces its own `dump/` directory, making it straightforward to compare results across time periods — both by inspection and by pointing an AI agent at a specific strategy folder.
475
+
476
+ ## 🔗 Shared Import Aliases
477
+
478
+ `@backtest-kit/cli` automatically turns every **top-level folder** in `process.cwd()` into a bare import alias available inside any strategy file. No configuration needed — just create the folder.
479
+
480
+ ### How It Works
481
+
482
+ When the CLI loads a strategy file, it scans the current working directory for subdirectories and registers each one as an import alias. The alias name is the folder name. Both barrel imports and deep subpath imports are supported:
483
+
484
+ | Import | Resolves to |
485
+ |--------|-------------|
486
+ | `import { fn } from "utils"` | `<cwd>/utils/index.ts` (or `.js`, `.mjs`, `.cjs`) |
487
+ | `import { calcRSI } from "math/rsi"` | `<cwd>/math/rsi.ts` |
488
+ | `import { research } from "logic"` | `<cwd>/logic/index.ts` |
489
+ | `import { ResearchResponseContract } from "logic/contract/ResearchResponse.contract"` | `<cwd>/logic/contract/ResearchResponse.contract.ts` |
490
+
491
+ ### Project Structure
492
+
493
+ ```
494
+ my-project/
495
+ ├── utils/ ← import { formatDate } from "utils"
496
+ │ └── index.ts
497
+ ├── math/ import { calcRSI } from "math/rsi"
498
+ │ └── rsi.ts
499
+ ├── logic/ ← import { research } from "logic"
500
+ │ ├── index.ts ← barrel
501
+ │ └── contract/
502
+ │ └── ResearchResponse.contract.ts ← import { ... } from "logic/contract/ResearchResponse.contract"
503
+ └── content/
504
+ ├── feb_2026.strategy.ts ← uses all three aliases freely
505
+ └── mar_2026.strategy.ts ← same aliases, no duplication
506
+ ```
507
+
508
+ This lets you extract shared utilities, math helpers, or AI agent logic (e.g. `agent-swarm-kit` workflows) into named folders and reuse them across every strategy in the project without relative path hell.
509
+
510
+ ### TypeScript Support
511
+
512
+ Add a matching `paths` entry to your `tsconfig.json` so the editor resolves the aliases:
513
+
514
+ ```json
515
+ {
516
+ "compilerOptions": {
517
+ "moduleResolution": "bundler",
518
+ "paths": {
519
+ "logic": ["./logic/index.ts"],
520
+ "logic/*": ["./logic/*"],
521
+ "math": ["./math/index.ts"],
522
+ "math/*": ["./math/*"],
523
+ "utils": ["./utils/index.ts"],
524
+ "utils/*": ["./utils/*"]
525
+ }
526
+ },
527
+ "include": [
528
+ "./logic",
529
+ "./math",
530
+ "./utils",
531
+ "./content",
532
+ "./modules",
533
+ ],
534
+ }
535
+ ```
536
+
537
+ ## 🔔 Integrations
538
+
539
+ ### Web Dashboard (`--ui`)
540
+
541
+ Starts `@backtest-kit/ui` server. Access the interactive dashboard at:
542
+
543
+ ```
544
+ http://localhost:60050
545
+ ```
546
+
547
+ Customize host/port via environment variables `CC_WWWROOT_HOST` and `CC_WWWROOT_PORT`.
548
+
549
+ #### Symbol List (`symbol.config`)
550
+
551
+ By default the UI shows all symbols from the exchange. To restrict or reorder the list, create a `config/symbol.config` file in your strategy directory (next to the entry point).
552
+
553
+ **Resolution order first match wins:**
554
+
555
+ | Priority | Path | Notes |
556
+ |----------|------|-------|
557
+ | 1 | `{strategyDir}/config/symbol.config` | per-strategy override (cwd after `chdir`) |
558
+ | 2 | `{projectRoot}/config/symbol.config` | project-root override (cwd where `npx` was invoked) |
559
+ | 3 | `@backtest-kit/cli/config/symbol.config` | built-in default shipped with the package |
560
+
561
+ Supported file formats (`.ts`, `.cjs`, `.mjs`, `.js` tried automatically):
562
+
563
+ ```ts
564
+ // config/symbol.config.ts
565
+ export const symbol_list = [
566
+ {
567
+ icon: "/icon/btc.png",
568
+ logo: "/icon/128/btc.png",
569
+ symbol: "BTCUSDT",
570
+ displayName: "Bitcoin",
571
+ color: "#F7931A",
572
+ priority: 50,
573
+ description: "Bitcoin - the first and most popular cryptocurrency",
574
+ },
575
+ {
576
+ icon: "/icon/eth.png",
577
+ logo: "/icon/128/eth.png",
578
+ symbol: "ETHUSDT",
579
+ displayName: "Ethereum",
580
+ color: "#6F42C1",
581
+ priority: 50,
582
+ description: "Ethereum - a blockchain platform for smart contracts",
583
+ },
584
+ ];
585
+ ```
586
+
587
+ #### Notification Filter (`notification.config`)
588
+
589
+ Controls which notification categories are shown in the UI dashboard. Create a `config/notification.config` file in your strategy directory to override the defaults.
590
+
591
+ **Resolution order — first match wins:**
592
+
593
+ | Priority | Path | Notes |
594
+ |----------|------|-------|
595
+ | 1 | `{strategyDir}/config/notification.config` | per-strategy override (cwd after `chdir`) |
596
+ | 2 | `{projectRoot}/config/notification.config` | project-root override (cwd where `npx` was invoked) |
597
+ | 3 | `@backtest-kit/cli/config/notification.config` | built-in default shipped with the package |
598
+
599
+ **Default values (built-in):**
600
+
601
+ | Key | Default | Description |
602
+ |-----|---------|-------------|
603
+ | `signal` | `true` | Signal lifecycle: opened, scheduled, closed, cancelled |
604
+ | `risk` | `true` | Risk manager rejection notifications |
605
+ | `info` | `true` | Informational messages attached to an active signal |
606
+ | `breakeven` | `true` | Breakeven level reached |
607
+ | `common_error` | `true` | Non-fatal runtime errors |
608
+ | `critical_error` | `true` | Fatal errors that terminate the session |
609
+ | `validation_error` | `true` | Strategy config / input validation errors |
610
+ | `strategy_commit` | `true` | All committed actions (partial close, DCA, trailing, etc.) |
611
+ | `partial_loss` | `false` | Partial loss level reached (before commit) |
612
+ | `partial_profit` | `false` | Partial profit level reached (before commit) |
613
+ | `signal_sync` | `false` | Live order fill / exit confirmations from exchange sync |
614
+
615
+ ```js
616
+ // config/notification.config.ts
617
+ export default {
618
+ signal: true,
619
+ risk: true,
620
+ info: true,
621
+ breakeven: true,
622
+ common_error: true,
623
+ critical_error: true,
624
+ validation_error: true,
625
+ strategy_commit: true,
626
+ partial_loss: false,
627
+ partial_profit: false,
628
+ signal_sync: false,
629
+ };
630
+ ```
631
+
632
+ ### Telegram Notifications (`--telegram`)
633
+
634
+ Sends formatted HTML messages with 1m / 15m / 1h price charts to your Telegram channel for every position event: opened, closed, scheduled, cancelled, risk rejection, partial profit/loss, trailing stop/take, and breakeven.
635
+
636
+ Requires `CC_TELEGRAM_TOKEN` and `CC_TELEGRAM_CHANNEL` in your environment.
637
+
638
+ #### Telegram Message Adapter (`telegram.config`)
639
+
640
+ By default messages are rendered from Mustache templates (`template/*.mustache`). To override rendering programmatically, create a `config/telegram.config` file and export an object with any subset of `get*Markdown` methods. Each method receives the event payload and must return a `Promise<string>` with the Markdown message body.
641
+
642
+ Resolution order is the same as other configs (strategy dir → project root → package default).
643
+
644
+ ```ts
645
+ // config/telegram.config.ts
646
+ import {
647
+ IStrategyTickResultOpened,
648
+ IStrategyTickResultClosed,
649
+ RiskContract,
650
+ } from "backtest-kit";
651
+
652
+ export default {
653
+ async getOpenedMarkdown(event: IStrategyTickResultOpened): Promise<string> {
654
+ return `**Opened** ${event.symbol} at ${event.priceOpen}`;
655
+ },
656
+ async getClosedMarkdown(event: IStrategyTickResultClosed): Promise<string> {
657
+ return `**Closed** ${event.symbol} at ${event.priceClosed}`;
658
+ },
659
+ async getRiskMarkdown(event: RiskContract): Promise<string> {
660
+ return `**Risk rejected** ${event.symbol}`;
661
+ },
662
+ };
663
+ ```
664
+
665
+ All methods are optional — unimplemented ones fall back to the Mustache template.
666
+
667
+ | Method | Event type |
668
+ |--------|------------|
669
+ | `getOpenedMarkdown` | `IStrategyTickResultOpened` |
670
+ | `getClosedMarkdown` | `IStrategyTickResultClosed` |
671
+ | `getScheduledMarkdown` | `IStrategyTickResultScheduled` |
672
+ | `getCancelledMarkdown` | `IStrategyTickResultCancelled` |
673
+ | `getRiskMarkdown` | `RiskContract` |
674
+ | `getPartialProfitMarkdown` | `PartialProfitCommit` |
675
+ | `getPartialLossMarkdown` | `PartialLossCommit` |
676
+ | `getBreakevenMarkdown` | `BreakevenCommit` |
677
+ | `getTrailingTakeMarkdown` | `TrailingTakeCommit` |
678
+ | `getTrailingStopMarkdown` | `TrailingStopCommit` |
679
+ | `getAverageBuyMarkdown` | `AverageBuyCommit` |
680
+ | `getSignalOpenMarkdown` | `SignalOpenContract` |
681
+ | `getSignalCloseMarkdown` | `SignalCloseContract` |
682
+ | `getCancelScheduledMarkdown` | `CancelScheduledCommit` |
683
+ | `getClosePendingMarkdown` | `ClosePendingCommit` |
684
+ | `getSignalInfoMarkdown` | `SignalInfoContract` |
685
+
686
+ ## 🧩 Module Hooks (Broker Adapter)
687
+
688
+ The CLI supports **mode-specific module files** that are loaded as side-effect imports before the strategy starts. Each file is expected to call `Broker.useBrokerAdapter()` from `backtest-kit` to register a broker adapter.
689
+
690
+ | Command Line Args | Module file | Loaded before |
691
+ |-------------------|---------------------------------|-----------------------------|
692
+ | `--live` | `./modules/live.module.mjs` | `Live.background()` |
693
+ | `--paper` | `./modules/paper.module.mjs` | `Live.background()` (paper) |
694
+ | `--backtest` | `./modules/backtest.module.mjs` | `Backtest.background()` |
695
+ | `--walker` | `./modules/walker.module.mjs` | `Walker.background()` |
696
+ | `--brokerdebug` | `./modules/brokerdebug.module.mjs` | broker commit test |
697
+
698
+ > File is resolved relative to `cwd` (the strategy directory). All of `.mjs`, `.cjs`, `.ts` extensions are tried automatically. Missing module is a soft warning — not an error.
699
+
700
+ ### How It Works
701
+
702
+ The module file is a side-effect import. When the CLI loads it, your code runs and registers the adapter. From that point on, `backtest-kit` intercepts every trade-mutating call through the adapter **before** updating internal state — if the adapter throws, the position state is never changed.
703
+
704
+ ```javascript
705
+ // live.module.mjs
706
+ import { Broker } from 'backtest-kit';
707
+ import { myExchange } from './exchange.mjs';
708
+
709
+ class MyBroker {
710
+ async onSignalOpenCommit({ symbol, priceOpen, direction }) {
711
+ await myExchange.openPosition(symbol, direction, priceOpen);
712
+ }
713
+
714
+ async onSignalCloseCommit({ symbol, priceClosed }) {
715
+ await myExchange.closePosition(symbol, priceClosed);
716
+ }
717
+
718
+ async onPartialProfitCommit({ symbol, cost, currentPrice }) {
719
+ await myExchange.createOrder({
720
+ symbol,
721
+ side: 'sell',
722
+ quantity: cost / currentPrice,
723
+ });
724
+ }
725
+
726
+ async onAverageBuyCommit({ symbol, cost, currentPrice }) {
727
+ await myExchange.createOrder({
728
+ symbol,
729
+ side: 'buy',
730
+ quantity: cost / currentPrice,
731
+ });
732
+ }
733
+ }
734
+
735
+ Broker.useBrokerAdapter(MyBroker);
736
+
737
+ Broker.enable();
738
+ ```
739
+
740
+ ### Available Broker Hooks
741
+
742
+ | Method | Payload type | Triggered on |
743
+ |--------------------------|------------------------------|---------------------------|
744
+ | `onSignalOpenCommit` | `BrokerSignalOpenPayload` | Position activation |
745
+ | `onSignalCloseCommit` | `BrokerSignalClosePayload` | SL / TP / manual close |
746
+ | `onPartialProfitCommit` | `BrokerPartialProfitPayload` | PP |
747
+ | `onPartialLossCommit` | `BrokerPartialLossPayload` | PL |
748
+ | `onTrailingStopCommit` | `BrokerTrailingStopPayload` | SL adjustment |
749
+ | `onTrailingTakeCommit` | `BrokerTrailingTakePayload` | TP adjustment |
750
+ | `onBreakevenCommit` | `BrokerBreakevenPayload` | SL moved to entry |
751
+ | `onAverageBuyCommit` | `BrokerAverageBuyPayload` | DCA entry |
752
+
753
+ All methods are optional. Unimplemented hooks are silently skipped. In backtest mode all broker calls are skipped automatically — no adapter code runs during backtests.
754
+
755
+ ### TypeScript
756
+
757
+ ```typescript
758
+ import { Broker, IBroker, BrokerSignalOpenPayload, BrokerSignalClosePayload } from 'backtest-kit';
759
+
760
+ class MyBroker implements Partial<IBroker> {
761
+ async onSignalOpenCommit(payload: BrokerSignalOpenPayload) {
762
+ // place open order on exchange
763
+ }
764
+
765
+ async onSignalCloseCommit(payload: BrokerSignalClosePayload) {
766
+ // place close order on exchange
767
+ }
768
+ }
769
+
770
+ Broker.useBrokerAdapter(MyBroker);
771
+
772
+ Broker.enable();
773
+ ```
774
+
775
+ ## ⚙️ Setup Hook (`config/setup.config`)
776
+
777
+ `@backtest-kit/cli` loads a `{projectRoot}/config/setup.config` file once before any module hooks or strategy code run. Use it to perform one-time initialization that must happen before the first persistence call — registering a custom storage backend, configuring a logger, seeding global state, or anything else the process needs before `backtest-kit` starts.
778
+
779
+ **Important:** When `setup.config` is present, the CLI skips its own default adapter registration — it does **not** call `usePersist()` / `useLocal()` / `useMemory()` for any of the persistence slots. This means your config takes full ownership of the persistence layer: whatever adapters you register in `{projectRoot}/config/setup.config` are the ones `backtest-kit` uses, with no interference from the CLI defaults.
780
+
781
+ ### Example: MongoDB + Redis persistence via `@backtest-kit/mongo`
782
+
783
+ The most common use-case is swapping the default file-based persistence for a production-grade backend. Install `@backtest-kit/mongo` and call `setup()` — it registers all 15 persistence adapters in one call and reads connection parameters from environment variables:
784
+
785
+ ```bash
786
+ npm install @backtest-kit/mongo
787
+ ```
788
+
789
+ ```ts
790
+ // config/setup.config.ts
791
+ import { setup } from '@backtest-kit/mongo';
792
+
793
+ setup();
794
+ ```
795
+
796
+ ```env
797
+ # .env
798
+ CC_MONGO_CONNECTION_STRING=mongodb://localhost:27017/backtest-kit
799
+ CC_REDIS_HOST=127.0.0.1
800
+ CC_REDIS_PORT=6379
801
+ ```
802
+
803
+ Or pass connection parameters explicitly:
804
+
805
+ ```ts
806
+ // config/setup.config.ts
807
+ import { setup } from '@backtest-kit/mongo';
808
+
809
+ setup({
810
+ CC_MONGO_CONNECTION_STRING: 'mongodb://mongo:27017/mydb',
811
+ CC_REDIS_HOST: 'redis',
812
+ CC_REDIS_PORT: 6379,
813
+ CC_REDIS_PASSWORD: 'secret',
814
+ });
815
+ ```
816
+
817
+ No changes to strategy code are needed `setup()` wires up the adapters transparently before `backtest-kit` makes its first persistence call.
818
+
819
+ ## 🧩 Module Loader (`config/loader.config`)
820
+
821
+ `@backtest-kit/cli` loads a `{projectRoot}/config/loader.config` file **after** `setup.config` but **before** any strategy or module code runs. Unlike `setup.config` (which is loaded for its side effects), `loader.config` exports a function that the CLI explicitly `await`s. Use it whenever you need to **wait for an async dependency** to be ready before the backtest starts.
822
+
823
+ ```bash
824
+ monorepo/
825
+ ├── packages/
826
+ │ ├── shared-broker/ # shared broker code
827
+ │ ├── shared-signals/ # common indicators (RSI, MACD)
828
+ │ ├── shared-db/ # mongodb wiring
829
+ │ ├── strategy-momentum/ # strategy code
830
+ │ └── strategy-mean-reversion/
831
+ ```
832
+
833
+ **When to use it:**
834
+
835
+ - **Wire microfrontends in a monorepo** — resolve and pre-load sibling packages, register cross-package services, or hydrate a shared DI container before strategies import from neighboring workspaces.
836
+ - **Wait for a database connection** — open a Mongo/Postgres/Redis connection and verify it's reachable before the first persistence call, so the backtest fails fast instead of mid-run.
837
+ - **Warm up caches or external APIs** — pre-fetch reference data (instruments list, calendar, fee tables) so the strategy's first tick doesn't pay the round-trip cost.
838
+ - **Run schema migrations** — apply any pending migrations to the persistence backend before signals start flowing.
839
+
840
+ `loader.config` supports exactly one of two export styles — **never both at once**. If both are present, the `default` export wins and the named `loader` is ignored.
841
+
842
+ ```ts
843
+ // config/loader.config.ts — default export (preferred, ESM style)
844
+ export default async () => {
845
+ await mongoose.connect(process.env.CC_MONGO_CONNECTION_STRING!);
846
+ await redis.ping();
847
+ };
848
+ ```
849
+
850
+ ```ts
851
+ // config/loader.config.ts — named export
852
+ export const loader = async () => {
853
+ await mongoose.connect(process.env.CC_MONGO_CONNECTION_STRING!);
854
+ await redis.ping();
855
+ };
856
+ ```
857
+
858
+ ### Example: wait for MongoDB before running a backtest
859
+
860
+ `@backtest-kit/mongo`'s `setup()` registers the adapters synchronously but doesn't block until the connection is established. If your backtest depends on data that must be present in Mongo before the first signal fires, use `loader.config` to gate the run on a real connection:
861
+
862
+ ```ts
863
+ // config/setup.config.ts
864
+ import { setup } from '@backtest-kit/mongo';
865
+
866
+ setup();
867
+ ```
868
+
869
+ ```ts
870
+ // config/loader.config.ts
871
+ import mongoose from 'mongoose';
872
+
873
+ export default async () => {
874
+ await mongoose.connect(process.env.CC_MONGO_CONNECTION_STRING!);
875
+ console.log('mongo connection verified, starting backtest');
876
+ };
877
+ ```
878
+
879
+ ### Example: stitch microfrontends in a monorepo
880
+
881
+ When `backtest-kit` strategies live in one workspace and shared services (broker adapters, signal feeds, dashboards) live in sibling workspaces, `loader.config` is the place to wire them together before the runner starts:
882
+
883
+ ```ts
884
+ // config/loader.config.ts
885
+ import "@my-org/brokers";
886
+ import "@my-org/signals";
887
+ ```
888
+
889
+ The `@my-org` alias should be declared in `config/alias.config`.
890
+
891
+ ## 🔀 Import Aliases (`config/alias.config`)
892
+
893
+ `@backtest-kit/cli` lets you override any nodejs module import — without touching the strategy code. Drop a `config/alias.config` file in your project root and export a mapping from module name to replacement module.
894
+
895
+ The alias table is loaded once (on the first `import` call) from `{projectRoot}/config/alias.config` and applied globally to every subsequent module load via `require`/ `import`.
896
+
897
+ **Use cases:**
898
+
899
+ - Replace a heavy dependency with a lighter stub for backtesting
900
+ - Swap any external api for a mock during CI runs
901
+
902
+ ```ts
903
+ // config/alias.config.ts — named export
904
+ export const ccxt = require("./stubs/ccxt.stub.cjs");
905
+ ```
906
+
907
+ ```js
908
+ // config/alias.config.cjs — default export
909
+ module.exports = {
910
+ ccxt: require("./stubs/ccxt.stub.cjs"),
911
+ };
912
+ ```
913
+
914
+ ```js
915
+ // config/alias.config.mjs default export
916
+ import ccxtStub from "./stubs/ccxt.stub.mjs";
917
+
918
+ export default {
919
+ ccxt: ccxtStub,
920
+ };
921
+ ```
922
+
923
+ **How it works:** when strategy code calls `require("ccxt")`, the loader checks `IMPORT_ALIAS` first. If a key matches, the mapped value is returned instead of the real module — no monkey-patching of `node_modules` needed.
924
+
925
+ **Important:** It is **not** per-strategy — it applies to all modules loaded in the current process.
926
+
927
+ ## 📦 Supported Entry Point Formats
928
+
929
+ `@backtest-kit/cli` automatically detects the format of your strategy file and loads it with the appropriate runtime — no flags or configuration required.
930
+
931
+ | Format | Extension | Runtime | Use Case |
932
+ |--------|-----------|---------|----------|
933
+ | **TypeScript** | `.ts` | [`tsx`](https://tsx.is/) via `tsImport()` | TypeScript strategies with cross-imports (ESM ↔ CJS) |
934
+ | **ES Module** | `.mjs` | Native `import()` | Modern JavaScript with top-level `await` and ESM syntax |
935
+ | **CommonJS** | `.cjs` | Native `require()` | Legacy or dual-package strategies |
936
+
937
+ ### TypeScript (`.ts`)
938
+
939
+ Run TypeScript strategy files directly no `tsc` compilation step needed. Powered by `tsx`, which handles cross-format imports transparently:
940
+
941
+ ```json
942
+ {
943
+ "scripts": {
944
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.ts"
945
+ },
946
+ "dependencies": {
947
+ "@backtest-kit/cli": "latest",
948
+ "backtest-kit": "latest",
949
+ "tsx": "latest"
950
+ }
951
+ }
952
+ ```
953
+
954
+ ### ES Module (`.mjs`)
955
+
956
+ Standard ESM format. Supports top-level `await`, named exports, and `import` syntax:
957
+
958
+ ```json
959
+ {
960
+ "scripts": {
961
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
962
+ }
963
+ }
964
+ ```
965
+
966
+ ### CommonJS (`.cjs`)
967
+
968
+ For projects that compile to or use CommonJS. Loaded via `require()`:
969
+
970
+ ```json
971
+ {
972
+ "scripts": {
973
+ "backtest": "npx @backtest-kit/cli --backtest ./dist/index.cjs"
974
+ }
975
+ }
976
+ ```
977
+
978
+ ## 🌲 Running Local PineScript Indicators
979
+
980
+ `@backtest-kit/cli` can execute any local `.pine` file against a real exchange and print the results as a Markdown table — no TradingView account required.
981
+
982
+ ### CLI Flags
983
+
984
+ | Flag | Type | Description |
985
+ |------|------|-------------|
986
+ | `--pine` | boolean | Enable PineScript execution mode |
987
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
988
+ | `--timeframe` | string | Candle interval (default: `"15m"`) |
989
+ | `--limit` | string | Number of candles to fetch (default: `250`) |
990
+ | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
991
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
992
+ | `--output` | string | Output file base name without extension (default: `.pine` file name) |
993
+ | `--json` | boolean | Write plots as a JSON array to `<pine-dir>/dump/{output}.json` |
994
+ | `--jsonl` | boolean | Write plots as JSONL (one row per line) to `<pine-dir>/dump/{output}.jsonl` |
995
+ | `--markdown` | boolean | Write Markdown table to `<pine-dir>/dump/{output}.md` |
996
+
997
+ **Important:** `limit` must cover indicator warmup bars — rows before warmup completes will show `N/A`
998
+
999
+ **Positional argument:** path to the `.pine` file.
1000
+
1001
+ ### Exchange via `pine.module`
1002
+
1003
+ By default the CLI registers CCXT Binance automatically. To use a different exchange — or to configure API keys, custom rate limits, or a non-spot market — create a `modules/pine.module.ts` file. The CLI loads it automatically before running the script.
1004
+
1005
+ The CLI looks for `modules/pine.module` in two locations (first match wins):
1006
+
1007
+ 1. **Next to the `.pine` file** — `<pine-file-dir>/modules/pine.module.ts`
1008
+ 2. **Project root** `<cwd>/modules/pine.module.ts`
1009
+
1010
+ ```
1011
+ my-project/
1012
+ ├── math/
1013
+ │ ├── impulse_trend_15m.pine ← indicator
1014
+ │ └── modules/
1015
+ │ └── pine.module.ts ← loaded first (next to .pine file)
1016
+ ├── modules/
1017
+ │ └── pine.module.ts ← fallback (project root)
1018
+ └── package.json
1019
+ ```
1020
+
1021
+ Inside `pine.module.ts` call `addExchangeSchema` from `backtest-kit` and give the exchange a name:
1022
+
1023
+ ```typescript
1024
+ // modules/pine.module.ts
1025
+ import { addExchangeSchema } from "backtest-kit";
1026
+ import ccxt from "ccxt";
1027
+
1028
+ addExchangeSchema({
1029
+ exchangeName: "my-exchange",
1030
+ getCandles: async (symbol, interval, since, limit) => {
1031
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1032
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1033
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1034
+ timestamp, open, high, low, close, volume,
1035
+ }));
1036
+ },
1037
+ formatPrice: (symbol, price) => price.toFixed(2),
1038
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1039
+ });
1040
+ ```
1041
+
1042
+ ### Environment variables (`.env`)
1043
+
1044
+ Before loading `pine.module`, the CLI loads `.env` files in the same order as for strategy modules — project root first, then the `.pine` file directory (overrides root):
1045
+
1046
+ ```
1047
+ my-project/
1048
+ ├── math/
1049
+ │ ├── .env ← loaded second (overrides root)
1050
+ │ └── impulse_trend_15m.pine
1051
+ ├── .env ← loaded first
1052
+ └── package.json
1053
+ ```
1054
+
1055
+ Use this to store API keys without hardcoding them:
1056
+
1057
+ ```env
1058
+ # .env
1059
+ BYBIT_API_KEY=xxx
1060
+ BYBIT_API_SECRET=yyy
1061
+ ```
1062
+
1063
+ ```typescript
1064
+ // modules/pine.module.ts
1065
+ addExchangeSchema({
1066
+ exchangeName: "my-exchange",
1067
+ getCandles: async (symbol, interval, since, limit) => {
1068
+ const exchange = new ccxt.bybit({
1069
+ apiKey: process.env.BYBIT_API_KEY,
1070
+ secret: process.env.BYBIT_API_SECRET,
1071
+ enableRateLimit: true,
1072
+ });
1073
+ // ...
1074
+ },
1075
+ });
1076
+ ```
1077
+
1078
+ Then run:
1079
+
1080
+ ```bash
1081
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine \
1082
+ --exchange my-exchange \
1083
+ --symbol BTCUSDT \
1084
+ --timeframe 15m \
1085
+ --limit 180 \
1086
+ --when "2025-09-24T12:00:00.000Z"
1087
+ ```
1088
+
1089
+ Or add it to `package.json`:
1090
+
1091
+ ```json
1092
+ {
1093
+ "scripts": {
1094
+ "pine": "npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --symbol BTCUSDT --timeframe 15m --limit 180"
1095
+ }
1096
+ }
1097
+ ```
1098
+
1099
+ ```bash
1100
+ npm run pine
1101
+ ```
1102
+
1103
+ ### PineScript Requirements
1104
+
1105
+ The CLI reads all `plot()` calls that use `display=display.data_window` as output columns. Every other `plot()` is ignored. Name each output plot explicitly:
1106
+
1107
+ ```pine
1108
+ //@version=5
1109
+ indicator("MyIndicator", overlay=true)
1110
+
1111
+ // ... computation ...
1112
+
1113
+ plot(close, "Close", display=display.data_window)
1114
+ plot(position, "Position", display=display.data_window)
1115
+ ```
1116
+
1117
+ The column names in the output Markdown table are taken directly from those plot names no manual schema definition needed.
1118
+
1119
+ ### Output
1120
+
1121
+ The CLI prints a Markdown table to stdout:
1122
+
1123
+ ```
1124
+ # PineScript Technical Analysis Dump
1125
+
1126
+ **Signal ID**: CLI execution 2025-09-24T12:00:00.000Z
1127
+
1128
+ | Close | Position | timestamp |
1129
+ | --- | --- | --- |
1130
+ | 112871.28 | -1.0000 | 2025-09-22T15:00:00.000Z |
1131
+ | 112666.69 | -1.0000 | 2025-09-22T15:15:00.000Z |
1132
+ | 112736.00 | 0.0000 | 2025-09-22T18:30:00.000Z |
1133
+ | 112653.90 | 1.0000 | 2025-09-22T22:15:00.000Z |
1134
+ ```
1135
+
1136
+ Save to `./math/dump/impulse_trend_15m.md` (uses `.pine` file name automatically, dump is created next to the `.pine` file):
1137
+
1138
+ ```bash
1139
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --markdown
1140
+ ```
1141
+
1142
+ Override the output name with `--output`:
1143
+
1144
+ ```bash
1145
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --jsonl --output feb2026_bb
1146
+ # ./math/dump/feb2026_bb.jsonl
1147
+ ```
1148
+
1149
+ Print to stdout (no flag):
1150
+
1151
+ ```bash
1152
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine
1153
+ ```
1154
+
1155
+ ## 🎨 Visual Pine Script Editor
1156
+
1157
+ ![pine](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot32.png)
1158
+
1159
+ `@backtest-kit/cli` ships a browser-based Pine Script editor powered by `@backtest-kit/ui`. It lets you write, run, and iterate on indicators interactively — with a live chart that updates as you hit **▶ Run**
1160
+
1161
+ ### Usage
1162
+
1163
+ ```bash
1164
+ npx @backtest-kit/cli --editor
1165
+ ```
1166
+
1167
+ The CLI will:
1168
+
1169
+ 1. Load `./modules/editor.module` (if it exists) use it to register your exchange schema, identical to `pine.module`
1170
+ 2. Start the `@backtest-kit/ui` server on `http://localhost:60050` (or `CC_WWWROOT_PORT`)
1171
+ 3. Open `http://localhost:{CC_WWWROOT_PORT}?pine=1` automatically in your default browser
1172
+
1173
+ Press **Ctrl+C** to stop the server.
1174
+
1175
+ ### Exchange via `editor.module`
1176
+
1177
+ Drop a `modules/editor.module.ts` next to your project to register the exchange that the editor's candle provider will use:
1178
+
1179
+ ```typescript
1180
+ // modules/editor.module.ts
1181
+ import { addExchangeSchema } from "backtest-kit";
1182
+ import ccxt from "ccxt";
1183
+
1184
+ addExchangeSchema({
1185
+ exchangeName: "my-exchange",
1186
+ getCandles: async (symbol, interval, since, limit) => {
1187
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1188
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1189
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1190
+ timestamp, open, high, low, close, volume,
1191
+ }));
1192
+ },
1193
+ formatPrice: (symbol, price) => price.toFixed(2),
1194
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1195
+ });
1196
+ ```
1197
+
1198
+ ### Environment Variables
1199
+
1200
+ | Variable | Default | Description |
1201
+ |-------------------|-----------|----------------------------------|
1202
+ | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1203
+ | `CC_WWWROOT_PORT` | `60050` | UI server port |
1204
+
1205
+ ### `package.json` script
1206
+
1207
+ ```json
1208
+ {
1209
+ "scripts": {
1210
+ "editor": "npx @backtest-kit/cli --editor"
1211
+ }
1212
+ }
1213
+ ```
1214
+
1215
+ ```bash
1216
+ npm run editor
1217
+ ```
1218
+
1219
+ ## 💾 Dumping Raw Candles
1220
+
1221
+ `@backtest-kit/cli` can fetch raw OHLCV candles from any registered exchange and save them to a file — no strategy file required.
1222
+
1223
+ ### CLI Flags
1224
+
1225
+ | Flag | Type | Description |
1226
+ |------|------|-------------|
1227
+ | `--dump` | boolean | Enable candle dump mode |
1228
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1229
+ | `--timeframe` | string | Candle interval (default: `"15m"`) |
1230
+ | `--limit` | string | Number of candles to fetch (default: `250`) |
1231
+ | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
1232
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1233
+ | `--output` | string | Output file base name without extension (default: `{SYMBOL}_{LIMIT}_{TIMEFRAME}_{TIMESTAMP}`) |
1234
+ | `--json` | boolean | Write candles as a JSON array to `./dump/{output}.json` |
1235
+ | `--jsonl` | boolean | Write candles as JSONL (one row per line) to `./dump/{output}.jsonl` |
1236
+
1237
+ The `dump/` directory is created in the current working directory (where the CLI is invoked from).
1238
+
1239
+ ### Exchange via `dump.module`
1240
+
1241
+ By default the CLI registers CCXT Binance automatically. To use a different exchange — or to configure API keys, custom rate limits, or a non-spot market — create a `modules/dump.module.ts` file. The CLI loads it automatically before fetching candles.
1242
+
1243
+ The CLI looks for `modules/dump.module` in the current working directory
1244
+
1245
+ ```
1246
+ my-project/
1247
+ ├── modules/
1248
+ │ └── dump.module.ts ← exchange registration
1249
+ ├── dump/ ← auto-created: candle output files
1250
+ └── package.json
1251
+ ```
1252
+
1253
+ Inside `dump.module.ts` call `addExchangeSchema` from `backtest-kit`:
1254
+
1255
+ ```typescript
1256
+ // modules/dump.module.ts
1257
+ import { addExchangeSchema } from "backtest-kit";
1258
+ import ccxt from "ccxt";
1259
+
1260
+ addExchangeSchema({
1261
+ exchangeName: "my-exchange",
1262
+ getCandles: async (symbol, interval, since, limit) => {
1263
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1264
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1265
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1266
+ timestamp, open, high, low, close, volume,
1267
+ }));
1268
+ },
1269
+ formatPrice: (symbol, price) => price.toFixed(2),
1270
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1271
+ });
1272
+ ```
1273
+
1274
+ ### Output
1275
+
1276
+ Each candle row contains OHLCV fields. Print to stdout:
1277
+
1278
+ ```bash
1279
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100
1280
+ ```
1281
+
1282
+ Save to `./dump/BTCUSDT_100_15m_{timestamp}.jsonl`:
1283
+
1284
+ ```bash
1285
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100 --jsonl
1286
+ ```
1287
+
1288
+ Fetch candles up to a specific date with `--when` and override the file name with `--output`:
1289
+
1290
+ ```bash
1291
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 \
1292
+ --when "2026-02-28T00:00:00.000Z" \
1293
+ --jsonl --output feb2026_btc
1294
+ # ./dump/feb2026_btc.jsonl
1295
+ ```
1296
+
1297
+ Or add it to `package.json`:
1298
+
1299
+ ```json
1300
+ {
1301
+ "scripts": {
1302
+ "dump": "npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl"
1303
+ }
1304
+ }
1305
+ ```
1306
+
1307
+ ```bash
1308
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl
1309
+ ```
1310
+
1311
+ ## 🐞 PnL Debug (`--pnldebug`)
1312
+
1313
+ `@backtest-kit/cli` can simulate a hypothetical position minute by minute and print running PnL, peak profit, and maximum drawdown for each candle — without placing any trades or loading a strategy file.
1314
+
1315
+ ### CLI Flags
1316
+
1317
+ | Flag | Type | Description |
1318
+ |------|------|-------------|
1319
+ | `--pnldebug` | boolean | Enable PnL debug mode |
1320
+ | `--priceopen` | number | Entry price (required) |
1321
+ | `--direction` | string | `long` or `short` (default: `long`) |
1322
+ | `--when` | string | Start timestamp — ISO 8601 or Unix ms (default: now) |
1323
+ | `--minutes` | string | Number of 1m candles to simulate (default: `60`) |
1324
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1325
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1326
+ | `--output` | string | Output file base name (default: `{SYMBOL}_{DIRECTION}_{PRICEOPEN}_{TIMESTAMP}`) |
1327
+ | `--json` | boolean | Save results as JSON array to `./dump/<output>.json` |
1328
+ | `--jsonl` | boolean | Save results as JSONL to `./dump/<output>.jsonl` |
1329
+ | `--markdown` | boolean | Save results as Markdown table to `./dump/<output>.md` |
1330
+
1331
+ ### Output columns
1332
+
1333
+ | Column | Description |
1334
+ |--------|-------------|
1335
+ | `min` | Minute offset from start (1-based) |
1336
+ | `timestamp` | Candle timestamp (ISO 8601) |
1337
+ | `close` | Candle close price |
1338
+ | `pnl%` | Running PnL vs entry price (signed %) |
1339
+ | `peak%` | Highest PnL reached so far (always ≥ 0) |
1340
+ | `drawdown%` | Lowest PnL reached so far (always 0) |
1341
+
1342
+ ### Exchange via `pnldebug.module`
1343
+
1344
+ By default the CLI registers CCXT Binance automatically. To use a different exchange, create a `modules/pnldebug.module.ts` file in the current working directory — the CLI loads it automatically before fetching candles.
1345
+
1346
+ ```typescript
1347
+ // modules/pnldebug.module.ts
1348
+ import { addExchangeSchema } from "backtest-kit";
1349
+ import ccxt from "ccxt";
1350
+
1351
+ addExchangeSchema({
1352
+ exchangeName: "my-exchange",
1353
+ getCandles: async (symbol, interval, since, limit) => {
1354
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1355
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1356
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1357
+ timestamp, open, high, low, close, volume,
1358
+ }));
1359
+ },
1360
+ formatPrice: (symbol, price) => price.toFixed(2),
1361
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1362
+ });
1363
+ ```
1364
+
1365
+ ### Usage
1366
+
1367
+ Print to stdout (default table format):
1368
+
1369
+ ```bash
1370
+ npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120
1371
+ ```
1372
+
1373
+ Save as Markdown:
1374
+
1375
+ ```bash
1376
+ npx @backtest-kit/cli --pnldebug --priceopen 67956.73 --direction long --when 1772064000000 --minutes 60 --markdown
1377
+ # → ./dump/BTCUSDT_long_67956.73_{timestamp}.md
1378
+ ```
1379
+
1380
+ Override the output file name with `--output`:
1381
+
1382
+ ```bash
1383
+ npx @backtest-kit/cli --pnldebug --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120 \
1384
+ --jsonl --output feb25_short_debug
1385
+ # ./dump/feb25_short_debug.jsonl
1386
+ ```
1387
+
1388
+ Or add it to `package.json`:
1389
+
1390
+ ```json
1391
+ {
1392
+ "scripts": {
1393
+ "pnldebug": "npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when \"2025-02-25\" --minutes 120"
1394
+ }
1395
+ }
1396
+ ```
1397
+
1398
+ ```bash
1399
+ npm run pnldebug
1400
+ ```
1401
+
1402
+ ### Example stdout output
1403
+
1404
+ ```
1405
+ Symbol: BTCUSDT | Direction: short | PriceOpen: 64069.50 | From: 2025-02-25T00:00:00.000Z | Minutes: 120
1406
+
1407
+ min | timestamp | close | pnl% | peak% | drawdown%
1408
+ -----------------------------------------------------------------------------------
1409
+ 1 | 2025-02-25T00:01:00.000Z | 64020.10 | +0.08% | +0.08% | 0.00%
1410
+ 2 | 2025-02-25T00:02:00.000Z | 64105.30 | -0.06% | +0.08% | -0.06%
1411
+ ...
1412
+ 120 | 2025-02-25T02:00:00.000Z | 63200.00 | +1.36% | +1.36% | -0.06%
1413
+ ```
1414
+
1415
+ ## 🐛 Broker Debug (`--brokerdebug`)
1416
+
1417
+ `@backtest-kit/cli` can fire a single broker commit against your live broker adapter without running a full strategy — useful for verifying that your `brokerdebug.module` correctly wires up exchange calls.
1418
+
1419
+ ### CLI Flags
1420
+
1421
+ | Flag | Type | Description |
1422
+ |------|------|-------------|
1423
+ | `--brokerdebug` | boolean | Enable broker debug mode |
1424
+ | `--commit` | string | Commit type to fire (default: `"signal-open"`) |
1425
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1426
+ | `--exchange` | string | Exchange name (default: first registered) |
1427
+
1428
+ **Available `--commit` values:**
1429
+
1430
+ | Value | Broker hook |
1431
+ |-------|-------------|
1432
+ | `signal-open` | `onSignalOpenCommit` |
1433
+ | `signal-close` | `onSignalCloseCommit` |
1434
+ | `partial-profit` | `onPartialProfitCommit` |
1435
+ | `partial-loss` | `onPartialLossCommit` |
1436
+ | `average-buy` | `onAverageBuyCommit` |
1437
+ | `trailing-stop` | `onTrailingStopCommit` |
1438
+ | `trailing-take` | `onTrailingTakeCommit` |
1439
+ | `breakeven` | `onBreakevenCommit` |
1440
+
1441
+ ### How It Works
1442
+
1443
+ The CLI loads `./modules/brokerdebug.module`, fetches the last candle for `--symbol` / `--timeframe`, derives synthetic payload values from `currentPrice` (TP = +2%, SL = -2%), and calls the selected broker hook once. Exits with code `0` on success.
1444
+
1445
+ ### Broker via `brokerdebug.module`
1446
+
1447
+ Create a `modules/brokerdebug.module.ts` file and register your broker adapter:
1448
+
1449
+ ```typescript
1450
+ // modules/brokerdebug.module.ts
1451
+ import { Broker } from 'backtest-kit';
1452
+ import { myExchange } from './exchange.mjs';
1453
+
1454
+ class MyBroker {
1455
+ async onSignalOpenCommit({ symbol, priceOpen, position }) {
1456
+ await myExchange.openPosition(symbol, position, priceOpen);
1457
+ }
1458
+ // ... other hooks
1459
+ }
1460
+
1461
+ Broker.useBrokerAdapter(MyBroker);
1462
+ Broker.enable();
1463
+ ```
1464
+
1465
+ ### Usage
1466
+
1467
+ ```bash
1468
+ npx @backtest-kit/cli --brokerdebug --commit signal-open --symbol BTCUSDT
1469
+ npx @backtest-kit/cli --brokerdebug --commit partial-profit --symbol ETHUSDT --timeframe 1h
1470
+ ```
1471
+
1472
+ ## 🗑️ Flushing Strategy Output (`--flush`)
1473
+
1474
+ `@backtest-kit/cli` can delete generated output folders from one or more strategy dump directories without touching cached candle data.
1475
+
1476
+ ### CLI Flags
1477
+
1478
+ | Flag | Type | Description |
1479
+ |------|------|-------------|
1480
+ | `--flush` | boolean | Enable flush mode |
1481
+
1482
+ **Positional arguments (required):** one or more strategy entry point files. For each entry point the CLI resolves its directory and removes the following subdirectories from `<entry-dir>/dump/`:
1483
+
1484
+ | Folder | Contents |
1485
+ |--------|----------|
1486
+ | `report` | Backtest report files (`.jsonl`) |
1487
+ | `log` | Run logs (`log.jsonl`) |
1488
+ | `markdown` | Exported Markdown reports |
1489
+ | `agent` | Agent outline files |
1490
+
1491
+ Candle cache (`dump/data/`) and AI forecast outlines (`dump/outline/`) are **not** removed.
1492
+
1493
+ ### Usage
1494
+
1495
+ Flush a single strategy:
1496
+
1497
+ ```bash
1498
+ npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts
1499
+ ```
1500
+
1501
+ Flush multiple strategies at once:
1502
+
1503
+ ```bash
1504
+ npx @backtest-kit/cli --flush \
1505
+ ./content/feb_2026.strategy/modules/backtest.module.ts \
1506
+ ./content/mar_2026.strategy/modules/backtest.module.ts
1507
+ ```
1508
+
1509
+ Or add it to `package.json`:
1510
+
1511
+ ```json
1512
+ {
1513
+ "scripts": {
1514
+ "flush": "npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts"
1515
+ }
1516
+ }
1517
+ ```
1518
+
1519
+ ```bash
1520
+ npm run flush
1521
+ ```
1522
+
1523
+ ## 🗂️ Scaffolding a New Project (`--init`)
1524
+
1525
+ `@backtest-kit/cli` can bootstrap a ready-to-use project directory with a pre-configured layout, example strategy files, and all documentation fetched automatically.
1526
+
1527
+ ### CLI Flags
1528
+
1529
+ | Flag | Type | Description |
1530
+ |------|------|-------------|
1531
+ | `--init` | boolean | Scaffold a new project |
1532
+ | `--output` | string | Target directory name (default: `backtest-kit-project`) |
1533
+
1534
+ ### Usage
1535
+
1536
+ ```bash
1537
+ npx @backtest-kit/cli --init
1538
+ ```
1539
+
1540
+ Creates `./backtest-kit-project/` in the current working directory.
1541
+
1542
+ Override the directory name with `--output`:
1543
+
1544
+ ```bash
1545
+ npx @backtest-kit/cli --init --output my-trading-bot
1546
+ ```
1547
+
1548
+ Creates `./my-trading-bot/`.
1549
+
1550
+ The target directory must not exist or must be empty — the command aborts if it contains any files.
1551
+
1552
+ ### Generated Project Structure
1553
+
1554
+ ```
1555
+ backtest-kit-project/
1556
+ ├── package.json # pre-configured with all backtest-kit dependencies
1557
+ ├── .gitignore
1558
+ ├── CLAUDE.md # AI-agent guide for writing strategies
1559
+ ├── content/
1560
+ │ └── feb_2026.strategy.ts # example strategy entry point
1561
+ ├── docs/
1562
+ │ ├── lib/ # fetched automatically (see below)
1563
+ │ ├── backtest_actions.md
1564
+ │ ├── backtest_graph_pattern.md
1565
+ │ ├── backtest_logging_jsonl.md
1566
+ │ ├── backtest_pinets_usage.md
1567
+ │ ├── backtest_risk_async.md
1568
+ │ ├── backtest_strategy_structure.md
1569
+ │ ├── pine_debug.md
1570
+ │ └── pine_indicator_warmup.md
1571
+ ├── math/
1572
+ │ └── feb_2026.pine # example PineScript indicator
1573
+ ├── modules/
1574
+ │ ├── dump.module.ts # exchange schema for --dump mode
1575
+ │ └── pine.module.ts # exchange schema for --pine mode
1576
+ ├── report/
1577
+ │ └── feb_2026.md # example strategy research report
1578
+ └── scripts/
1579
+ └── fetch_docs.mjs # utility: downloads library READMEs into docs/lib/
1580
+ ```
1581
+
1582
+ ### Automatic Documentation Fetch
1583
+
1584
+ After scaffolding, the CLI immediately runs `scripts/fetch_docs.mjs` inside the new project, which downloads the latest README files for all bundled libraries into `docs/lib/`:
1585
+
1586
+ | File | Source |
1587
+ |------|--------|
1588
+ | `backtest-kit.md` | `backtest-kit` README |
1589
+ | `backtest-kit__graph.md` | `@backtest-kit/graph` README |
1590
+ | `backtest-kit__pinets.md` | `@backtest-kit/pinets` README |
1591
+ | `backtest-kit__cli.md` | `@backtest-kit/cli` README |
1592
+ | `garch.md` | `garch` README |
1593
+ | `volume-anomaly.md` | `volume-anomaly` README |
1594
+ | `agent-swarm-kit.md` | `agent-swarm-kit` README |
1595
+ | `functools-kit.md` | `functools-kit` README |
1596
+
1597
+ You can re-run this script at any time to refresh the docs:
1598
+
1599
+ ```bash
1600
+ cd backtest-kit-project
1601
+ node ./scripts/fetch_docs.mjs
1602
+ ```
1603
+
1604
+ Or via the pre-configured npm script:
1605
+
1606
+ ```bash
1607
+ npm run sync:lib
1608
+ ```
1609
+
1610
+ ## 🐳 Running in Docker (`--docker`)
1611
+
1612
+ CLI can create a ready-to-use Docker workspace: self-contained directory with `docker-compose.yaml` and a strategy entry point.
1613
+
1614
+ ### CLI Flags
1615
+
1616
+ | Flag | Type | Description |
1617
+ |------|------|-------------|
1618
+ | `--docker` | boolean | Scaffold a Docker workspace |
1619
+ | `--output` | string | Target directory name (default: `backtest-kit-docker`) |
1620
+
1621
+ ### Usage
1622
+
1623
+ ```bash
1624
+ npx @backtest-kit/cli --docker
1625
+ ```
1626
+
1627
+ Creates `./backtest-kit-docker/` in the current working directory.
1628
+
1629
+ Override the directory name with `--output`:
1630
+
1631
+ ```bash
1632
+ npx @backtest-kit/cli --docker --output my-docker-workspace
1633
+ ```
1634
+
1635
+ The target directory must not exist or must be empty the command aborts if it contains any files.
1636
+
1637
+ ### Two Launch Modes
1638
+
1639
+ The Docker image entrypoint supports two ways to run a strategy:
1640
+
1641
+ #### 1. `command:` in `docker-compose.yaml`
1642
+
1643
+ Pin mode and flags directly in the compose file. The entrypoint forwards all arguments to the CLI unchanged:
1644
+
1645
+ ```yaml
1646
+ command:
1647
+ - --live
1648
+ - --symbol
1649
+ - TRXUSDT
1650
+ - --strategy
1651
+ - feb_2026_strategy
1652
+ - --exchange
1653
+ - ccxt-exchange
1654
+ - ./content/feb_2026/feb_2026.strategy.ts
1655
+ - --ui
1656
+ ```
1657
+
1658
+ #### 2. Inline environment variables
1659
+
1660
+ Pass `MODE` and `STRATEGY_FILE` on the command line — no file edits needed:
1661
+
1662
+ ```bash
1663
+ MODE=live SYMBOL=TRXUSDT STRATEGY_FILE=./content/feb_2026/feb_2026.strategy.ts docker-compose up -d
1664
+ ```
1665
+
1666
+ | Variable | Required | Default | Description |
1667
+ |----------|----------|---------|-------------|
1668
+ | `MODE` | yes | — | `backtest` \| `live` \| `paper` \| `walker` |
1669
+ | `STRATEGY_FILE` | yes | — | Path to strategy entry point (relative to `working_dir`) |
1670
+ | `SYMBOL` | no | `BTCUSDT` | Trading pair |
1671
+ | `STRATEGY` | no | first registered | Strategy name |
1672
+ | `EXCHANGE` | no | first registered | Exchange name |
1673
+ | `FRAME` | no | first registered | Frame name (backtest only) |
1674
+ | `UI` | no | — | Any non-empty value enables `--ui` |
1675
+ | `TELEGRAM` | no | — | Any non-empty value enables `--telegram` |
1676
+ | `VERBOSE` | no | — | Any non-empty value enables `--verbose` |
1677
+ | `NO_CACHE` | no | — | Any non-empty value enables `--noCache` |
1678
+ | `NO_FLUSH` | no | — | Any non-empty value enables `--noFlush` |
1679
+ | `ENTRY` | no | — | Any non-empty value enables multiple symbols from userspace |
1680
+
1681
+ ## 🌍 Environment Variables
1682
+
1683
+ Create a `.env` file in your project root:
1684
+
1685
+ ```env
1686
+ # Telegram notifications (required for --telegram)
1687
+ CC_TELEGRAM_TOKEN=your_bot_token_here
1688
+ CC_TELEGRAM_CHANNEL=-100123456789
1689
+
1690
+ # Web UI server (optional, defaults shown)
1691
+ CC_WWWROOT_HOST=0.0.0.0
1692
+ CC_WWWROOT_PORT=60050
1693
+
1694
+ # Custom QuickChart service URL (optional)
1695
+ CC_QUICKCHART_HOST=
1696
+ ```
1697
+
1698
+ | Variable | Default | Description |
1699
+ |------------------------|-------------|---------------------------------------|
1700
+ | `CC_TELEGRAM_TOKEN` | — | Telegram bot token (from @BotFather) |
1701
+ | `CC_TELEGRAM_CHANNEL` | — | Telegram channel or chat ID |
1702
+ | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1703
+ | `CC_WWWROOT_PORT` | `60050` | UI server port |
1704
+ | `CC_QUICKCHART_HOST` | — | Self-hosted QuickChart instance URL |
1705
+
1706
+ ## ⚙️ Default Behaviors
1707
+
1708
+ When your strategy module does not register an exchange, frame, or strategy name, the CLI falls back to built-in defaults and prints a console warning:
1709
+
1710
+ | Component | Default | Warning |
1711
+ |--------------|--------------------------------|---------------------------------------------------------------------------|
1712
+ | **Exchange** | CCXT Binance (`default_exchange`) | `Warning: The default exchange schema is set to CCXT Binance...` |
1713
+ | **Frame** | February 2024 (`default_frame`) | `Warning: The default frame schema is set to February 2024...` |
1714
+ | **Symbol** | `BTCUSDT` | — |
1715
+ | **Cache intervals** | `1m, 15m, 30m, 4h` | Used if `--cacheInterval` not provided; skip entirely with `--noCache` |
1716
+
1717
+ > **Note:** The default exchange schema **does not support order book fetching in backtest mode**. If your strategy calls `getOrderBook()` during backtest, you must register a custom exchange schema with your own snapshot storage.
1718
+
1719
+ ## 🔧 Programmatic API
1720
+
1721
+ In addition to the CLI, `@backtest-kit/cli` can be used as a library — call `run()` directly from your own script without spawning a child process or parsing CLI flags.
1722
+
1723
+ ### `run(mode, args)`
1724
+
1725
+ ```typescript
1726
+ import { run } from '@backtest-kit/cli';
1727
+
1728
+ await run(mode, args);
1729
+ ```
1730
+
1731
+ | Parameter | Description |
1732
+ |-----------|-------------|
1733
+ | `mode` | `"backtest" \| "paper" \| "live"` — Execution mode |
1734
+ | `args` | Mode-specific options (all optional — same defaults as CLI) |
1735
+
1736
+ `run()` can be called **only once per process**. A second call throws `"Should be called only once"`.
1737
+
1738
+ ### Payload fields
1739
+
1740
+ **Backtest** (`mode: "backtest"`):
1741
+
1742
+ | Field | Type | Description |
1743
+ |-------|------|-------------|
1744
+ | `entryPoint` | `string` | Path to strategy entry point file |
1745
+ | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1746
+ | `strategy` | `string` | Strategy name (default: first registered) |
1747
+ | `exchange` | `string` | Exchange name (default: first registered) |
1748
+ | `frame` | `string` | Frame name (default: first registered) |
1749
+ | `cacheInterval` | `CandleInterval[]` | Intervals to pre-cache (default: `["1m","15m","30m","1h","4h"]`) |
1750
+ | `noCache` | `boolean` | Skip candle cache warming (default: `false`) |
1751
+ | `noFlush` | `boolean` | Skip removing report/log/markdown/agent folders before the run (default: `false`) |
1752
+ | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1753
+
1754
+ **Paper** and **Live** (`mode: "paper"` / `mode: "live"`):
1755
+
1756
+ | Field | Type | Description |
1757
+ |-------|------|-------------|
1758
+ | `entryPoint` | `string` | Path to strategy entry point file |
1759
+ | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1760
+ | `strategy` | `string` | Strategy name (default: first registered) |
1761
+ | `exchange` | `string` | Exchange name (default: first registered) |
1762
+ | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1763
+
1764
+ ### Examples
1765
+
1766
+ **Backtest:**
1767
+
1768
+ ```typescript
1769
+ import { run } from '@backtest-kit/cli';
1770
+
1771
+ await run('backtest', {
1772
+ entryPoint: './src/index.mjs',
1773
+ symbol: 'ETHUSDT',
1774
+ frame: 'feb-2024',
1775
+ cacheInterval: ['1m', '15m', '1h'],
1776
+ verbose: true,
1777
+ });
1778
+ ```
1779
+
1780
+ **Paper trading:**
1781
+
1782
+ ```typescript
1783
+ import { run } from '@backtest-kit/cli';
1784
+
1785
+ await run('paper', {
1786
+ entryPoint: './src/index.mjs',
1787
+ symbol: 'BTCUSDT',
1788
+ });
1789
+ ```
1790
+
1791
+ **Live trading:**
1792
+
1793
+ ```typescript
1794
+ import { run } from '@backtest-kit/cli';
1795
+
1796
+ await run('live', {
1797
+ entryPoint: './src/index.mjs',
1798
+ symbol: 'BTCUSDT',
1799
+ verbose: true,
1800
+ });
1801
+ ```
1802
+
1803
+ ## 💡 Why Use @backtest-kit/cli?
1804
+
1805
+ Instead of writing infrastructure code for every project:
1806
+
1807
+ **❌ Without @backtest-kit/cli (manual setup)**
1808
+
1809
+ ```typescript
1810
+ // index.ts
1811
+ import { setLogger, setConfig, Storage, Notification, Report, Markdown } from 'backtest-kit';
1812
+ import { serve } from '@backtest-kit/ui';
1813
+
1814
+ setLogger({ log: console.log, ... });
1815
+ Storage.enable();
1816
+ Notification.enable();
1817
+ Report.enable();
1818
+ Markdown.disable();
1819
+
1820
+ // ... parse CLI args manually
1821
+ // ... register exchange schema
1822
+ // ... warm candle cache
1823
+ // ... set up Telegram bot
1824
+ // ... handle SIGINT gracefully
1825
+ // ... load and run backtest
1826
+ ```
1827
+
1828
+ **✅ With @backtest-kit/cli (one script)**
1829
+
1830
+ ```json
1831
+ { "scripts": { "backtest": "npx @backtest-kit/cli --backtest --ui --telegram ./src/index.mjs" } }
1832
+ ```
1833
+
1834
+ ```bash
1835
+ npm run backtest
1836
+ ```
1837
+
1838
+ **Benefits:**
1839
+
1840
+ - 🚀 From zero to running backtest in seconds
1841
+ - 💾 Automatic candle cache warming with retry logic
1842
+ - 🌐 Production-ready web dashboard out of the box
1843
+ - 📬 Telegram notifications with price charts — no chart code needed
1844
+ - 🛑 Graceful shutdown on SIGINT — no hanging processes
1845
+ - 🔌 Works with any `backtest-kit` strategy file as-is
1846
+ - 🧩 Broker adapter hooks via side-effect module files — no CLI internals to touch
1847
+
1848
+ ## 🤝 Contribute
1849
+
1850
+ Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
1851
+
1852
+ ## 📜 License
1853
+
1854
+ MIT © [tripolskypetr](https://github.com/tripolskypetr)