backtest-kit 1.4.1 → 1.4.2

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
@@ -13571,7 +13571,89 @@ class PartialUtils {
13571
13571
  */
13572
13572
  const Partial = new PartialUtils();
13573
13573
 
13574
+ /**
13575
+ * Utility class containing predefined trading constants for take-profit and stop-loss levels.
13576
+ *
13577
+ * Based on Kelly Criterion with exponential risk decay.
13578
+ * Values represent percentage of distance traveled towards final TP/SL target.
13579
+ *
13580
+ * Example: If final TP is at +10% profit:
13581
+ * - TP_LEVEL1 (30) triggers when price reaches 30% of distance = +3% profit
13582
+ * - TP_LEVEL2 (60) triggers when price reaches 60% of distance = +6% profit
13583
+ * - TP_LEVEL3 (90) triggers when price reaches 90% of distance = +9% profit
13584
+ */
13585
+ class ConstantUtils {
13586
+ constructor() {
13587
+ /**
13588
+ * Take Profit Level 1 (Kelly-optimized early partial).
13589
+ * Triggers at 30% of distance to final TP target.
13590
+ * Lock in profit early, let rest run.
13591
+ */
13592
+ this.TP_LEVEL1 = 30;
13593
+ /**
13594
+ * Take Profit Level 2 (Kelly-optimized mid partial).
13595
+ * Triggers at 60% of distance to final TP target.
13596
+ * Secure majority of position while trend continues.
13597
+ */
13598
+ this.TP_LEVEL2 = 60;
13599
+ /**
13600
+ * Take Profit Level 3 (Kelly-optimized final partial).
13601
+ * Triggers at 90% of distance to final TP target.
13602
+ * Near-complete exit, minimal exposure remains.
13603
+ */
13604
+ this.TP_LEVEL3 = 90;
13605
+ /**
13606
+ * Stop Loss Level 1 (Kelly-optimized early warning).
13607
+ * Triggers at 40% of distance to final SL target.
13608
+ * Reduce exposure when setup weakens.
13609
+ */
13610
+ this.SL_LEVEL1 = 40;
13611
+ /**
13612
+ * Stop Loss Level 2 (Kelly-optimized final exit).
13613
+ * Triggers at 80% of distance to final SL target.
13614
+ * Exit remaining position before catastrophic loss.
13615
+ */
13616
+ this.SL_LEVEL2 = 80;
13617
+ }
13618
+ }
13619
+ /**
13620
+ * Global singleton instance of ConstantUtils.
13621
+ * Provides static-like access to predefined trading level constants.
13622
+ *
13623
+ * Kelly-optimized scaling strategy:
13624
+ * Profit side (pyramiding out):
13625
+ * - Close 33% at 30% progress (quick profit lock)
13626
+ * - Close 33% at 60% progress (secure gains)
13627
+ * - Close 34% at 90% progress (exit near target)
13628
+ *
13629
+ * Loss side (damage control):
13630
+ * - Close 50% at 40% progress (reduce risk early)
13631
+ * - Close 50% at 80% progress (exit before full stop)
13632
+ *
13633
+ * @example
13634
+ * ```typescript
13635
+ * // Final targets: TP at +10%, SL at -5%
13636
+ * listenPartialProfit(async (event) => {
13637
+ * // event.level emits: 10, 20, 30, 40, 50...
13638
+ * if (event.level === Constant.TP_LEVEL1) { await close(33); } // at +3% profit
13639
+ * if (event.level === Constant.TP_LEVEL2) { await close(33); } // at +6% profit
13640
+ * if (event.level === Constant.TP_LEVEL3) { await close(34); } // at +9% profit
13641
+ * });
13642
+ * ```
13643
+ *
13644
+ * @example
13645
+ * ```typescript
13646
+ * listenPartialLoss(async (event) => {
13647
+ * // event.level emits: 10, 20, 30, 40, 50...
13648
+ * if (event.level === Constant.SL_LEVEL1) { await close(50); } // at -2% loss
13649
+ * if (event.level === Constant.SL_LEVEL2) { await close(50); } // at -4% loss
13650
+ * });
13651
+ * ```
13652
+ */
13653
+ const Constant = new ConstantUtils();
13654
+
13574
13655
  exports.Backtest = Backtest;
13656
+ exports.Constant = Constant;
13575
13657
  exports.ExecutionContextService = ExecutionContextService;
13576
13658
  exports.Heat = Heat;
13577
13659
  exports.Live = Live;
package/build/index.mjs CHANGED
@@ -13569,4 +13569,85 @@ class PartialUtils {
13569
13569
  */
13570
13570
  const Partial = new PartialUtils();
13571
13571
 
13572
- export { Backtest, ExecutionContextService, Heat, Live, MethodContextService, Optimizer, Partial, Performance, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, Schedule, Walker, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, setConfig, setLogger };
13572
+ /**
13573
+ * Utility class containing predefined trading constants for take-profit and stop-loss levels.
13574
+ *
13575
+ * Based on Kelly Criterion with exponential risk decay.
13576
+ * Values represent percentage of distance traveled towards final TP/SL target.
13577
+ *
13578
+ * Example: If final TP is at +10% profit:
13579
+ * - TP_LEVEL1 (30) triggers when price reaches 30% of distance = +3% profit
13580
+ * - TP_LEVEL2 (60) triggers when price reaches 60% of distance = +6% profit
13581
+ * - TP_LEVEL3 (90) triggers when price reaches 90% of distance = +9% profit
13582
+ */
13583
+ class ConstantUtils {
13584
+ constructor() {
13585
+ /**
13586
+ * Take Profit Level 1 (Kelly-optimized early partial).
13587
+ * Triggers at 30% of distance to final TP target.
13588
+ * Lock in profit early, let rest run.
13589
+ */
13590
+ this.TP_LEVEL1 = 30;
13591
+ /**
13592
+ * Take Profit Level 2 (Kelly-optimized mid partial).
13593
+ * Triggers at 60% of distance to final TP target.
13594
+ * Secure majority of position while trend continues.
13595
+ */
13596
+ this.TP_LEVEL2 = 60;
13597
+ /**
13598
+ * Take Profit Level 3 (Kelly-optimized final partial).
13599
+ * Triggers at 90% of distance to final TP target.
13600
+ * Near-complete exit, minimal exposure remains.
13601
+ */
13602
+ this.TP_LEVEL3 = 90;
13603
+ /**
13604
+ * Stop Loss Level 1 (Kelly-optimized early warning).
13605
+ * Triggers at 40% of distance to final SL target.
13606
+ * Reduce exposure when setup weakens.
13607
+ */
13608
+ this.SL_LEVEL1 = 40;
13609
+ /**
13610
+ * Stop Loss Level 2 (Kelly-optimized final exit).
13611
+ * Triggers at 80% of distance to final SL target.
13612
+ * Exit remaining position before catastrophic loss.
13613
+ */
13614
+ this.SL_LEVEL2 = 80;
13615
+ }
13616
+ }
13617
+ /**
13618
+ * Global singleton instance of ConstantUtils.
13619
+ * Provides static-like access to predefined trading level constants.
13620
+ *
13621
+ * Kelly-optimized scaling strategy:
13622
+ * Profit side (pyramiding out):
13623
+ * - Close 33% at 30% progress (quick profit lock)
13624
+ * - Close 33% at 60% progress (secure gains)
13625
+ * - Close 34% at 90% progress (exit near target)
13626
+ *
13627
+ * Loss side (damage control):
13628
+ * - Close 50% at 40% progress (reduce risk early)
13629
+ * - Close 50% at 80% progress (exit before full stop)
13630
+ *
13631
+ * @example
13632
+ * ```typescript
13633
+ * // Final targets: TP at +10%, SL at -5%
13634
+ * listenPartialProfit(async (event) => {
13635
+ * // event.level emits: 10, 20, 30, 40, 50...
13636
+ * if (event.level === Constant.TP_LEVEL1) { await close(33); } // at +3% profit
13637
+ * if (event.level === Constant.TP_LEVEL2) { await close(33); } // at +6% profit
13638
+ * if (event.level === Constant.TP_LEVEL3) { await close(34); } // at +9% profit
13639
+ * });
13640
+ * ```
13641
+ *
13642
+ * @example
13643
+ * ```typescript
13644
+ * listenPartialLoss(async (event) => {
13645
+ * // event.level emits: 10, 20, 30, 40, 50...
13646
+ * if (event.level === Constant.SL_LEVEL1) { await close(50); } // at -2% loss
13647
+ * if (event.level === Constant.SL_LEVEL2) { await close(50); } // at -4% loss
13648
+ * });
13649
+ * ```
13650
+ */
13651
+ const Constant = new ConstantUtils();
13652
+
13653
+ export { Backtest, Constant, ExecutionContextService, Heat, Live, MethodContextService, Optimizer, Partial, Performance, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, Schedule, Walker, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, setConfig, setLogger };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backtest-kit",
3
- "version": "1.4.1",
3
+ "version": "1.4.2",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -5975,6 +5975,85 @@ declare class PartialUtils {
5975
5975
  */
5976
5976
  declare const Partial$1: PartialUtils;
5977
5977
 
5978
+ /**
5979
+ * Utility class containing predefined trading constants for take-profit and stop-loss levels.
5980
+ *
5981
+ * Based on Kelly Criterion with exponential risk decay.
5982
+ * Values represent percentage of distance traveled towards final TP/SL target.
5983
+ *
5984
+ * Example: If final TP is at +10% profit:
5985
+ * - TP_LEVEL1 (30) triggers when price reaches 30% of distance = +3% profit
5986
+ * - TP_LEVEL2 (60) triggers when price reaches 60% of distance = +6% profit
5987
+ * - TP_LEVEL3 (90) triggers when price reaches 90% of distance = +9% profit
5988
+ */
5989
+ declare class ConstantUtils {
5990
+ /**
5991
+ * Take Profit Level 1 (Kelly-optimized early partial).
5992
+ * Triggers at 30% of distance to final TP target.
5993
+ * Lock in profit early, let rest run.
5994
+ */
5995
+ readonly TP_LEVEL1 = 30;
5996
+ /**
5997
+ * Take Profit Level 2 (Kelly-optimized mid partial).
5998
+ * Triggers at 60% of distance to final TP target.
5999
+ * Secure majority of position while trend continues.
6000
+ */
6001
+ readonly TP_LEVEL2 = 60;
6002
+ /**
6003
+ * Take Profit Level 3 (Kelly-optimized final partial).
6004
+ * Triggers at 90% of distance to final TP target.
6005
+ * Near-complete exit, minimal exposure remains.
6006
+ */
6007
+ readonly TP_LEVEL3 = 90;
6008
+ /**
6009
+ * Stop Loss Level 1 (Kelly-optimized early warning).
6010
+ * Triggers at 40% of distance to final SL target.
6011
+ * Reduce exposure when setup weakens.
6012
+ */
6013
+ readonly SL_LEVEL1 = 40;
6014
+ /**
6015
+ * Stop Loss Level 2 (Kelly-optimized final exit).
6016
+ * Triggers at 80% of distance to final SL target.
6017
+ * Exit remaining position before catastrophic loss.
6018
+ */
6019
+ readonly SL_LEVEL2 = 80;
6020
+ }
6021
+ /**
6022
+ * Global singleton instance of ConstantUtils.
6023
+ * Provides static-like access to predefined trading level constants.
6024
+ *
6025
+ * Kelly-optimized scaling strategy:
6026
+ * Profit side (pyramiding out):
6027
+ * - Close 33% at 30% progress (quick profit lock)
6028
+ * - Close 33% at 60% progress (secure gains)
6029
+ * - Close 34% at 90% progress (exit near target)
6030
+ *
6031
+ * Loss side (damage control):
6032
+ * - Close 50% at 40% progress (reduce risk early)
6033
+ * - Close 50% at 80% progress (exit before full stop)
6034
+ *
6035
+ * @example
6036
+ * ```typescript
6037
+ * // Final targets: TP at +10%, SL at -5%
6038
+ * listenPartialProfit(async (event) => {
6039
+ * // event.level emits: 10, 20, 30, 40, 50...
6040
+ * if (event.level === Constant.TP_LEVEL1) { await close(33); } // at +3% profit
6041
+ * if (event.level === Constant.TP_LEVEL2) { await close(33); } // at +6% profit
6042
+ * if (event.level === Constant.TP_LEVEL3) { await close(34); } // at +9% profit
6043
+ * });
6044
+ * ```
6045
+ *
6046
+ * @example
6047
+ * ```typescript
6048
+ * listenPartialLoss(async (event) => {
6049
+ * // event.level emits: 10, 20, 30, 40, 50...
6050
+ * if (event.level === Constant.SL_LEVEL1) { await close(50); } // at -2% loss
6051
+ * if (event.level === Constant.SL_LEVEL2) { await close(50); } // at -4% loss
6052
+ * });
6053
+ * ```
6054
+ */
6055
+ declare const Constant: ConstantUtils;
6056
+
5978
6057
  /**
5979
6058
  * Global signal emitter for all trading events (live + backtest).
5980
6059
  * Emits all signal events regardless of execution mode.
@@ -8540,4 +8619,4 @@ declare const backtest: {
8540
8619
  loggerService: LoggerService;
8541
8620
  };
8542
8621
 
8543
- export { Backtest, type BacktestStatistics, type CandleInterval, type DoneContract, type EntityId, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type ICandleData, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, type IHeatmapStatistics, type IOptimizerCallbacks, type IOptimizerData, type IOptimizerFetchArgs, type IOptimizerFilterArgs, type IOptimizerRange, type IOptimizerSchema, type IOptimizerSource, type IOptimizerStrategy, type IOptimizerTemplate, type IPersistBase, type IPositionSizeATRParams, type IPositionSizeFixedPercentageParams, type IPositionSizeKellyParams, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalRow, type ISignalDto, type ISignalRow, type ISizingCalculateParams, type ISizingCalculateParamsATR, type ISizingCalculateParamsFixedPercentage, type ISizingCalculateParamsKelly, type ISizingSchema, type ISizingSchemaATR, type ISizingSchemaFixedPercentage, type ISizingSchemaKelly, type IStrategyPnL, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, Live, type LiveStatistics, type MessageModel, type MessageRole, MethodContextService, Optimizer, Partial$1 as Partial, type PartialData, type PartialLossContract, type PartialProfitContract, type PartialStatistics, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatistics, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, type ProgressBacktestContract, type ProgressWalkerContract, type RiskData, Schedule, type ScheduleData, type ScheduleStatistics, type SignalData, type SignalInterval, type TPersistBase, type TPersistBaseCtor, Walker, type WalkerContract, type WalkerMetric, type WalkerStatistics, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, setConfig, setLogger };
8622
+ export { Backtest, type BacktestStatistics, type CandleInterval, Constant, type DoneContract, type EntityId, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type ICandleData, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, type IHeatmapStatistics, type IOptimizerCallbacks, type IOptimizerData, type IOptimizerFetchArgs, type IOptimizerFilterArgs, type IOptimizerRange, type IOptimizerSchema, type IOptimizerSource, type IOptimizerStrategy, type IOptimizerTemplate, type IPersistBase, type IPositionSizeATRParams, type IPositionSizeFixedPercentageParams, type IPositionSizeKellyParams, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalRow, type ISignalDto, type ISignalRow, type ISizingCalculateParams, type ISizingCalculateParamsATR, type ISizingCalculateParamsFixedPercentage, type ISizingCalculateParamsKelly, type ISizingSchema, type ISizingSchemaATR, type ISizingSchemaFixedPercentage, type ISizingSchemaKelly, type IStrategyPnL, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, Live, type LiveStatistics, type MessageModel, type MessageRole, MethodContextService, Optimizer, Partial$1 as Partial, type PartialData, type PartialLossContract, type PartialProfitContract, type PartialStatistics, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatistics, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, type ProgressBacktestContract, type ProgressWalkerContract, type RiskData, Schedule, type ScheduleData, type ScheduleStatistics, type SignalData, type SignalInterval, type TPersistBase, type TPersistBaseCtor, Walker, type WalkerContract, type WalkerMetric, type WalkerStatistics, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, setConfig, setLogger };