tradelab 0.1.2 → 0.2.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 +38 -15
- package/dist/cjs/data.cjs +1718 -0
- package/dist/cjs/index.cjs +2294 -0
- package/package.json +28 -6
- package/src/data/yahoo.js +40 -17
- package/src/engine/backtest.js +46 -4
- package/src/index.js +1 -0
- package/src/metrics/buildMetrics.js +32 -16
- package/src/reporting/exportBacktestArtifacts.js +13 -0
- package/src/reporting/exportMetricsJson.js +24 -0
- package/src/reporting/renderHtmlReport.js +26 -9
- package/types/data.d.ts +13 -0
- package/types/index.d.ts +512 -0
package/README.md
CHANGED
|
@@ -2,21 +2,25 @@
|
|
|
2
2
|
|
|
3
3
|
# tradelab
|
|
4
4
|
|
|
5
|
-
`tradelab` is a
|
|
6
|
-
-
|
|
7
|
-
-
|
|
5
|
+
`tradelab` is a Node.js backtesting toolkit for trading strategy research. It lets you:
|
|
6
|
+
- load candles from Yahoo Finance or CSV
|
|
7
|
+
- run candle-based backtests with sizing, exits, and risk controls
|
|
8
|
+
- export trades, metrics, and HTML reports
|
|
8
9
|
|
|
9
|
-
The package
|
|
10
|
+
The package is modular by design, so you can use just the parts you need: data loading, backtesting, reporting, or the utility layer on its own.
|
|
11
|
+
|
|
12
|
+
It is built for historical research and testing, not broker connectivity or live trading.
|
|
10
13
|
|
|
11
14
|
## Features
|
|
12
15
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
- Utility indicators and session helpers for strategy
|
|
16
|
+
- Modular structure: use the full workflow or just the engine, data layer, reporting, or helpers
|
|
17
|
+
- Backtest engine with pending entries, OCO exits, scale-outs, pyramiding, cooldowns, daily loss limits, and optional replay/equity capture
|
|
18
|
+
- Historical data loading from Yahoo Finance, with local caching to avoid repeated downloads
|
|
19
|
+
- CSV import for common OHLCV formats and custom column mappings
|
|
20
|
+
- Position-level and leg-level metrics, including drawdown, expectancy, hold-time stats, and side breakdowns
|
|
21
|
+
- HTML report export, metrics JSON export, and trade CSV export
|
|
22
|
+
- Utility indicators and session helpers for strategy development
|
|
23
|
+
- TypeScript definitions for the public API
|
|
20
24
|
|
|
21
25
|
## Installation
|
|
22
26
|
|
|
@@ -26,6 +30,22 @@ npm install tradelab
|
|
|
26
30
|
|
|
27
31
|
Node `18+` is required.
|
|
28
32
|
|
|
33
|
+
## Importing
|
|
34
|
+
|
|
35
|
+
### CommonJS
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
const { backtest, getHistoricalCandles, ema } = require("tradelab");
|
|
39
|
+
const { fetchHistorical } = require("tradelab/data");
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### ESM
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { backtest, getHistoricalCandles, ema } from "tradelab";
|
|
46
|
+
import { fetchHistorical } from "tradelab/data";
|
|
47
|
+
```
|
|
48
|
+
|
|
29
49
|
## Quick Start
|
|
30
50
|
|
|
31
51
|
```js
|
|
@@ -72,7 +92,7 @@ exportBacktestArtifacts({
|
|
|
72
92
|
|
|
73
93
|
## Getting Historical Data
|
|
74
94
|
|
|
75
|
-
The simplest entry point is `getHistoricalCandles()`.
|
|
95
|
+
The simplest entry point is `getHistoricalCandles()`. For most users, it is the only data-loading function you need.
|
|
76
96
|
|
|
77
97
|
### Yahoo Finance
|
|
78
98
|
|
|
@@ -154,6 +174,7 @@ Quality-of-life behavior:
|
|
|
154
174
|
- `takeProfit` can be omitted if `rr` or `_rr` is provided
|
|
155
175
|
- `qty` or `size` can override risk-based sizing
|
|
156
176
|
- `riskPct` or `riskFraction` can override the global risk setting per signal
|
|
177
|
+
- `strict: true` throws if the strategy directly accesses candles beyond the current index
|
|
157
178
|
|
|
158
179
|
Optional engine hints:
|
|
159
180
|
|
|
@@ -173,8 +194,8 @@ Optional engine hints:
|
|
|
173
194
|
|
|
174
195
|
- `trades`: every realized leg, including scale-outs
|
|
175
196
|
- `positions`: completed positions only
|
|
176
|
-
- `metrics`: aggregate
|
|
177
|
-
- `eqSeries`: realized equity history
|
|
197
|
+
- `metrics`: aggregate stats including `winRate`, `expectancy`, `profitFactor`, `maxDrawdown`, `sharpe`, `avgHold`, and `sideBreakdown`
|
|
198
|
+
- `eqSeries`: realized equity history as `{ time, timestamp, equity }`
|
|
178
199
|
- `replay`: chart-friendly frame and event data
|
|
179
200
|
|
|
180
201
|
## Main Exports
|
|
@@ -186,11 +207,12 @@ Optional engine hints:
|
|
|
186
207
|
- `loadCandlesFromCSV(filePath, options)`
|
|
187
208
|
- `saveCandlesToCache(candles, meta)`
|
|
188
209
|
- `loadCandlesFromCache(symbol, interval, period, outDir)`
|
|
210
|
+
- `exportMetricsJSON({ result, outDir })`
|
|
189
211
|
- `exportBacktestArtifacts({ result, outDir })`
|
|
190
212
|
|
|
191
213
|
## Reports
|
|
192
214
|
|
|
193
|
-
The HTML report is self-contained apart from the Plotly CDN script. Report markup, CSS, and client-side chart code live under `templates
|
|
215
|
+
The HTML report is self-contained apart from the Plotly CDN script. Report markup, CSS, and client-side chart code live under `templates/`.
|
|
194
216
|
|
|
195
217
|
Export helpers default CSV output to completed positions. Use `csvSource: "trades"` if you want every realized leg in the CSV.
|
|
196
218
|
|
|
@@ -206,3 +228,4 @@ node examples/yahooEmaCross.js SPY 1d 1y
|
|
|
206
228
|
- Yahoo downloads can be cached under `output/data` by default.
|
|
207
229
|
- The engine is intended for historical research, not brokerage execution.
|
|
208
230
|
- File output only happens through the reporting and cache helpers.
|
|
231
|
+
- CommonJS and ESM are both supported.
|