backtest-kit 6.5.0 → 6.5.1

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/types.d.ts CHANGED
@@ -25085,36 +25085,139 @@ declare class StrategyCoreService implements TStrategy {
25085
25085
  exchangeName: ExchangeName;
25086
25086
  frameName: FrameName;
25087
25087
  }) => Promise<number | null>;
25088
+ /**
25089
+ * Returns the effective (DCA-averaged) entry price for the current pending signal.
25090
+ *
25091
+ * This is the harmonic mean of all _entry prices, which is the correct
25092
+ * cost-basis price used in all PNL calculations.
25093
+ * With no DCA entries, equals the original priceOpen.
25094
+ *
25095
+ * Returns null if no pending signal exists.
25096
+ *
25097
+ * @param backtest - Whether running in backtest mode
25098
+ * @param symbol - Trading pair symbol
25099
+ * @param context - Execution context with strategyName, exchangeName, frameName
25100
+ * @returns Promise resolving to effective entry price or null
25101
+ */
25088
25102
  getPositionEffectivePrice: (backtest: boolean, symbol: string, context: {
25089
25103
  strategyName: StrategyName;
25090
25104
  exchangeName: ExchangeName;
25091
25105
  frameName: FrameName;
25092
25106
  }) => Promise<number | null>;
25107
+ /**
25108
+ * Returns the number of DCA entries made for the current pending signal.
25109
+ *
25110
+ * 1 = original entry only (no DCA).
25111
+ * Increases by 1 with each successful commitAverageBuy().
25112
+ *
25113
+ * Returns null if no pending signal exists.
25114
+ *
25115
+ * @param backtest - Whether running in backtest mode
25116
+ * @param symbol - Trading pair symbol
25117
+ * @param context - Execution context with strategyName, exchangeName, frameName
25118
+ * @returns Promise resolving to entry count or null
25119
+ */
25093
25120
  getPositionInvestedCount: (backtest: boolean, symbol: string, context: {
25094
25121
  strategyName: StrategyName;
25095
25122
  exchangeName: ExchangeName;
25096
25123
  frameName: FrameName;
25097
25124
  }) => Promise<number | null>;
25125
+ /**
25126
+ * Returns the total invested cost basis in dollars for the current pending signal.
25127
+ *
25128
+ * Equal to entryCount × $100 (COST_BASIS_PER_ENTRY).
25129
+ * 1 entry = $100, 2 entries = $200, etc.
25130
+ *
25131
+ * Returns null if no pending signal exists.
25132
+ *
25133
+ * @param backtest - Whether running in backtest mode
25134
+ * @param symbol - Trading pair symbol
25135
+ * @param context - Execution context with strategyName, exchangeName, frameName
25136
+ * @returns Promise resolving to total invested cost in dollars or null
25137
+ */
25098
25138
  getPositionInvestedCost: (backtest: boolean, symbol: string, context: {
25099
25139
  strategyName: StrategyName;
25100
25140
  exchangeName: ExchangeName;
25101
25141
  frameName: FrameName;
25102
25142
  }) => Promise<number | null>;
25143
+ /**
25144
+ * Returns the unrealized PNL percentage for the current pending signal at currentPrice.
25145
+ *
25146
+ * Accounts for partial closes, DCA entries, slippage and fees
25147
+ * (delegates to toProfitLossDto).
25148
+ *
25149
+ * Returns null if no pending signal exists.
25150
+ *
25151
+ * @param backtest - Whether running in backtest mode
25152
+ * @param symbol - Trading pair symbol
25153
+ * @param currentPrice - Current market price
25154
+ * @param context - Execution context with strategyName, exchangeName, frameName
25155
+ * @returns Promise resolving to pnlPercentage or null
25156
+ */
25103
25157
  getPositionPnlPercent: (backtest: boolean, symbol: string, currentPrice: number, context: {
25104
25158
  strategyName: StrategyName;
25105
25159
  exchangeName: ExchangeName;
25106
25160
  frameName: FrameName;
25107
25161
  }) => Promise<number | null>;
25162
+ /**
25163
+ * Returns the unrealized PNL in dollars for the current pending signal at currentPrice.
25164
+ *
25165
+ * Calculated as: pnlPercentage / 100 × totalInvestedCost
25166
+ * Accounts for partial closes, DCA entries, slippage and fees.
25167
+ *
25168
+ * Returns null if no pending signal exists.
25169
+ *
25170
+ * @param backtest - Whether running in backtest mode
25171
+ * @param symbol - Trading pair symbol
25172
+ * @param currentPrice - Current market price
25173
+ * @param context - Execution context with strategyName, exchangeName, frameName
25174
+ * @returns Promise resolving to pnl in dollars or null
25175
+ */
25108
25176
  getPositionPnlCost: (backtest: boolean, symbol: string, currentPrice: number, context: {
25109
25177
  strategyName: StrategyName;
25110
25178
  exchangeName: ExchangeName;
25111
25179
  frameName: FrameName;
25112
25180
  }) => Promise<number | null>;
25181
+ /**
25182
+ * Returns the list of DCA entry prices for the current pending signal.
25183
+ *
25184
+ * The first element is always the original priceOpen (initial entry).
25185
+ * Each subsequent element is a price added by commitAverageBuy().
25186
+ *
25187
+ * Returns null if no pending signal exists.
25188
+ * Returns a single-element array [priceOpen] if no DCA entries were made.
25189
+ *
25190
+ * @param backtest - Whether running in backtest mode
25191
+ * @param symbol - Trading pair symbol
25192
+ * @param context - Execution context with strategyName, exchangeName, frameName
25193
+ * @returns Promise resolving to array of entry prices or null
25194
+ *
25195
+ * @example
25196
+ * ```typescript
25197
+ * // No DCA: [43000]
25198
+ * // One DCA: [43000, 42000]
25199
+ * // Two DCA: [43000, 42000, 41500]
25200
+ * ```
25201
+ */
25113
25202
  getPositionLevels: (backtest: boolean, symbol: string, context: {
25114
25203
  strategyName: StrategyName;
25115
25204
  exchangeName: ExchangeName;
25116
25205
  frameName: FrameName;
25117
25206
  }) => Promise<number[] | null>;
25207
+ /**
25208
+ * Returns the list of partial closes for the current pending signal.
25209
+ *
25210
+ * Each entry records a partial profit or loss close event with its type,
25211
+ * percent closed, price at close, cost basis snapshot, and entry count at close.
25212
+ *
25213
+ * Returns null if no pending signal exists.
25214
+ * Returns an empty array if no partial closes have been executed.
25215
+ *
25216
+ * @param backtest - Whether running in backtest mode
25217
+ * @param symbol - Trading pair symbol
25218
+ * @param context - Execution context with strategyName, exchangeName, frameName
25219
+ * @returns Promise resolving to array of partial close records or null
25220
+ */
25118
25221
  getPositionPartials: (backtest: boolean, symbol: string, context: {
25119
25222
  strategyName: StrategyName;
25120
25223
  exchangeName: ExchangeName;
@@ -25127,6 +25230,27 @@ declare class StrategyCoreService implements TStrategy {
25127
25230
  entryCountAtClose: number;
25128
25231
  timestamp: number;
25129
25232
  }[]>;
25233
+ /**
25234
+ * Returns the list of DCA entry prices and costs for the current pending signal.
25235
+ *
25236
+ * Each entry records the price and cost of a single position entry.
25237
+ * The first element is always the original priceOpen (initial entry).
25238
+ * Each subsequent element is an entry added by averageBuy().
25239
+ *
25240
+ * Returns null if no pending signal exists.
25241
+ * Returns a single-element array [{ price: priceOpen, cost }] if no DCA entries were made.
25242
+ *
25243
+ * @param backtest - Whether running in backtest mode
25244
+ * @param symbol - Trading pair symbol
25245
+ * @param context - Execution context with strategyName, exchangeName, frameName
25246
+ * @returns Promise resolving to array of entry records or null
25247
+ *
25248
+ * @example
25249
+ * ```typescript
25250
+ * // No DCA: [{ price: 43000, cost: 100 }]
25251
+ * // One DCA: [{ price: 43000, cost: 100 }, { price: 42000, cost: 100 }]
25252
+ * ```
25253
+ */
25130
25254
  getPositionEntries: (backtest: boolean, symbol: string, context: {
25131
25255
  strategyName: StrategyName;
25132
25256
  exchangeName: ExchangeName;
@@ -25425,6 +25549,22 @@ declare class StrategyCoreService implements TStrategy {
25425
25549
  exchangeName: ExchangeName;
25426
25550
  frameName: FrameName;
25427
25551
  }) => Promise<boolean>;
25552
+ /**
25553
+ * Checks whether `trailingStop` would succeed without executing it.
25554
+ * Validates context, then delegates to StrategyConnectionService.validateTrailingStop().
25555
+ *
25556
+ * @param backtest - Whether running in backtest mode
25557
+ * @param symbol - Trading pair symbol
25558
+ * @param percentShift - Percentage shift of ORIGINAL SL distance [-100, 100], excluding 0
25559
+ * @param currentPrice - Current market price to validate against
25560
+ * @param context - Execution context with strategyName, exchangeName, frameName
25561
+ * @returns Promise<boolean> - true if `trailingStop` would execute, false otherwise
25562
+ */
25563
+ validateTrailingStop: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25564
+ strategyName: StrategyName;
25565
+ exchangeName: ExchangeName;
25566
+ frameName: FrameName;
25567
+ }) => Promise<boolean>;
25428
25568
  /**
25429
25569
  * Adjusts the trailing stop-loss distance for an active pending signal.
25430
25570
  *
@@ -25438,7 +25578,7 @@ declare class StrategyCoreService implements TStrategy {
25438
25578
  * @param percentShift - Percentage adjustment to SL distance (-100 to 100)
25439
25579
  * @param currentPrice - Current market price to check for intrusion
25440
25580
  * @param context - Execution context with strategyName, exchangeName, frameName
25441
- * @returns Promise that resolves when trailing SL is updated
25581
+ * @returns Promise<boolean> - true if trailing SL was updated, false otherwise
25442
25582
  *
25443
25583
  * @example
25444
25584
  * ```typescript
@@ -25453,23 +25593,23 @@ declare class StrategyCoreService implements TStrategy {
25453
25593
  * );
25454
25594
  * ```
25455
25595
  */
25456
- validateTrailingStop: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25596
+ trailingStop: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25457
25597
  strategyName: StrategyName;
25458
25598
  exchangeName: ExchangeName;
25459
25599
  frameName: FrameName;
25460
25600
  }) => Promise<boolean>;
25461
25601
  /**
25462
- * Checks whether `trailingStop` would succeed without executing it.
25463
- * Validates context, then delegates to StrategyConnectionService.validateTrailingStop().
25602
+ * Checks whether `trailingTake` would succeed without executing it.
25603
+ * Validates context, then delegates to StrategyConnectionService.validateTrailingTake().
25464
25604
  *
25465
25605
  * @param backtest - Whether running in backtest mode
25466
25606
  * @param symbol - Trading pair symbol
25467
- * @param percentShift - Percentage shift of ORIGINAL SL distance [-100, 100], excluding 0
25607
+ * @param percentShift - Percentage adjustment to ORIGINAL TP distance [-100, 100], excluding 0
25468
25608
  * @param currentPrice - Current market price to validate against
25469
25609
  * @param context - Execution context with strategyName, exchangeName, frameName
25470
- * @returns Promise<boolean> - true if `trailingStop` would execute, false otherwise
25610
+ * @returns Promise<boolean> - true if `trailingTake` would execute, false otherwise
25471
25611
  */
25472
- trailingStop: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25612
+ validateTrailingTake: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25473
25613
  strategyName: StrategyName;
25474
25614
  exchangeName: ExchangeName;
25475
25615
  frameName: FrameName;
@@ -25483,7 +25623,7 @@ declare class StrategyCoreService implements TStrategy {
25483
25623
  * @param percentShift - Percentage adjustment to TP distance (-100 to 100)
25484
25624
  * @param currentPrice - Current market price to check for intrusion
25485
25625
  * @param context - Strategy context with strategyName, exchangeName, frameName
25486
- * @returns Promise that resolves when trailing TP is updated
25626
+ * @returns Promise<boolean> - true if trailing TP was updated, false otherwise
25487
25627
  *
25488
25628
  * @example
25489
25629
  * ```typescript
@@ -25498,23 +25638,22 @@ declare class StrategyCoreService implements TStrategy {
25498
25638
  * );
25499
25639
  * ```
25500
25640
  */
25501
- validateTrailingTake: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25641
+ trailingTake: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25502
25642
  strategyName: StrategyName;
25503
25643
  exchangeName: ExchangeName;
25504
25644
  frameName: FrameName;
25505
25645
  }) => Promise<boolean>;
25506
25646
  /**
25507
- * Checks whether `trailingTake` would succeed without executing it.
25508
- * Validates context, then delegates to StrategyConnectionService.validateTrailingTake().
25647
+ * Checks whether `breakeven` would succeed without executing it.
25648
+ * Validates context, then delegates to StrategyConnectionService.validateBreakeven().
25509
25649
  *
25510
25650
  * @param backtest - Whether running in backtest mode
25511
25651
  * @param symbol - Trading pair symbol
25512
- * @param percentShift - Percentage adjustment to ORIGINAL TP distance [-100, 100], excluding 0
25513
25652
  * @param currentPrice - Current market price to validate against
25514
25653
  * @param context - Execution context with strategyName, exchangeName, frameName
25515
- * @returns Promise<boolean> - true if `trailingTake` would execute, false otherwise
25654
+ * @returns Promise<boolean> - true if `breakeven` would execute, false otherwise
25516
25655
  */
25517
- trailingTake: (backtest: boolean, symbol: string, percentShift: number, currentPrice: number, context: {
25656
+ validateBreakeven: (backtest: boolean, symbol: string, currentPrice: number, context: {
25518
25657
  strategyName: StrategyName;
25519
25658
  exchangeName: ExchangeName;
25520
25659
  frameName: FrameName;
@@ -25539,21 +25678,6 @@ declare class StrategyCoreService implements TStrategy {
25539
25678
  * );
25540
25679
  * ```
25541
25680
  */
25542
- validateBreakeven: (backtest: boolean, symbol: string, currentPrice: number, context: {
25543
- strategyName: StrategyName;
25544
- exchangeName: ExchangeName;
25545
- frameName: FrameName;
25546
- }) => Promise<boolean>;
25547
- /**
25548
- * Checks whether `breakeven` would succeed without executing it.
25549
- * Validates context, then delegates to StrategyConnectionService.validateBreakeven().
25550
- *
25551
- * @param backtest - Whether running in backtest mode
25552
- * @param symbol - Trading pair symbol
25553
- * @param currentPrice - Current market price to validate against
25554
- * @param context - Execution context with strategyName, exchangeName, frameName
25555
- * @returns Promise<boolean> - true if `breakeven` would execute, false otherwise
25556
- */
25557
25681
  breakeven: (backtest: boolean, symbol: string, currentPrice: number, context: {
25558
25682
  strategyName: StrategyName;
25559
25683
  exchangeName: ExchangeName;
@@ -25588,16 +25712,14 @@ declare class StrategyCoreService implements TStrategy {
25588
25712
  frameName: FrameName;
25589
25713
  }, activateId?: string) => Promise<void>;
25590
25714
  /**
25591
- * Adds a new DCA entry to the active pending signal.
25592
- *
25593
- * Validates strategy existence and delegates to connection service
25594
- * to add a new averaging entry to the position.
25715
+ * Checks whether `averageBuy` would succeed without executing it.
25716
+ * Validates context, then delegates to StrategyConnectionService.validateAverageBuy().
25595
25717
  *
25596
25718
  * @param backtest - Whether running in backtest mode
25597
25719
  * @param symbol - Trading pair symbol
25598
- * @param currentPrice - New entry price to add to the averaging history
25720
+ * @param currentPrice - New entry price to validate
25599
25721
  * @param context - Execution context with strategyName, exchangeName, frameName
25600
- * @returns Promise<boolean> - true if entry added, false if rejected
25722
+ * @returns Promise<boolean> - true if `averageBuy` would execute, false otherwise
25601
25723
  */
25602
25724
  validateAverageBuy: (backtest: boolean, symbol: string, currentPrice: number, context: {
25603
25725
  strategyName: StrategyName;
@@ -25605,14 +25727,17 @@ declare class StrategyCoreService implements TStrategy {
25605
25727
  frameName: FrameName;
25606
25728
  }) => Promise<boolean>;
25607
25729
  /**
25608
- * Checks whether `averageBuy` would succeed without executing it.
25609
- * Validates context, then delegates to StrategyConnectionService.validateAverageBuy().
25730
+ * Adds a new DCA entry to the active pending signal.
25731
+ *
25732
+ * Validates strategy existence and delegates to connection service
25733
+ * to add a new averaging entry to the position.
25610
25734
  *
25611
25735
  * @param backtest - Whether running in backtest mode
25612
25736
  * @param symbol - Trading pair symbol
25613
- * @param currentPrice - New entry price to validate
25737
+ * @param currentPrice - New entry price to add to the averaging history
25738
+ * @param cost - Cost basis for this entry in dollars
25614
25739
  * @param context - Execution context with strategyName, exchangeName, frameName
25615
- * @returns Promise<boolean> - true if `averageBuy` would execute, false otherwise
25740
+ * @returns Promise<boolean> - true if entry added, false if rejected
25616
25741
  */
25617
25742
  averageBuy: (backtest: boolean, symbol: string, currentPrice: number, context: {
25618
25743
  strategyName: StrategyName;