@backtest-kit/cli 8.4.2 → 8.4.3

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.
package/README.md CHANGED
@@ -1,1615 +1,1615 @@
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
- ## 🗂️ Monorepo Usage
281
-
282
- `@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.
283
-
284
- ### How It Works
285
-
286
- Internally, `ResolveService` does the following before executing your entry point:
287
-
288
- ```
289
- process.chdir(path.dirname(entryPoint)) // cwd → strategy directory
290
- dotenv.config({ path: rootDir + '/.env' }) // load root .env first
291
- dotenv.config({ path: strategyDir + '/.env', override: true }) // strategy .env overrides
292
- ```
293
-
294
- Everything that follows — candle cache warming, report generation, module loading, template resolution — uses the new cwd automatically.
295
-
296
- ### Project Structure
297
-
298
- ```
299
- monorepo/
300
- ├── package.json # root scripts (one per strategy)
301
- ├── .env # shared API keys (exchange, Telegram, etc.)
302
- └── strategies/
303
- ├── oct_2025/
304
- │ ├── index.mjs # entry point — registers exchange/frame/strategy schemas
305
- │ ├── .env # overrides root .env for this strategy
306
- │ ├── modules (optional)
307
- │ | ├── live.module.mjs # broker adapter for --live mode (optional)
308
- │ | ├── paper.module.mjs # broker adapter for --paper mode (optional)
309
- │ | ├── backtest.module.mjs # broker adapter for --backtest mode (optional)
310
- │ ├── template/ # custom Mustache templates (optional)
311
- │ └── dump/ # auto-created: candle cache + backtest reports
312
- └── dec_2025/
313
- ├── index.mjs
314
- ├── .env
315
- └── dump/
316
- ```
317
-
318
- ### Root `package.json`
319
-
320
- ```json
321
- {
322
- "scripts": {
323
- "backtest:oct": "npx @backtest-kit/cli --backtest ./strategies/oct_2025/index.mjs",
324
- "backtest:dec": "npx @backtest-kit/cli --backtest ./strategies/dec_2025/index.mjs"
325
- },
326
- "dependencies": {
327
- "@backtest-kit/cli": "latest",
328
- "backtest-kit": "latest",
329
- "ccxt": "latest"
330
- }
331
- }
332
- ```
333
-
334
- ```bash
335
- npm run backtest:oct
336
- npm run backtest:dec
337
- ```
338
-
339
- ### Isolated Resources Per Strategy
340
-
341
- | Resource | Path (relative to strategy dir) | Isolated |
342
- |--------------------------|-----------------------------------|------------------|
343
- | Candle cache | `./dump/data/candle/` | ✅ per-strategy |
344
- | Backtest reports | `./dump/` | ✅ per-strategy |
345
- | Broker module (live) | `./modules/live.module.mjs` | ✅ per-strategy |
346
- | Broker module (paper) | `./modules/paper.module.mjs` | ✅ per-strategy |
347
- | Broker module (backtest) | `./modules/backtest.module.mjs` | ✅ per-strategy |
348
- | Config module (walker) | `./modules/walker.module.mjs` | ✅ loaded once |
349
- | Telegram templates | `./template/*.mustache` | ✅ per-strategy |
350
- | Environment variables | `./.env` (overrides root) | ✅ per-strategy |
351
-
352
- 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.
353
-
354
- ## 🔗 Shared Import Aliases
355
-
356
- `@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.
357
-
358
- ### How It Works
359
-
360
- 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:
361
-
362
- | Import | Resolves to |
363
- |--------|-------------|
364
- | `import { fn } from "utils"` | `<cwd>/utils/index.ts` (or `.js`, `.mjs`, `.cjs`) |
365
- | `import { calcRSI } from "math/rsi"` | `<cwd>/math/rsi.ts` |
366
- | `import { research } from "logic"` | `<cwd>/logic/index.ts` |
367
- | `import { ResearchResponseContract } from "logic/contract/ResearchResponse.contract"` | `<cwd>/logic/contract/ResearchResponse.contract.ts` |
368
-
369
- ### Project Structure
370
-
371
- ```
372
- my-project/
373
- ├── utils/ ← import { formatDate } from "utils"
374
- │ └── index.ts
375
- ├── math/ ← import { calcRSI } from "math/rsi"
376
- │ └── rsi.ts
377
- ├── logic/ ← import { research } from "logic"
378
- │ ├── index.ts ← barrel
379
- │ └── contract/
380
- │ └── ResearchResponse.contract.ts ← import { ... } from "logic/contract/ResearchResponse.contract"
381
- └── content/
382
- ├── feb_2026.strategy.ts ← uses all three aliases freely
383
- └── mar_2026.strategy.ts ← same aliases, no duplication
384
- ```
385
-
386
- 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.
387
-
388
- ### TypeScript Support
389
-
390
- Add a matching `paths` entry to your `tsconfig.json` so the editor resolves the aliases:
391
-
392
- ```json
393
- {
394
- "compilerOptions": {
395
- "moduleResolution": "bundler",
396
- "paths": {
397
- "logic": ["./logic/index.ts"],
398
- "logic/*": ["./logic/*"],
399
- "math": ["./math/index.ts"],
400
- "math/*": ["./math/*"],
401
- "utils": ["./utils/index.ts"],
402
- "utils/*": ["./utils/*"]
403
- }
404
- },
405
- "include": [
406
- "./logic",
407
- "./math",
408
- "./utils",
409
- "./content",
410
- "./modules",
411
- ],
412
- }
413
- ```
414
-
415
- ## 🔔 Integrations
416
-
417
- ### Web Dashboard (`--ui`)
418
-
419
- Starts `@backtest-kit/ui` server. Access the interactive dashboard at:
420
-
421
- ```
422
- http://localhost:60050
423
- ```
424
-
425
- Customize host/port via environment variables `CC_WWWROOT_HOST` and `CC_WWWROOT_PORT`.
426
-
427
- #### Symbol List (`symbol.config`)
428
-
429
- 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).
430
-
431
- **Resolution order — first match wins:**
432
-
433
- | Priority | Path | Notes |
434
- |----------|------|-------|
435
- | 1 | `{strategyDir}/config/symbol.config` | per-strategy override (cwd after `chdir`) |
436
- | 2 | `{projectRoot}/config/symbol.config` | project-root override (cwd where `npx` was invoked) |
437
- | 3 | `@backtest-kit/cli/config/symbol.config` | built-in default shipped with the package |
438
-
439
- Supported file formats (`.ts`, `.cjs`, `.mjs`, `.js` tried automatically):
440
-
441
- ```ts
442
- // config/symbol.config.ts
443
- export const symbol_list = [
444
- {
445
- icon: "/icon/btc.png",
446
- logo: "/icon/128/btc.png",
447
- symbol: "BTCUSDT",
448
- displayName: "Bitcoin",
449
- color: "#F7931A",
450
- priority: 50,
451
- description: "Bitcoin - the first and most popular cryptocurrency",
452
- },
453
- {
454
- icon: "/icon/eth.png",
455
- logo: "/icon/128/eth.png",
456
- symbol: "ETHUSDT",
457
- displayName: "Ethereum",
458
- color: "#6F42C1",
459
- priority: 50,
460
- description: "Ethereum - a blockchain platform for smart contracts",
461
- },
462
- ];
463
- ```
464
-
465
- #### Notification Filter (`notification.config`)
466
-
467
- Controls which notification categories are shown in the UI dashboard. Create a `config/notification.config` file in your strategy directory to override the defaults.
468
-
469
- **Resolution order — first match wins:**
470
-
471
- | Priority | Path | Notes |
472
- |----------|------|-------|
473
- | 1 | `{strategyDir}/config/notification.config` | per-strategy override (cwd after `chdir`) |
474
- | 2 | `{projectRoot}/config/notification.config` | project-root override (cwd where `npx` was invoked) |
475
- | 3 | `@backtest-kit/cli/config/notification.config` | built-in default shipped with the package |
476
-
477
- **Default values (built-in):**
478
-
479
- | Key | Default | Description |
480
- |-----|---------|-------------|
481
- | `signal` | `true` | Signal lifecycle: opened, scheduled, closed, cancelled |
482
- | `risk` | `true` | Risk manager rejection notifications |
483
- | `info` | `true` | Informational messages attached to an active signal |
484
- | `breakeven` | `true` | Breakeven level reached |
485
- | `common_error` | `true` | Non-fatal runtime errors |
486
- | `critical_error` | `true` | Fatal errors that terminate the session |
487
- | `validation_error` | `true` | Strategy config / input validation errors |
488
- | `strategy_commit` | `true` | All committed actions (partial close, DCA, trailing, etc.) |
489
- | `partial_loss` | `false` | Partial loss level reached (before commit) |
490
- | `partial_profit` | `false` | Partial profit level reached (before commit) |
491
- | `signal_sync` | `false` | Live order fill / exit confirmations from exchange sync |
492
-
493
- ```js
494
- // config/notification.config.ts
495
- export default {
496
- signal: true,
497
- risk: true,
498
- info: true,
499
- breakeven: true,
500
- common_error: true,
501
- critical_error: true,
502
- validation_error: true,
503
- strategy_commit: true,
504
- partial_loss: false,
505
- partial_profit: false,
506
- signal_sync: false,
507
- };
508
- ```
509
-
510
- ### Telegram Notifications (`--telegram`)
511
-
512
- 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.
513
-
514
- Requires `CC_TELEGRAM_TOKEN` and `CC_TELEGRAM_CHANNEL` in your environment.
515
-
516
- #### Telegram Message Adapter (`telegram.config`)
517
-
518
- 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.
519
-
520
- Resolution order is the same as other configs (strategy dir → project root → package default).
521
-
522
- ```ts
523
- // config/telegram.config.ts
524
- import {
525
- IStrategyTickResultOpened,
526
- IStrategyTickResultClosed,
527
- RiskContract,
528
- } from "backtest-kit";
529
-
530
- export default {
531
- async getOpenedMarkdown(event: IStrategyTickResultOpened): Promise<string> {
532
- return `**Opened** ${event.symbol} at ${event.priceOpen}`;
533
- },
534
- async getClosedMarkdown(event: IStrategyTickResultClosed): Promise<string> {
535
- return `**Closed** ${event.symbol} at ${event.priceClosed}`;
536
- },
537
- async getRiskMarkdown(event: RiskContract): Promise<string> {
538
- return `**Risk rejected** ${event.symbol}`;
539
- },
540
- };
541
- ```
542
-
543
- All methods are optional — unimplemented ones fall back to the Mustache template.
544
-
545
- | Method | Event type |
546
- |--------|------------|
547
- | `getOpenedMarkdown` | `IStrategyTickResultOpened` |
548
- | `getClosedMarkdown` | `IStrategyTickResultClosed` |
549
- | `getScheduledMarkdown` | `IStrategyTickResultScheduled` |
550
- | `getCancelledMarkdown` | `IStrategyTickResultCancelled` |
551
- | `getRiskMarkdown` | `RiskContract` |
552
- | `getPartialProfitMarkdown` | `PartialProfitCommit` |
553
- | `getPartialLossMarkdown` | `PartialLossCommit` |
554
- | `getBreakevenMarkdown` | `BreakevenCommit` |
555
- | `getTrailingTakeMarkdown` | `TrailingTakeCommit` |
556
- | `getTrailingStopMarkdown` | `TrailingStopCommit` |
557
- | `getAverageBuyMarkdown` | `AverageBuyCommit` |
558
- | `getSignalOpenMarkdown` | `SignalOpenContract` |
559
- | `getSignalCloseMarkdown` | `SignalCloseContract` |
560
- | `getCancelScheduledMarkdown` | `CancelScheduledCommit` |
561
- | `getClosePendingMarkdown` | `ClosePendingCommit` |
562
- | `getSignalInfoMarkdown` | `SignalInfoContract` |
563
-
564
- ## 🧩 Module Hooks (Broker Adapter)
565
-
566
- 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.
567
-
568
- | Command Line Args | Module file | Loaded before |
569
- |-------------------|---------------------------------|-----------------------------|
570
- | `--live` | `./modules/live.module.mjs` | `Live.background()` |
571
- | `--paper` | `./modules/paper.module.mjs` | `Live.background()` (paper) |
572
- | `--backtest` | `./modules/backtest.module.mjs` | `Backtest.background()` |
573
- | `--walker` | `./modules/walker.module.mjs` | `Walker.background()` |
574
- | `--brokerdebug` | `./modules/brokerdebug.module.mjs` | broker commit test |
575
-
576
- > 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.
577
-
578
- ### How It Works
579
-
580
- 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.
581
-
582
- ```javascript
583
- // live.module.mjs
584
- import { Broker } from 'backtest-kit';
585
- import { myExchange } from './exchange.mjs';
586
-
587
- class MyBroker {
588
- async onSignalOpenCommit({ symbol, priceOpen, direction }) {
589
- await myExchange.openPosition(symbol, direction, priceOpen);
590
- }
591
-
592
- async onSignalCloseCommit({ symbol, priceClosed }) {
593
- await myExchange.closePosition(symbol, priceClosed);
594
- }
595
-
596
- async onPartialProfitCommit({ symbol, cost, currentPrice }) {
597
- await myExchange.createOrder({
598
- symbol,
599
- side: 'sell',
600
- quantity: cost / currentPrice,
601
- });
602
- }
603
-
604
- async onAverageBuyCommit({ symbol, cost, currentPrice }) {
605
- await myExchange.createOrder({
606
- symbol,
607
- side: 'buy',
608
- quantity: cost / currentPrice,
609
- });
610
- }
611
- }
612
-
613
- Broker.useBrokerAdapter(MyBroker);
614
-
615
- Broker.enable();
616
- ```
617
-
618
- ### Available Broker Hooks
619
-
620
- | Method | Payload type | Triggered on |
621
- |--------------------------|------------------------------|---------------------------|
622
- | `onSignalOpenCommit` | `BrokerSignalOpenPayload` | Position activation |
623
- | `onSignalCloseCommit` | `BrokerSignalClosePayload` | SL / TP / manual close |
624
- | `onPartialProfitCommit` | `BrokerPartialProfitPayload` | PP |
625
- | `onPartialLossCommit` | `BrokerPartialLossPayload` | PL |
626
- | `onTrailingStopCommit` | `BrokerTrailingStopPayload` | SL adjustment |
627
- | `onTrailingTakeCommit` | `BrokerTrailingTakePayload` | TP adjustment |
628
- | `onBreakevenCommit` | `BrokerBreakevenPayload` | SL moved to entry |
629
- | `onAverageBuyCommit` | `BrokerAverageBuyPayload` | DCA entry |
630
-
631
- All methods are optional. Unimplemented hooks are silently skipped. In backtest mode all broker calls are skipped automatically — no adapter code runs during backtests.
632
-
633
- ### TypeScript
634
-
635
- ```typescript
636
- import { Broker, IBroker, BrokerSignalOpenPayload, BrokerSignalClosePayload } from 'backtest-kit';
637
-
638
- class MyBroker implements Partial<IBroker> {
639
- async onSignalOpenCommit(payload: BrokerSignalOpenPayload) {
640
- // place open order on exchange
641
- }
642
-
643
- async onSignalCloseCommit(payload: BrokerSignalClosePayload) {
644
- // place close order on exchange
645
- }
646
- }
647
-
648
- Broker.useBrokerAdapter(MyBroker);
649
-
650
- Broker.enable();
651
- ```
652
-
653
- ## 🔀 Import Aliases (`alias.module`)
654
-
655
- `@backtest-kit/cli` lets you override any nodejs module import — without touching the strategy code. Drop a `config/alias.module` file in your project root and export a mapping from module name to replacement module.
656
-
657
- The alias table is loaded once (on the first `import` call) from `{projectRoot}/config/alias.module` and applied globally to every subsequent module load via `require`/ `import`.
658
-
659
- **Use cases:**
660
-
661
- - Replace a heavy dependency with a lighter stub for backtesting
662
- - Swap any external api for a mock during CI runs
663
-
664
- ```ts
665
- // config/alias.module.ts — named export
666
- export const ccxt = require("./stubs/ccxt.stub.cjs");
667
- ```
668
-
669
- ```js
670
- // config/alias.module.cjs — default export
671
- module.exports = {
672
- ccxt: require("./stubs/ccxt.stub.cjs"),
673
- };
674
- ```
675
-
676
- ```js
677
- // config/alias.module.mjs — default export
678
- import ccxtStub from "./stubs/ccxt.stub.mjs";
679
-
680
- export default {
681
- ccxt: ccxtStub,
682
- };
683
- ```
684
-
685
- **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.
686
-
687
- **Important:** It is **not** per-strategy — it applies to all modules loaded in the current process.
688
-
689
- ## 📦 Supported Entry Point Formats
690
-
691
- `@backtest-kit/cli` automatically detects the format of your strategy file and loads it with the appropriate runtime — no flags or configuration required.
692
-
693
- | Format | Extension | Runtime | Use Case |
694
- |--------|-----------|---------|----------|
695
- | **TypeScript** | `.ts` | [`tsx`](https://tsx.is/) via `tsImport()` | TypeScript strategies with cross-imports (ESM ↔ CJS) |
696
- | **ES Module** | `.mjs` | Native `import()` | Modern JavaScript with top-level `await` and ESM syntax |
697
- | **CommonJS** | `.cjs` | Native `require()` | Legacy or dual-package strategies |
698
-
699
- ### TypeScript (`.ts`)
700
-
701
- Run TypeScript strategy files directly — no `tsc` compilation step needed. Powered by `tsx`, which handles cross-format imports transparently:
702
-
703
- ```json
704
- {
705
- "scripts": {
706
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.ts"
707
- },
708
- "dependencies": {
709
- "@backtest-kit/cli": "latest",
710
- "backtest-kit": "latest",
711
- "tsx": "latest"
712
- }
713
- }
714
- ```
715
-
716
- ### ES Module (`.mjs`)
717
-
718
- Standard ESM format. Supports top-level `await`, named exports, and `import` syntax:
719
-
720
- ```json
721
- {
722
- "scripts": {
723
- "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
724
- }
725
- }
726
- ```
727
-
728
- ### CommonJS (`.cjs`)
729
-
730
- For projects that compile to or use CommonJS. Loaded via `require()`:
731
-
732
- ```json
733
- {
734
- "scripts": {
735
- "backtest": "npx @backtest-kit/cli --backtest ./dist/index.cjs"
736
- }
737
- }
738
- ```
739
-
740
- ## 🌲 Running Local PineScript Indicators
741
-
742
- `@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.
743
-
744
- ### CLI Flags
745
-
746
- | Flag | Type | Description |
747
- |------|------|-------------|
748
- | `--pine` | boolean | Enable PineScript execution mode |
749
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
750
- | `--timeframe` | string | Candle interval (default: `"15m"`) |
751
- | `--limit` | string | Number of candles to fetch (default: `250`) |
752
- | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
753
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
754
- | `--output` | string | Output file base name without extension (default: `.pine` file name) |
755
- | `--json` | boolean | Write plots as a JSON array to `<pine-dir>/dump/{output}.json` |
756
- | `--jsonl` | boolean | Write plots as JSONL (one row per line) to `<pine-dir>/dump/{output}.jsonl` |
757
- | `--markdown` | boolean | Write Markdown table to `<pine-dir>/dump/{output}.md` |
758
-
759
- **Important:** `limit` must cover indicator warmup bars — rows before warmup completes will show `N/A`
760
-
761
- **Positional argument:** path to the `.pine` file.
762
-
763
- ### Exchange via `pine.module`
764
-
765
- 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.
766
-
767
- The CLI looks for `modules/pine.module` in two locations (first match wins):
768
-
769
- 1. **Next to the `.pine` file** — `<pine-file-dir>/modules/pine.module.ts`
770
- 2. **Project root** — `<cwd>/modules/pine.module.ts`
771
-
772
- ```
773
- my-project/
774
- ├── math/
775
- │ ├── impulse_trend_15m.pine ← indicator
776
- │ └── modules/
777
- │ └── pine.module.ts ← loaded first (next to .pine file)
778
- ├── modules/
779
- │ └── pine.module.ts ← fallback (project root)
780
- └── package.json
781
- ```
782
-
783
- Inside `pine.module.ts` call `addExchangeSchema` from `backtest-kit` and give the exchange a name:
784
-
785
- ```typescript
786
- // modules/pine.module.ts
787
- import { addExchangeSchema } from "backtest-kit";
788
- import ccxt from "ccxt";
789
-
790
- addExchangeSchema({
791
- exchangeName: "my-exchange",
792
- getCandles: async (symbol, interval, since, limit) => {
793
- const exchange = new ccxt.bybit({ enableRateLimit: true });
794
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
795
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
796
- timestamp, open, high, low, close, volume,
797
- }));
798
- },
799
- formatPrice: (symbol, price) => price.toFixed(2),
800
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
801
- });
802
- ```
803
-
804
- ### Environment variables (`.env`)
805
-
806
- 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):
807
-
808
- ```
809
- my-project/
810
- ├── math/
811
- │ ├── .env ← loaded second (overrides root)
812
- │ └── impulse_trend_15m.pine
813
- ├── .env ← loaded first
814
- └── package.json
815
- ```
816
-
817
- Use this to store API keys without hardcoding them:
818
-
819
- ```env
820
- # .env
821
- BYBIT_API_KEY=xxx
822
- BYBIT_API_SECRET=yyy
823
- ```
824
-
825
- ```typescript
826
- // modules/pine.module.ts
827
- addExchangeSchema({
828
- exchangeName: "my-exchange",
829
- getCandles: async (symbol, interval, since, limit) => {
830
- const exchange = new ccxt.bybit({
831
- apiKey: process.env.BYBIT_API_KEY,
832
- secret: process.env.BYBIT_API_SECRET,
833
- enableRateLimit: true,
834
- });
835
- // ...
836
- },
837
- });
838
- ```
839
-
840
- Then run:
841
-
842
- ```bash
843
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine \
844
- --exchange my-exchange \
845
- --symbol BTCUSDT \
846
- --timeframe 15m \
847
- --limit 180 \
848
- --when "2025-09-24T12:00:00.000Z"
849
- ```
850
-
851
- Or add it to `package.json`:
852
-
853
- ```json
854
- {
855
- "scripts": {
856
- "pine": "npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --symbol BTCUSDT --timeframe 15m --limit 180"
857
- }
858
- }
859
- ```
860
-
861
- ```bash
862
- npm run pine
863
- ```
864
-
865
- ### PineScript Requirements
866
-
867
- 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:
868
-
869
- ```pine
870
- //@version=5
871
- indicator("MyIndicator", overlay=true)
872
-
873
- // ... computation ...
874
-
875
- plot(close, "Close", display=display.data_window)
876
- plot(position, "Position", display=display.data_window)
877
- ```
878
-
879
- The column names in the output Markdown table are taken directly from those plot names — no manual schema definition needed.
880
-
881
- ### Output
882
-
883
- The CLI prints a Markdown table to stdout:
884
-
885
- ```
886
- # PineScript Technical Analysis Dump
887
-
888
- **Signal ID**: CLI execution 2025-09-24T12:00:00.000Z
889
-
890
- | Close | Position | timestamp |
891
- | --- | --- | --- |
892
- | 112871.28 | -1.0000 | 2025-09-22T15:00:00.000Z |
893
- | 112666.69 | -1.0000 | 2025-09-22T15:15:00.000Z |
894
- | 112736.00 | 0.0000 | 2025-09-22T18:30:00.000Z |
895
- | 112653.90 | 1.0000 | 2025-09-22T22:15:00.000Z |
896
- ```
897
-
898
- Save to `./math/dump/impulse_trend_15m.md` (uses `.pine` file name automatically, dump is created next to the `.pine` file):
899
-
900
- ```bash
901
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --markdown
902
- ```
903
-
904
- Override the output name with `--output`:
905
-
906
- ```bash
907
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --jsonl --output feb2026_bb
908
- # → ./math/dump/feb2026_bb.jsonl
909
- ```
910
-
911
- Print to stdout (no flag):
912
-
913
- ```bash
914
- npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine
915
- ```
916
-
917
- ## 🎨 Visual Pine Script Editor
918
-
919
- ![pine](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot32.png)
920
-
921
- `@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**
922
-
923
- ### Usage
924
-
925
- ```bash
926
- npx @backtest-kit/cli --editor
927
- ```
928
-
929
- The CLI will:
930
-
931
- 1. Load `./modules/editor.module` (if it exists) — use it to register your exchange schema, identical to `pine.module`
932
- 2. Start the `@backtest-kit/ui` server on `http://localhost:60050` (or `CC_WWWROOT_PORT`)
933
- 3. Open `http://localhost:{CC_WWWROOT_PORT}?pine=1` automatically in your default browser
934
-
935
- Press **Ctrl+C** to stop the server.
936
-
937
- ### Exchange via `editor.module`
938
-
939
- Drop a `modules/editor.module.ts` next to your project to register the exchange that the editor's candle provider will use:
940
-
941
- ```typescript
942
- // modules/editor.module.ts
943
- import { addExchangeSchema } from "backtest-kit";
944
- import ccxt from "ccxt";
945
-
946
- addExchangeSchema({
947
- exchangeName: "my-exchange",
948
- getCandles: async (symbol, interval, since, limit) => {
949
- const exchange = new ccxt.bybit({ enableRateLimit: true });
950
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
951
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
952
- timestamp, open, high, low, close, volume,
953
- }));
954
- },
955
- formatPrice: (symbol, price) => price.toFixed(2),
956
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
957
- });
958
- ```
959
-
960
- ### Environment Variables
961
-
962
- | Variable | Default | Description |
963
- |-------------------|-----------|----------------------------------|
964
- | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
965
- | `CC_WWWROOT_PORT` | `60050` | UI server port |
966
-
967
- ### `package.json` script
968
-
969
- ```json
970
- {
971
- "scripts": {
972
- "editor": "npx @backtest-kit/cli --editor"
973
- }
974
- }
975
- ```
976
-
977
- ```bash
978
- npm run editor
979
- ```
980
-
981
- ## 💾 Dumping Raw Candles
982
-
983
- `@backtest-kit/cli` can fetch raw OHLCV candles from any registered exchange and save them to a file — no strategy file required.
984
-
985
- ### CLI Flags
986
-
987
- | Flag | Type | Description |
988
- |------|------|-------------|
989
- | `--dump` | boolean | Enable candle dump mode |
990
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
991
- | `--timeframe` | string | Candle interval (default: `"15m"`) |
992
- | `--limit` | string | Number of candles to fetch (default: `250`) |
993
- | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
994
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
995
- | `--output` | string | Output file base name without extension (default: `{SYMBOL}_{LIMIT}_{TIMEFRAME}_{TIMESTAMP}`) |
996
- | `--json` | boolean | Write candles as a JSON array to `./dump/{output}.json` |
997
- | `--jsonl` | boolean | Write candles as JSONL (one row per line) to `./dump/{output}.jsonl` |
998
-
999
- The `dump/` directory is created in the current working directory (where the CLI is invoked from).
1000
-
1001
- ### Exchange via `dump.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/dump.module.ts` file. The CLI loads it automatically before fetching candles.
1004
-
1005
- The CLI looks for `modules/dump.module` in the current working directory
1006
-
1007
- ```
1008
- my-project/
1009
- ├── modules/
1010
- │ └── dump.module.ts ← exchange registration
1011
- ├── dump/ ← auto-created: candle output files
1012
- └── package.json
1013
- ```
1014
-
1015
- Inside `dump.module.ts` call `addExchangeSchema` from `backtest-kit`:
1016
-
1017
- ```typescript
1018
- // modules/dump.module.ts
1019
- import { addExchangeSchema } from "backtest-kit";
1020
- import ccxt from "ccxt";
1021
-
1022
- addExchangeSchema({
1023
- exchangeName: "my-exchange",
1024
- getCandles: async (symbol, interval, since, limit) => {
1025
- const exchange = new ccxt.bybit({ enableRateLimit: true });
1026
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1027
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1028
- timestamp, open, high, low, close, volume,
1029
- }));
1030
- },
1031
- formatPrice: (symbol, price) => price.toFixed(2),
1032
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1033
- });
1034
- ```
1035
-
1036
- ### Output
1037
-
1038
- Each candle row contains OHLCV fields. Print to stdout:
1039
-
1040
- ```bash
1041
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100
1042
- ```
1043
-
1044
- Save to `./dump/BTCUSDT_100_15m_{timestamp}.jsonl`:
1045
-
1046
- ```bash
1047
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100 --jsonl
1048
- ```
1049
-
1050
- Fetch candles up to a specific date with `--when` and override the file name with `--output`:
1051
-
1052
- ```bash
1053
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 \
1054
- --when "2026-02-28T00:00:00.000Z" \
1055
- --jsonl --output feb2026_btc
1056
- # → ./dump/feb2026_btc.jsonl
1057
- ```
1058
-
1059
- Or add it to `package.json`:
1060
-
1061
- ```json
1062
- {
1063
- "scripts": {
1064
- "dump": "npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl"
1065
- }
1066
- }
1067
- ```
1068
-
1069
- ```bash
1070
- npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl
1071
- ```
1072
-
1073
- ## 🐞 PnL Debug (`--pnldebug`)
1074
-
1075
- `@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.
1076
-
1077
- ### CLI Flags
1078
-
1079
- | Flag | Type | Description |
1080
- |------|------|-------------|
1081
- | `--pnldebug` | boolean | Enable PnL debug mode |
1082
- | `--priceopen` | number | Entry price (required) |
1083
- | `--direction` | string | `long` or `short` (default: `long`) |
1084
- | `--when` | string | Start timestamp — ISO 8601 or Unix ms (default: now) |
1085
- | `--minutes` | string | Number of 1m candles to simulate (default: `60`) |
1086
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1087
- | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1088
- | `--output` | string | Output file base name (default: `{SYMBOL}_{DIRECTION}_{PRICEOPEN}_{TIMESTAMP}`) |
1089
- | `--json` | boolean | Save results as JSON array to `./dump/<output>.json` |
1090
- | `--jsonl` | boolean | Save results as JSONL to `./dump/<output>.jsonl` |
1091
- | `--markdown` | boolean | Save results as Markdown table to `./dump/<output>.md` |
1092
-
1093
- ### Output columns
1094
-
1095
- | Column | Description |
1096
- |--------|-------------|
1097
- | `min` | Minute offset from start (1-based) |
1098
- | `timestamp` | Candle timestamp (ISO 8601) |
1099
- | `close` | Candle close price |
1100
- | `pnl%` | Running PnL vs entry price (signed %) |
1101
- | `peak%` | Highest PnL reached so far (always ≥ 0) |
1102
- | `drawdown%` | Lowest PnL reached so far (always ≤ 0) |
1103
-
1104
- ### Exchange via `pnldebug.module`
1105
-
1106
- 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.
1107
-
1108
- ```typescript
1109
- // modules/pnldebug.module.ts
1110
- import { addExchangeSchema } from "backtest-kit";
1111
- import ccxt from "ccxt";
1112
-
1113
- addExchangeSchema({
1114
- exchangeName: "my-exchange",
1115
- getCandles: async (symbol, interval, since, limit) => {
1116
- const exchange = new ccxt.bybit({ enableRateLimit: true });
1117
- const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1118
- return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1119
- timestamp, open, high, low, close, volume,
1120
- }));
1121
- },
1122
- formatPrice: (symbol, price) => price.toFixed(2),
1123
- formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1124
- });
1125
- ```
1126
-
1127
- ### Usage
1128
-
1129
- Print to stdout (default table format):
1130
-
1131
- ```bash
1132
- npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120
1133
- ```
1134
-
1135
- Save as Markdown:
1136
-
1137
- ```bash
1138
- npx @backtest-kit/cli --pnldebug --priceopen 67956.73 --direction long --when 1772064000000 --minutes 60 --markdown
1139
- # → ./dump/BTCUSDT_long_67956.73_{timestamp}.md
1140
- ```
1141
-
1142
- Override the output file name with `--output`:
1143
-
1144
- ```bash
1145
- npx @backtest-kit/cli --pnldebug --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120 \
1146
- --jsonl --output feb25_short_debug
1147
- # → ./dump/feb25_short_debug.jsonl
1148
- ```
1149
-
1150
- Or add it to `package.json`:
1151
-
1152
- ```json
1153
- {
1154
- "scripts": {
1155
- "pnldebug": "npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when \"2025-02-25\" --minutes 120"
1156
- }
1157
- }
1158
- ```
1159
-
1160
- ```bash
1161
- npm run pnldebug
1162
- ```
1163
-
1164
- ### Example stdout output
1165
-
1166
- ```
1167
- Symbol: BTCUSDT | Direction: short | PriceOpen: 64069.50 | From: 2025-02-25T00:00:00.000Z | Minutes: 120
1168
-
1169
- min | timestamp | close | pnl% | peak% | drawdown%
1170
- -----------------------------------------------------------------------------------
1171
- 1 | 2025-02-25T00:01:00.000Z | 64020.10 | +0.08% | +0.08% | 0.00%
1172
- 2 | 2025-02-25T00:02:00.000Z | 64105.30 | -0.06% | +0.08% | -0.06%
1173
- ...
1174
- 120 | 2025-02-25T02:00:00.000Z | 63200.00 | +1.36% | +1.36% | -0.06%
1175
- ```
1176
-
1177
- ## 🐛 Broker Debug (`--brokerdebug`)
1178
-
1179
- `@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.
1180
-
1181
- ### CLI Flags
1182
-
1183
- | Flag | Type | Description |
1184
- |------|------|-------------|
1185
- | `--brokerdebug` | boolean | Enable broker debug mode |
1186
- | `--commit` | string | Commit type to fire (default: `"signal-open"`) |
1187
- | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1188
- | `--exchange` | string | Exchange name (default: first registered) |
1189
-
1190
- **Available `--commit` values:**
1191
-
1192
- | Value | Broker hook |
1193
- |-------|-------------|
1194
- | `signal-open` | `onSignalOpenCommit` |
1195
- | `signal-close` | `onSignalCloseCommit` |
1196
- | `partial-profit` | `onPartialProfitCommit` |
1197
- | `partial-loss` | `onPartialLossCommit` |
1198
- | `average-buy` | `onAverageBuyCommit` |
1199
- | `trailing-stop` | `onTrailingStopCommit` |
1200
- | `trailing-take` | `onTrailingTakeCommit` |
1201
- | `breakeven` | `onBreakevenCommit` |
1202
-
1203
- ### How It Works
1204
-
1205
- 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.
1206
-
1207
- ### Broker via `brokerdebug.module`
1208
-
1209
- Create a `modules/brokerdebug.module.ts` file and register your broker adapter:
1210
-
1211
- ```typescript
1212
- // modules/brokerdebug.module.ts
1213
- import { Broker } from 'backtest-kit';
1214
- import { myExchange } from './exchange.mjs';
1215
-
1216
- class MyBroker {
1217
- async onSignalOpenCommit({ symbol, priceOpen, position }) {
1218
- await myExchange.openPosition(symbol, position, priceOpen);
1219
- }
1220
- // ... other hooks
1221
- }
1222
-
1223
- Broker.useBrokerAdapter(MyBroker);
1224
- Broker.enable();
1225
- ```
1226
-
1227
- ### Usage
1228
-
1229
- ```bash
1230
- npx @backtest-kit/cli --brokerdebug --commit signal-open --symbol BTCUSDT
1231
- npx @backtest-kit/cli --brokerdebug --commit partial-profit --symbol ETHUSDT --timeframe 1h
1232
- ```
1233
-
1234
- ## 🗑️ Flushing Strategy Output (`--flush`)
1235
-
1236
- `@backtest-kit/cli` can delete generated output folders from one or more strategy dump directories without touching cached candle data.
1237
-
1238
- ### CLI Flags
1239
-
1240
- | Flag | Type | Description |
1241
- |------|------|-------------|
1242
- | `--flush` | boolean | Enable flush mode |
1243
-
1244
- **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/`:
1245
-
1246
- | Folder | Contents |
1247
- |--------|----------|
1248
- | `report` | Backtest report files (`.jsonl`) |
1249
- | `log` | Run logs (`log.jsonl`) |
1250
- | `markdown` | Exported Markdown reports |
1251
- | `agent` | Agent outline files |
1252
-
1253
- Candle cache (`dump/data/`) and AI forecast outlines (`dump/outline/`) are **not** removed.
1254
-
1255
- ### Usage
1256
-
1257
- Flush a single strategy:
1258
-
1259
- ```bash
1260
- npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts
1261
- ```
1262
-
1263
- Flush multiple strategies at once:
1264
-
1265
- ```bash
1266
- npx @backtest-kit/cli --flush \
1267
- ./content/feb_2026.strategy/modules/backtest.module.ts \
1268
- ./content/mar_2026.strategy/modules/backtest.module.ts
1269
- ```
1270
-
1271
- Or add it to `package.json`:
1272
-
1273
- ```json
1274
- {
1275
- "scripts": {
1276
- "flush": "npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts"
1277
- }
1278
- }
1279
- ```
1280
-
1281
- ```bash
1282
- npm run flush
1283
- ```
1284
-
1285
- ## 🗂️ Scaffolding a New Project (`--init`)
1286
-
1287
- `@backtest-kit/cli` can bootstrap a ready-to-use project directory with a pre-configured layout, example strategy files, and all documentation fetched automatically.
1288
-
1289
- ### CLI Flags
1290
-
1291
- | Flag | Type | Description |
1292
- |------|------|-------------|
1293
- | `--init` | boolean | Scaffold a new project |
1294
- | `--output` | string | Target directory name (default: `backtest-kit-project`) |
1295
-
1296
- ### Usage
1297
-
1298
- ```bash
1299
- npx @backtest-kit/cli --init
1300
- ```
1301
-
1302
- Creates `./backtest-kit-project/` in the current working directory.
1303
-
1304
- Override the directory name with `--output`:
1305
-
1306
- ```bash
1307
- npx @backtest-kit/cli --init --output my-trading-bot
1308
- ```
1309
-
1310
- Creates `./my-trading-bot/`.
1311
-
1312
- The target directory must not exist or must be empty — the command aborts if it contains any files.
1313
-
1314
- ### Generated Project Structure
1315
-
1316
- ```
1317
- backtest-kit-project/
1318
- ├── package.json # pre-configured with all backtest-kit dependencies
1319
- ├── .gitignore
1320
- ├── CLAUDE.md # AI-agent guide for writing strategies
1321
- ├── content/
1322
- │ └── feb_2026.strategy.ts # example strategy entry point
1323
- ├── docs/
1324
- │ ├── lib/ # fetched automatically (see below)
1325
- │ ├── backtest_actions.md
1326
- │ ├── backtest_graph_pattern.md
1327
- │ ├── backtest_logging_jsonl.md
1328
- │ ├── backtest_pinets_usage.md
1329
- │ ├── backtest_risk_async.md
1330
- │ ├── backtest_strategy_structure.md
1331
- │ ├── pine_debug.md
1332
- │ └── pine_indicator_warmup.md
1333
- ├── math/
1334
- │ └── feb_2026.pine # example PineScript indicator
1335
- ├── modules/
1336
- │ ├── dump.module.ts # exchange schema for --dump mode
1337
- │ └── pine.module.ts # exchange schema for --pine mode
1338
- ├── report/
1339
- │ └── feb_2026.md # example strategy research report
1340
- └── scripts/
1341
- └── fetch_docs.mjs # utility: downloads library READMEs into docs/lib/
1342
- ```
1343
-
1344
- ### Automatic Documentation Fetch
1345
-
1346
- 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/`:
1347
-
1348
- | File | Source |
1349
- |------|--------|
1350
- | `backtest-kit.md` | `backtest-kit` README |
1351
- | `backtest-kit__graph.md` | `@backtest-kit/graph` README |
1352
- | `backtest-kit__pinets.md` | `@backtest-kit/pinets` README |
1353
- | `backtest-kit__cli.md` | `@backtest-kit/cli` README |
1354
- | `garch.md` | `garch` README |
1355
- | `volume-anomaly.md` | `volume-anomaly` README |
1356
- | `agent-swarm-kit.md` | `agent-swarm-kit` README |
1357
- | `functools-kit.md` | `functools-kit` README |
1358
-
1359
- You can re-run this script at any time to refresh the docs:
1360
-
1361
- ```bash
1362
- cd backtest-kit-project
1363
- node ./scripts/fetch_docs.mjs
1364
- ```
1365
-
1366
- Or via the pre-configured npm script:
1367
-
1368
- ```bash
1369
- npm run sync:lib
1370
- ```
1371
-
1372
- ## 🐳 Running in Docker (`--docker`)
1373
-
1374
- CLI can create a ready-to-use Docker workspace: self-contained directory with `docker-compose.yaml` and a strategy entry point.
1375
-
1376
- ### CLI Flags
1377
-
1378
- | Flag | Type | Description |
1379
- |------|------|-------------|
1380
- | `--docker` | boolean | Scaffold a Docker workspace |
1381
- | `--output` | string | Target directory name (default: `backtest-kit-docker`) |
1382
-
1383
- ### Usage
1384
-
1385
- ```bash
1386
- npx @backtest-kit/cli --docker
1387
- ```
1388
-
1389
- Creates `./backtest-kit-docker/` in the current working directory.
1390
-
1391
- Override the directory name with `--output`:
1392
-
1393
- ```bash
1394
- npx @backtest-kit/cli --docker --output my-docker-workspace
1395
- ```
1396
-
1397
- The target directory must not exist or must be empty — the command aborts if it contains any files.
1398
-
1399
- ### Two Launch Modes
1400
-
1401
- The Docker image entrypoint supports two ways to run a strategy:
1402
-
1403
- #### 1. `command:` in `docker-compose.yaml`
1404
-
1405
- Pin mode and flags directly in the compose file. The entrypoint forwards all arguments to the CLI unchanged:
1406
-
1407
- ```yaml
1408
- command:
1409
- - --live
1410
- - --symbol
1411
- - TRXUSDT
1412
- - --strategy
1413
- - feb_2026_strategy
1414
- - --exchange
1415
- - ccxt-exchange
1416
- - ./content/feb_2026/feb_2026.strategy.ts
1417
- - --ui
1418
- ```
1419
-
1420
- #### 2. Inline environment variables
1421
-
1422
- Pass `MODE` and `STRATEGY_FILE` on the command line — no file edits needed:
1423
-
1424
- ```bash
1425
- MODE=live SYMBOL=TRXUSDT UI=1 docker-compose up -d
1426
- ```
1427
-
1428
- | Variable | Required | Default | Description |
1429
- |----------|----------|---------|-------------|
1430
- | `MODE` | yes | — | `backtest` \| `live` \| `paper` \| `walker` |
1431
- | `STRATEGY_FILE` | yes | — | Path to strategy entry point (relative to `working_dir`) |
1432
- | `SYMBOL` | no | `BTCUSDT` | Trading pair |
1433
- | `STRATEGY` | no | first registered | Strategy name |
1434
- | `EXCHANGE` | no | first registered | Exchange name |
1435
- | `FRAME` | no | first registered | Frame name (backtest only) |
1436
- | `UI` | no | — | Any non-empty value enables `--ui` |
1437
- | `TELEGRAM` | no | — | Any non-empty value enables `--telegram` |
1438
- | `VERBOSE` | no | — | Any non-empty value enables `--verbose` |
1439
- | `NO_CACHE` | no | — | Any non-empty value enables `--noCache` |
1440
- | `NO_FLUSH` | no | — | Any non-empty value enables `--noFlush` |
1441
-
1442
- ## 🌍 Environment Variables
1443
-
1444
- Create a `.env` file in your project root:
1445
-
1446
- ```env
1447
- # Telegram notifications (required for --telegram)
1448
- CC_TELEGRAM_TOKEN=your_bot_token_here
1449
- CC_TELEGRAM_CHANNEL=-100123456789
1450
-
1451
- # Web UI server (optional, defaults shown)
1452
- CC_WWWROOT_HOST=0.0.0.0
1453
- CC_WWWROOT_PORT=60050
1454
-
1455
- # Custom QuickChart service URL (optional)
1456
- CC_QUICKCHART_HOST=
1457
- ```
1458
-
1459
- | Variable | Default | Description |
1460
- |------------------------|-------------|---------------------------------------|
1461
- | `CC_TELEGRAM_TOKEN` | — | Telegram bot token (from @BotFather) |
1462
- | `CC_TELEGRAM_CHANNEL` | — | Telegram channel or chat ID |
1463
- | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1464
- | `CC_WWWROOT_PORT` | `60050` | UI server port |
1465
- | `CC_QUICKCHART_HOST` | — | Self-hosted QuickChart instance URL |
1466
-
1467
- ## ⚙️ Default Behaviors
1468
-
1469
- 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:
1470
-
1471
- | Component | Default | Warning |
1472
- |--------------|--------------------------------|---------------------------------------------------------------------------|
1473
- | **Exchange** | CCXT Binance (`default_exchange`) | `Warning: The default exchange schema is set to CCXT Binance...` |
1474
- | **Frame** | February 2024 (`default_frame`) | `Warning: The default frame schema is set to February 2024...` |
1475
- | **Symbol** | `BTCUSDT` | — |
1476
- | **Cache intervals** | `1m, 15m, 30m, 4h` | Used if `--cacheInterval` not provided; skip entirely with `--noCache` |
1477
-
1478
- > **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.
1479
-
1480
- ## 🔧 Programmatic API
1481
-
1482
- 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.
1483
-
1484
- ### `run(mode, args)`
1485
-
1486
- ```typescript
1487
- import { run } from '@backtest-kit/cli';
1488
-
1489
- await run(mode, args);
1490
- ```
1491
-
1492
- | Parameter | Description |
1493
- |-----------|-------------|
1494
- | `mode` | `"backtest" \| "paper" \| "live"` — Execution mode |
1495
- | `args` | Mode-specific options (all optional — same defaults as CLI) |
1496
-
1497
- `run()` can be called **only once per process**. A second call throws `"Should be called only once"`.
1498
-
1499
- ### Payload fields
1500
-
1501
- **Backtest** (`mode: "backtest"`):
1502
-
1503
- | Field | Type | Description |
1504
- |-------|------|-------------|
1505
- | `entryPoint` | `string` | Path to strategy entry point file |
1506
- | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1507
- | `strategy` | `string` | Strategy name (default: first registered) |
1508
- | `exchange` | `string` | Exchange name (default: first registered) |
1509
- | `frame` | `string` | Frame name (default: first registered) |
1510
- | `cacheInterval` | `CandleInterval[]` | Intervals to pre-cache (default: `["1m","15m","30m","1h","4h"]`) |
1511
- | `noCache` | `boolean` | Skip candle cache warming (default: `false`) |
1512
- | `noFlush` | `boolean` | Skip removing report/log/markdown/agent folders before the run (default: `false`) |
1513
- | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1514
-
1515
- **Paper** and **Live** (`mode: "paper"` / `mode: "live"`):
1516
-
1517
- | Field | Type | Description |
1518
- |-------|------|-------------|
1519
- | `entryPoint` | `string` | Path to strategy entry point file |
1520
- | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1521
- | `strategy` | `string` | Strategy name (default: first registered) |
1522
- | `exchange` | `string` | Exchange name (default: first registered) |
1523
- | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1524
-
1525
- ### Examples
1526
-
1527
- **Backtest:**
1528
-
1529
- ```typescript
1530
- import { run } from '@backtest-kit/cli';
1531
-
1532
- await run('backtest', {
1533
- entryPoint: './src/index.mjs',
1534
- symbol: 'ETHUSDT',
1535
- frame: 'feb-2024',
1536
- cacheInterval: ['1m', '15m', '1h'],
1537
- verbose: true,
1538
- });
1539
- ```
1540
-
1541
- **Paper trading:**
1542
-
1543
- ```typescript
1544
- import { run } from '@backtest-kit/cli';
1545
-
1546
- await run('paper', {
1547
- entryPoint: './src/index.mjs',
1548
- symbol: 'BTCUSDT',
1549
- });
1550
- ```
1551
-
1552
- **Live trading:**
1553
-
1554
- ```typescript
1555
- import { run } from '@backtest-kit/cli';
1556
-
1557
- await run('live', {
1558
- entryPoint: './src/index.mjs',
1559
- symbol: 'BTCUSDT',
1560
- verbose: true,
1561
- });
1562
- ```
1563
-
1564
- ## 💡 Why Use @backtest-kit/cli?
1565
-
1566
- Instead of writing infrastructure code for every project:
1567
-
1568
- **❌ Without @backtest-kit/cli (manual setup)**
1569
-
1570
- ```typescript
1571
- // index.ts
1572
- import { setLogger, setConfig, Storage, Notification, Report, Markdown } from 'backtest-kit';
1573
- import { serve } from '@backtest-kit/ui';
1574
-
1575
- setLogger({ log: console.log, ... });
1576
- Storage.enable();
1577
- Notification.enable();
1578
- Report.enable();
1579
- Markdown.disable();
1580
-
1581
- // ... parse CLI args manually
1582
- // ... register exchange schema
1583
- // ... warm candle cache
1584
- // ... set up Telegram bot
1585
- // ... handle SIGINT gracefully
1586
- // ... load and run backtest
1587
- ```
1588
-
1589
- **✅ With @backtest-kit/cli (one script)**
1590
-
1591
- ```json
1592
- { "scripts": { "backtest": "npx @backtest-kit/cli --backtest --ui --telegram ./src/index.mjs" } }
1593
- ```
1594
-
1595
- ```bash
1596
- npm run backtest
1597
- ```
1598
-
1599
- **Benefits:**
1600
-
1601
- - 🚀 From zero to running backtest in seconds
1602
- - 💾 Automatic candle cache warming with retry logic
1603
- - 🌐 Production-ready web dashboard out of the box
1604
- - 📬 Telegram notifications with price charts — no chart code needed
1605
- - 🛑 Graceful shutdown on SIGINT — no hanging processes
1606
- - 🔌 Works with any `backtest-kit` strategy file as-is
1607
- - 🧩 Broker adapter hooks via side-effect module files — no CLI internals to touch
1608
-
1609
- ## 🤝 Contribute
1610
-
1611
- Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
1612
-
1613
- ## 📜 License
1614
-
1615
- 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
+ ## ✨ 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
+ ## 🗂️ Monorepo Usage
281
+
282
+ `@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.
283
+
284
+ ### How It Works
285
+
286
+ Internally, `ResolveService` does the following before executing your entry point:
287
+
288
+ ```
289
+ process.chdir(path.dirname(entryPoint)) // cwd → strategy directory
290
+ dotenv.config({ path: rootDir + '/.env' }) // load root .env first
291
+ dotenv.config({ path: strategyDir + '/.env', override: true }) // strategy .env overrides
292
+ ```
293
+
294
+ Everything that follows — candle cache warming, report generation, module loading, template resolution — uses the new cwd automatically.
295
+
296
+ ### Project Structure
297
+
298
+ ```
299
+ monorepo/
300
+ ├── package.json # root scripts (one per strategy)
301
+ ├── .env # shared API keys (exchange, Telegram, etc.)
302
+ └── strategies/
303
+ ├── oct_2025/
304
+ │ ├── index.mjs # entry point — registers exchange/frame/strategy schemas
305
+ │ ├── .env # overrides root .env for this strategy
306
+ │ ├── modules (optional)
307
+ │ | ├── live.module.mjs # broker adapter for --live mode (optional)
308
+ │ | ├── paper.module.mjs # broker adapter for --paper mode (optional)
309
+ │ | ├── backtest.module.mjs # broker adapter for --backtest mode (optional)
310
+ │ ├── template/ # custom Mustache templates (optional)
311
+ │ └── dump/ # auto-created: candle cache + backtest reports
312
+ └── dec_2025/
313
+ ├── index.mjs
314
+ ├── .env
315
+ └── dump/
316
+ ```
317
+
318
+ ### Root `package.json`
319
+
320
+ ```json
321
+ {
322
+ "scripts": {
323
+ "backtest:oct": "npx @backtest-kit/cli --backtest ./strategies/oct_2025/index.mjs",
324
+ "backtest:dec": "npx @backtest-kit/cli --backtest ./strategies/dec_2025/index.mjs"
325
+ },
326
+ "dependencies": {
327
+ "@backtest-kit/cli": "latest",
328
+ "backtest-kit": "latest",
329
+ "ccxt": "latest"
330
+ }
331
+ }
332
+ ```
333
+
334
+ ```bash
335
+ npm run backtest:oct
336
+ npm run backtest:dec
337
+ ```
338
+
339
+ ### Isolated Resources Per Strategy
340
+
341
+ | Resource | Path (relative to strategy dir) | Isolated |
342
+ |--------------------------|-----------------------------------|------------------|
343
+ | Candle cache | `./dump/data/candle/` | ✅ per-strategy |
344
+ | Backtest reports | `./dump/` | ✅ per-strategy |
345
+ | Broker module (live) | `./modules/live.module.mjs` | ✅ per-strategy |
346
+ | Broker module (paper) | `./modules/paper.module.mjs` | ✅ per-strategy |
347
+ | Broker module (backtest) | `./modules/backtest.module.mjs` | ✅ per-strategy |
348
+ | Config module (walker) | `./modules/walker.module.mjs` | ✅ loaded once |
349
+ | Telegram templates | `./template/*.mustache` | ✅ per-strategy |
350
+ | Environment variables | `./.env` (overrides root) | ✅ per-strategy |
351
+
352
+ 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.
353
+
354
+ ## 🔗 Shared Import Aliases
355
+
356
+ `@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.
357
+
358
+ ### How It Works
359
+
360
+ 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:
361
+
362
+ | Import | Resolves to |
363
+ |--------|-------------|
364
+ | `import { fn } from "utils"` | `<cwd>/utils/index.ts` (or `.js`, `.mjs`, `.cjs`) |
365
+ | `import { calcRSI } from "math/rsi"` | `<cwd>/math/rsi.ts` |
366
+ | `import { research } from "logic"` | `<cwd>/logic/index.ts` |
367
+ | `import { ResearchResponseContract } from "logic/contract/ResearchResponse.contract"` | `<cwd>/logic/contract/ResearchResponse.contract.ts` |
368
+
369
+ ### Project Structure
370
+
371
+ ```
372
+ my-project/
373
+ ├── utils/ ← import { formatDate } from "utils"
374
+ │ └── index.ts
375
+ ├── math/ ← import { calcRSI } from "math/rsi"
376
+ │ └── rsi.ts
377
+ ├── logic/ ← import { research } from "logic"
378
+ │ ├── index.ts ← barrel
379
+ │ └── contract/
380
+ │ └── ResearchResponse.contract.ts ← import { ... } from "logic/contract/ResearchResponse.contract"
381
+ └── content/
382
+ ├── feb_2026.strategy.ts ← uses all three aliases freely
383
+ └── mar_2026.strategy.ts ← same aliases, no duplication
384
+ ```
385
+
386
+ 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.
387
+
388
+ ### TypeScript Support
389
+
390
+ Add a matching `paths` entry to your `tsconfig.json` so the editor resolves the aliases:
391
+
392
+ ```json
393
+ {
394
+ "compilerOptions": {
395
+ "moduleResolution": "bundler",
396
+ "paths": {
397
+ "logic": ["./logic/index.ts"],
398
+ "logic/*": ["./logic/*"],
399
+ "math": ["./math/index.ts"],
400
+ "math/*": ["./math/*"],
401
+ "utils": ["./utils/index.ts"],
402
+ "utils/*": ["./utils/*"]
403
+ }
404
+ },
405
+ "include": [
406
+ "./logic",
407
+ "./math",
408
+ "./utils",
409
+ "./content",
410
+ "./modules",
411
+ ],
412
+ }
413
+ ```
414
+
415
+ ## 🔔 Integrations
416
+
417
+ ### Web Dashboard (`--ui`)
418
+
419
+ Starts `@backtest-kit/ui` server. Access the interactive dashboard at:
420
+
421
+ ```
422
+ http://localhost:60050
423
+ ```
424
+
425
+ Customize host/port via environment variables `CC_WWWROOT_HOST` and `CC_WWWROOT_PORT`.
426
+
427
+ #### Symbol List (`symbol.config`)
428
+
429
+ 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).
430
+
431
+ **Resolution order — first match wins:**
432
+
433
+ | Priority | Path | Notes |
434
+ |----------|------|-------|
435
+ | 1 | `{strategyDir}/config/symbol.config` | per-strategy override (cwd after `chdir`) |
436
+ | 2 | `{projectRoot}/config/symbol.config` | project-root override (cwd where `npx` was invoked) |
437
+ | 3 | `@backtest-kit/cli/config/symbol.config` | built-in default shipped with the package |
438
+
439
+ Supported file formats (`.ts`, `.cjs`, `.mjs`, `.js` tried automatically):
440
+
441
+ ```ts
442
+ // config/symbol.config.ts
443
+ export const symbol_list = [
444
+ {
445
+ icon: "/icon/btc.png",
446
+ logo: "/icon/128/btc.png",
447
+ symbol: "BTCUSDT",
448
+ displayName: "Bitcoin",
449
+ color: "#F7931A",
450
+ priority: 50,
451
+ description: "Bitcoin - the first and most popular cryptocurrency",
452
+ },
453
+ {
454
+ icon: "/icon/eth.png",
455
+ logo: "/icon/128/eth.png",
456
+ symbol: "ETHUSDT",
457
+ displayName: "Ethereum",
458
+ color: "#6F42C1",
459
+ priority: 50,
460
+ description: "Ethereum - a blockchain platform for smart contracts",
461
+ },
462
+ ];
463
+ ```
464
+
465
+ #### Notification Filter (`notification.config`)
466
+
467
+ Controls which notification categories are shown in the UI dashboard. Create a `config/notification.config` file in your strategy directory to override the defaults.
468
+
469
+ **Resolution order — first match wins:**
470
+
471
+ | Priority | Path | Notes |
472
+ |----------|------|-------|
473
+ | 1 | `{strategyDir}/config/notification.config` | per-strategy override (cwd after `chdir`) |
474
+ | 2 | `{projectRoot}/config/notification.config` | project-root override (cwd where `npx` was invoked) |
475
+ | 3 | `@backtest-kit/cli/config/notification.config` | built-in default shipped with the package |
476
+
477
+ **Default values (built-in):**
478
+
479
+ | Key | Default | Description |
480
+ |-----|---------|-------------|
481
+ | `signal` | `true` | Signal lifecycle: opened, scheduled, closed, cancelled |
482
+ | `risk` | `true` | Risk manager rejection notifications |
483
+ | `info` | `true` | Informational messages attached to an active signal |
484
+ | `breakeven` | `true` | Breakeven level reached |
485
+ | `common_error` | `true` | Non-fatal runtime errors |
486
+ | `critical_error` | `true` | Fatal errors that terminate the session |
487
+ | `validation_error` | `true` | Strategy config / input validation errors |
488
+ | `strategy_commit` | `true` | All committed actions (partial close, DCA, trailing, etc.) |
489
+ | `partial_loss` | `false` | Partial loss level reached (before commit) |
490
+ | `partial_profit` | `false` | Partial profit level reached (before commit) |
491
+ | `signal_sync` | `false` | Live order fill / exit confirmations from exchange sync |
492
+
493
+ ```js
494
+ // config/notification.config.ts
495
+ export default {
496
+ signal: true,
497
+ risk: true,
498
+ info: true,
499
+ breakeven: true,
500
+ common_error: true,
501
+ critical_error: true,
502
+ validation_error: true,
503
+ strategy_commit: true,
504
+ partial_loss: false,
505
+ partial_profit: false,
506
+ signal_sync: false,
507
+ };
508
+ ```
509
+
510
+ ### Telegram Notifications (`--telegram`)
511
+
512
+ 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.
513
+
514
+ Requires `CC_TELEGRAM_TOKEN` and `CC_TELEGRAM_CHANNEL` in your environment.
515
+
516
+ #### Telegram Message Adapter (`telegram.config`)
517
+
518
+ 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.
519
+
520
+ Resolution order is the same as other configs (strategy dir → project root → package default).
521
+
522
+ ```ts
523
+ // config/telegram.config.ts
524
+ import {
525
+ IStrategyTickResultOpened,
526
+ IStrategyTickResultClosed,
527
+ RiskContract,
528
+ } from "backtest-kit";
529
+
530
+ export default {
531
+ async getOpenedMarkdown(event: IStrategyTickResultOpened): Promise<string> {
532
+ return `**Opened** ${event.symbol} at ${event.priceOpen}`;
533
+ },
534
+ async getClosedMarkdown(event: IStrategyTickResultClosed): Promise<string> {
535
+ return `**Closed** ${event.symbol} at ${event.priceClosed}`;
536
+ },
537
+ async getRiskMarkdown(event: RiskContract): Promise<string> {
538
+ return `**Risk rejected** ${event.symbol}`;
539
+ },
540
+ };
541
+ ```
542
+
543
+ All methods are optional — unimplemented ones fall back to the Mustache template.
544
+
545
+ | Method | Event type |
546
+ |--------|------------|
547
+ | `getOpenedMarkdown` | `IStrategyTickResultOpened` |
548
+ | `getClosedMarkdown` | `IStrategyTickResultClosed` |
549
+ | `getScheduledMarkdown` | `IStrategyTickResultScheduled` |
550
+ | `getCancelledMarkdown` | `IStrategyTickResultCancelled` |
551
+ | `getRiskMarkdown` | `RiskContract` |
552
+ | `getPartialProfitMarkdown` | `PartialProfitCommit` |
553
+ | `getPartialLossMarkdown` | `PartialLossCommit` |
554
+ | `getBreakevenMarkdown` | `BreakevenCommit` |
555
+ | `getTrailingTakeMarkdown` | `TrailingTakeCommit` |
556
+ | `getTrailingStopMarkdown` | `TrailingStopCommit` |
557
+ | `getAverageBuyMarkdown` | `AverageBuyCommit` |
558
+ | `getSignalOpenMarkdown` | `SignalOpenContract` |
559
+ | `getSignalCloseMarkdown` | `SignalCloseContract` |
560
+ | `getCancelScheduledMarkdown` | `CancelScheduledCommit` |
561
+ | `getClosePendingMarkdown` | `ClosePendingCommit` |
562
+ | `getSignalInfoMarkdown` | `SignalInfoContract` |
563
+
564
+ ## 🧩 Module Hooks (Broker Adapter)
565
+
566
+ 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.
567
+
568
+ | Command Line Args | Module file | Loaded before |
569
+ |-------------------|---------------------------------|-----------------------------|
570
+ | `--live` | `./modules/live.module.mjs` | `Live.background()` |
571
+ | `--paper` | `./modules/paper.module.mjs` | `Live.background()` (paper) |
572
+ | `--backtest` | `./modules/backtest.module.mjs` | `Backtest.background()` |
573
+ | `--walker` | `./modules/walker.module.mjs` | `Walker.background()` |
574
+ | `--brokerdebug` | `./modules/brokerdebug.module.mjs` | broker commit test |
575
+
576
+ > 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.
577
+
578
+ ### How It Works
579
+
580
+ 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.
581
+
582
+ ```javascript
583
+ // live.module.mjs
584
+ import { Broker } from 'backtest-kit';
585
+ import { myExchange } from './exchange.mjs';
586
+
587
+ class MyBroker {
588
+ async onSignalOpenCommit({ symbol, priceOpen, direction }) {
589
+ await myExchange.openPosition(symbol, direction, priceOpen);
590
+ }
591
+
592
+ async onSignalCloseCommit({ symbol, priceClosed }) {
593
+ await myExchange.closePosition(symbol, priceClosed);
594
+ }
595
+
596
+ async onPartialProfitCommit({ symbol, cost, currentPrice }) {
597
+ await myExchange.createOrder({
598
+ symbol,
599
+ side: 'sell',
600
+ quantity: cost / currentPrice,
601
+ });
602
+ }
603
+
604
+ async onAverageBuyCommit({ symbol, cost, currentPrice }) {
605
+ await myExchange.createOrder({
606
+ symbol,
607
+ side: 'buy',
608
+ quantity: cost / currentPrice,
609
+ });
610
+ }
611
+ }
612
+
613
+ Broker.useBrokerAdapter(MyBroker);
614
+
615
+ Broker.enable();
616
+ ```
617
+
618
+ ### Available Broker Hooks
619
+
620
+ | Method | Payload type | Triggered on |
621
+ |--------------------------|------------------------------|---------------------------|
622
+ | `onSignalOpenCommit` | `BrokerSignalOpenPayload` | Position activation |
623
+ | `onSignalCloseCommit` | `BrokerSignalClosePayload` | SL / TP / manual close |
624
+ | `onPartialProfitCommit` | `BrokerPartialProfitPayload` | PP |
625
+ | `onPartialLossCommit` | `BrokerPartialLossPayload` | PL |
626
+ | `onTrailingStopCommit` | `BrokerTrailingStopPayload` | SL adjustment |
627
+ | `onTrailingTakeCommit` | `BrokerTrailingTakePayload` | TP adjustment |
628
+ | `onBreakevenCommit` | `BrokerBreakevenPayload` | SL moved to entry |
629
+ | `onAverageBuyCommit` | `BrokerAverageBuyPayload` | DCA entry |
630
+
631
+ All methods are optional. Unimplemented hooks are silently skipped. In backtest mode all broker calls are skipped automatically — no adapter code runs during backtests.
632
+
633
+ ### TypeScript
634
+
635
+ ```typescript
636
+ import { Broker, IBroker, BrokerSignalOpenPayload, BrokerSignalClosePayload } from 'backtest-kit';
637
+
638
+ class MyBroker implements Partial<IBroker> {
639
+ async onSignalOpenCommit(payload: BrokerSignalOpenPayload) {
640
+ // place open order on exchange
641
+ }
642
+
643
+ async onSignalCloseCommit(payload: BrokerSignalClosePayload) {
644
+ // place close order on exchange
645
+ }
646
+ }
647
+
648
+ Broker.useBrokerAdapter(MyBroker);
649
+
650
+ Broker.enable();
651
+ ```
652
+
653
+ ## 🔀 Import Aliases (`alias.module`)
654
+
655
+ `@backtest-kit/cli` lets you override any nodejs module import — without touching the strategy code. Drop a `config/alias.module` file in your project root and export a mapping from module name to replacement module.
656
+
657
+ The alias table is loaded once (on the first `import` call) from `{projectRoot}/config/alias.module` and applied globally to every subsequent module load via `require`/ `import`.
658
+
659
+ **Use cases:**
660
+
661
+ - Replace a heavy dependency with a lighter stub for backtesting
662
+ - Swap any external api for a mock during CI runs
663
+
664
+ ```ts
665
+ // config/alias.module.ts — named export
666
+ export const ccxt = require("./stubs/ccxt.stub.cjs");
667
+ ```
668
+
669
+ ```js
670
+ // config/alias.module.cjs — default export
671
+ module.exports = {
672
+ ccxt: require("./stubs/ccxt.stub.cjs"),
673
+ };
674
+ ```
675
+
676
+ ```js
677
+ // config/alias.module.mjs — default export
678
+ import ccxtStub from "./stubs/ccxt.stub.mjs";
679
+
680
+ export default {
681
+ ccxt: ccxtStub,
682
+ };
683
+ ```
684
+
685
+ **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.
686
+
687
+ **Important:** It is **not** per-strategy — it applies to all modules loaded in the current process.
688
+
689
+ ## 📦 Supported Entry Point Formats
690
+
691
+ `@backtest-kit/cli` automatically detects the format of your strategy file and loads it with the appropriate runtime — no flags or configuration required.
692
+
693
+ | Format | Extension | Runtime | Use Case |
694
+ |--------|-----------|---------|----------|
695
+ | **TypeScript** | `.ts` | [`tsx`](https://tsx.is/) via `tsImport()` | TypeScript strategies with cross-imports (ESM ↔ CJS) |
696
+ | **ES Module** | `.mjs` | Native `import()` | Modern JavaScript with top-level `await` and ESM syntax |
697
+ | **CommonJS** | `.cjs` | Native `require()` | Legacy or dual-package strategies |
698
+
699
+ ### TypeScript (`.ts`)
700
+
701
+ Run TypeScript strategy files directly — no `tsc` compilation step needed. Powered by `tsx`, which handles cross-format imports transparently:
702
+
703
+ ```json
704
+ {
705
+ "scripts": {
706
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.ts"
707
+ },
708
+ "dependencies": {
709
+ "@backtest-kit/cli": "latest",
710
+ "backtest-kit": "latest",
711
+ "tsx": "latest"
712
+ }
713
+ }
714
+ ```
715
+
716
+ ### ES Module (`.mjs`)
717
+
718
+ Standard ESM format. Supports top-level `await`, named exports, and `import` syntax:
719
+
720
+ ```json
721
+ {
722
+ "scripts": {
723
+ "backtest": "npx @backtest-kit/cli --backtest ./src/index.mjs"
724
+ }
725
+ }
726
+ ```
727
+
728
+ ### CommonJS (`.cjs`)
729
+
730
+ For projects that compile to or use CommonJS. Loaded via `require()`:
731
+
732
+ ```json
733
+ {
734
+ "scripts": {
735
+ "backtest": "npx @backtest-kit/cli --backtest ./dist/index.cjs"
736
+ }
737
+ }
738
+ ```
739
+
740
+ ## 🌲 Running Local PineScript Indicators
741
+
742
+ `@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.
743
+
744
+ ### CLI Flags
745
+
746
+ | Flag | Type | Description |
747
+ |------|------|-------------|
748
+ | `--pine` | boolean | Enable PineScript execution mode |
749
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
750
+ | `--timeframe` | string | Candle interval (default: `"15m"`) |
751
+ | `--limit` | string | Number of candles to fetch (default: `250`) |
752
+ | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
753
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
754
+ | `--output` | string | Output file base name without extension (default: `.pine` file name) |
755
+ | `--json` | boolean | Write plots as a JSON array to `<pine-dir>/dump/{output}.json` |
756
+ | `--jsonl` | boolean | Write plots as JSONL (one row per line) to `<pine-dir>/dump/{output}.jsonl` |
757
+ | `--markdown` | boolean | Write Markdown table to `<pine-dir>/dump/{output}.md` |
758
+
759
+ **Important:** `limit` must cover indicator warmup bars — rows before warmup completes will show `N/A`
760
+
761
+ **Positional argument:** path to the `.pine` file.
762
+
763
+ ### Exchange via `pine.module`
764
+
765
+ 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.
766
+
767
+ The CLI looks for `modules/pine.module` in two locations (first match wins):
768
+
769
+ 1. **Next to the `.pine` file** — `<pine-file-dir>/modules/pine.module.ts`
770
+ 2. **Project root** — `<cwd>/modules/pine.module.ts`
771
+
772
+ ```
773
+ my-project/
774
+ ├── math/
775
+ │ ├── impulse_trend_15m.pine ← indicator
776
+ │ └── modules/
777
+ │ └── pine.module.ts ← loaded first (next to .pine file)
778
+ ├── modules/
779
+ │ └── pine.module.ts ← fallback (project root)
780
+ └── package.json
781
+ ```
782
+
783
+ Inside `pine.module.ts` call `addExchangeSchema` from `backtest-kit` and give the exchange a name:
784
+
785
+ ```typescript
786
+ // modules/pine.module.ts
787
+ import { addExchangeSchema } from "backtest-kit";
788
+ import ccxt from "ccxt";
789
+
790
+ addExchangeSchema({
791
+ exchangeName: "my-exchange",
792
+ getCandles: async (symbol, interval, since, limit) => {
793
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
794
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
795
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
796
+ timestamp, open, high, low, close, volume,
797
+ }));
798
+ },
799
+ formatPrice: (symbol, price) => price.toFixed(2),
800
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
801
+ });
802
+ ```
803
+
804
+ ### Environment variables (`.env`)
805
+
806
+ 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):
807
+
808
+ ```
809
+ my-project/
810
+ ├── math/
811
+ │ ├── .env ← loaded second (overrides root)
812
+ │ └── impulse_trend_15m.pine
813
+ ├── .env ← loaded first
814
+ └── package.json
815
+ ```
816
+
817
+ Use this to store API keys without hardcoding them:
818
+
819
+ ```env
820
+ # .env
821
+ BYBIT_API_KEY=xxx
822
+ BYBIT_API_SECRET=yyy
823
+ ```
824
+
825
+ ```typescript
826
+ // modules/pine.module.ts
827
+ addExchangeSchema({
828
+ exchangeName: "my-exchange",
829
+ getCandles: async (symbol, interval, since, limit) => {
830
+ const exchange = new ccxt.bybit({
831
+ apiKey: process.env.BYBIT_API_KEY,
832
+ secret: process.env.BYBIT_API_SECRET,
833
+ enableRateLimit: true,
834
+ });
835
+ // ...
836
+ },
837
+ });
838
+ ```
839
+
840
+ Then run:
841
+
842
+ ```bash
843
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine \
844
+ --exchange my-exchange \
845
+ --symbol BTCUSDT \
846
+ --timeframe 15m \
847
+ --limit 180 \
848
+ --when "2025-09-24T12:00:00.000Z"
849
+ ```
850
+
851
+ Or add it to `package.json`:
852
+
853
+ ```json
854
+ {
855
+ "scripts": {
856
+ "pine": "npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --symbol BTCUSDT --timeframe 15m --limit 180"
857
+ }
858
+ }
859
+ ```
860
+
861
+ ```bash
862
+ npm run pine
863
+ ```
864
+
865
+ ### PineScript Requirements
866
+
867
+ 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:
868
+
869
+ ```pine
870
+ //@version=5
871
+ indicator("MyIndicator", overlay=true)
872
+
873
+ // ... computation ...
874
+
875
+ plot(close, "Close", display=display.data_window)
876
+ plot(position, "Position", display=display.data_window)
877
+ ```
878
+
879
+ The column names in the output Markdown table are taken directly from those plot names — no manual schema definition needed.
880
+
881
+ ### Output
882
+
883
+ The CLI prints a Markdown table to stdout:
884
+
885
+ ```
886
+ # PineScript Technical Analysis Dump
887
+
888
+ **Signal ID**: CLI execution 2025-09-24T12:00:00.000Z
889
+
890
+ | Close | Position | timestamp |
891
+ | --- | --- | --- |
892
+ | 112871.28 | -1.0000 | 2025-09-22T15:00:00.000Z |
893
+ | 112666.69 | -1.0000 | 2025-09-22T15:15:00.000Z |
894
+ | 112736.00 | 0.0000 | 2025-09-22T18:30:00.000Z |
895
+ | 112653.90 | 1.0000 | 2025-09-22T22:15:00.000Z |
896
+ ```
897
+
898
+ Save to `./math/dump/impulse_trend_15m.md` (uses `.pine` file name automatically, dump is created next to the `.pine` file):
899
+
900
+ ```bash
901
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --markdown
902
+ ```
903
+
904
+ Override the output name with `--output`:
905
+
906
+ ```bash
907
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine --jsonl --output feb2026_bb
908
+ # → ./math/dump/feb2026_bb.jsonl
909
+ ```
910
+
911
+ Print to stdout (no flag):
912
+
913
+ ```bash
914
+ npx @backtest-kit/cli --pine ./math/impulse_trend_15m.pine
915
+ ```
916
+
917
+ ## 🎨 Visual Pine Script Editor
918
+
919
+ ![pine](https://raw.githubusercontent.com/tripolskypetr/backtest-kit/HEAD/assets/screenshots/screenshot32.png)
920
+
921
+ `@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**
922
+
923
+ ### Usage
924
+
925
+ ```bash
926
+ npx @backtest-kit/cli --editor
927
+ ```
928
+
929
+ The CLI will:
930
+
931
+ 1. Load `./modules/editor.module` (if it exists) — use it to register your exchange schema, identical to `pine.module`
932
+ 2. Start the `@backtest-kit/ui` server on `http://localhost:60050` (or `CC_WWWROOT_PORT`)
933
+ 3. Open `http://localhost:{CC_WWWROOT_PORT}?pine=1` automatically in your default browser
934
+
935
+ Press **Ctrl+C** to stop the server.
936
+
937
+ ### Exchange via `editor.module`
938
+
939
+ Drop a `modules/editor.module.ts` next to your project to register the exchange that the editor's candle provider will use:
940
+
941
+ ```typescript
942
+ // modules/editor.module.ts
943
+ import { addExchangeSchema } from "backtest-kit";
944
+ import ccxt from "ccxt";
945
+
946
+ addExchangeSchema({
947
+ exchangeName: "my-exchange",
948
+ getCandles: async (symbol, interval, since, limit) => {
949
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
950
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
951
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
952
+ timestamp, open, high, low, close, volume,
953
+ }));
954
+ },
955
+ formatPrice: (symbol, price) => price.toFixed(2),
956
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
957
+ });
958
+ ```
959
+
960
+ ### Environment Variables
961
+
962
+ | Variable | Default | Description |
963
+ |-------------------|-----------|----------------------------------|
964
+ | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
965
+ | `CC_WWWROOT_PORT` | `60050` | UI server port |
966
+
967
+ ### `package.json` script
968
+
969
+ ```json
970
+ {
971
+ "scripts": {
972
+ "editor": "npx @backtest-kit/cli --editor"
973
+ }
974
+ }
975
+ ```
976
+
977
+ ```bash
978
+ npm run editor
979
+ ```
980
+
981
+ ## 💾 Dumping Raw Candles
982
+
983
+ `@backtest-kit/cli` can fetch raw OHLCV candles from any registered exchange and save them to a file — no strategy file required.
984
+
985
+ ### CLI Flags
986
+
987
+ | Flag | Type | Description |
988
+ |------|------|-------------|
989
+ | `--dump` | boolean | Enable candle dump mode |
990
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
991
+ | `--timeframe` | string | Candle interval (default: `"15m"`) |
992
+ | `--limit` | string | Number of candles to fetch (default: `250`) |
993
+ | `--when` | string | End date for candle window — ISO 8601 or Unix ms (default: now) |
994
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
995
+ | `--output` | string | Output file base name without extension (default: `{SYMBOL}_{LIMIT}_{TIMEFRAME}_{TIMESTAMP}`) |
996
+ | `--json` | boolean | Write candles as a JSON array to `./dump/{output}.json` |
997
+ | `--jsonl` | boolean | Write candles as JSONL (one row per line) to `./dump/{output}.jsonl` |
998
+
999
+ The `dump/` directory is created in the current working directory (where the CLI is invoked from).
1000
+
1001
+ ### Exchange via `dump.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/dump.module.ts` file. The CLI loads it automatically before fetching candles.
1004
+
1005
+ The CLI looks for `modules/dump.module` in the current working directory
1006
+
1007
+ ```
1008
+ my-project/
1009
+ ├── modules/
1010
+ │ └── dump.module.ts ← exchange registration
1011
+ ├── dump/ ← auto-created: candle output files
1012
+ └── package.json
1013
+ ```
1014
+
1015
+ Inside `dump.module.ts` call `addExchangeSchema` from `backtest-kit`:
1016
+
1017
+ ```typescript
1018
+ // modules/dump.module.ts
1019
+ import { addExchangeSchema } from "backtest-kit";
1020
+ import ccxt from "ccxt";
1021
+
1022
+ addExchangeSchema({
1023
+ exchangeName: "my-exchange",
1024
+ getCandles: async (symbol, interval, since, limit) => {
1025
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1026
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1027
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1028
+ timestamp, open, high, low, close, volume,
1029
+ }));
1030
+ },
1031
+ formatPrice: (symbol, price) => price.toFixed(2),
1032
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1033
+ });
1034
+ ```
1035
+
1036
+ ### Output
1037
+
1038
+ Each candle row contains OHLCV fields. Print to stdout:
1039
+
1040
+ ```bash
1041
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100
1042
+ ```
1043
+
1044
+ Save to `./dump/BTCUSDT_100_15m_{timestamp}.jsonl`:
1045
+
1046
+ ```bash
1047
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 100 --jsonl
1048
+ ```
1049
+
1050
+ Fetch candles up to a specific date with `--when` and override the file name with `--output`:
1051
+
1052
+ ```bash
1053
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 \
1054
+ --when "2026-02-28T00:00:00.000Z" \
1055
+ --jsonl --output feb2026_btc
1056
+ # → ./dump/feb2026_btc.jsonl
1057
+ ```
1058
+
1059
+ Or add it to `package.json`:
1060
+
1061
+ ```json
1062
+ {
1063
+ "scripts": {
1064
+ "dump": "npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl"
1065
+ }
1066
+ }
1067
+ ```
1068
+
1069
+ ```bash
1070
+ npx @backtest-kit/cli --dump --symbol BTCUSDT --timeframe 15m --limit 500 --jsonl
1071
+ ```
1072
+
1073
+ ## 🐞 PnL Debug (`--pnldebug`)
1074
+
1075
+ `@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.
1076
+
1077
+ ### CLI Flags
1078
+
1079
+ | Flag | Type | Description |
1080
+ |------|------|-------------|
1081
+ | `--pnldebug` | boolean | Enable PnL debug mode |
1082
+ | `--priceopen` | number | Entry price (required) |
1083
+ | `--direction` | string | `long` or `short` (default: `long`) |
1084
+ | `--when` | string | Start timestamp — ISO 8601 or Unix ms (default: now) |
1085
+ | `--minutes` | string | Number of 1m candles to simulate (default: `60`) |
1086
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1087
+ | `--exchange` | string | Exchange name (default: first registered, falls back to CCXT Binance) |
1088
+ | `--output` | string | Output file base name (default: `{SYMBOL}_{DIRECTION}_{PRICEOPEN}_{TIMESTAMP}`) |
1089
+ | `--json` | boolean | Save results as JSON array to `./dump/<output>.json` |
1090
+ | `--jsonl` | boolean | Save results as JSONL to `./dump/<output>.jsonl` |
1091
+ | `--markdown` | boolean | Save results as Markdown table to `./dump/<output>.md` |
1092
+
1093
+ ### Output columns
1094
+
1095
+ | Column | Description |
1096
+ |--------|-------------|
1097
+ | `min` | Minute offset from start (1-based) |
1098
+ | `timestamp` | Candle timestamp (ISO 8601) |
1099
+ | `close` | Candle close price |
1100
+ | `pnl%` | Running PnL vs entry price (signed %) |
1101
+ | `peak%` | Highest PnL reached so far (always ≥ 0) |
1102
+ | `drawdown%` | Lowest PnL reached so far (always ≤ 0) |
1103
+
1104
+ ### Exchange via `pnldebug.module`
1105
+
1106
+ 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.
1107
+
1108
+ ```typescript
1109
+ // modules/pnldebug.module.ts
1110
+ import { addExchangeSchema } from "backtest-kit";
1111
+ import ccxt from "ccxt";
1112
+
1113
+ addExchangeSchema({
1114
+ exchangeName: "my-exchange",
1115
+ getCandles: async (symbol, interval, since, limit) => {
1116
+ const exchange = new ccxt.bybit({ enableRateLimit: true });
1117
+ const ohlcv = await exchange.fetchOHLCV(symbol, interval, since.getTime(), limit);
1118
+ return ohlcv.map(([timestamp, open, high, low, close, volume]) => ({
1119
+ timestamp, open, high, low, close, volume,
1120
+ }));
1121
+ },
1122
+ formatPrice: (symbol, price) => price.toFixed(2),
1123
+ formatQuantity: (symbol, quantity) => quantity.toFixed(8),
1124
+ });
1125
+ ```
1126
+
1127
+ ### Usage
1128
+
1129
+ Print to stdout (default table format):
1130
+
1131
+ ```bash
1132
+ npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120
1133
+ ```
1134
+
1135
+ Save as Markdown:
1136
+
1137
+ ```bash
1138
+ npx @backtest-kit/cli --pnldebug --priceopen 67956.73 --direction long --when 1772064000000 --minutes 60 --markdown
1139
+ # → ./dump/BTCUSDT_long_67956.73_{timestamp}.md
1140
+ ```
1141
+
1142
+ Override the output file name with `--output`:
1143
+
1144
+ ```bash
1145
+ npx @backtest-kit/cli --pnldebug --priceopen 64069.50 --direction short --when "2025-02-25" --minutes 120 \
1146
+ --jsonl --output feb25_short_debug
1147
+ # → ./dump/feb25_short_debug.jsonl
1148
+ ```
1149
+
1150
+ Or add it to `package.json`:
1151
+
1152
+ ```json
1153
+ {
1154
+ "scripts": {
1155
+ "pnldebug": "npx @backtest-kit/cli --pnldebug --symbol BTCUSDT --priceopen 64069.50 --direction short --when \"2025-02-25\" --minutes 120"
1156
+ }
1157
+ }
1158
+ ```
1159
+
1160
+ ```bash
1161
+ npm run pnldebug
1162
+ ```
1163
+
1164
+ ### Example stdout output
1165
+
1166
+ ```
1167
+ Symbol: BTCUSDT | Direction: short | PriceOpen: 64069.50 | From: 2025-02-25T00:00:00.000Z | Minutes: 120
1168
+
1169
+ min | timestamp | close | pnl% | peak% | drawdown%
1170
+ -----------------------------------------------------------------------------------
1171
+ 1 | 2025-02-25T00:01:00.000Z | 64020.10 | +0.08% | +0.08% | 0.00%
1172
+ 2 | 2025-02-25T00:02:00.000Z | 64105.30 | -0.06% | +0.08% | -0.06%
1173
+ ...
1174
+ 120 | 2025-02-25T02:00:00.000Z | 63200.00 | +1.36% | +1.36% | -0.06%
1175
+ ```
1176
+
1177
+ ## 🐛 Broker Debug (`--brokerdebug`)
1178
+
1179
+ `@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.
1180
+
1181
+ ### CLI Flags
1182
+
1183
+ | Flag | Type | Description |
1184
+ |------|------|-------------|
1185
+ | `--brokerdebug` | boolean | Enable broker debug mode |
1186
+ | `--commit` | string | Commit type to fire (default: `"signal-open"`) |
1187
+ | `--symbol` | string | Trading pair (default: `"BTCUSDT"`) |
1188
+ | `--exchange` | string | Exchange name (default: first registered) |
1189
+
1190
+ **Available `--commit` values:**
1191
+
1192
+ | Value | Broker hook |
1193
+ |-------|-------------|
1194
+ | `signal-open` | `onSignalOpenCommit` |
1195
+ | `signal-close` | `onSignalCloseCommit` |
1196
+ | `partial-profit` | `onPartialProfitCommit` |
1197
+ | `partial-loss` | `onPartialLossCommit` |
1198
+ | `average-buy` | `onAverageBuyCommit` |
1199
+ | `trailing-stop` | `onTrailingStopCommit` |
1200
+ | `trailing-take` | `onTrailingTakeCommit` |
1201
+ | `breakeven` | `onBreakevenCommit` |
1202
+
1203
+ ### How It Works
1204
+
1205
+ 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.
1206
+
1207
+ ### Broker via `brokerdebug.module`
1208
+
1209
+ Create a `modules/brokerdebug.module.ts` file and register your broker adapter:
1210
+
1211
+ ```typescript
1212
+ // modules/brokerdebug.module.ts
1213
+ import { Broker } from 'backtest-kit';
1214
+ import { myExchange } from './exchange.mjs';
1215
+
1216
+ class MyBroker {
1217
+ async onSignalOpenCommit({ symbol, priceOpen, position }) {
1218
+ await myExchange.openPosition(symbol, position, priceOpen);
1219
+ }
1220
+ // ... other hooks
1221
+ }
1222
+
1223
+ Broker.useBrokerAdapter(MyBroker);
1224
+ Broker.enable();
1225
+ ```
1226
+
1227
+ ### Usage
1228
+
1229
+ ```bash
1230
+ npx @backtest-kit/cli --brokerdebug --commit signal-open --symbol BTCUSDT
1231
+ npx @backtest-kit/cli --brokerdebug --commit partial-profit --symbol ETHUSDT --timeframe 1h
1232
+ ```
1233
+
1234
+ ## 🗑️ Flushing Strategy Output (`--flush`)
1235
+
1236
+ `@backtest-kit/cli` can delete generated output folders from one or more strategy dump directories without touching cached candle data.
1237
+
1238
+ ### CLI Flags
1239
+
1240
+ | Flag | Type | Description |
1241
+ |------|------|-------------|
1242
+ | `--flush` | boolean | Enable flush mode |
1243
+
1244
+ **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/`:
1245
+
1246
+ | Folder | Contents |
1247
+ |--------|----------|
1248
+ | `report` | Backtest report files (`.jsonl`) |
1249
+ | `log` | Run logs (`log.jsonl`) |
1250
+ | `markdown` | Exported Markdown reports |
1251
+ | `agent` | Agent outline files |
1252
+
1253
+ Candle cache (`dump/data/`) and AI forecast outlines (`dump/outline/`) are **not** removed.
1254
+
1255
+ ### Usage
1256
+
1257
+ Flush a single strategy:
1258
+
1259
+ ```bash
1260
+ npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts
1261
+ ```
1262
+
1263
+ Flush multiple strategies at once:
1264
+
1265
+ ```bash
1266
+ npx @backtest-kit/cli --flush \
1267
+ ./content/feb_2026.strategy/modules/backtest.module.ts \
1268
+ ./content/mar_2026.strategy/modules/backtest.module.ts
1269
+ ```
1270
+
1271
+ Or add it to `package.json`:
1272
+
1273
+ ```json
1274
+ {
1275
+ "scripts": {
1276
+ "flush": "npx @backtest-kit/cli --flush ./content/feb_2026.strategy/modules/backtest.module.ts"
1277
+ }
1278
+ }
1279
+ ```
1280
+
1281
+ ```bash
1282
+ npm run flush
1283
+ ```
1284
+
1285
+ ## 🗂️ Scaffolding a New Project (`--init`)
1286
+
1287
+ `@backtest-kit/cli` can bootstrap a ready-to-use project directory with a pre-configured layout, example strategy files, and all documentation fetched automatically.
1288
+
1289
+ ### CLI Flags
1290
+
1291
+ | Flag | Type | Description |
1292
+ |------|------|-------------|
1293
+ | `--init` | boolean | Scaffold a new project |
1294
+ | `--output` | string | Target directory name (default: `backtest-kit-project`) |
1295
+
1296
+ ### Usage
1297
+
1298
+ ```bash
1299
+ npx @backtest-kit/cli --init
1300
+ ```
1301
+
1302
+ Creates `./backtest-kit-project/` in the current working directory.
1303
+
1304
+ Override the directory name with `--output`:
1305
+
1306
+ ```bash
1307
+ npx @backtest-kit/cli --init --output my-trading-bot
1308
+ ```
1309
+
1310
+ Creates `./my-trading-bot/`.
1311
+
1312
+ The target directory must not exist or must be empty — the command aborts if it contains any files.
1313
+
1314
+ ### Generated Project Structure
1315
+
1316
+ ```
1317
+ backtest-kit-project/
1318
+ ├── package.json # pre-configured with all backtest-kit dependencies
1319
+ ├── .gitignore
1320
+ ├── CLAUDE.md # AI-agent guide for writing strategies
1321
+ ├── content/
1322
+ │ └── feb_2026.strategy.ts # example strategy entry point
1323
+ ├── docs/
1324
+ │ ├── lib/ # fetched automatically (see below)
1325
+ │ ├── backtest_actions.md
1326
+ │ ├── backtest_graph_pattern.md
1327
+ │ ├── backtest_logging_jsonl.md
1328
+ │ ├── backtest_pinets_usage.md
1329
+ │ ├── backtest_risk_async.md
1330
+ │ ├── backtest_strategy_structure.md
1331
+ │ ├── pine_debug.md
1332
+ │ └── pine_indicator_warmup.md
1333
+ ├── math/
1334
+ │ └── feb_2026.pine # example PineScript indicator
1335
+ ├── modules/
1336
+ │ ├── dump.module.ts # exchange schema for --dump mode
1337
+ │ └── pine.module.ts # exchange schema for --pine mode
1338
+ ├── report/
1339
+ │ └── feb_2026.md # example strategy research report
1340
+ └── scripts/
1341
+ └── fetch_docs.mjs # utility: downloads library READMEs into docs/lib/
1342
+ ```
1343
+
1344
+ ### Automatic Documentation Fetch
1345
+
1346
+ 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/`:
1347
+
1348
+ | File | Source |
1349
+ |------|--------|
1350
+ | `backtest-kit.md` | `backtest-kit` README |
1351
+ | `backtest-kit__graph.md` | `@backtest-kit/graph` README |
1352
+ | `backtest-kit__pinets.md` | `@backtest-kit/pinets` README |
1353
+ | `backtest-kit__cli.md` | `@backtest-kit/cli` README |
1354
+ | `garch.md` | `garch` README |
1355
+ | `volume-anomaly.md` | `volume-anomaly` README |
1356
+ | `agent-swarm-kit.md` | `agent-swarm-kit` README |
1357
+ | `functools-kit.md` | `functools-kit` README |
1358
+
1359
+ You can re-run this script at any time to refresh the docs:
1360
+
1361
+ ```bash
1362
+ cd backtest-kit-project
1363
+ node ./scripts/fetch_docs.mjs
1364
+ ```
1365
+
1366
+ Or via the pre-configured npm script:
1367
+
1368
+ ```bash
1369
+ npm run sync:lib
1370
+ ```
1371
+
1372
+ ## 🐳 Running in Docker (`--docker`)
1373
+
1374
+ CLI can create a ready-to-use Docker workspace: self-contained directory with `docker-compose.yaml` and a strategy entry point.
1375
+
1376
+ ### CLI Flags
1377
+
1378
+ | Flag | Type | Description |
1379
+ |------|------|-------------|
1380
+ | `--docker` | boolean | Scaffold a Docker workspace |
1381
+ | `--output` | string | Target directory name (default: `backtest-kit-docker`) |
1382
+
1383
+ ### Usage
1384
+
1385
+ ```bash
1386
+ npx @backtest-kit/cli --docker
1387
+ ```
1388
+
1389
+ Creates `./backtest-kit-docker/` in the current working directory.
1390
+
1391
+ Override the directory name with `--output`:
1392
+
1393
+ ```bash
1394
+ npx @backtest-kit/cli --docker --output my-docker-workspace
1395
+ ```
1396
+
1397
+ The target directory must not exist or must be empty — the command aborts if it contains any files.
1398
+
1399
+ ### Two Launch Modes
1400
+
1401
+ The Docker image entrypoint supports two ways to run a strategy:
1402
+
1403
+ #### 1. `command:` in `docker-compose.yaml`
1404
+
1405
+ Pin mode and flags directly in the compose file. The entrypoint forwards all arguments to the CLI unchanged:
1406
+
1407
+ ```yaml
1408
+ command:
1409
+ - --live
1410
+ - --symbol
1411
+ - TRXUSDT
1412
+ - --strategy
1413
+ - feb_2026_strategy
1414
+ - --exchange
1415
+ - ccxt-exchange
1416
+ - ./content/feb_2026/feb_2026.strategy.ts
1417
+ - --ui
1418
+ ```
1419
+
1420
+ #### 2. Inline environment variables
1421
+
1422
+ Pass `MODE` and `STRATEGY_FILE` on the command line — no file edits needed:
1423
+
1424
+ ```bash
1425
+ MODE=live SYMBOL=TRXUSDT UI=1 docker-compose up -d
1426
+ ```
1427
+
1428
+ | Variable | Required | Default | Description |
1429
+ |----------|----------|---------|-------------|
1430
+ | `MODE` | yes | — | `backtest` \| `live` \| `paper` \| `walker` |
1431
+ | `STRATEGY_FILE` | yes | — | Path to strategy entry point (relative to `working_dir`) |
1432
+ | `SYMBOL` | no | `BTCUSDT` | Trading pair |
1433
+ | `STRATEGY` | no | first registered | Strategy name |
1434
+ | `EXCHANGE` | no | first registered | Exchange name |
1435
+ | `FRAME` | no | first registered | Frame name (backtest only) |
1436
+ | `UI` | no | — | Any non-empty value enables `--ui` |
1437
+ | `TELEGRAM` | no | — | Any non-empty value enables `--telegram` |
1438
+ | `VERBOSE` | no | — | Any non-empty value enables `--verbose` |
1439
+ | `NO_CACHE` | no | — | Any non-empty value enables `--noCache` |
1440
+ | `NO_FLUSH` | no | — | Any non-empty value enables `--noFlush` |
1441
+
1442
+ ## 🌍 Environment Variables
1443
+
1444
+ Create a `.env` file in your project root:
1445
+
1446
+ ```env
1447
+ # Telegram notifications (required for --telegram)
1448
+ CC_TELEGRAM_TOKEN=your_bot_token_here
1449
+ CC_TELEGRAM_CHANNEL=-100123456789
1450
+
1451
+ # Web UI server (optional, defaults shown)
1452
+ CC_WWWROOT_HOST=0.0.0.0
1453
+ CC_WWWROOT_PORT=60050
1454
+
1455
+ # Custom QuickChart service URL (optional)
1456
+ CC_QUICKCHART_HOST=
1457
+ ```
1458
+
1459
+ | Variable | Default | Description |
1460
+ |------------------------|-------------|---------------------------------------|
1461
+ | `CC_TELEGRAM_TOKEN` | — | Telegram bot token (from @BotFather) |
1462
+ | `CC_TELEGRAM_CHANNEL` | — | Telegram channel or chat ID |
1463
+ | `CC_WWWROOT_HOST` | `0.0.0.0` | UI server bind address |
1464
+ | `CC_WWWROOT_PORT` | `60050` | UI server port |
1465
+ | `CC_QUICKCHART_HOST` | — | Self-hosted QuickChart instance URL |
1466
+
1467
+ ## ⚙️ Default Behaviors
1468
+
1469
+ 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:
1470
+
1471
+ | Component | Default | Warning |
1472
+ |--------------|--------------------------------|---------------------------------------------------------------------------|
1473
+ | **Exchange** | CCXT Binance (`default_exchange`) | `Warning: The default exchange schema is set to CCXT Binance...` |
1474
+ | **Frame** | February 2024 (`default_frame`) | `Warning: The default frame schema is set to February 2024...` |
1475
+ | **Symbol** | `BTCUSDT` | — |
1476
+ | **Cache intervals** | `1m, 15m, 30m, 4h` | Used if `--cacheInterval` not provided; skip entirely with `--noCache` |
1477
+
1478
+ > **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.
1479
+
1480
+ ## 🔧 Programmatic API
1481
+
1482
+ 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.
1483
+
1484
+ ### `run(mode, args)`
1485
+
1486
+ ```typescript
1487
+ import { run } from '@backtest-kit/cli';
1488
+
1489
+ await run(mode, args);
1490
+ ```
1491
+
1492
+ | Parameter | Description |
1493
+ |-----------|-------------|
1494
+ | `mode` | `"backtest" \| "paper" \| "live"` — Execution mode |
1495
+ | `args` | Mode-specific options (all optional — same defaults as CLI) |
1496
+
1497
+ `run()` can be called **only once per process**. A second call throws `"Should be called only once"`.
1498
+
1499
+ ### Payload fields
1500
+
1501
+ **Backtest** (`mode: "backtest"`):
1502
+
1503
+ | Field | Type | Description |
1504
+ |-------|------|-------------|
1505
+ | `entryPoint` | `string` | Path to strategy entry point file |
1506
+ | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1507
+ | `strategy` | `string` | Strategy name (default: first registered) |
1508
+ | `exchange` | `string` | Exchange name (default: first registered) |
1509
+ | `frame` | `string` | Frame name (default: first registered) |
1510
+ | `cacheInterval` | `CandleInterval[]` | Intervals to pre-cache (default: `["1m","15m","30m","1h","4h"]`) |
1511
+ | `noCache` | `boolean` | Skip candle cache warming (default: `false`) |
1512
+ | `noFlush` | `boolean` | Skip removing report/log/markdown/agent folders before the run (default: `false`) |
1513
+ | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1514
+
1515
+ **Paper** and **Live** (`mode: "paper"` / `mode: "live"`):
1516
+
1517
+ | Field | Type | Description |
1518
+ |-------|------|-------------|
1519
+ | `entryPoint` | `string` | Path to strategy entry point file |
1520
+ | `symbol` | `string` | Trading pair (default: `"BTCUSDT"`) |
1521
+ | `strategy` | `string` | Strategy name (default: first registered) |
1522
+ | `exchange` | `string` | Exchange name (default: first registered) |
1523
+ | `verbose` | `boolean` | Log each candle fetch (default: `false`) |
1524
+
1525
+ ### Examples
1526
+
1527
+ **Backtest:**
1528
+
1529
+ ```typescript
1530
+ import { run } from '@backtest-kit/cli';
1531
+
1532
+ await run('backtest', {
1533
+ entryPoint: './src/index.mjs',
1534
+ symbol: 'ETHUSDT',
1535
+ frame: 'feb-2024',
1536
+ cacheInterval: ['1m', '15m', '1h'],
1537
+ verbose: true,
1538
+ });
1539
+ ```
1540
+
1541
+ **Paper trading:**
1542
+
1543
+ ```typescript
1544
+ import { run } from '@backtest-kit/cli';
1545
+
1546
+ await run('paper', {
1547
+ entryPoint: './src/index.mjs',
1548
+ symbol: 'BTCUSDT',
1549
+ });
1550
+ ```
1551
+
1552
+ **Live trading:**
1553
+
1554
+ ```typescript
1555
+ import { run } from '@backtest-kit/cli';
1556
+
1557
+ await run('live', {
1558
+ entryPoint: './src/index.mjs',
1559
+ symbol: 'BTCUSDT',
1560
+ verbose: true,
1561
+ });
1562
+ ```
1563
+
1564
+ ## 💡 Why Use @backtest-kit/cli?
1565
+
1566
+ Instead of writing infrastructure code for every project:
1567
+
1568
+ **❌ Without @backtest-kit/cli (manual setup)**
1569
+
1570
+ ```typescript
1571
+ // index.ts
1572
+ import { setLogger, setConfig, Storage, Notification, Report, Markdown } from 'backtest-kit';
1573
+ import { serve } from '@backtest-kit/ui';
1574
+
1575
+ setLogger({ log: console.log, ... });
1576
+ Storage.enable();
1577
+ Notification.enable();
1578
+ Report.enable();
1579
+ Markdown.disable();
1580
+
1581
+ // ... parse CLI args manually
1582
+ // ... register exchange schema
1583
+ // ... warm candle cache
1584
+ // ... set up Telegram bot
1585
+ // ... handle SIGINT gracefully
1586
+ // ... load and run backtest
1587
+ ```
1588
+
1589
+ **✅ With @backtest-kit/cli (one script)**
1590
+
1591
+ ```json
1592
+ { "scripts": { "backtest": "npx @backtest-kit/cli --backtest --ui --telegram ./src/index.mjs" } }
1593
+ ```
1594
+
1595
+ ```bash
1596
+ npm run backtest
1597
+ ```
1598
+
1599
+ **Benefits:**
1600
+
1601
+ - 🚀 From zero to running backtest in seconds
1602
+ - 💾 Automatic candle cache warming with retry logic
1603
+ - 🌐 Production-ready web dashboard out of the box
1604
+ - 📬 Telegram notifications with price charts — no chart code needed
1605
+ - 🛑 Graceful shutdown on SIGINT — no hanging processes
1606
+ - 🔌 Works with any `backtest-kit` strategy file as-is
1607
+ - 🧩 Broker adapter hooks via side-effect module files — no CLI internals to touch
1608
+
1609
+ ## 🤝 Contribute
1610
+
1611
+ Fork/PR on [GitHub](https://github.com/tripolskypetr/backtest-kit).
1612
+
1613
+ ## 📜 License
1614
+
1615
+ MIT © [tripolskypetr](https://github.com/tripolskypetr)