backtest-kit 1.6.2 → 1.6.4
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/README.md +1 -1
- package/build/index.cjs +209 -39
- package/build/index.mjs +208 -40
- package/package.json +1 -1
- package/types.d.ts +529 -477
package/build/index.mjs
CHANGED
|
@@ -2399,7 +2399,7 @@ const CALL_PING_CALLBACKS_FN = trycatch(async (self, scheduled, timestamp) => {
|
|
|
2399
2399
|
backtest$1.loggerService.warn(message, payload);
|
|
2400
2400
|
console.warn(message, payload);
|
|
2401
2401
|
errorEmitter.next(error);
|
|
2402
|
-
}
|
|
2402
|
+
},
|
|
2403
2403
|
});
|
|
2404
2404
|
const RETURN_SCHEDULED_SIGNAL_ACTIVE_FN = async (self, scheduled, currentPrice) => {
|
|
2405
2405
|
await CALL_PING_CALLBACKS_FN(self, scheduled, self.params.execution.context.when.getTime());
|
|
@@ -3758,6 +3758,15 @@ const GET_RISK_FN = (dto, backtest, self) => {
|
|
|
3758
3758
|
...dto.riskList.map((riskName) => self.riskConnectionService.getRisk(riskName, backtest)),
|
|
3759
3759
|
]);
|
|
3760
3760
|
};
|
|
3761
|
+
/**
|
|
3762
|
+
* Creates a unique key for memoizing ClientStrategy instances.
|
|
3763
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
3764
|
+
* @param symbol - Trading pair symbol
|
|
3765
|
+
* @param strategyName - Name of the strategy
|
|
3766
|
+
* @param backtest - Whether running in backtest mode
|
|
3767
|
+
* @returns Unique string key for memoization
|
|
3768
|
+
*/
|
|
3769
|
+
const CREATE_KEY_FN$9 = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
3761
3770
|
/**
|
|
3762
3771
|
* Callback function for emitting ping events to pingSubject.
|
|
3763
3772
|
*
|
|
@@ -3818,7 +3827,7 @@ class StrategyConnectionService {
|
|
|
3818
3827
|
* @param strategyName - Name of registered strategy schema
|
|
3819
3828
|
* @returns Configured ClientStrategy instance
|
|
3820
3829
|
*/
|
|
3821
|
-
this.getStrategy = memoize(([symbol, strategyName, backtest]) =>
|
|
3830
|
+
this.getStrategy = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN$9(symbol, strategyName, backtest), (symbol, strategyName, backtest) => {
|
|
3822
3831
|
const { riskName = "", riskList = [], getSignal, interval, callbacks, } = this.strategySchemaService.get(strategyName);
|
|
3823
3832
|
return new ClientStrategy({
|
|
3824
3833
|
symbol,
|
|
@@ -3985,7 +3994,7 @@ class StrategyConnectionService {
|
|
|
3985
3994
|
ctx,
|
|
3986
3995
|
});
|
|
3987
3996
|
if (ctx) {
|
|
3988
|
-
const key =
|
|
3997
|
+
const key = CREATE_KEY_FN$9(ctx.symbol, ctx.strategyName, backtest);
|
|
3989
3998
|
this.getStrategy.clear(key);
|
|
3990
3999
|
}
|
|
3991
4000
|
else {
|
|
@@ -4588,6 +4597,14 @@ class ClientRisk {
|
|
|
4588
4597
|
}
|
|
4589
4598
|
}
|
|
4590
4599
|
|
|
4600
|
+
/**
|
|
4601
|
+
* Creates a unique key for memoizing ClientRisk instances.
|
|
4602
|
+
* Key format: "riskName:backtest" or "riskName:live"
|
|
4603
|
+
* @param riskName - Name of the risk schema
|
|
4604
|
+
* @param backtest - Whether running in backtest mode
|
|
4605
|
+
* @returns Unique string key for memoization
|
|
4606
|
+
*/
|
|
4607
|
+
const CREATE_KEY_FN$8 = (riskName, backtest) => `${riskName}:${backtest ? "backtest" : "live"}`;
|
|
4591
4608
|
/**
|
|
4592
4609
|
* Callback function for emitting risk rejection events to riskSubject.
|
|
4593
4610
|
*
|
|
@@ -4659,7 +4676,7 @@ class RiskConnectionService {
|
|
|
4659
4676
|
* @param backtest - True if backtest mode, false if live mode
|
|
4660
4677
|
* @returns Configured ClientRisk instance
|
|
4661
4678
|
*/
|
|
4662
|
-
this.getRisk = memoize(([riskName, backtest]) =>
|
|
4679
|
+
this.getRisk = memoize(([riskName, backtest]) => CREATE_KEY_FN$8(riskName, backtest), (riskName, backtest) => {
|
|
4663
4680
|
const schema = this.riskSchemaService.get(riskName);
|
|
4664
4681
|
return new ClientRisk({
|
|
4665
4682
|
...schema,
|
|
@@ -4717,15 +4734,21 @@ class RiskConnectionService {
|
|
|
4717
4734
|
/**
|
|
4718
4735
|
* Clears the cached ClientRisk instance for the given risk name.
|
|
4719
4736
|
*
|
|
4720
|
-
* @param
|
|
4737
|
+
* @param backtest - Whether running in backtest mode
|
|
4738
|
+
* @param ctx - Optional context with riskName (clears all if not provided)
|
|
4721
4739
|
*/
|
|
4722
|
-
this.clear = async (backtest,
|
|
4740
|
+
this.clear = async (backtest, ctx) => {
|
|
4723
4741
|
this.loggerService.log("riskConnectionService clear", {
|
|
4724
|
-
|
|
4742
|
+
ctx,
|
|
4725
4743
|
backtest,
|
|
4726
4744
|
});
|
|
4727
|
-
|
|
4728
|
-
|
|
4745
|
+
if (ctx) {
|
|
4746
|
+
const key = CREATE_KEY_FN$8(ctx.riskName, backtest);
|
|
4747
|
+
this.getRisk.clear(key);
|
|
4748
|
+
}
|
|
4749
|
+
else {
|
|
4750
|
+
this.getRisk.clear();
|
|
4751
|
+
}
|
|
4729
4752
|
};
|
|
4730
4753
|
}
|
|
4731
4754
|
}
|
|
@@ -5254,19 +5277,20 @@ class RiskGlobalService {
|
|
|
5254
5277
|
};
|
|
5255
5278
|
/**
|
|
5256
5279
|
* Clears risk data.
|
|
5257
|
-
* If
|
|
5258
|
-
* If no
|
|
5259
|
-
* @param
|
|
5280
|
+
* If ctx is provided, clears data for that specific risk instance.
|
|
5281
|
+
* If no ctx is provided, clears all risk data.
|
|
5282
|
+
* @param backtest - Whether running in backtest mode
|
|
5283
|
+
* @param ctx - Optional context with riskName (clears all if not provided)
|
|
5260
5284
|
*/
|
|
5261
|
-
this.clear = async (backtest,
|
|
5285
|
+
this.clear = async (backtest, ctx) => {
|
|
5262
5286
|
this.loggerService.log("riskGlobalService clear", {
|
|
5263
|
-
|
|
5287
|
+
ctx,
|
|
5264
5288
|
backtest,
|
|
5265
5289
|
});
|
|
5266
|
-
if (
|
|
5267
|
-
await this.validate(riskName);
|
|
5290
|
+
if (ctx) {
|
|
5291
|
+
await this.validate(ctx.riskName);
|
|
5268
5292
|
}
|
|
5269
|
-
return await this.riskConnectionService.clear(backtest,
|
|
5293
|
+
return await this.riskConnectionService.clear(backtest, ctx);
|
|
5270
5294
|
};
|
|
5271
5295
|
}
|
|
5272
5296
|
}
|
|
@@ -7759,6 +7783,15 @@ const COLUMN_CONFIG = {
|
|
|
7759
7783
|
*/
|
|
7760
7784
|
const DEFAULT_COLUMNS = Object.freeze({ ...COLUMN_CONFIG });
|
|
7761
7785
|
|
|
7786
|
+
/**
|
|
7787
|
+
* Creates a unique key for memoizing ReportStorage instances.
|
|
7788
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
7789
|
+
* @param symbol - Trading pair symbol
|
|
7790
|
+
* @param strategyName - Name of the strategy
|
|
7791
|
+
* @param backtest - Whether running in backtest mode
|
|
7792
|
+
* @returns Unique string key for memoization
|
|
7793
|
+
*/
|
|
7794
|
+
const CREATE_KEY_FN$7 = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
7762
7795
|
/**
|
|
7763
7796
|
* Checks if a value is unsafe for display (not a number, NaN, or Infinity).
|
|
7764
7797
|
*
|
|
@@ -7967,7 +8000,7 @@ class BacktestMarkdownService {
|
|
|
7967
8000
|
* Memoized function to get or create ReportStorage for a symbol-strategy-backtest triple.
|
|
7968
8001
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
7969
8002
|
*/
|
|
7970
|
-
this.getStorage = memoize(([symbol, strategyName, backtest]) =>
|
|
8003
|
+
this.getStorage = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN$7(symbol, strategyName, backtest), () => new ReportStorage$5());
|
|
7971
8004
|
/**
|
|
7972
8005
|
* Processes tick events and accumulates closed signals.
|
|
7973
8006
|
* Should be called from IStrategyCallbacks.onTick.
|
|
@@ -8105,7 +8138,7 @@ class BacktestMarkdownService {
|
|
|
8105
8138
|
ctx,
|
|
8106
8139
|
});
|
|
8107
8140
|
if (ctx) {
|
|
8108
|
-
const key =
|
|
8141
|
+
const key = CREATE_KEY_FN$7(ctx.symbol, ctx.strategyName, backtest);
|
|
8109
8142
|
this.getStorage.clear(key);
|
|
8110
8143
|
}
|
|
8111
8144
|
else {
|
|
@@ -8130,6 +8163,15 @@ class BacktestMarkdownService {
|
|
|
8130
8163
|
}
|
|
8131
8164
|
}
|
|
8132
8165
|
|
|
8166
|
+
/**
|
|
8167
|
+
* Creates a unique key for memoizing ReportStorage instances.
|
|
8168
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
8169
|
+
* @param symbol - Trading pair symbol
|
|
8170
|
+
* @param strategyName - Name of the strategy
|
|
8171
|
+
* @param backtest - Whether running in backtest mode
|
|
8172
|
+
* @returns Unique string key for memoization
|
|
8173
|
+
*/
|
|
8174
|
+
const CREATE_KEY_FN$6 = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
8133
8175
|
/**
|
|
8134
8176
|
* Checks if a value is unsafe for display (not a number, NaN, or Infinity).
|
|
8135
8177
|
*
|
|
@@ -8459,7 +8501,7 @@ class LiveMarkdownService {
|
|
|
8459
8501
|
* Memoized function to get or create ReportStorage for a symbol-strategy-backtest triple.
|
|
8460
8502
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
8461
8503
|
*/
|
|
8462
|
-
this.getStorage = memoize(([symbol, strategyName, backtest]) =>
|
|
8504
|
+
this.getStorage = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN$6(symbol, strategyName, backtest), () => new ReportStorage$4());
|
|
8463
8505
|
/**
|
|
8464
8506
|
* Processes tick events and accumulates all event types.
|
|
8465
8507
|
* Should be called from IStrategyCallbacks.onTick.
|
|
@@ -8607,7 +8649,7 @@ class LiveMarkdownService {
|
|
|
8607
8649
|
ctx,
|
|
8608
8650
|
});
|
|
8609
8651
|
if (ctx) {
|
|
8610
|
-
const key =
|
|
8652
|
+
const key = CREATE_KEY_FN$6(ctx.symbol, ctx.strategyName, backtest);
|
|
8611
8653
|
this.getStorage.clear(key);
|
|
8612
8654
|
}
|
|
8613
8655
|
else {
|
|
@@ -8632,6 +8674,15 @@ class LiveMarkdownService {
|
|
|
8632
8674
|
}
|
|
8633
8675
|
}
|
|
8634
8676
|
|
|
8677
|
+
/**
|
|
8678
|
+
* Creates a unique key for memoizing ReportStorage instances.
|
|
8679
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
8680
|
+
* @param symbol - Trading pair symbol
|
|
8681
|
+
* @param strategyName - Name of the strategy
|
|
8682
|
+
* @param backtest - Whether running in backtest mode
|
|
8683
|
+
* @returns Unique string key for memoization
|
|
8684
|
+
*/
|
|
8685
|
+
const CREATE_KEY_FN$5 = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
8635
8686
|
/** Maximum number of events to store in schedule reports */
|
|
8636
8687
|
const MAX_EVENTS$4 = 250;
|
|
8637
8688
|
/**
|
|
@@ -8867,7 +8918,7 @@ class ScheduleMarkdownService {
|
|
|
8867
8918
|
* Memoized function to get or create ReportStorage for a symbol-strategy-backtest triple.
|
|
8868
8919
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
8869
8920
|
*/
|
|
8870
|
-
this.getStorage = memoize(([symbol, strategyName, backtest]) =>
|
|
8921
|
+
this.getStorage = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN$5(symbol, strategyName, backtest), () => new ReportStorage$3());
|
|
8871
8922
|
/**
|
|
8872
8923
|
* Processes tick events and accumulates scheduled/opened/cancelled events.
|
|
8873
8924
|
* Should be called from signalEmitter subscription.
|
|
@@ -9009,7 +9060,7 @@ class ScheduleMarkdownService {
|
|
|
9009
9060
|
ctx,
|
|
9010
9061
|
});
|
|
9011
9062
|
if (ctx) {
|
|
9012
|
-
const key =
|
|
9063
|
+
const key = CREATE_KEY_FN$5(ctx.symbol, ctx.strategyName, backtest);
|
|
9013
9064
|
this.getStorage.clear(key);
|
|
9014
9065
|
}
|
|
9015
9066
|
else {
|
|
@@ -9034,6 +9085,15 @@ class ScheduleMarkdownService {
|
|
|
9034
9085
|
}
|
|
9035
9086
|
}
|
|
9036
9087
|
|
|
9088
|
+
/**
|
|
9089
|
+
* Creates a unique key for memoizing PerformanceStorage instances.
|
|
9090
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
9091
|
+
* @param symbol - Trading pair symbol
|
|
9092
|
+
* @param strategyName - Name of the strategy
|
|
9093
|
+
* @param backtest - Whether running in backtest mode
|
|
9094
|
+
* @returns Unique string key for memoization
|
|
9095
|
+
*/
|
|
9096
|
+
const CREATE_KEY_FN$4 = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
9037
9097
|
/**
|
|
9038
9098
|
* Calculates percentile value from sorted array.
|
|
9039
9099
|
*/
|
|
@@ -9251,7 +9311,7 @@ class PerformanceMarkdownService {
|
|
|
9251
9311
|
* Memoized function to get or create PerformanceStorage for a symbol-strategy-backtest triple.
|
|
9252
9312
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
9253
9313
|
*/
|
|
9254
|
-
this.getStorage = memoize(([symbol, strategyName, backtest]) =>
|
|
9314
|
+
this.getStorage = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN$4(symbol, strategyName, backtest), () => new PerformanceStorage());
|
|
9255
9315
|
/**
|
|
9256
9316
|
* Processes performance events and accumulates metrics.
|
|
9257
9317
|
* Should be called from performance tracking code.
|
|
@@ -9356,7 +9416,7 @@ class PerformanceMarkdownService {
|
|
|
9356
9416
|
ctx,
|
|
9357
9417
|
});
|
|
9358
9418
|
if (ctx) {
|
|
9359
|
-
const key =
|
|
9419
|
+
const key = CREATE_KEY_FN$4(ctx.symbol, ctx.strategyName, backtest);
|
|
9360
9420
|
this.getStorage.clear(key);
|
|
9361
9421
|
}
|
|
9362
9422
|
else {
|
|
@@ -9783,6 +9843,14 @@ class WalkerMarkdownService {
|
|
|
9783
9843
|
}
|
|
9784
9844
|
}
|
|
9785
9845
|
|
|
9846
|
+
/**
|
|
9847
|
+
* Creates a unique key for memoizing HeatmapStorage instances.
|
|
9848
|
+
* Key format: "strategyName:backtest" or "strategyName:live"
|
|
9849
|
+
* @param strategyName - Name of the strategy
|
|
9850
|
+
* @param backtest - Whether running in backtest mode
|
|
9851
|
+
* @returns Unique string key for memoization
|
|
9852
|
+
*/
|
|
9853
|
+
const CREATE_KEY_FN$3 = (strategyName, backtest) => `${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
9786
9854
|
const HEATMAP_METHOD_NAME_GET_DATA = "HeatMarkdownService.getData";
|
|
9787
9855
|
const HEATMAP_METHOD_NAME_GET_REPORT = "HeatMarkdownService.getReport";
|
|
9788
9856
|
const HEATMAP_METHOD_NAME_DUMP = "HeatMarkdownService.dump";
|
|
@@ -10126,7 +10194,7 @@ class HeatMarkdownService {
|
|
|
10126
10194
|
* Memoized function to get or create HeatmapStorage for a strategy and backtest mode.
|
|
10127
10195
|
* Each strategy + backtest mode combination gets its own isolated heatmap storage instance.
|
|
10128
10196
|
*/
|
|
10129
|
-
this.getStorage = memoize(([strategyName, backtest]) =>
|
|
10197
|
+
this.getStorage = memoize(([strategyName, backtest]) => CREATE_KEY_FN$3(strategyName, backtest), () => new HeatmapStorage());
|
|
10130
10198
|
/**
|
|
10131
10199
|
* Processes tick events and accumulates closed signals.
|
|
10132
10200
|
* Should be called from signal emitter subscription.
|
|
@@ -10262,7 +10330,7 @@ class HeatMarkdownService {
|
|
|
10262
10330
|
ctx,
|
|
10263
10331
|
});
|
|
10264
10332
|
if (ctx) {
|
|
10265
|
-
const key =
|
|
10333
|
+
const key = CREATE_KEY_FN$3(ctx.strategyName, backtest);
|
|
10266
10334
|
this.getStorage.clear(key);
|
|
10267
10335
|
}
|
|
10268
10336
|
else {
|
|
@@ -12472,6 +12540,15 @@ class ClientPartial {
|
|
|
12472
12540
|
}
|
|
12473
12541
|
}
|
|
12474
12542
|
|
|
12543
|
+
/**
|
|
12544
|
+
* Creates a unique key for memoizing ClientPartial instances.
|
|
12545
|
+
* Key format: "signalId:backtest" or "signalId:live"
|
|
12546
|
+
*
|
|
12547
|
+
* @param signalId - Signal ID
|
|
12548
|
+
* @param backtest - Whether running in backtest mode
|
|
12549
|
+
* @returns Unique string key for memoization
|
|
12550
|
+
*/
|
|
12551
|
+
const CREATE_KEY_FN$2 = (signalId, backtest) => `${signalId}:${backtest ? "backtest" : "live"}`;
|
|
12475
12552
|
/**
|
|
12476
12553
|
* Callback function for emitting profit events to partialProfitSubject.
|
|
12477
12554
|
*
|
|
@@ -12569,7 +12646,7 @@ class PartialConnectionService {
|
|
|
12569
12646
|
* Key format: "signalId:backtest" or "signalId:live"
|
|
12570
12647
|
* Value: ClientPartial instance with logger and event emitters
|
|
12571
12648
|
*/
|
|
12572
|
-
this.getPartial = memoize(([signalId, backtest]) =>
|
|
12649
|
+
this.getPartial = memoize(([signalId, backtest]) => CREATE_KEY_FN$2(signalId, backtest), (signalId, backtest) => {
|
|
12573
12650
|
return new ClientPartial({
|
|
12574
12651
|
signalId,
|
|
12575
12652
|
logger: this.loggerService,
|
|
@@ -12659,12 +12736,19 @@ class PartialConnectionService {
|
|
|
12659
12736
|
const partial = this.getPartial(data.id, backtest);
|
|
12660
12737
|
await partial.waitForInit(symbol, data.strategyName);
|
|
12661
12738
|
await partial.clear(symbol, data, priceClose, backtest);
|
|
12662
|
-
const key =
|
|
12739
|
+
const key = CREATE_KEY_FN$2(data.id, backtest);
|
|
12663
12740
|
this.getPartial.clear(key);
|
|
12664
12741
|
};
|
|
12665
12742
|
}
|
|
12666
12743
|
}
|
|
12667
12744
|
|
|
12745
|
+
/**
|
|
12746
|
+
* Creates a unique key for memoizing ReportStorage instances.
|
|
12747
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
12748
|
+
* @param param0 - Tuple of symbol, strategyName and backtest boolean
|
|
12749
|
+
* @returns Unique string key for memoization
|
|
12750
|
+
*/
|
|
12751
|
+
const CREATE_KEY_FN$1 = ([symbol, strategyName, backtest]) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
12668
12752
|
/** Maximum number of events to store in partial reports */
|
|
12669
12753
|
const MAX_EVENTS$1 = 250;
|
|
12670
12754
|
/**
|
|
@@ -12839,7 +12923,7 @@ class PartialMarkdownService {
|
|
|
12839
12923
|
* Memoized function to get or create ReportStorage for a symbol-strategy-backtest triple.
|
|
12840
12924
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
12841
12925
|
*/
|
|
12842
|
-
this.getStorage = memoize(
|
|
12926
|
+
this.getStorage = memoize(CREATE_KEY_FN$1, () => new ReportStorage$1());
|
|
12843
12927
|
/**
|
|
12844
12928
|
* Processes profit events and accumulates them.
|
|
12845
12929
|
* Should be called from partialProfitSubject subscription.
|
|
@@ -12986,7 +13070,7 @@ class PartialMarkdownService {
|
|
|
12986
13070
|
ctx,
|
|
12987
13071
|
});
|
|
12988
13072
|
if (ctx) {
|
|
12989
|
-
const key =
|
|
13073
|
+
const key = CREATE_KEY_FN$1([ctx.symbol, ctx.strategyName, backtest]);
|
|
12990
13074
|
this.getStorage.clear(key);
|
|
12991
13075
|
}
|
|
12992
13076
|
else {
|
|
@@ -13421,6 +13505,15 @@ class ConfigValidationService {
|
|
|
13421
13505
|
}
|
|
13422
13506
|
}
|
|
13423
13507
|
|
|
13508
|
+
/**
|
|
13509
|
+
* Creates a unique key for memoizing ReportStorage instances.
|
|
13510
|
+
* Key format: "symbol:strategyName:backtest" or "symbol:strategyName:live"
|
|
13511
|
+
* @param symbol - Trading pair symbol
|
|
13512
|
+
* @param strategyName - Name of the strategy
|
|
13513
|
+
* @param backtest - Whether running in backtest mode
|
|
13514
|
+
* @returns Unique string key for memoization
|
|
13515
|
+
*/
|
|
13516
|
+
const CREATE_KEY_FN = (symbol, strategyName, backtest) => `${symbol}:${strategyName}:${backtest ? "backtest" : "live"}`;
|
|
13424
13517
|
/** Maximum number of events to store in risk reports */
|
|
13425
13518
|
const MAX_EVENTS = 250;
|
|
13426
13519
|
/**
|
|
@@ -13565,7 +13658,7 @@ class RiskMarkdownService {
|
|
|
13565
13658
|
* Memoized function to get or create ReportStorage for a symbol-strategy-backtest triple.
|
|
13566
13659
|
* Each symbol-strategy-backtest combination gets its own isolated storage instance.
|
|
13567
13660
|
*/
|
|
13568
|
-
this.getStorage = memoize(([symbol, strategyName, backtest]) =>
|
|
13661
|
+
this.getStorage = memoize(([symbol, strategyName, backtest]) => CREATE_KEY_FN(symbol, strategyName, backtest), () => new ReportStorage());
|
|
13569
13662
|
/**
|
|
13570
13663
|
* Processes risk rejection events and accumulates them.
|
|
13571
13664
|
* Should be called from riskSubject subscription.
|
|
@@ -13693,7 +13786,7 @@ class RiskMarkdownService {
|
|
|
13693
13786
|
ctx,
|
|
13694
13787
|
});
|
|
13695
13788
|
if (ctx) {
|
|
13696
|
-
const key =
|
|
13789
|
+
const key = CREATE_KEY_FN(ctx.symbol, ctx.strategyName, backtest);
|
|
13697
13790
|
this.getStorage.clear(key);
|
|
13698
13791
|
}
|
|
13699
13792
|
else {
|
|
@@ -14180,6 +14273,81 @@ async function validate(args = {}) {
|
|
|
14180
14273
|
return await validateInternal(args);
|
|
14181
14274
|
}
|
|
14182
14275
|
|
|
14276
|
+
const STOP_METHOD_NAME = "strategy.stop";
|
|
14277
|
+
const CANCEL_METHOD_NAME = "strategy.cancel";
|
|
14278
|
+
/**
|
|
14279
|
+
* Stops the strategy from generating new signals.
|
|
14280
|
+
*
|
|
14281
|
+
* Sets internal flag to prevent strategy from opening new signals.
|
|
14282
|
+
* Current active signal (if any) will complete normally.
|
|
14283
|
+
* Backtest/Live mode will stop at the next safe point (idle state or after signal closes).
|
|
14284
|
+
*
|
|
14285
|
+
* Automatically detects backtest/live mode from execution context.
|
|
14286
|
+
*
|
|
14287
|
+
* @param symbol - Trading pair symbol
|
|
14288
|
+
* @param strategyName - Strategy name to stop
|
|
14289
|
+
* @returns Promise that resolves when stop flag is set
|
|
14290
|
+
*
|
|
14291
|
+
* @example
|
|
14292
|
+
* ```typescript
|
|
14293
|
+
* import { stop } from "backtest-kit";
|
|
14294
|
+
*
|
|
14295
|
+
* // Stop strategy after some condition
|
|
14296
|
+
* await stop("BTCUSDT", "my-strategy");
|
|
14297
|
+
* ```
|
|
14298
|
+
*/
|
|
14299
|
+
async function stop(symbol, strategyName) {
|
|
14300
|
+
backtest$1.loggerService.info(STOP_METHOD_NAME, {
|
|
14301
|
+
symbol,
|
|
14302
|
+
strategyName,
|
|
14303
|
+
});
|
|
14304
|
+
if (!ExecutionContextService.hasContext()) {
|
|
14305
|
+
throw new Error("stop requires an execution context");
|
|
14306
|
+
}
|
|
14307
|
+
if (!MethodContextService.hasContext()) {
|
|
14308
|
+
throw new Error("stop requires a method context");
|
|
14309
|
+
}
|
|
14310
|
+
const { backtest: isBacktest } = backtest$1.executionContextService.context;
|
|
14311
|
+
await backtest$1.strategyCoreService.stop(isBacktest, { symbol, strategyName });
|
|
14312
|
+
}
|
|
14313
|
+
/**
|
|
14314
|
+
* Cancels the scheduled signal without stopping the strategy.
|
|
14315
|
+
*
|
|
14316
|
+
* Clears the scheduled signal (waiting for priceOpen activation).
|
|
14317
|
+
* Does NOT affect active pending signals or strategy operation.
|
|
14318
|
+
* Does NOT set stop flag - strategy can continue generating new signals.
|
|
14319
|
+
*
|
|
14320
|
+
* Automatically detects backtest/live mode from execution context.
|
|
14321
|
+
*
|
|
14322
|
+
* @param symbol - Trading pair symbol
|
|
14323
|
+
* @param strategyName - Strategy name
|
|
14324
|
+
* @param cancelId - Optional cancellation ID for tracking user-initiated cancellations
|
|
14325
|
+
* @returns Promise that resolves when scheduled signal is cancelled
|
|
14326
|
+
*
|
|
14327
|
+
* @example
|
|
14328
|
+
* ```typescript
|
|
14329
|
+
* import { cancel } from "backtest-kit";
|
|
14330
|
+
*
|
|
14331
|
+
* // Cancel scheduled signal with custom ID
|
|
14332
|
+
* await cancel("BTCUSDT", "my-strategy", "manual-cancel-001");
|
|
14333
|
+
* ```
|
|
14334
|
+
*/
|
|
14335
|
+
async function cancel(symbol, strategyName, cancelId) {
|
|
14336
|
+
backtest$1.loggerService.info(CANCEL_METHOD_NAME, {
|
|
14337
|
+
symbol,
|
|
14338
|
+
strategyName,
|
|
14339
|
+
cancelId,
|
|
14340
|
+
});
|
|
14341
|
+
if (!ExecutionContextService.hasContext()) {
|
|
14342
|
+
throw new Error("cancel requires an execution context");
|
|
14343
|
+
}
|
|
14344
|
+
if (!MethodContextService.hasContext()) {
|
|
14345
|
+
throw new Error("cancel requires a method context");
|
|
14346
|
+
}
|
|
14347
|
+
const { backtest: isBacktest } = backtest$1.executionContextService.context;
|
|
14348
|
+
await backtest$1.strategyCoreService.cancel(isBacktest, { symbol, strategyName }, cancelId);
|
|
14349
|
+
}
|
|
14350
|
+
|
|
14183
14351
|
/**
|
|
14184
14352
|
* Sets custom logger implementation for the framework.
|
|
14185
14353
|
*
|
|
@@ -16330,8 +16498,8 @@ class BacktestInstance {
|
|
|
16330
16498
|
}
|
|
16331
16499
|
{
|
|
16332
16500
|
const { riskName, riskList } = backtest$1.strategySchemaService.get(context.strategyName);
|
|
16333
|
-
riskName && backtest$1.riskGlobalService.clear(true, riskName);
|
|
16334
|
-
riskList && riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(true, riskName));
|
|
16501
|
+
riskName && backtest$1.riskGlobalService.clear(true, { riskName });
|
|
16502
|
+
riskList && riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(true, { riskName }));
|
|
16335
16503
|
}
|
|
16336
16504
|
return backtest$1.backtestCommandService.run(symbol, context);
|
|
16337
16505
|
};
|
|
@@ -17004,8 +17172,8 @@ class LiveInstance {
|
|
|
17004
17172
|
}
|
|
17005
17173
|
{
|
|
17006
17174
|
const { riskName, riskList } = backtest$1.strategySchemaService.get(context.strategyName);
|
|
17007
|
-
riskName && backtest$1.riskGlobalService.clear(false, riskName);
|
|
17008
|
-
riskList && riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(false, riskName));
|
|
17175
|
+
riskName && backtest$1.riskGlobalService.clear(false, { riskName });
|
|
17176
|
+
riskList && riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(false, { riskName }));
|
|
17009
17177
|
}
|
|
17010
17178
|
return backtest$1.liveCommandService.run(symbol, context);
|
|
17011
17179
|
};
|
|
@@ -17956,9 +18124,9 @@ class WalkerInstance {
|
|
|
17956
18124
|
}
|
|
17957
18125
|
{
|
|
17958
18126
|
const { riskName, riskList } = backtest$1.strategySchemaService.get(strategyName);
|
|
17959
|
-
riskName && backtest$1.riskGlobalService.clear(true, riskName);
|
|
18127
|
+
riskName && backtest$1.riskGlobalService.clear(true, { riskName });
|
|
17960
18128
|
riskList &&
|
|
17961
|
-
riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(true, riskName));
|
|
18129
|
+
riskList.forEach((riskName) => backtest$1.riskGlobalService.clear(true, { riskName }));
|
|
17962
18130
|
}
|
|
17963
18131
|
}
|
|
17964
18132
|
return backtest$1.walkerCommandService.run(symbol, {
|
|
@@ -19592,4 +19760,4 @@ class CacheUtils {
|
|
|
19592
19760
|
*/
|
|
19593
19761
|
const Cache = new CacheUtils();
|
|
19594
19762
|
|
|
19595
|
-
export { Backtest, Cache, Constant, Exchange, ExecutionContextService, Heat, Live, MethodContextService, Optimizer, Partial, Performance, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, Risk, Schedule, Walker, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, dumpSignal, emitters, formatPrice, formatQuantity, getAveragePrice, getCandles, getColumns, getConfig, getDate, getDefaultColumns, getDefaultConfig, getMode, hasTradeContext, backtest as lib, listExchanges, listFrames, listOptimizers, listRisks, listSizings, listStrategies, listWalkers, listenBacktestProgress, 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, setColumns, setConfig, setLogger, validate };
|
|
19763
|
+
export { Backtest, Cache, Constant, Exchange, ExecutionContextService, Heat, Live, MethodContextService, Optimizer, Partial, Performance, PersistBase, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PositionSize, Risk, Schedule, Walker, addExchange, addFrame, addOptimizer, addRisk, addSizing, addStrategy, addWalker, 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, 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, setColumns, setConfig, setLogger, stop, validate };
|