backtest-kit 1.5.34 → 1.5.35

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/build/index.cjs CHANGED
@@ -18426,6 +18426,7 @@ class ExchangeUtils {
18426
18426
  */
18427
18427
  const Exchange = new ExchangeUtils();
18428
18428
 
18429
+ const CACHE_METHOD_NAME_CLEAR = "CacheUtils.clear";
18429
18430
  const CACHE_METHOD_NAME_RUN = "CacheInstance.run";
18430
18431
  const CACHE_METHOD_NAME_FN = "CacheUtils.fn";
18431
18432
  const INTERVAL_MINUTES = {
@@ -18474,12 +18475,24 @@ class CacheInstance {
18474
18475
  /**
18475
18476
  * Execute function with caching based on timeframe intervals.
18476
18477
  *
18477
- * Caches function results and invalidates when execution time moves to a different
18478
- * interval boundary. Uses execution context (strategy, exchange, backtest mode) and
18479
- * method context for cache key generation.
18478
+ * This method implements intelligent time-based caching:
18479
+ * 1. Generates cache key from strategy name, exchange name, and execution mode (backtest/live)
18480
+ * 2. Checks if cached value exists and is still valid for current interval
18481
+ * 3. Returns cached value if time elapsed is less than interval duration
18482
+ * 4. Recomputes and caches new value when moving to next interval boundary
18483
+ *
18484
+ * Cache invalidation example with 15m interval:
18485
+ * - 10:00 AM: First call → computes and caches result
18486
+ * - 10:05 AM: Same interval → returns cached result
18487
+ * - 10:15 AM: New interval → recomputes and caches new result
18488
+ *
18489
+ * Requires active execution context (strategy, exchange, backtest mode) and method context.
18490
+ * Each unique combination of these contexts maintains separate cache entries.
18480
18491
  *
18481
18492
  * @param args - Arguments to pass to the cached function
18482
- * @returns Cached result containing value and timestamp
18493
+ * @returns Cached result object containing:
18494
+ * - `value`: The computed or cached function result
18495
+ * - `when`: Timestamp when this value was cached
18483
18496
  * @throws Error if interval is unknown or required context is missing
18484
18497
  *
18485
18498
  * @example
@@ -18575,6 +18588,37 @@ class CacheUtils {
18575
18588
  return instance.run(...args).value;
18576
18589
  };
18577
18590
  };
18591
+ /**
18592
+ * Clear cached instances for specific function or all cached functions.
18593
+ *
18594
+ * This method delegates to the memoized `_getInstance` function's clear method,
18595
+ * which removes cached CacheInstance objects. When a CacheInstance is removed,
18596
+ * all cached function results for that instance are also discarded.
18597
+ *
18598
+ * Use cases:
18599
+ * - Clear cache for a specific function when its implementation changes
18600
+ * - Free memory by removing unused cached instances
18601
+ * - Reset all caches when switching contexts (e.g., between different backtests)
18602
+ *
18603
+ * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
18604
+ *
18605
+ * @example
18606
+ * ```typescript
18607
+ * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
18608
+ *
18609
+ * // Clear cache for specific function
18610
+ * Cache.clear(calculateIndicator);
18611
+ *
18612
+ * // Clear all cached instances
18613
+ * Cache.clear();
18614
+ * ```
18615
+ */
18616
+ this.clear = (run) => {
18617
+ backtest$1.loggerService.debug(CACHE_METHOD_NAME_CLEAR, {
18618
+ run,
18619
+ });
18620
+ this._getInstance.clear(run);
18621
+ };
18578
18622
  }
18579
18623
  }
18580
18624
  /**
package/build/index.mjs CHANGED
@@ -18424,6 +18424,7 @@ class ExchangeUtils {
18424
18424
  */
18425
18425
  const Exchange = new ExchangeUtils();
18426
18426
 
18427
+ const CACHE_METHOD_NAME_CLEAR = "CacheUtils.clear";
18427
18428
  const CACHE_METHOD_NAME_RUN = "CacheInstance.run";
18428
18429
  const CACHE_METHOD_NAME_FN = "CacheUtils.fn";
18429
18430
  const INTERVAL_MINUTES = {
@@ -18472,12 +18473,24 @@ class CacheInstance {
18472
18473
  /**
18473
18474
  * Execute function with caching based on timeframe intervals.
18474
18475
  *
18475
- * Caches function results and invalidates when execution time moves to a different
18476
- * interval boundary. Uses execution context (strategy, exchange, backtest mode) and
18477
- * method context for cache key generation.
18476
+ * This method implements intelligent time-based caching:
18477
+ * 1. Generates cache key from strategy name, exchange name, and execution mode (backtest/live)
18478
+ * 2. Checks if cached value exists and is still valid for current interval
18479
+ * 3. Returns cached value if time elapsed is less than interval duration
18480
+ * 4. Recomputes and caches new value when moving to next interval boundary
18481
+ *
18482
+ * Cache invalidation example with 15m interval:
18483
+ * - 10:00 AM: First call → computes and caches result
18484
+ * - 10:05 AM: Same interval → returns cached result
18485
+ * - 10:15 AM: New interval → recomputes and caches new result
18486
+ *
18487
+ * Requires active execution context (strategy, exchange, backtest mode) and method context.
18488
+ * Each unique combination of these contexts maintains separate cache entries.
18478
18489
  *
18479
18490
  * @param args - Arguments to pass to the cached function
18480
- * @returns Cached result containing value and timestamp
18491
+ * @returns Cached result object containing:
18492
+ * - `value`: The computed or cached function result
18493
+ * - `when`: Timestamp when this value was cached
18481
18494
  * @throws Error if interval is unknown or required context is missing
18482
18495
  *
18483
18496
  * @example
@@ -18573,6 +18586,37 @@ class CacheUtils {
18573
18586
  return instance.run(...args).value;
18574
18587
  };
18575
18588
  };
18589
+ /**
18590
+ * Clear cached instances for specific function or all cached functions.
18591
+ *
18592
+ * This method delegates to the memoized `_getInstance` function's clear method,
18593
+ * which removes cached CacheInstance objects. When a CacheInstance is removed,
18594
+ * all cached function results for that instance are also discarded.
18595
+ *
18596
+ * Use cases:
18597
+ * - Clear cache for a specific function when its implementation changes
18598
+ * - Free memory by removing unused cached instances
18599
+ * - Reset all caches when switching contexts (e.g., between different backtests)
18600
+ *
18601
+ * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
18602
+ *
18603
+ * @example
18604
+ * ```typescript
18605
+ * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
18606
+ *
18607
+ * // Clear cache for specific function
18608
+ * Cache.clear(calculateIndicator);
18609
+ *
18610
+ * // Clear all cached instances
18611
+ * Cache.clear();
18612
+ * ```
18613
+ */
18614
+ this.clear = (run) => {
18615
+ backtest$1.loggerService.debug(CACHE_METHOD_NAME_CLEAR, {
18616
+ run,
18617
+ });
18618
+ this._getInstance.clear(run);
18619
+ };
18576
18620
  }
18577
18621
  }
18578
18622
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backtest-kit",
3
- "version": "1.5.34",
3
+ "version": "1.5.35",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -7866,6 +7866,32 @@ declare class CacheUtils {
7866
7866
  fn: <T extends Function>(run: T, context: {
7867
7867
  interval: CandleInterval;
7868
7868
  }) => Function;
7869
+ /**
7870
+ * Clear cached instances for specific function or all cached functions.
7871
+ *
7872
+ * This method delegates to the memoized `_getInstance` function's clear method,
7873
+ * which removes cached CacheInstance objects. When a CacheInstance is removed,
7874
+ * all cached function results for that instance are also discarded.
7875
+ *
7876
+ * Use cases:
7877
+ * - Clear cache for a specific function when its implementation changes
7878
+ * - Free memory by removing unused cached instances
7879
+ * - Reset all caches when switching contexts (e.g., between different backtests)
7880
+ *
7881
+ * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
7882
+ *
7883
+ * @example
7884
+ * ```typescript
7885
+ * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
7886
+ *
7887
+ * // Clear cache for specific function
7888
+ * Cache.clear(calculateIndicator);
7889
+ *
7890
+ * // Clear all cached instances
7891
+ * Cache.clear();
7892
+ * ```
7893
+ */
7894
+ clear: <T extends Function>(run?: T) => void;
7869
7895
  }
7870
7896
  /**
7871
7897
  * Singleton instance of CacheUtils for convenient function caching.