@backtest-kit/cli 7.8.0 → 8.0.0

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