backtest-kit 6.2.0 → 6.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backtest-kit",
3
- "version": "6.2.0",
3
+ "version": "6.4.0",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -10854,6 +10854,12 @@ declare class PersistSignalUtils {
10854
10854
  * @returns Promise that resolves when write is complete
10855
10855
  */
10856
10856
  writeSignalData: (signalRow: ISignalRow | null, symbol: string, strategyName: StrategyName, exchangeName: ExchangeName) => Promise<void>;
10857
+ /**
10858
+ * Clears the memoized storage cache.
10859
+ * Call this when process.cwd() changes between strategy iterations
10860
+ * so new storage instances are created with the updated base path.
10861
+ */
10862
+ clear(): void;
10857
10863
  /**
10858
10864
  * Switches to the default JSON persist adapter.
10859
10865
  * All future persistence writes will use JSON storage.
@@ -10939,6 +10945,12 @@ declare class PersistRiskUtils {
10939
10945
  * @returns Promise that resolves when write is complete
10940
10946
  */
10941
10947
  writePositionData: (riskRow: RiskData, riskName: RiskName, exchangeName: ExchangeName) => Promise<void>;
10948
+ /**
10949
+ * Clears the memoized storage cache.
10950
+ * Call this when process.cwd() changes between strategy iterations
10951
+ * so new storage instances are created with the updated base path.
10952
+ */
10953
+ clear(): void;
10942
10954
  /**
10943
10955
  * Switches to the default JSON persist adapter.
10944
10956
  * All future persistence writes will use JSON storage.
@@ -11026,6 +11038,12 @@ declare class PersistScheduleUtils {
11026
11038
  * @returns Promise that resolves when write is complete
11027
11039
  */
11028
11040
  writeScheduleData: (scheduledSignalRow: IScheduledSignalRow | null, symbol: string, strategyName: StrategyName, exchangeName: ExchangeName) => Promise<void>;
11041
+ /**
11042
+ * Clears the memoized storage cache.
11043
+ * Call this when process.cwd() changes between strategy iterations
11044
+ * so new storage instances are created with the updated base path.
11045
+ */
11046
+ clear(): void;
11029
11047
  /**
11030
11048
  * Switches to the default JSON persist adapter.
11031
11049
  * All future persistence writes will use JSON storage.
@@ -11115,6 +11133,12 @@ declare class PersistPartialUtils {
11115
11133
  * @returns Promise that resolves when write is complete
11116
11134
  */
11117
11135
  writePartialData: (partialData: PartialData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<void>;
11136
+ /**
11137
+ * Clears the memoized storage cache.
11138
+ * Call this when process.cwd() changes between strategy iterations
11139
+ * so new storage instances are created with the updated base path.
11140
+ */
11141
+ clear(): void;
11118
11142
  /**
11119
11143
  * Switches to the default JSON persist adapter.
11120
11144
  * All future persistence writes will use JSON storage.
@@ -11239,6 +11263,12 @@ declare class PersistBreakevenUtils {
11239
11263
  * @returns Promise that resolves when write is complete
11240
11264
  */
11241
11265
  writeBreakevenData: (breakevenData: BreakevenData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<void>;
11266
+ /**
11267
+ * Clears the memoized storage cache.
11268
+ * Call this when process.cwd() changes between strategy iterations
11269
+ * so new storage instances are created with the updated base path.
11270
+ */
11271
+ clear(): void;
11242
11272
  /**
11243
11273
  * Switches to the default JSON persist adapter.
11244
11274
  * All future persistence writes will use JSON storage.
@@ -11327,6 +11357,12 @@ declare class PersistCandleUtils {
11327
11357
  * @returns Promise that resolves when all writes are complete
11328
11358
  */
11329
11359
  writeCandlesData: (candles: CandleData[], symbol: string, interval: CandleInterval, exchangeName: ExchangeName) => Promise<void>;
11360
+ /**
11361
+ * Clears the memoized storage cache.
11362
+ * Call this when process.cwd() changes between strategy iterations
11363
+ * so new storage instances are created with the updated base path.
11364
+ */
11365
+ clear(): void;
11330
11366
  /**
11331
11367
  * Switches to the default JSON persist adapter.
11332
11368
  * All future persistence writes will use JSON storage.
@@ -11403,6 +11439,12 @@ declare class PersistStorageUtils {
11403
11439
  * @returns Promise that resolves when write is complete
11404
11440
  */
11405
11441
  writeStorageData: (signalData: StorageData, backtest: boolean) => Promise<void>;
11442
+ /**
11443
+ * Clears the memoized storage cache.
11444
+ * Call this when process.cwd() changes between strategy iterations
11445
+ * so new storage instances are created with the updated base path.
11446
+ */
11447
+ clear(): void;
11406
11448
  /**
11407
11449
  * Switches to the default JSON persist adapter.
11408
11450
  * All future persistence writes will use JSON storage.
@@ -11468,6 +11510,12 @@ declare class PersistNotificationUtils {
11468
11510
  * @returns Promise that resolves when write is complete
11469
11511
  */
11470
11512
  writeNotificationData: (notificationData: NotificationData, backtest: boolean) => Promise<void>;
11513
+ /**
11514
+ * Clears the memoized storage cache.
11515
+ * Call this when process.cwd() changes between strategy iterations
11516
+ * so new storage instances are created with the updated base path.
11517
+ */
11518
+ clear(): void;
11471
11519
  /**
11472
11520
  * Switches to the default JSON persist adapter.
11473
11521
  * All future persistence writes will use JSON storage.
@@ -11533,6 +11581,12 @@ declare class PersistLogUtils {
11533
11581
  * @returns Promise that resolves when write is complete
11534
11582
  */
11535
11583
  writeLogData: (logData: LogData) => Promise<void>;
11584
+ /**
11585
+ * Clears the cached storage instance.
11586
+ * Call this when process.cwd() changes between strategy iterations
11587
+ * so a new storage instance is created with the updated base path.
11588
+ */
11589
+ clear(): void;
11536
11590
  /**
11537
11591
  * Switches to the default JSON persist adapter.
11538
11592
  * All future persistence writes will use JSON storage.
@@ -11586,6 +11640,12 @@ declare class PersistMeasureUtils {
11586
11640
  * @returns Promise that resolves when write is complete
11587
11641
  */
11588
11642
  writeMeasureData: (data: MeasureData, bucket: string, key: string) => Promise<void>;
11643
+ /**
11644
+ * Clears the memoized storage cache.
11645
+ * Call this when process.cwd() changes between strategy iterations
11646
+ * so new storage instances are created with the updated base path.
11647
+ */
11648
+ clear(): void;
11589
11649
  /**
11590
11650
  * Switches to the default JSON persist adapter.
11591
11651
  */
@@ -11700,12 +11760,20 @@ declare class PersistMemoryUtils {
11700
11760
  data: MemoryData;
11701
11761
  }>;
11702
11762
  /**
11703
- * Dispose persist adapter to prevent memory leak
11763
+ * Clears the memoized storage cache.
11764
+ * Call this when process.cwd() changes between strategy iterations
11765
+ * so new storage instances are created with the updated base path.
11766
+ */
11767
+ clear: () => void;
11768
+ /**
11769
+ * Disposes of the memory adapter and releases any resources.
11770
+ * Call this when a signal is removed to clean up its associated storage.
11704
11771
  *
11705
11772
  * @param signalId - Signal identifier
11706
11773
  * @param bucketName - Bucket name
11774
+ * @returns void
11707
11775
  */
11708
- clear: (signalId: string, bucketName: string) => void;
11776
+ dispose: (signalId: string, bucketName: string) => void;
11709
11777
  /**
11710
11778
  * Switches to the default JSON persist adapter.
11711
11779
  * All future persistence writes will use JSON storage.
@@ -12004,6 +12072,12 @@ declare class ReportAdapter extends ReportUtils {
12004
12072
  * @internal - Automatically called by report services, not for direct use
12005
12073
  */
12006
12074
  writeData: <T = any>(reportName: ReportName, data: T, options: IReportDumpOptions) => Promise<void>;
12075
+ /**
12076
+ * Clears the memoized storage cache.
12077
+ * Call this when process.cwd() changes between strategy iterations
12078
+ * so new storage instances are created with the updated base path.
12079
+ */
12080
+ clear(): void;
12007
12081
  /**
12008
12082
  * Switches to a dummy report adapter that discards all writes.
12009
12083
  * All future report writes will be no-ops.
@@ -12344,6 +12418,12 @@ declare class MarkdownAdapter extends MarkdownUtils {
12344
12418
  * All dumps append to a single .jsonl file per markdown type.
12345
12419
  */
12346
12420
  useJsonl(): void;
12421
+ /**
12422
+ * Clears the memoized storage cache.
12423
+ * Call this when process.cwd() changes between strategy iterations
12424
+ * so new storage instances are created with the updated base path.
12425
+ */
12426
+ clear(): void;
12347
12427
  /**
12348
12428
  * Switches to a dummy markdown adapter that discards all writes.
12349
12429
  * All future markdown writes will be no-ops.
@@ -12447,6 +12527,12 @@ declare class LogAdapter implements ILog {
12447
12527
  * @param dirName - Directory for the JSONL file (default: ./dump/log)
12448
12528
  */
12449
12529
  useJsonl: (fileName?: string, dirName?: string) => void;
12530
+ /**
12531
+ * Clears the cached log instance by resetting to the default in-memory adapter.
12532
+ * Call this when process.cwd() changes between strategy iterations
12533
+ * so a new adapter instance is created with the updated base path.
12534
+ */
12535
+ clear: () => void;
12450
12536
  }
12451
12537
  /**
12452
12538
  * Global singleton instance of LogAdapter.
@@ -17965,6 +18051,12 @@ declare class StorageBacktestAdapter implements IStorageUtils {
17965
18051
  * Signals will be stored in memory only.
17966
18052
  */
17967
18053
  useMemory: () => void;
18054
+ /**
18055
+ * Clears the cached utils instance by resetting to the default in-memory adapter.
18056
+ * Call this when process.cwd() changes between strategy iterations
18057
+ * so a new instance is created with the updated base path.
18058
+ */
18059
+ clear: () => void;
17968
18060
  }
17969
18061
  /**
17970
18062
  * Live trading storage adapter with pluggable storage backend.
@@ -18039,6 +18131,12 @@ declare class StorageLiveAdapter implements IStorageUtils {
18039
18131
  * Signals will be stored in memory only.
18040
18132
  */
18041
18133
  useMemory: () => void;
18134
+ /**
18135
+ * Clears the cached utils instance by resetting to the default persistent adapter.
18136
+ * Call this when process.cwd() changes between strategy iterations
18137
+ * so a new instance is created with the updated base path.
18138
+ */
18139
+ clear: () => void;
18042
18140
  }
18043
18141
  /**
18044
18142
  * Main storage adapter that manages both backtest and live signal storage.
@@ -18165,7 +18263,7 @@ interface INotificationUtils {
18165
18263
  /**
18166
18264
  * Clears all stored notifications.
18167
18265
  */
18168
- clear(): Promise<void>;
18266
+ dispose(): Promise<void>;
18169
18267
  }
18170
18268
  /**
18171
18269
  * Constructor type for notification adapters.
@@ -18254,7 +18352,7 @@ declare class NotificationBacktestAdapter implements INotificationUtils {
18254
18352
  * Clears all stored notifications.
18255
18353
  * Proxies call to the underlying notification adapter.
18256
18354
  */
18257
- clear: () => Promise<void>;
18355
+ dispose: () => Promise<void>;
18258
18356
  /**
18259
18357
  * Sets the notification adapter constructor.
18260
18358
  * All future notification operations will use this adapter.
@@ -18277,6 +18375,12 @@ declare class NotificationBacktestAdapter implements INotificationUtils {
18277
18375
  * Notifications will be persisted to disk.
18278
18376
  */
18279
18377
  usePersist: () => void;
18378
+ /**
18379
+ * Resets the cached utils instance to the default in-memory adapter.
18380
+ * Call this when process.cwd() changes between strategy iterations
18381
+ * so a new instance is created with the updated base path.
18382
+ */
18383
+ clear: () => void;
18280
18384
  }
18281
18385
  /**
18282
18386
  * Live trading notification adapter with pluggable notification backend.
@@ -18360,7 +18464,7 @@ declare class NotificationLiveAdapter implements INotificationUtils {
18360
18464
  * Clears all stored notifications.
18361
18465
  * Proxies call to the underlying notification adapter.
18362
18466
  */
18363
- clear: () => Promise<void>;
18467
+ dispose: () => Promise<void>;
18364
18468
  /**
18365
18469
  * Sets the notification adapter constructor.
18366
18470
  * All future notification operations will use this adapter.
@@ -18383,6 +18487,12 @@ declare class NotificationLiveAdapter implements INotificationUtils {
18383
18487
  * Notifications will be persisted to disk.
18384
18488
  */
18385
18489
  usePersist: () => void;
18490
+ /**
18491
+ * Resets the cached utils instance to the default in-memory adapter.
18492
+ * Call this when process.cwd() changes between strategy iterations
18493
+ * so a new instance is created with the updated base path.
18494
+ */
18495
+ clear: () => void;
18386
18496
  }
18387
18497
  /**
18388
18498
  * Main notification adapter that manages both backtest and live notification storage.
@@ -18418,7 +18528,7 @@ declare class NotificationAdapter {
18418
18528
  *
18419
18529
  * @throws Error if NotificationAdapter is not enabled
18420
18530
  */
18421
- clear: (isBacktest: boolean) => Promise<void>;
18531
+ dispose: (isBacktest: boolean) => Promise<void>;
18422
18532
  }
18423
18533
  /**
18424
18534
  * Global singleton instance of NotificationAdapter.
@@ -18635,6 +18745,12 @@ declare class MemoryAdapter implements TMemoryInstance {
18635
18745
  * Switches to dummy adapter that discards all writes.
18636
18746
  */
18637
18747
  useDummy: () => void;
18748
+ /**
18749
+ * Clears the memoized instance cache.
18750
+ * Call this when process.cwd() changes between strategy iterations
18751
+ * so new instances are created with the updated base path.
18752
+ */
18753
+ clear: () => void;
18638
18754
  /**
18639
18755
  * Releases resources held by this adapter.
18640
18756
  * Delegates to disable() to unsubscribe from signal lifecycle events.
@@ -18797,6 +18913,12 @@ declare class DumpAdapter {
18797
18913
  * @param Ctor - Constructor for the custom dump implementation
18798
18914
  */
18799
18915
  useDumpAdapter: (Ctor: TDumpInstanceCtor) => void;
18916
+ /**
18917
+ * Clears the memoized instance cache.
18918
+ * Call this when process.cwd() changes between strategy iterations
18919
+ * so new instances are created with the updated base path.
18920
+ */
18921
+ clear: () => void;
18800
18922
  }
18801
18923
  declare const Dump: DumpAdapter;
18802
18924
 
@@ -19037,7 +19159,10 @@ declare class CacheUtils {
19037
19159
  fn: <T extends Function, K = symbol>(run: T, context: {
19038
19160
  interval: CandleInterval;
19039
19161
  key?: (args: Parameters<T>) => K;
19040
- }) => T;
19162
+ }) => T & {
19163
+ clear(): void;
19164
+ gc(): number | undefined;
19165
+ };
19041
19166
  /**
19042
19167
  * Wrap an async function with persistent file-based caching.
19043
19168
  *
@@ -19080,90 +19205,34 @@ declare class CacheUtils {
19080
19205
  interval: CandleInterval;
19081
19206
  name: string;
19082
19207
  key?: (args: CacheFileKeyArgs<T>) => string;
19083
- }) => T;
19084
- /**
19085
- * Flush (remove) cached CacheInstance for a specific function or all functions.
19086
- *
19087
- * This method removes CacheInstance objects from the internal memoization cache.
19088
- * When a CacheInstance is flushed, all cached results across all contexts
19089
- * (all strategy/exchange/mode combinations) for that function are discarded.
19090
- *
19091
- * Use cases:
19092
- * - Remove specific function's CacheInstance when implementation changes
19093
- * - Free memory by removing unused CacheInstances
19094
- * - Reset all CacheInstances when switching between different test scenarios
19095
- *
19096
- * Note: This is different from `clear()` which only removes cached values
19097
- * for the current context within an existing CacheInstance.
19098
- *
19099
- * @template T - Function type
19100
- * @param run - Optional function to flush CacheInstance for. If omitted, flushes all CacheInstances.
19101
- *
19102
- * @example
19103
- * ```typescript
19104
- * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
19105
- *
19106
- * // Flush CacheInstance for specific function
19107
- * Cache.flush(calculateIndicator);
19108
- *
19109
- * // Flush all CacheInstances
19110
- * Cache.flush();
19111
- * ```
19112
- */
19113
- flush: <T extends Function>(run?: T) => void;
19208
+ }) => T & {
19209
+ clear(): void;
19210
+ };
19114
19211
  /**
19115
- * Clear cached value for current execution context of a specific function.
19116
- *
19117
- * Removes the cached entry for the current strategy/exchange/mode combination
19118
- * from the specified function's CacheInstance. The next call to the wrapped function
19119
- * will recompute the value for that context.
19212
+ * Dispose (remove) the memoized CacheInstance for a specific function.
19120
19213
  *
19121
- * This only clears the cache for the current execution context, not all contexts.
19122
- * Use `flush()` to remove the entire CacheInstance across all contexts.
19123
- *
19124
- * Requires active execution context (strategy, exchange, backtest mode) and method context.
19214
+ * Removes the CacheInstance from the internal memoization cache, discarding all cached
19215
+ * results across all contexts (all strategy/exchange/mode combinations) for that function.
19216
+ * The next call to the wrapped function will create a fresh CacheInstance.
19125
19217
  *
19126
19218
  * @template T - Function type
19127
- * @param run - Function whose cache should be cleared for current context
19219
+ * @param run - Function whose CacheInstance should be disposed.
19128
19220
  *
19129
19221
  * @example
19130
19222
  * ```typescript
19131
19223
  * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
19132
19224
  *
19133
- * // Within strategy execution context
19134
- * const result1 = cachedFn("BTCUSDT", 14); // Computed
19135
- * const result2 = cachedFn("BTCUSDT", 14); // Cached
19136
- *
19137
- * Cache.clear(calculateIndicator); // Clear cache for current context only
19138
- *
19139
- * const result3 = cachedFn("BTCUSDT", 14); // Recomputed for this context
19140
- * // Other contexts (different strategies/exchanges) remain cached
19225
+ * // Dispose CacheInstance for a specific function
19226
+ * Cache.dispose(calculateIndicator);
19141
19227
  * ```
19142
19228
  */
19143
- clear: <T extends Function>(run: T) => void;
19229
+ dispose: <T extends Function>(run: T) => void;
19144
19230
  /**
19145
- * Garbage collect expired cache entries for a specific function.
19146
- *
19147
- * Removes all cached entries whose interval has expired (not aligned with current time).
19148
- * Call this periodically to free memory from stale cache entries.
19149
- *
19150
- * Requires active execution context to get current time.
19151
- *
19152
- * @template T - Function type
19153
- * @param run - Function whose expired cache entries should be removed
19154
- * @returns Number of entries removed
19155
- *
19156
- * @example
19157
- * ```typescript
19158
- * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
19159
- *
19160
- * cachedFn("BTCUSDT", 14); // Cached at 10:00
19161
- * cachedFn("ETHUSDT", 14); // Cached at 10:00
19162
- * // Time passes to 11:00
19163
- * const removed = Cache.gc(calculateIndicator); // Returns 2
19164
- * ```
19231
+ * Clears all memoized CacheInstance and CacheFileInstance objects.
19232
+ * Call this when process.cwd() changes between strategy iterations
19233
+ * so new instances are created with the updated base path.
19165
19234
  */
19166
- gc: <T extends Function>(run: T) => number;
19235
+ clear: () => void;
19167
19236
  }
19168
19237
  /**
19169
19238
  * Singleton instance of CacheUtils for convenient function caching.
@@ -21389,6 +21458,12 @@ declare class BrokerAdapter {
21389
21458
  * ```
21390
21459
  */
21391
21460
  disable: () => void;
21461
+ /**
21462
+ * Clears the cached broker instance and resets the enable singleshot.
21463
+ * Call this when process.cwd() changes between strategy iterations
21464
+ * so a new broker instance is created with the updated base path.
21465
+ */
21466
+ clear: () => void;
21392
21467
  }
21393
21468
  /**
21394
21469
  * Base class for custom broker adapter implementations.