backtest-kit 1.5.36 → 1.5.37

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,7 +18426,8 @@ class ExchangeUtils {
18426
18426
  */
18427
18427
  const Exchange = new ExchangeUtils();
18428
18428
 
18429
- const CACHE_METHOD_NAME_CLEAR = "CacheUtils.clear";
18429
+ const CACHE_METHOD_NAME_FLUSH = "CacheUtils.flush";
18430
+ const CACHE_METHOD_NAME_CLEAR = "CacheInstance.clear";
18430
18431
  const CACHE_METHOD_NAME_RUN = "CacheInstance.run";
18431
18432
  const CACHE_METHOD_NAME_FN = "CacheUtils.fn";
18432
18433
  const INTERVAL_MINUTES = {
@@ -18506,14 +18507,16 @@ class CacheInstance {
18506
18507
  this.run = (...args) => {
18507
18508
  backtest$1.loggerService.debug(CACHE_METHOD_NAME_RUN, { args });
18508
18509
  const step = INTERVAL_MINUTES[this.interval];
18509
- if (!step) {
18510
- throw new Error(`CacheInstance unknown cache ttl interval=${this.interval}`);
18511
- }
18512
- if (!MethodContextService.hasContext()) {
18513
- throw new Error("CacheInstance run requires method context");
18514
- }
18515
- if (!ExecutionContextService.hasContext()) {
18516
- throw new Error("CacheInstance run requires execution context");
18510
+ {
18511
+ if (!MethodContextService.hasContext()) {
18512
+ throw new Error("CacheInstance run requires method context");
18513
+ }
18514
+ if (!ExecutionContextService.hasContext()) {
18515
+ throw new Error("CacheInstance run requires execution context");
18516
+ }
18517
+ if (!step) {
18518
+ throw new Error(`CacheInstance unknown cache ttl interval=${this.interval}`);
18519
+ }
18517
18520
  }
18518
18521
  const key = createKey(backtest$1.methodContextService.context.strategyName, backtest$1.methodContextService.context.exchangeName, backtest$1.executionContextService.context.backtest);
18519
18522
  const currentWhen = backtest$1.executionContextService.context.when;
@@ -18532,6 +18535,30 @@ class CacheInstance {
18532
18535
  this._cacheMap.set(key, newCache);
18533
18536
  return newCache;
18534
18537
  };
18538
+ /**
18539
+ * Clear cached value for current execution context.
18540
+ *
18541
+ * Removes the cached entry for the current strategy/exchange/mode combination
18542
+ * from this instance's cache map. The next `run()` call will recompute the value.
18543
+ *
18544
+ * Requires active execution context (strategy, exchange, backtest mode) and method context
18545
+ * to determine which cache entry to clear.
18546
+ *
18547
+ * @example
18548
+ * ```typescript
18549
+ * const instance = new CacheInstance(calculateIndicator, "1h");
18550
+ * const result1 = instance.run("BTCUSDT", 14); // Computed
18551
+ * const result2 = instance.run("BTCUSDT", 14); // Cached
18552
+ *
18553
+ * instance.clear(); // Clear cache for current context
18554
+ *
18555
+ * const result3 = instance.run("BTCUSDT", 14); // Recomputed
18556
+ * ```
18557
+ */
18558
+ this.clear = () => {
18559
+ const key = createKey(backtest$1.methodContextService.context.strategyName, backtest$1.methodContextService.context.exchangeName, backtest$1.executionContextService.context.backtest);
18560
+ this._cacheMap.delete(key);
18561
+ };
18535
18562
  }
18536
18563
  }
18537
18564
  /**
@@ -18580,45 +18607,85 @@ class CacheUtils {
18580
18607
  * ```
18581
18608
  */
18582
18609
  this.fn = (run, context) => {
18583
- backtest$1.loggerService.debug(CACHE_METHOD_NAME_FN, {
18610
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_FN, {
18584
18611
  context,
18585
18612
  });
18586
- return (...args) => {
18613
+ const wrappedFn = (...args) => {
18587
18614
  const instance = this._getInstance(run, context.interval);
18588
18615
  return instance.run(...args).value;
18589
18616
  };
18617
+ return wrappedFn;
18590
18618
  };
18591
18619
  /**
18592
- * Clear cached instances for specific function or all cached functions.
18620
+ * Flush (remove) cached CacheInstance for a specific function or all functions.
18593
18621
  *
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.
18622
+ * This method removes CacheInstance objects from the internal memoization cache.
18623
+ * When a CacheInstance is flushed, all cached results across all contexts
18624
+ * (all strategy/exchange/mode combinations) for that function are discarded.
18597
18625
  *
18598
18626
  * 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)
18627
+ * - Remove specific function's CacheInstance when implementation changes
18628
+ * - Free memory by removing unused CacheInstances
18629
+ * - Reset all CacheInstances when switching between different test scenarios
18630
+ *
18631
+ * Note: This is different from `clear()` which only removes cached values
18632
+ * for the current context within an existing CacheInstance.
18602
18633
  *
18603
- * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
18634
+ * @template T - Function type
18635
+ * @param run - Optional function to flush CacheInstance for. If omitted, flushes all CacheInstances.
18604
18636
  *
18605
18637
  * @example
18606
18638
  * ```typescript
18607
- * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
18639
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
18608
18640
  *
18609
- * // Clear cache for specific function
18610
- * Cache.clear(calculateIndicator);
18641
+ * // Flush CacheInstance for specific function
18642
+ * Cache.flush(calculateIndicator);
18611
18643
  *
18612
- * // Clear all cached instances
18613
- * Cache.clear();
18644
+ * // Flush all CacheInstances
18645
+ * Cache.flush();
18614
18646
  * ```
18615
18647
  */
18616
- this.clear = (run) => {
18617
- backtest$1.loggerService.debug(CACHE_METHOD_NAME_CLEAR, {
18648
+ this.flush = (run) => {
18649
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_FLUSH, {
18618
18650
  run,
18619
18651
  });
18620
18652
  this._getInstance.clear(run);
18621
18653
  };
18654
+ /**
18655
+ * Clear cached value for current execution context of a specific function.
18656
+ *
18657
+ * Removes the cached entry for the current strategy/exchange/mode combination
18658
+ * from the specified function's CacheInstance. The next call to the wrapped function
18659
+ * will recompute the value for that context.
18660
+ *
18661
+ * This only clears the cache for the current execution context, not all contexts.
18662
+ * Use `flush()` to remove the entire CacheInstance across all contexts.
18663
+ *
18664
+ * Requires active execution context (strategy, exchange, backtest mode) and method context.
18665
+ *
18666
+ * @template T - Function type
18667
+ * @param run - Function whose cache should be cleared for current context
18668
+ *
18669
+ * @example
18670
+ * ```typescript
18671
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
18672
+ *
18673
+ * // Within strategy execution context
18674
+ * const result1 = cachedFn("BTCUSDT", 14); // Computed
18675
+ * const result2 = cachedFn("BTCUSDT", 14); // Cached
18676
+ *
18677
+ * Cache.clear(calculateIndicator); // Clear cache for current context only
18678
+ *
18679
+ * const result3 = cachedFn("BTCUSDT", 14); // Recomputed for this context
18680
+ * // Other contexts (different strategies/exchanges) remain cached
18681
+ * ```
18682
+ */
18683
+ this.clear = (run) => {
18684
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_CLEAR, {
18685
+ run,
18686
+ });
18687
+ this._getInstance.get(run).clear();
18688
+ };
18622
18689
  }
18623
18690
  }
18624
18691
  /**
package/build/index.mjs CHANGED
@@ -18424,7 +18424,8 @@ class ExchangeUtils {
18424
18424
  */
18425
18425
  const Exchange = new ExchangeUtils();
18426
18426
 
18427
- const CACHE_METHOD_NAME_CLEAR = "CacheUtils.clear";
18427
+ const CACHE_METHOD_NAME_FLUSH = "CacheUtils.flush";
18428
+ const CACHE_METHOD_NAME_CLEAR = "CacheInstance.clear";
18428
18429
  const CACHE_METHOD_NAME_RUN = "CacheInstance.run";
18429
18430
  const CACHE_METHOD_NAME_FN = "CacheUtils.fn";
18430
18431
  const INTERVAL_MINUTES = {
@@ -18504,14 +18505,16 @@ class CacheInstance {
18504
18505
  this.run = (...args) => {
18505
18506
  backtest$1.loggerService.debug(CACHE_METHOD_NAME_RUN, { args });
18506
18507
  const step = INTERVAL_MINUTES[this.interval];
18507
- if (!step) {
18508
- throw new Error(`CacheInstance unknown cache ttl interval=${this.interval}`);
18509
- }
18510
- if (!MethodContextService.hasContext()) {
18511
- throw new Error("CacheInstance run requires method context");
18512
- }
18513
- if (!ExecutionContextService.hasContext()) {
18514
- throw new Error("CacheInstance run requires execution context");
18508
+ {
18509
+ if (!MethodContextService.hasContext()) {
18510
+ throw new Error("CacheInstance run requires method context");
18511
+ }
18512
+ if (!ExecutionContextService.hasContext()) {
18513
+ throw new Error("CacheInstance run requires execution context");
18514
+ }
18515
+ if (!step) {
18516
+ throw new Error(`CacheInstance unknown cache ttl interval=${this.interval}`);
18517
+ }
18515
18518
  }
18516
18519
  const key = createKey(backtest$1.methodContextService.context.strategyName, backtest$1.methodContextService.context.exchangeName, backtest$1.executionContextService.context.backtest);
18517
18520
  const currentWhen = backtest$1.executionContextService.context.when;
@@ -18530,6 +18533,30 @@ class CacheInstance {
18530
18533
  this._cacheMap.set(key, newCache);
18531
18534
  return newCache;
18532
18535
  };
18536
+ /**
18537
+ * Clear cached value for current execution context.
18538
+ *
18539
+ * Removes the cached entry for the current strategy/exchange/mode combination
18540
+ * from this instance's cache map. The next `run()` call will recompute the value.
18541
+ *
18542
+ * Requires active execution context (strategy, exchange, backtest mode) and method context
18543
+ * to determine which cache entry to clear.
18544
+ *
18545
+ * @example
18546
+ * ```typescript
18547
+ * const instance = new CacheInstance(calculateIndicator, "1h");
18548
+ * const result1 = instance.run("BTCUSDT", 14); // Computed
18549
+ * const result2 = instance.run("BTCUSDT", 14); // Cached
18550
+ *
18551
+ * instance.clear(); // Clear cache for current context
18552
+ *
18553
+ * const result3 = instance.run("BTCUSDT", 14); // Recomputed
18554
+ * ```
18555
+ */
18556
+ this.clear = () => {
18557
+ const key = createKey(backtest$1.methodContextService.context.strategyName, backtest$1.methodContextService.context.exchangeName, backtest$1.executionContextService.context.backtest);
18558
+ this._cacheMap.delete(key);
18559
+ };
18533
18560
  }
18534
18561
  }
18535
18562
  /**
@@ -18578,45 +18605,85 @@ class CacheUtils {
18578
18605
  * ```
18579
18606
  */
18580
18607
  this.fn = (run, context) => {
18581
- backtest$1.loggerService.debug(CACHE_METHOD_NAME_FN, {
18608
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_FN, {
18582
18609
  context,
18583
18610
  });
18584
- return (...args) => {
18611
+ const wrappedFn = (...args) => {
18585
18612
  const instance = this._getInstance(run, context.interval);
18586
18613
  return instance.run(...args).value;
18587
18614
  };
18615
+ return wrappedFn;
18588
18616
  };
18589
18617
  /**
18590
- * Clear cached instances for specific function or all cached functions.
18618
+ * Flush (remove) cached CacheInstance for a specific function or all functions.
18591
18619
  *
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.
18620
+ * This method removes CacheInstance objects from the internal memoization cache.
18621
+ * When a CacheInstance is flushed, all cached results across all contexts
18622
+ * (all strategy/exchange/mode combinations) for that function are discarded.
18595
18623
  *
18596
18624
  * 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)
18625
+ * - Remove specific function's CacheInstance when implementation changes
18626
+ * - Free memory by removing unused CacheInstances
18627
+ * - Reset all CacheInstances when switching between different test scenarios
18628
+ *
18629
+ * Note: This is different from `clear()` which only removes cached values
18630
+ * for the current context within an existing CacheInstance.
18600
18631
  *
18601
- * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
18632
+ * @template T - Function type
18633
+ * @param run - Optional function to flush CacheInstance for. If omitted, flushes all CacheInstances.
18602
18634
  *
18603
18635
  * @example
18604
18636
  * ```typescript
18605
- * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
18637
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
18606
18638
  *
18607
- * // Clear cache for specific function
18608
- * Cache.clear(calculateIndicator);
18639
+ * // Flush CacheInstance for specific function
18640
+ * Cache.flush(calculateIndicator);
18609
18641
  *
18610
- * // Clear all cached instances
18611
- * Cache.clear();
18642
+ * // Flush all CacheInstances
18643
+ * Cache.flush();
18612
18644
  * ```
18613
18645
  */
18614
- this.clear = (run) => {
18615
- backtest$1.loggerService.debug(CACHE_METHOD_NAME_CLEAR, {
18646
+ this.flush = (run) => {
18647
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_FLUSH, {
18616
18648
  run,
18617
18649
  });
18618
18650
  this._getInstance.clear(run);
18619
18651
  };
18652
+ /**
18653
+ * Clear cached value for current execution context of a specific function.
18654
+ *
18655
+ * Removes the cached entry for the current strategy/exchange/mode combination
18656
+ * from the specified function's CacheInstance. The next call to the wrapped function
18657
+ * will recompute the value for that context.
18658
+ *
18659
+ * This only clears the cache for the current execution context, not all contexts.
18660
+ * Use `flush()` to remove the entire CacheInstance across all contexts.
18661
+ *
18662
+ * Requires active execution context (strategy, exchange, backtest mode) and method context.
18663
+ *
18664
+ * @template T - Function type
18665
+ * @param run - Function whose cache should be cleared for current context
18666
+ *
18667
+ * @example
18668
+ * ```typescript
18669
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
18670
+ *
18671
+ * // Within strategy execution context
18672
+ * const result1 = cachedFn("BTCUSDT", 14); // Computed
18673
+ * const result2 = cachedFn("BTCUSDT", 14); // Cached
18674
+ *
18675
+ * Cache.clear(calculateIndicator); // Clear cache for current context only
18676
+ *
18677
+ * const result3 = cachedFn("BTCUSDT", 14); // Recomputed for this context
18678
+ * // Other contexts (different strategies/exchanges) remain cached
18679
+ * ```
18680
+ */
18681
+ this.clear = (run) => {
18682
+ backtest$1.loggerService.info(CACHE_METHOD_NAME_CLEAR, {
18683
+ run,
18684
+ });
18685
+ this._getInstance.get(run).clear();
18686
+ };
18620
18687
  }
18621
18688
  }
18622
18689
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backtest-kit",
3
- "version": "1.5.36",
3
+ "version": "1.5.37",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -7865,33 +7865,67 @@ declare class CacheUtils {
7865
7865
  */
7866
7866
  fn: <T extends Function>(run: T, context: {
7867
7867
  interval: CandleInterval;
7868
- }) => Function;
7868
+ }) => T;
7869
7869
  /**
7870
- * Clear cached instances for specific function or all cached functions.
7870
+ * Flush (remove) cached CacheInstance for a specific function or all functions.
7871
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.
7872
+ * This method removes CacheInstance objects from the internal memoization cache.
7873
+ * When a CacheInstance is flushed, all cached results across all contexts
7874
+ * (all strategy/exchange/mode combinations) for that function are discarded.
7875
7875
  *
7876
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)
7877
+ * - Remove specific function's CacheInstance when implementation changes
7878
+ * - Free memory by removing unused CacheInstances
7879
+ * - Reset all CacheInstances when switching between different test scenarios
7880
7880
  *
7881
- * @param run - Optional function to clear cache for. If omitted, clears all cached instances.
7881
+ * Note: This is different from `clear()` which only removes cached values
7882
+ * for the current context within an existing CacheInstance.
7883
+ *
7884
+ * @template T - Function type
7885
+ * @param run - Optional function to flush CacheInstance for. If omitted, flushes all CacheInstances.
7886
+ *
7887
+ * @example
7888
+ * ```typescript
7889
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
7890
+ *
7891
+ * // Flush CacheInstance for specific function
7892
+ * Cache.flush(calculateIndicator);
7893
+ *
7894
+ * // Flush all CacheInstances
7895
+ * Cache.flush();
7896
+ * ```
7897
+ */
7898
+ flush: <T extends Function>(run?: T) => void;
7899
+ /**
7900
+ * Clear cached value for current execution context of a specific function.
7901
+ *
7902
+ * Removes the cached entry for the current strategy/exchange/mode combination
7903
+ * from the specified function's CacheInstance. The next call to the wrapped function
7904
+ * will recompute the value for that context.
7905
+ *
7906
+ * This only clears the cache for the current execution context, not all contexts.
7907
+ * Use `flush()` to remove the entire CacheInstance across all contexts.
7908
+ *
7909
+ * Requires active execution context (strategy, exchange, backtest mode) and method context.
7910
+ *
7911
+ * @template T - Function type
7912
+ * @param run - Function whose cache should be cleared for current context
7882
7913
  *
7883
7914
  * @example
7884
7915
  * ```typescript
7885
- * const cachedCalc = Cache.fn(calculateIndicator, { interval: "1h" });
7916
+ * const cachedFn = Cache.fn(calculateIndicator, { interval: "1h" });
7917
+ *
7918
+ * // Within strategy execution context
7919
+ * const result1 = cachedFn("BTCUSDT", 14); // Computed
7920
+ * const result2 = cachedFn("BTCUSDT", 14); // Cached
7886
7921
  *
7887
- * // Clear cache for specific function
7888
- * Cache.clear(calculateIndicator);
7922
+ * Cache.clear(calculateIndicator); // Clear cache for current context only
7889
7923
  *
7890
- * // Clear all cached instances
7891
- * Cache.clear();
7924
+ * const result3 = cachedFn("BTCUSDT", 14); // Recomputed for this context
7925
+ * // Other contexts (different strategies/exchanges) remain cached
7892
7926
  * ```
7893
7927
  */
7894
- clear: <T extends Function>(run?: T) => void;
7928
+ clear: <T extends Function>(run: T) => void;
7895
7929
  }
7896
7930
  /**
7897
7931
  * Singleton instance of CacheUtils for convenient function caching.