tradelab 0.1.1 → 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 +39 -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
|
@@ -1,21 +1,26 @@
|
|
|
1
|
+
<img src="https://i.imgur.com/HGvvQbq.png" width="500" alt="tradelab logo"/>
|
|
2
|
+
|
|
1
3
|
# tradelab
|
|
2
4
|
|
|
3
|
-
`tradelab` is a
|
|
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
|
|
4
9
|
|
|
5
|
-
|
|
6
|
-
- you want to fetch Yahoo Finance data or import CSVs and backtest with minimal setup
|
|
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.
|
|
7
11
|
|
|
8
|
-
|
|
12
|
+
It is built for historical research and testing, not broker connectivity or live trading.
|
|
9
13
|
|
|
10
14
|
## Features
|
|
11
15
|
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
- 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
|
|
19
24
|
|
|
20
25
|
## Installation
|
|
21
26
|
|
|
@@ -25,6 +30,22 @@ npm install tradelab
|
|
|
25
30
|
|
|
26
31
|
Node `18+` is required.
|
|
27
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
|
+
|
|
28
49
|
## Quick Start
|
|
29
50
|
|
|
30
51
|
```js
|
|
@@ -71,7 +92,7 @@ exportBacktestArtifacts({
|
|
|
71
92
|
|
|
72
93
|
## Getting Historical Data
|
|
73
94
|
|
|
74
|
-
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.
|
|
75
96
|
|
|
76
97
|
### Yahoo Finance
|
|
77
98
|
|
|
@@ -153,6 +174,7 @@ Quality-of-life behavior:
|
|
|
153
174
|
- `takeProfit` can be omitted if `rr` or `_rr` is provided
|
|
154
175
|
- `qty` or `size` can override risk-based sizing
|
|
155
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
|
|
156
178
|
|
|
157
179
|
Optional engine hints:
|
|
158
180
|
|
|
@@ -172,8 +194,8 @@ Optional engine hints:
|
|
|
172
194
|
|
|
173
195
|
- `trades`: every realized leg, including scale-outs
|
|
174
196
|
- `positions`: completed positions only
|
|
175
|
-
- `metrics`: aggregate
|
|
176
|
-
- `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 }`
|
|
177
199
|
- `replay`: chart-friendly frame and event data
|
|
178
200
|
|
|
179
201
|
## Main Exports
|
|
@@ -185,11 +207,12 @@ Optional engine hints:
|
|
|
185
207
|
- `loadCandlesFromCSV(filePath, options)`
|
|
186
208
|
- `saveCandlesToCache(candles, meta)`
|
|
187
209
|
- `loadCandlesFromCache(symbol, interval, period, outDir)`
|
|
210
|
+
- `exportMetricsJSON({ result, outDir })`
|
|
188
211
|
- `exportBacktestArtifacts({ result, outDir })`
|
|
189
212
|
|
|
190
213
|
## Reports
|
|
191
214
|
|
|
192
|
-
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/`.
|
|
193
216
|
|
|
194
217
|
Export helpers default CSV output to completed positions. Use `csvSource: "trades"` if you want every realized leg in the CSV.
|
|
195
218
|
|
|
@@ -205,3 +228,4 @@ node examples/yahooEmaCross.js SPY 1d 1y
|
|
|
205
228
|
- Yahoo downloads can be cached under `output/data` by default.
|
|
206
229
|
- The engine is intended for historical research, not brokerage execution.
|
|
207
230
|
- File output only happens through the reporting and cache helpers.
|
|
231
|
+
- CommonJS and ESM are both supported.
|