backtest-kit 2.2.8 → 2.2.10

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
@@ -12107,6 +12107,7 @@ class BacktestLogicPrivateService {
12107
12107
  if (result.action === "scheduled") {
12108
12108
  const signalStartTime = performance.now();
12109
12109
  const signal = result.signal;
12110
+ yield result;
12110
12111
  this.loggerService.info("backtestLogicPrivateService scheduled signal detected", {
12111
12112
  symbol,
12112
12113
  signalId: signal.id,
@@ -12153,11 +12154,13 @@ class BacktestLogicPrivateService {
12153
12154
  // и если активируется - продолжит с TP/SL мониторингом
12154
12155
  let backtestResult;
12155
12156
  let unScheduleOpen;
12157
+ let scheduleOpenResult;
12156
12158
  {
12157
12159
  const { strategyName, exchangeName, frameName } = this.methodContextService.context;
12158
12160
  unScheduleOpen = backtestScheduleOpenSubject.filter((event) => {
12159
12161
  let isOk = true;
12160
12162
  {
12163
+ isOk = isOk && event.action === "opened";
12161
12164
  isOk = isOk && event.strategyName === strategyName;
12162
12165
  isOk = isOk && event.exchangeName === exchangeName;
12163
12166
  isOk = isOk && event.frameName === frameName;
@@ -12165,6 +12168,8 @@ class BacktestLogicPrivateService {
12165
12168
  }
12166
12169
  return isOk;
12167
12170
  }).connect(async (tick) => {
12171
+ scheduleOpenResult = tick;
12172
+ await signalEmitter.next(tick);
12168
12173
  await signalBacktestEmitter.next(tick);
12169
12174
  await this.actionCoreService.signalBacktest(true, tick, {
12170
12175
  strategyName,
@@ -12223,6 +12228,9 @@ class BacktestLogicPrivateService {
12223
12228
  timeframes[i].getTime() < backtestResult.closeTimestamp) {
12224
12229
  i++;
12225
12230
  }
12231
+ if (scheduleOpenResult) {
12232
+ yield scheduleOpenResult;
12233
+ }
12226
12234
  yield backtestResult;
12227
12235
  // Check if strategy should stop after signal is closed
12228
12236
  if (await this.strategyCoreService.getStopped(true, symbol, {
@@ -12243,6 +12251,7 @@ class BacktestLogicPrivateService {
12243
12251
  if (result.action === "opened") {
12244
12252
  const signalStartTime = performance.now();
12245
12253
  const signal = result.signal;
12254
+ yield result;
12246
12255
  this.loggerService.info("backtestLogicPrivateService signal opened", {
12247
12256
  symbol,
12248
12257
  signalId: signal.id,
@@ -32435,6 +32444,7 @@ class NotificationInstance {
32435
32444
  this._notifications = [];
32436
32445
  /**
32437
32446
  * Processes signal events and creates appropriate notifications.
32447
+ * Sorts signal notifications by createdAt to maintain chronological order.
32438
32448
  */
32439
32449
  this._handleSignal = async (data) => {
32440
32450
  if (data.action === "opened") {
@@ -32513,6 +32523,12 @@ class NotificationInstance {
32513
32523
  createdAt: data.createdAt,
32514
32524
  });
32515
32525
  }
32526
+ // Sort signal notifications by createdAt (newest first)
32527
+ this._notifications.sort((a, b) => {
32528
+ const aCreatedAt = "createdAt" in a ? a.createdAt : 0;
32529
+ const bCreatedAt = "createdAt" in b ? b.createdAt : 0;
32530
+ return bCreatedAt - aCreatedAt;
32531
+ });
32516
32532
  };
32517
32533
  /**
32518
32534
  * Processes partial profit events.
package/build/index.mjs CHANGED
@@ -12087,6 +12087,7 @@ class BacktestLogicPrivateService {
12087
12087
  if (result.action === "scheduled") {
12088
12088
  const signalStartTime = performance.now();
12089
12089
  const signal = result.signal;
12090
+ yield result;
12090
12091
  this.loggerService.info("backtestLogicPrivateService scheduled signal detected", {
12091
12092
  symbol,
12092
12093
  signalId: signal.id,
@@ -12133,11 +12134,13 @@ class BacktestLogicPrivateService {
12133
12134
  // и если активируется - продолжит с TP/SL мониторингом
12134
12135
  let backtestResult;
12135
12136
  let unScheduleOpen;
12137
+ let scheduleOpenResult;
12136
12138
  {
12137
12139
  const { strategyName, exchangeName, frameName } = this.methodContextService.context;
12138
12140
  unScheduleOpen = backtestScheduleOpenSubject.filter((event) => {
12139
12141
  let isOk = true;
12140
12142
  {
12143
+ isOk = isOk && event.action === "opened";
12141
12144
  isOk = isOk && event.strategyName === strategyName;
12142
12145
  isOk = isOk && event.exchangeName === exchangeName;
12143
12146
  isOk = isOk && event.frameName === frameName;
@@ -12145,6 +12148,8 @@ class BacktestLogicPrivateService {
12145
12148
  }
12146
12149
  return isOk;
12147
12150
  }).connect(async (tick) => {
12151
+ scheduleOpenResult = tick;
12152
+ await signalEmitter.next(tick);
12148
12153
  await signalBacktestEmitter.next(tick);
12149
12154
  await this.actionCoreService.signalBacktest(true, tick, {
12150
12155
  strategyName,
@@ -12203,6 +12208,9 @@ class BacktestLogicPrivateService {
12203
12208
  timeframes[i].getTime() < backtestResult.closeTimestamp) {
12204
12209
  i++;
12205
12210
  }
12211
+ if (scheduleOpenResult) {
12212
+ yield scheduleOpenResult;
12213
+ }
12206
12214
  yield backtestResult;
12207
12215
  // Check if strategy should stop after signal is closed
12208
12216
  if (await this.strategyCoreService.getStopped(true, symbol, {
@@ -12223,6 +12231,7 @@ class BacktestLogicPrivateService {
12223
12231
  if (result.action === "opened") {
12224
12232
  const signalStartTime = performance.now();
12225
12233
  const signal = result.signal;
12234
+ yield result;
12226
12235
  this.loggerService.info("backtestLogicPrivateService signal opened", {
12227
12236
  symbol,
12228
12237
  signalId: signal.id,
@@ -32415,6 +32424,7 @@ class NotificationInstance {
32415
32424
  this._notifications = [];
32416
32425
  /**
32417
32426
  * Processes signal events and creates appropriate notifications.
32427
+ * Sorts signal notifications by createdAt to maintain chronological order.
32418
32428
  */
32419
32429
  this._handleSignal = async (data) => {
32420
32430
  if (data.action === "opened") {
@@ -32493,6 +32503,12 @@ class NotificationInstance {
32493
32503
  createdAt: data.createdAt,
32494
32504
  });
32495
32505
  }
32506
+ // Sort signal notifications by createdAt (newest first)
32507
+ this._notifications.sort((a, b) => {
32508
+ const aCreatedAt = "createdAt" in a ? a.createdAt : 0;
32509
+ const bCreatedAt = "createdAt" in b ? b.createdAt : 0;
32510
+ return bCreatedAt - aCreatedAt;
32511
+ });
32496
32512
  };
32497
32513
  /**
32498
32514
  * Processes partial profit events.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "backtest-kit",
3
- "version": "2.2.8",
3
+ "version": "2.2.10",
4
4
  "description": "A TypeScript library for trading system backtest",
5
5
  "author": {
6
6
  "name": "Petr Tripolsky",
package/types.d.ts CHANGED
@@ -1439,7 +1439,7 @@ type IStrategyTickResult = IStrategyTickResultIdle | IStrategyTickResultSchedule
1439
1439
  /**
1440
1440
  * Backtest returns closed result (TP/SL or time_expired) or cancelled result (scheduled signal never activated).
1441
1441
  */
1442
- type IStrategyBacktestResult = IStrategyTickResultClosed | IStrategyTickResultCancelled;
1442
+ type IStrategyBacktestResult = IStrategyTickResultOpened | IStrategyTickResultScheduled | IStrategyTickResultClosed | IStrategyTickResultCancelled;
1443
1443
  /**
1444
1444
  * Strategy interface implemented by ClientStrategy.
1445
1445
  * Defines core strategy execution methods.
@@ -8899,7 +8899,7 @@ declare class BacktestUtils {
8899
8899
  strategyName: StrategyName;
8900
8900
  exchangeName: ExchangeName;
8901
8901
  frameName: FrameName;
8902
- }) => AsyncGenerator<IStrategyBacktestResult, void, unknown>;
8902
+ }) => AsyncGenerator<IStrategyTickResultScheduled | IStrategyTickResultOpened | IStrategyTickResultClosed | IStrategyTickResultCancelled, void, unknown>;
8903
8903
  /**
8904
8904
  * Runs backtest in background without yielding results.
8905
8905
  *
@@ -13305,7 +13305,7 @@ declare class StrategyCoreService implements TStrategy$1 {
13305
13305
  strategyName: StrategyName;
13306
13306
  exchangeName: ExchangeName;
13307
13307
  frameName: FrameName;
13308
- }) => Promise<IStrategyBacktestResult>;
13308
+ }) => Promise<IStrategyTickResultClosed | IStrategyTickResultCancelled>;
13309
13309
  /**
13310
13310
  * Stops the strategy from generating new signals.
13311
13311
  *
@@ -15954,7 +15954,7 @@ declare class StrategyConnectionService implements TStrategy {
15954
15954
  strategyName: StrategyName;
15955
15955
  exchangeName: ExchangeName;
15956
15956
  frameName: FrameName;
15957
- }, candles: ICandleData[]) => Promise<IStrategyBacktestResult>;
15957
+ }, candles: ICandleData[]) => Promise<IStrategyTickResultClosed | IStrategyTickResultCancelled>;
15958
15958
  /**
15959
15959
  * Stops the specified strategy from generating new signals.
15960
15960
  *
@@ -17451,7 +17451,7 @@ declare class BacktestLogicPrivateService {
17451
17451
  * }
17452
17452
  * ```
17453
17453
  */
17454
- run(symbol: string): AsyncGenerator<IStrategyBacktestResult, void, unknown>;
17454
+ run(symbol: string): AsyncGenerator<IStrategyTickResultScheduled | IStrategyTickResultOpened | IStrategyTickResultClosed | IStrategyTickResultCancelled, void, unknown>;
17455
17455
  }
17456
17456
 
17457
17457
  /**
@@ -17558,7 +17558,7 @@ declare class BacktestLogicPublicService implements TBacktestLogicPrivateService
17558
17558
  strategyName: StrategyName;
17559
17559
  exchangeName: ExchangeName;
17560
17560
  frameName: FrameName;
17561
- }) => AsyncGenerator<IStrategyBacktestResult, void, unknown>;
17561
+ }) => AsyncGenerator<IStrategyTickResultScheduled | IStrategyTickResultOpened | IStrategyTickResultClosed | IStrategyTickResultCancelled, void, unknown>;
17562
17562
  }
17563
17563
 
17564
17564
  /**
@@ -17697,7 +17697,7 @@ declare class BacktestCommandService implements TBacktestLogicPublicService {
17697
17697
  strategyName: StrategyName;
17698
17698
  exchangeName: ExchangeName;
17699
17699
  frameName: FrameName;
17700
- }) => AsyncGenerator<IStrategyBacktestResult, void, unknown>;
17700
+ }) => AsyncGenerator<IStrategyTickResultScheduled | IStrategyTickResultOpened | IStrategyTickResultClosed | IStrategyTickResultCancelled, void, unknown>;
17701
17701
  }
17702
17702
 
17703
17703
  /**