backtest-kit 1.10.2 → 1.10.3

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": "1.10.2",
3
+ "version": "1.10.3",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -228,6 +228,30 @@ declare function partialLoss(symbol: string, percentToClose: number): Promise<vo
228
228
  * ```
229
229
  */
230
230
  declare function trailingStop(symbol: string, percentShift: number, currentPrice: number): Promise<void>;
231
+ /**
232
+ * Adjusts the trailing take-profit distance for an active pending signal.
233
+ *
234
+ * Updates the take-profit distance by a percentage adjustment relative to the original TP distance.
235
+ * Negative percentShift brings TP closer to entry, positive percentShift moves it further.
236
+ * Once direction is set on first call, subsequent calls must continue in same direction.
237
+ *
238
+ * Automatically detects backtest/live mode from execution context.
239
+ *
240
+ * @param symbol - Trading pair symbol
241
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
242
+ * @param currentPrice - Current market price to check for intrusion
243
+ * @returns Promise that resolves when trailing TP is updated
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * import { trailingProfit } from "backtest-kit";
248
+ *
249
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
250
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
251
+ * await trailingProfit("BTCUSDT", 50, 102);
252
+ * ```
253
+ */
254
+ declare function trailingProfit(symbol: string, percentShift: number, currentPrice: number): Promise<void>;
231
255
  /**
232
256
  * Moves stop-loss to breakeven when price reaches threshold.
233
257
  *
@@ -1097,6 +1121,17 @@ interface ISignalRow extends ISignalDto {
1097
1121
  * Original priceStopLoss is preserved in persistence but ignored during execution.
1098
1122
  */
1099
1123
  _trailingPriceStopLoss?: number;
1124
+ /**
1125
+ * Trailing take-profit price that overrides priceTakeProfit when set.
1126
+ * Created and managed by trailingProfit() method for dynamic TP adjustment.
1127
+ * Allows moving TP further from or closer to current price based on strategy.
1128
+ * Updated by trailingProfit() method based on position type and percentage distance.
1129
+ * - For LONG: can move upward (further) or downward (closer) from entry
1130
+ * - For SHORT: can move downward (further) or upward (closer) from entry
1131
+ * When _trailingPriceTakeProfit is set, it replaces priceTakeProfit for TP/SL checks.
1132
+ * Original priceTakeProfit is preserved in persistence but ignored during execution.
1133
+ */
1134
+ _trailingPriceTakeProfit?: number;
1100
1135
  }
1101
1136
  /**
1102
1137
  * Scheduled signal row for delayed entry at specific price.
@@ -1109,13 +1144,13 @@ interface IScheduledSignalRow extends ISignalRow {
1109
1144
  priceOpen: number;
1110
1145
  }
1111
1146
  /**
1112
- * Public signal row with original stop-loss price.
1113
- * Extends ISignalRow to include originalPriceStopLoss for external visibility.
1114
- * Used in public APIs to show user the original SL even if trailing SL is active.
1115
- * This allows users to see both the current effective SL and the original SL set at signal creation.
1116
- * The originalPriceStopLoss remains unchanged even if _trailingPriceStopLoss modifies the effective SL.
1147
+ * Public signal row with original stop-loss and take-profit prices.
1148
+ * Extends ISignalRow to include originalPriceStopLoss and originalPriceTakeProfit for external visibility.
1149
+ * Used in public APIs to show user the original SL/TP even if trailing SL/TP are active.
1150
+ * This allows users to see both the current effective SL/TP and the original values set at signal creation.
1151
+ * The original prices remain unchanged even if _trailingPriceStopLoss or _trailingPriceTakeProfit modify the effective values.
1117
1152
  * Useful for transparency in reporting and user interfaces.
1118
- * Note: originalPriceStopLoss is identical to priceStopLoss at signal creation time.
1153
+ * Note: originalPriceStopLoss/originalPriceTakeProfit are identical to priceStopLoss/priceTakeProfit at signal creation time.
1119
1154
  */
1120
1155
  interface IPublicSignalRow extends ISignalRow {
1121
1156
  /**
@@ -1124,11 +1159,17 @@ interface IPublicSignalRow extends ISignalRow {
1124
1159
  * Used for user visibility of initial SL parameters.
1125
1160
  */
1126
1161
  originalPriceStopLoss: number;
1162
+ /**
1163
+ * Original take-profit price set at signal creation.
1164
+ * Remains unchanged even if trailing take-profit modifies effective TP.
1165
+ * Used for user visibility of initial TP parameters.
1166
+ */
1167
+ originalPriceTakeProfit: number;
1127
1168
  }
1128
1169
  /**
1129
1170
  * Risk signal row for internal risk management.
1130
- * Extends ISignalDto to include priceOpen and originalPriceStopLoss.
1131
- * Used in risk validation to access entry price and original SL.
1171
+ * Extends ISignalDto to include priceOpen, originalPriceStopLoss and originalPriceTakeProfit.
1172
+ * Used in risk validation to access entry price and original SL/TP.
1132
1173
  */
1133
1174
  interface IRiskSignalRow extends ISignalDto {
1134
1175
  /**
@@ -1139,6 +1180,10 @@ interface IRiskSignalRow extends ISignalDto {
1139
1180
  * Original stop-loss price set at signal creation.
1140
1181
  */
1141
1182
  originalPriceStopLoss: number;
1183
+ /**
1184
+ * Original take-profit price set at signal creation.
1185
+ */
1186
+ originalPriceTakeProfit: number;
1142
1187
  }
1143
1188
  /**
1144
1189
  * Scheduled signal row with cancellation ID.
@@ -1628,6 +1673,34 @@ interface IStrategy {
1628
1673
  * ```
1629
1674
  */
1630
1675
  trailingStop: (symbol: string, percentShift: number, currentPrice: number, backtest: boolean) => Promise<void>;
1676
+ /**
1677
+ * Adjusts the trailing take-profit distance for an active pending signal.
1678
+ *
1679
+ * Updates the take-profit distance by a percentage adjustment relative to the original TP distance.
1680
+ * Negative percentShift brings TP closer to entry, positive percentShift moves it further.
1681
+ * Once direction is set on first call, subsequent calls must continue in same direction.
1682
+ *
1683
+ * Price intrusion protection: If current price has already crossed the new TP level,
1684
+ * the update is skipped to prevent immediate TP triggering.
1685
+ *
1686
+ * @param symbol - Trading pair symbol
1687
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
1688
+ * @param currentPrice - Current market price to check for intrusion
1689
+ * @param backtest - Whether running in backtest mode
1690
+ * @returns Promise that resolves when trailing TP is updated
1691
+ *
1692
+ * @example
1693
+ * ```typescript
1694
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
1695
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
1696
+ * await strategy.trailingProfit(symbol, 50, 102, backtest);
1697
+ *
1698
+ * // SHORT: entry=100, originalTP=90, distance=10%, currentPrice=98
1699
+ * // Move TP closer by 30%: newTP = 100 - 7% = 93
1700
+ * await strategy.trailingProfit(symbol, -30, 98, backtest);
1701
+ * ```
1702
+ */
1703
+ trailingProfit: (symbol: string, percentShift: number, currentPrice: number, backtest: boolean) => Promise<void>;
1631
1704
  /**
1632
1705
  * Moves stop-loss to breakeven (entry price) when price reaches threshold.
1633
1706
  *
@@ -6904,6 +6977,35 @@ declare class BacktestUtils {
6904
6977
  exchangeName: ExchangeName;
6905
6978
  frameName: FrameName;
6906
6979
  }) => Promise<void>;
6980
+ /**
6981
+ * Adjusts the trailing take-profit distance for an active pending signal.
6982
+ *
6983
+ * Updates the take-profit distance by a percentage adjustment relative to the original TP distance.
6984
+ * Negative percentShift brings TP closer to entry, positive percentShift moves it further.
6985
+ * Once direction is set on first call, subsequent calls must continue in same direction.
6986
+ *
6987
+ * @param symbol - Trading pair symbol
6988
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
6989
+ * @param currentPrice - Current market price to check for intrusion
6990
+ * @param context - Execution context with strategyName, exchangeName, and frameName
6991
+ * @returns Promise that resolves when trailing TP is updated
6992
+ *
6993
+ * @example
6994
+ * ```typescript
6995
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
6996
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
6997
+ * await Backtest.trailingProfit("BTCUSDT", 50, 102, {
6998
+ * exchangeName: "binance",
6999
+ * frameName: "frame1",
7000
+ * strategyName: "my-strategy"
7001
+ * });
7002
+ * ```
7003
+ */
7004
+ trailingProfit: (symbol: string, percentShift: number, currentPrice: number, context: {
7005
+ strategyName: StrategyName;
7006
+ exchangeName: ExchangeName;
7007
+ frameName: FrameName;
7008
+ }) => Promise<void>;
6907
7009
  /**
6908
7010
  * Moves stop-loss to breakeven when price reaches threshold.
6909
7011
  *
@@ -7523,6 +7625,33 @@ declare class LiveUtils {
7523
7625
  strategyName: StrategyName;
7524
7626
  exchangeName: ExchangeName;
7525
7627
  }) => Promise<void>;
7628
+ /**
7629
+ * Adjusts the trailing take-profit distance for an active pending signal.
7630
+ *
7631
+ * Updates the take-profit distance by a percentage adjustment relative to the original TP distance.
7632
+ * Negative percentShift brings TP closer to entry, positive percentShift moves it further.
7633
+ * Once direction is set on first call, subsequent calls must continue in same direction.
7634
+ *
7635
+ * @param symbol - Trading pair symbol
7636
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
7637
+ * @param currentPrice - Current market price to check for intrusion
7638
+ * @param context - Execution context with strategyName and exchangeName
7639
+ * @returns Promise that resolves when trailing TP is updated
7640
+ *
7641
+ * @example
7642
+ * ```typescript
7643
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
7644
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
7645
+ * await Live.trailingProfit("BTCUSDT", 50, 102, {
7646
+ * exchangeName: "binance",
7647
+ * strategyName: "my-strategy"
7648
+ * });
7649
+ * ```
7650
+ */
7651
+ trailingProfit: (symbol: string, percentShift: number, currentPrice: number, context: {
7652
+ strategyName: StrategyName;
7653
+ exchangeName: ExchangeName;
7654
+ }) => Promise<void>;
7526
7655
  /**
7527
7656
  * Moves stop-loss to breakeven when price reaches threshold.
7528
7657
  *
@@ -11714,6 +11843,39 @@ declare class StrategyConnectionService implements TStrategy$1 {
11714
11843
  exchangeName: ExchangeName;
11715
11844
  frameName: FrameName;
11716
11845
  }) => Promise<void>;
11846
+ /**
11847
+ * Adjusts the trailing take-profit distance for an active pending signal.
11848
+ *
11849
+ * Updates the take-profit distance by a percentage adjustment relative to the original TP distance.
11850
+ * Negative percentShift brings TP closer to entry, positive percentShift moves it further.
11851
+ *
11852
+ * Delegates to ClientStrategy.trailingProfit() with current execution context.
11853
+ *
11854
+ * @param backtest - Whether running in backtest mode
11855
+ * @param symbol - Trading pair symbol
11856
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
11857
+ * @param currentPrice - Current market price to check for intrusion
11858
+ * @param context - Execution context with strategyName, exchangeName, frameName
11859
+ * @returns Promise that resolves when trailing TP is updated
11860
+ *
11861
+ * @example
11862
+ * ```typescript
11863
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
11864
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
11865
+ * await strategyConnectionService.trailingProfit(
11866
+ * false,
11867
+ * "BTCUSDT",
11868
+ * 50,
11869
+ * 102,
11870
+ * { strategyName: "my-strategy", exchangeName: "binance", frameName: "" }
11871
+ * );
11872
+ * ```
11873
+ */
11874
+ trailingProfit: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
11875
+ strategyName: StrategyName;
11876
+ exchangeName: ExchangeName;
11877
+ frameName: FrameName;
11878
+ }) => Promise<void>;
11717
11879
  /**
11718
11880
  * Delegates to ClientStrategy.breakeven() with current execution context.
11719
11881
  *
@@ -12281,6 +12443,35 @@ declare class StrategyCoreService implements TStrategy {
12281
12443
  exchangeName: ExchangeName;
12282
12444
  frameName: FrameName;
12283
12445
  }) => Promise<void>;
12446
+ /**
12447
+ * Adjusts the trailing take-profit distance for an active pending signal.
12448
+ * Validates context and delegates to StrategyConnectionService.
12449
+ *
12450
+ * @param backtest - Whether running in backtest mode
12451
+ * @param symbol - Trading pair symbol
12452
+ * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
12453
+ * @param currentPrice - Current market price to check for intrusion
12454
+ * @param context - Strategy context with strategyName, exchangeName, frameName
12455
+ * @returns Promise that resolves when trailing TP is updated
12456
+ *
12457
+ * @example
12458
+ * ```typescript
12459
+ * // LONG: entry=100, originalTP=110, distance=10%, currentPrice=102
12460
+ * // Move TP further by 50%: newTP = 100 + 15% = 115
12461
+ * await strategyCoreService.trailingProfit(
12462
+ * false,
12463
+ * "BTCUSDT",
12464
+ * 50,
12465
+ * 102,
12466
+ * { strategyName: "my-strategy", exchangeName: "binance", frameName: "" }
12467
+ * );
12468
+ * ```
12469
+ */
12470
+ trailingProfit: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
12471
+ strategyName: StrategyName;
12472
+ exchangeName: ExchangeName;
12473
+ frameName: FrameName;
12474
+ }) => Promise<void>;
12284
12475
  /**
12285
12476
  * Moves stop-loss to breakeven when price reaches threshold.
12286
12477
  * Validates context and delegates to StrategyConnectionService.
@@ -14234,4 +14425,4 @@ declare const backtest: {
14234
14425
  loggerService: LoggerService;
14235
14426
  };
14236
14427
 
14237
- export { Backtest, type BacktestDoneNotification, type BacktestStatisticsModel, type BootstrapNotification, Breakeven, type BreakevenContract, type BreakevenData, Cache, type CandleInterval, type ColumnConfig, type ColumnModel, Constant, type CriticalErrorNotification, type DoneContract, type EntityId, Exchange, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type HeatmapStatisticsModel, type ICandleData, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, 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 IPublicSignalRow, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalCancelRow, 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 IStrategyResult, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, type InfoErrorNotification, Live, type LiveDoneNotification, type LiveStatisticsModel, type MessageModel, type MessageRole, MethodContextService, type MetricStats, Notification, type NotificationModel, Optimizer, Partial$1 as Partial, type PartialData, type PartialEvent, type PartialLossContract, type PartialLossNotification, type PartialProfitContract, type PartialProfitNotification, type PartialStatisticsModel, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatisticsModel, PersistBase, PersistBreakevenAdapter, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, type PingContract, PositionSize, type ProgressBacktestContract, type ProgressBacktestNotification, type ProgressOptimizerContract, type ProgressWalkerContract, Risk, type RiskContract, type RiskData, type RiskEvent, type RiskRejectionNotification, type RiskStatisticsModel, Schedule, type ScheduleData, type ScheduleStatisticsModel, type ScheduledEvent, type SignalCancelledNotification, type SignalClosedNotification, type SignalData, type SignalInterval, type SignalOpenedNotification, type SignalScheduledNotification, type TPersistBase, type TPersistBaseCtor, type TickEvent, type ValidationErrorNotification, Walker, type WalkerCompleteContract, type WalkerContract, type WalkerMetric, type SignalData$1 as WalkerSignalData, type WalkerStatisticsModel, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, breakeven, cancel, dumpSignal, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getColumns, getConfig, getDate, getDefaultColumns, getDefaultConfig, getMode, hasTradeContext, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenBreakeven, listenBreakevenOnce, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenExit, listenOptimizerProgress, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenPing, listenPingOnce, listenRisk, listenRiskOnce, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, partialLoss, partialProfit, setColumns, setConfig, setLogger, stop, trailingStop, validate };
14428
+ export { Backtest, type BacktestDoneNotification, type BacktestStatisticsModel, type BootstrapNotification, Breakeven, type BreakevenContract, type BreakevenData, Cache, type CandleInterval, type ColumnConfig, type ColumnModel, Constant, type CriticalErrorNotification, type DoneContract, type EntityId, Exchange, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type HeatmapStatisticsModel, type ICandleData, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, 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 IPublicSignalRow, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalCancelRow, 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 IStrategyResult, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, type InfoErrorNotification, Live, type LiveDoneNotification, type LiveStatisticsModel, type MessageModel, type MessageRole, MethodContextService, type MetricStats, Notification, type NotificationModel, Optimizer, Partial$1 as Partial, type PartialData, type PartialEvent, type PartialLossContract, type PartialLossNotification, type PartialProfitContract, type PartialProfitNotification, type PartialStatisticsModel, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatisticsModel, PersistBase, PersistBreakevenAdapter, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, type PingContract, PositionSize, type ProgressBacktestContract, type ProgressBacktestNotification, type ProgressOptimizerContract, type ProgressWalkerContract, Risk, type RiskContract, type RiskData, type RiskEvent, type RiskRejectionNotification, type RiskStatisticsModel, Schedule, type ScheduleData, type ScheduleStatisticsModel, type ScheduledEvent, type SignalCancelledNotification, type SignalClosedNotification, type SignalData, type SignalInterval, type SignalOpenedNotification, type SignalScheduledNotification, type TPersistBase, type TPersistBaseCtor, type TickEvent, type ValidationErrorNotification, Walker, type WalkerCompleteContract, type WalkerContract, type WalkerMetric, type SignalData$1 as WalkerSignalData, type WalkerStatisticsModel, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, breakeven, cancel, dumpSignal, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getColumns, getConfig, getDate, getDefaultColumns, getDefaultConfig, getMode, hasTradeContext, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, listenBreakeven, listenBreakevenOnce, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenExit, listenOptimizerProgress, listenPartialLoss, listenPartialLossOnce, listenPartialProfit, listenPartialProfitOnce, listenPerformance, listenPing, listenPingOnce, listenRisk, listenRiskOnce, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, partialLoss, partialProfit, setColumns, setConfig, setLogger, stop, trailingProfit, trailingStop, validate };