backtest-kit 3.8.0 → 3.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. package/README.md +19 -15
  2. package/build/index.cjs +2087 -669
  3. package/build/index.mjs +2085 -670
  4. package/package.json +1 -1
  5. package/types.d.ts +2638 -1723
package/README.md CHANGED
@@ -51,12 +51,16 @@ npm install backtest-kit ccxt ollama uuid
51
51
 
52
52
  ## 📋 Supported Order Types
53
53
 
54
+ > With the calculation of PnL
55
+
54
56
  - Market/Limit entries
55
57
  - TP/SL/OCO exits
56
58
  - Grid with auto-cancel on unmet conditions
57
59
  - Partial profit/loss levels
58
60
  - Trailing stop-loss
59
61
  - Breakeven protection
62
+ - Stop limit entries (before OCO)
63
+ - Dollar cost averaging
60
64
 
61
65
  ## 📚 Code Samples
62
66
 
@@ -209,7 +213,7 @@ Backtest Kit is **not a data-processing library** - it is a **time execution eng
209
213
  These three functions work together to manage a position dynamically. To reduce position linearity, the framework treats every DCA entry as a fixed **$100 unit** regardless of price — this flattens the effective entry curve and makes PNL weighting independent of position size.
210
214
 
211
215
  **Public API:**
212
- - **`commitAverageBuy`** — adds a new DCA entry. For LONG, **only accepted when current price is below a new low**. Silently rejected otherwise. This prevents averaging up.
216
+ - **`commitAverageBuy`** — adds a new DCA entry. For LONG, **only accepted when current price is below a new low**. Silently rejected otherwise. This prevents averaging up. Can be overridden using `setConfig`
213
217
  - **`commitPartialProfit`** — closes X% of the position at a profit. Locks in gains while keeping exposure.
214
218
  - **`commitPartialLoss`** — closes X% of the position at a loss. Cuts exposure before the stop-loss is hit.
215
219
 
@@ -230,7 +234,7 @@ These three functions work together to manage a position dynamically. To reduce
230
234
  commitPartialLoss(20%) @ 860 ← cnt=3
231
235
  entry#4 @ 920 → 0.10870 coins
232
236
  commitPartialProfit(40%) @ 1050 ← cnt=4
233
- entry#5 @ 980 ✗ REJECTED (980 > ep3≈929.90)
237
+ entry#5 @ 980 ✗ REJECTED (980 > ep3≈929.92)
234
238
  totalInvested = $400
235
239
  ```
236
240
 
@@ -255,9 +259,9 @@ These three functions work together to manage a position dynamically. To reduce
255
259
  **Partial#2 — commitPartialLoss @ 860, 20%, cnt=3**
256
260
  ```
257
261
  costBasis = 70 + 100 + 100 = $270
258
- ep2 = 270 / 0.28890 ≈ 934.93
262
+ ep2 = 270 / 0.28890 ≈ 934.58
259
263
  partialDollarValue = 20% × 270 = $54 → weight = 54/400 = 0.135
260
- pnl = (860−934.93)/934.93 × 100 ≈ −8.01%
264
+ pnl = (860−934.58)/934.58 × 100 ≈ −7.98%
261
265
  costBasis → $216
262
266
  coins sold: 0.05778 × 860 = $49.69
263
267
  remaining: 0.23112
@@ -265,16 +269,16 @@ These three functions work together to manage a position dynamically. To reduce
265
269
 
266
270
  **DCA after Partial#2**
267
271
  ```
268
- entry#4 @ 920 (920 < ep2=934.93 ✓ accepted)
272
+ entry#4 @ 920 (920 < ep2=934.58 ✓ accepted)
269
273
  coins: 0.23112 + 0.10870 = 0.33982
270
274
  ```
271
275
 
272
276
  **Partial#3 — commitPartialProfit @ 1050, 40%, cnt=4**
273
277
  ```
274
278
  costBasis = 216 + 100 = $316
275
- ep3 = 316 / 0.33982 ≈ 929.90
279
+ ep3 = 316 / 0.33982 ≈ 929.92
276
280
  partialDollarValue = 40% × 316 = $126.4 → weight = 126.4/400 = 0.316
277
- pnl = (1050−929.90)/929.90 × 100 ≈ +12.92%
281
+ pnl = (1050−929.92)/929.92 × 100 ≈ +12.91%
278
282
  costBasis → $189.6
279
283
  coins sold: 0.13593 × 1050 = $142.72
280
284
  remaining: 0.20389
@@ -282,28 +286,28 @@ These three functions work together to manage a position dynamically. To reduce
282
286
 
283
287
  **DCA after Partial#3 — rejected**
284
288
  ```
285
- entry#5 @ 980 (980 > ep3≈929.90 ✗ REJECTED)
289
+ entry#5 @ 980 (980 > ep3≈929.92 ✗ REJECTED)
286
290
  ```
287
291
 
288
292
  **Close at TP @ 1200**
289
293
  ```
290
- ep_final = ep3 ≈ 929.90 (no new entries)
294
+ ep_final = ep3 ≈ 929.92 (no new entries)
291
295
  coins: 0.20389
292
296
 
293
297
  remainingDollarValue = 400 − 30 − 54 − 126.4 = $189.6
294
298
  weight = 189.6/400 = 0.474
295
- pnl = (1200−929.90)/929.90 × 100 ≈ +29.05%
299
+ pnl = (1200−929.92)/929.92 × 100 ≈ +29.04%
296
300
  coins sold: 0.20389 × 1200 = $244.67
297
301
  ```
298
302
 
299
303
  **Result (toProfitLossDto)**
300
304
  ```
301
305
  0.075 × (+15.00) = +1.125
302
- 0.135 × (−8.01) = −1.081
303
- 0.316 × (+12.92) = +4.083
304
- 0.474 × (+29.05) = +13.770
306
+ 0.135 × (−7.98) = −1.077
307
+ 0.316 × (+12.91) = +4.080
308
+ 0.474 × (+29.04) = +13.765
305
309
  ─────────────────────────────
306
- ≈ +17.90%
310
+ ≈ +17.89%
307
311
 
308
312
  Cross-check (coins):
309
313
  34.50 + 49.69 + 142.72 + 244.67 = $471.58
@@ -769,7 +773,7 @@ npm start
769
773
 
770
774
  ## ✅ Tested & Reliable
771
775
 
772
- 365+ tests cover validation, recovery, reports, and events.
776
+ 450+ tests cover validation, recovery, reports, and events.
773
777
 
774
778
  ## 🤝 Contribute
775
779