backtest-kit 9.0.1 → 9.0.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/build/index.cjs +169 -133
- package/build/index.mjs +169 -133
- package/package.json +1 -1
- package/types.d.ts +156 -132
package/package.json
CHANGED
package/types.d.ts
CHANGED
|
@@ -142,6 +142,95 @@ interface IFrame {
|
|
|
142
142
|
*/
|
|
143
143
|
type FrameName = string;
|
|
144
144
|
|
|
145
|
+
/**
|
|
146
|
+
* Service for tracking the latest candle timestamp per symbol-strategy-exchange-frame combination.
|
|
147
|
+
*
|
|
148
|
+
* Maintains a memoized BehaviorSubject per unique key that is updated on every strategy tick
|
|
149
|
+
* by StrategyConnectionService. Consumers can synchronously read the last known timestamp or
|
|
150
|
+
* await the first value if none has arrived yet.
|
|
151
|
+
*
|
|
152
|
+
* Primary use case: providing the current candle time outside of a tick execution context,
|
|
153
|
+
* e.g., when a command is triggered between ticks.
|
|
154
|
+
*
|
|
155
|
+
* Features:
|
|
156
|
+
* - One BehaviorSubject per (symbol, strategyName, exchangeName, frameName, backtest) key
|
|
157
|
+
* - Falls back to ExecutionContextService.context.when when called inside an execution context
|
|
158
|
+
* - Waits up to LISTEN_TIMEOUT ms for the first timestamp if none is cached yet
|
|
159
|
+
* - clear() disposes the BehaviorSubject for a single key or all keys
|
|
160
|
+
*
|
|
161
|
+
* Architecture:
|
|
162
|
+
* - Registered as singleton in DI container
|
|
163
|
+
* - Updated by StrategyConnectionService after each tick
|
|
164
|
+
* - Cleared by Backtest/Live/Walker at strategy start to prevent stale data
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const ts = await backtest.timeMetaService.getTimestamp("BTCUSDT", context, false);
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
declare class TimeMetaService {
|
|
172
|
+
private readonly loggerService;
|
|
173
|
+
private readonly executionContextService;
|
|
174
|
+
/**
|
|
175
|
+
* Memoized factory for BehaviorSubject streams keyed by (symbol, strategyName, exchangeName, frameName, backtest).
|
|
176
|
+
*
|
|
177
|
+
* Each subject holds the latest createdAt timestamp emitted by the strategy iterator for that key.
|
|
178
|
+
* Instances are cached until clear() is called.
|
|
179
|
+
*/
|
|
180
|
+
private getSource;
|
|
181
|
+
/**
|
|
182
|
+
* Returns the current candle timestamp (in milliseconds) for the given symbol and context.
|
|
183
|
+
*
|
|
184
|
+
* When called inside an execution context (i.e., during a signal handler or action),
|
|
185
|
+
* reads the timestamp directly from ExecutionContextService.context.when.
|
|
186
|
+
* Otherwise, reads the last value from the cached BehaviorSubject. If no value has
|
|
187
|
+
* been emitted yet, waits up to LISTEN_TIMEOUT ms for the first tick before throwing.
|
|
188
|
+
*
|
|
189
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
190
|
+
* @param context - Strategy, exchange, and frame identifiers
|
|
191
|
+
* @param backtest - True if backtest mode, false if live mode
|
|
192
|
+
* @returns Unix timestamp in milliseconds of the latest processed candle
|
|
193
|
+
* @throws When no timestamp arrives within LISTEN_TIMEOUT ms
|
|
194
|
+
*/
|
|
195
|
+
getTimestamp: (symbol: string, context: {
|
|
196
|
+
strategyName: string;
|
|
197
|
+
exchangeName: string;
|
|
198
|
+
frameName: string;
|
|
199
|
+
}, backtest: boolean) => Promise<number>;
|
|
200
|
+
/**
|
|
201
|
+
* Pushes a new timestamp value into the BehaviorSubject for the given key.
|
|
202
|
+
*
|
|
203
|
+
* Called by StrategyConnectionService after each strategy tick to keep
|
|
204
|
+
* the cached timestamp up to date.
|
|
205
|
+
*
|
|
206
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
207
|
+
* @param timestamp - The createdAt timestamp from the tick (milliseconds)
|
|
208
|
+
* @param context - Strategy, exchange, and frame identifiers
|
|
209
|
+
* @param backtest - True if backtest mode, false if live mode
|
|
210
|
+
*/
|
|
211
|
+
next: (symbol: string, timestamp: number, context: {
|
|
212
|
+
strategyName: string;
|
|
213
|
+
exchangeName: string;
|
|
214
|
+
frameName: string;
|
|
215
|
+
}, backtest: boolean) => Promise<void>;
|
|
216
|
+
/**
|
|
217
|
+
* Disposes cached BehaviorSubject(s) to free memory and prevent stale data.
|
|
218
|
+
*
|
|
219
|
+
* When called without arguments, clears all memoized timestamp streams.
|
|
220
|
+
* When called with a payload, clears only the stream for the specified key.
|
|
221
|
+
* Should be called at strategy start (Backtest/Live/Walker) to reset state.
|
|
222
|
+
*
|
|
223
|
+
* @param payload - Optional key to clear a single stream; omit to clear all
|
|
224
|
+
*/
|
|
225
|
+
clear: (payload?: {
|
|
226
|
+
symbol: string;
|
|
227
|
+
strategyName: string;
|
|
228
|
+
exchangeName: string;
|
|
229
|
+
frameName: string;
|
|
230
|
+
backtest: boolean;
|
|
231
|
+
}) => void;
|
|
232
|
+
}
|
|
233
|
+
|
|
145
234
|
/**
|
|
146
235
|
* Risk rejection result type.
|
|
147
236
|
* Can be void, null, or an IRiskRejectionResult object.
|
|
@@ -281,8 +370,8 @@ interface IRiskParams extends IRiskSchema {
|
|
|
281
370
|
exchangeName: ExchangeName;
|
|
282
371
|
/** Logger service for debug output */
|
|
283
372
|
logger: ILogger;
|
|
284
|
-
/**
|
|
285
|
-
|
|
373
|
+
/** Time context service (when date in backtest/live to prevent look ahead bias) */
|
|
374
|
+
time: TimeMetaService;
|
|
286
375
|
/** True if backtest mode, false if live mode */
|
|
287
376
|
backtest: boolean;
|
|
288
377
|
/**
|
|
@@ -13161,16 +13250,18 @@ interface IPersistRiskInstance {
|
|
|
13161
13250
|
/**
|
|
13162
13251
|
* Read persisted active positions for this context.
|
|
13163
13252
|
*
|
|
13253
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13164
13254
|
* @returns Promise resolving to position entries (empty array if none persisted)
|
|
13165
13255
|
*/
|
|
13166
|
-
readPositionData(): Promise<RiskData>;
|
|
13256
|
+
readPositionData(when: Date): Promise<RiskData>;
|
|
13167
13257
|
/**
|
|
13168
13258
|
* Write active positions for this context.
|
|
13169
13259
|
*
|
|
13170
13260
|
* @param riskRow - Position entries to persist
|
|
13261
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13171
13262
|
* @returns Promise that resolves when write is complete
|
|
13172
13263
|
*/
|
|
13173
|
-
writePositionData(riskRow: RiskData): Promise<void>;
|
|
13264
|
+
writePositionData(riskRow: RiskData, when: Date): Promise<void>;
|
|
13174
13265
|
}
|
|
13175
13266
|
/**
|
|
13176
13267
|
* Default file-based implementation of IPersistRiskInstance.
|
|
@@ -13214,14 +13305,15 @@ declare class PersistRiskInstance implements IPersistRiskInstance {
|
|
|
13214
13305
|
*
|
|
13215
13306
|
* @returns Promise resolving to positions (empty array if none persisted)
|
|
13216
13307
|
*/
|
|
13217
|
-
readPositionData(): Promise<RiskData>;
|
|
13308
|
+
readPositionData(_when: Date): Promise<RiskData>;
|
|
13218
13309
|
/**
|
|
13219
13310
|
* Writes the positions array using the fixed STORAGE_KEY.
|
|
13220
13311
|
*
|
|
13221
13312
|
* @param riskRow - Position entries to persist
|
|
13313
|
+
* @param when - Logical timestamp (reserved for API consistency; not used)
|
|
13222
13314
|
* @returns Promise that resolves when write is complete
|
|
13223
13315
|
*/
|
|
13224
|
-
writePositionData(riskRow: RiskData): Promise<void>;
|
|
13316
|
+
writePositionData(riskRow: RiskData, _when: Date): Promise<void>;
|
|
13225
13317
|
}
|
|
13226
13318
|
/**
|
|
13227
13319
|
* Constructor type for IPersistRiskInstance.
|
|
@@ -13262,9 +13354,10 @@ declare class PersistRiskUtils {
|
|
|
13262
13354
|
*
|
|
13263
13355
|
* @param riskName - Risk profile identifier
|
|
13264
13356
|
* @param exchangeName - Exchange identifier
|
|
13357
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13265
13358
|
* @returns Promise resolving to position entries (empty array if none)
|
|
13266
13359
|
*/
|
|
13267
|
-
readPositionData: (riskName: RiskName, exchangeName: ExchangeName) => Promise<RiskData>;
|
|
13360
|
+
readPositionData: (riskName: RiskName, exchangeName: ExchangeName, when: Date) => Promise<RiskData>;
|
|
13268
13361
|
/**
|
|
13269
13362
|
* Writes active positions for the given risk context.
|
|
13270
13363
|
* Lazily initializes the instance on first access.
|
|
@@ -13272,9 +13365,10 @@ declare class PersistRiskUtils {
|
|
|
13272
13365
|
* @param riskRow - Position entries to persist
|
|
13273
13366
|
* @param riskName - Risk profile identifier
|
|
13274
13367
|
* @param exchangeName - Exchange identifier
|
|
13368
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13275
13369
|
* @returns Promise that resolves when write is complete
|
|
13276
13370
|
*/
|
|
13277
|
-
writePositionData: (riskRow: RiskData, riskName: RiskName, exchangeName: ExchangeName) => Promise<void>;
|
|
13371
|
+
writePositionData: (riskRow: RiskData, riskName: RiskName, exchangeName: ExchangeName, when: Date) => Promise<void>;
|
|
13278
13372
|
/**
|
|
13279
13373
|
* Clears the memoized instance cache.
|
|
13280
13374
|
* Call when process.cwd() changes between strategy iterations.
|
|
@@ -13503,17 +13597,19 @@ interface IPersistPartialInstance {
|
|
|
13503
13597
|
* Read persisted partial data for a specific signal.
|
|
13504
13598
|
*
|
|
13505
13599
|
* @param signalId - Signal identifier
|
|
13600
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13506
13601
|
* @returns Promise resolving to partial data record (empty object if none persisted)
|
|
13507
13602
|
*/
|
|
13508
|
-
readPartialData(signalId: string): Promise<PartialData>;
|
|
13603
|
+
readPartialData(signalId: string, when: Date): Promise<PartialData>;
|
|
13509
13604
|
/**
|
|
13510
13605
|
* Write partial data for a specific signal.
|
|
13511
13606
|
*
|
|
13512
13607
|
* @param data - Partial data record to persist
|
|
13513
13608
|
* @param signalId - Signal identifier
|
|
13609
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13514
13610
|
* @returns Promise that resolves when write is complete
|
|
13515
13611
|
*/
|
|
13516
|
-
writePartialData(data: PartialData, signalId: string): Promise<void>;
|
|
13612
|
+
writePartialData(data: PartialData, signalId: string, when: Date): Promise<void>;
|
|
13517
13613
|
}
|
|
13518
13614
|
/**
|
|
13519
13615
|
* Default file-based implementation of IPersistPartialInstance.
|
|
@@ -13558,15 +13654,16 @@ declare class PersistPartialInstance implements IPersistPartialInstance {
|
|
|
13558
13654
|
* @param signalId - Signal identifier
|
|
13559
13655
|
* @returns Promise resolving to partial data record (empty object if not found)
|
|
13560
13656
|
*/
|
|
13561
|
-
readPartialData(signalId: string): Promise<PartialData>;
|
|
13657
|
+
readPartialData(signalId: string, _when: Date): Promise<PartialData>;
|
|
13562
13658
|
/**
|
|
13563
13659
|
* Writes the partial data for the given signal using `signalId` as the entity key.
|
|
13564
13660
|
*
|
|
13565
13661
|
* @param data - Partial data record to persist
|
|
13566
13662
|
* @param signalId - Signal identifier
|
|
13663
|
+
* @param when - Logical timestamp (reserved for API consistency; not used)
|
|
13567
13664
|
* @returns Promise that resolves when write is complete
|
|
13568
13665
|
*/
|
|
13569
|
-
writePartialData(data: PartialData, signalId: string): Promise<void>;
|
|
13666
|
+
writePartialData(data: PartialData, signalId: string, _when: Date): Promise<void>;
|
|
13570
13667
|
}
|
|
13571
13668
|
/**
|
|
13572
13669
|
* Constructor type for IPersistPartialInstance.
|
|
@@ -13610,9 +13707,10 @@ declare class PersistPartialUtils {
|
|
|
13610
13707
|
* @param strategyName - Strategy identifier
|
|
13611
13708
|
* @param signalId - Signal identifier
|
|
13612
13709
|
* @param exchangeName - Exchange identifier
|
|
13710
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13613
13711
|
* @returns Promise resolving to partial data record (empty object if none)
|
|
13614
13712
|
*/
|
|
13615
|
-
readPartialData: (symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<PartialData>;
|
|
13713
|
+
readPartialData: (symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName, when: Date) => Promise<PartialData>;
|
|
13616
13714
|
/**
|
|
13617
13715
|
* Writes partial data for the given context and signalId.
|
|
13618
13716
|
* Lazily initializes the instance on first access.
|
|
@@ -13622,9 +13720,10 @@ declare class PersistPartialUtils {
|
|
|
13622
13720
|
* @param strategyName - Strategy identifier
|
|
13623
13721
|
* @param signalId - Signal identifier
|
|
13624
13722
|
* @param exchangeName - Exchange identifier
|
|
13723
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13625
13724
|
* @returns Promise that resolves when write is complete
|
|
13626
13725
|
*/
|
|
13627
|
-
writePartialData: (partialData: PartialData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<void>;
|
|
13726
|
+
writePartialData: (partialData: PartialData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName, when: Date) => Promise<void>;
|
|
13628
13727
|
/**
|
|
13629
13728
|
* Clears the memoized instance cache.
|
|
13630
13729
|
* Call when process.cwd() changes between strategy iterations.
|
|
@@ -13683,17 +13782,19 @@ interface IPersistBreakevenInstance {
|
|
|
13683
13782
|
* Read persisted breakeven data for a specific signal.
|
|
13684
13783
|
*
|
|
13685
13784
|
* @param signalId - Signal identifier
|
|
13785
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13686
13786
|
* @returns Promise resolving to breakeven data record (empty object if none persisted)
|
|
13687
13787
|
*/
|
|
13688
|
-
readBreakevenData(signalId: string): Promise<BreakevenData>;
|
|
13788
|
+
readBreakevenData(signalId: string, when: Date): Promise<BreakevenData>;
|
|
13689
13789
|
/**
|
|
13690
13790
|
* Write breakeven data for a specific signal.
|
|
13691
13791
|
*
|
|
13692
13792
|
* @param data - Breakeven data record to persist
|
|
13693
13793
|
* @param signalId - Signal identifier
|
|
13794
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13694
13795
|
* @returns Promise that resolves when write is complete
|
|
13695
13796
|
*/
|
|
13696
|
-
writeBreakevenData(data: BreakevenData, signalId: string): Promise<void>;
|
|
13797
|
+
writeBreakevenData(data: BreakevenData, signalId: string, when: Date): Promise<void>;
|
|
13697
13798
|
}
|
|
13698
13799
|
/**
|
|
13699
13800
|
* Default file-based implementation of IPersistBreakevenInstance.
|
|
@@ -13738,15 +13839,16 @@ declare class PersistBreakevenInstance implements IPersistBreakevenInstance {
|
|
|
13738
13839
|
* @param signalId - Signal identifier
|
|
13739
13840
|
* @returns Promise resolving to breakeven data record (empty object if not found)
|
|
13740
13841
|
*/
|
|
13741
|
-
readBreakevenData(signalId: string): Promise<BreakevenData>;
|
|
13842
|
+
readBreakevenData(signalId: string, _when: Date): Promise<BreakevenData>;
|
|
13742
13843
|
/**
|
|
13743
13844
|
* Writes the breakeven data for the given signal using `signalId` as the entity key.
|
|
13744
13845
|
*
|
|
13745
13846
|
* @param data - Breakeven data record to persist
|
|
13746
13847
|
* @param signalId - Signal identifier
|
|
13848
|
+
* @param when - Logical timestamp (reserved for API consistency; not used)
|
|
13747
13849
|
* @returns Promise that resolves when write is complete
|
|
13748
13850
|
*/
|
|
13749
|
-
writeBreakevenData(data: BreakevenData, signalId: string): Promise<void>;
|
|
13851
|
+
writeBreakevenData(data: BreakevenData, signalId: string, _when: Date): Promise<void>;
|
|
13750
13852
|
}
|
|
13751
13853
|
/**
|
|
13752
13854
|
* Constructor type for IPersistBreakevenInstance.
|
|
@@ -13810,9 +13912,10 @@ declare class PersistBreakevenUtils {
|
|
|
13810
13912
|
* @param strategyName - Strategy identifier
|
|
13811
13913
|
* @param signalId - Signal identifier
|
|
13812
13914
|
* @param exchangeName - Exchange identifier
|
|
13915
|
+
* @param when - Logical timestamp at which the read is happening (reserved for API consistency)
|
|
13813
13916
|
* @returns Promise resolving to breakeven data record (empty object if none)
|
|
13814
13917
|
*/
|
|
13815
|
-
readBreakevenData: (symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<BreakevenData>;
|
|
13918
|
+
readBreakevenData: (symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName, when: Date) => Promise<BreakevenData>;
|
|
13816
13919
|
/**
|
|
13817
13920
|
* Writes breakeven data for the given context and signalId.
|
|
13818
13921
|
* Lazily initializes the instance on first access.
|
|
@@ -13822,9 +13925,10 @@ declare class PersistBreakevenUtils {
|
|
|
13822
13925
|
* @param strategyName - Strategy identifier
|
|
13823
13926
|
* @param signalId - Signal identifier
|
|
13824
13927
|
* @param exchangeName - Exchange identifier
|
|
13928
|
+
* @param when - Logical timestamp this write belongs to (reserved for API consistency)
|
|
13825
13929
|
* @returns Promise that resolves when write is complete
|
|
13826
13930
|
*/
|
|
13827
|
-
writeBreakevenData: (breakevenData: BreakevenData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName) => Promise<void>;
|
|
13931
|
+
writeBreakevenData: (breakevenData: BreakevenData, symbol: string, strategyName: StrategyName, signalId: string, exchangeName: ExchangeName, when: Date) => Promise<void>;
|
|
13828
13932
|
/**
|
|
13829
13933
|
* Clears the memoized instance cache.
|
|
13830
13934
|
* Call when process.cwd() changes between strategy iterations.
|
|
@@ -14964,11 +15068,12 @@ interface IPersistMemoryInstance {
|
|
|
14964
15068
|
/**
|
|
14965
15069
|
* Write a memory entry.
|
|
14966
15070
|
*
|
|
14967
|
-
* @param data - Entry data to persist
|
|
15071
|
+
* @param data - Entry data to persist (already carries `data.when`)
|
|
14968
15072
|
* @param memoryId - Memory entry identifier
|
|
15073
|
+
* @param when - Logical timestamp this entry belongs to (duplicates `data.when` for API consistency)
|
|
14969
15074
|
* @returns Promise that resolves when write is complete
|
|
14970
15075
|
*/
|
|
14971
|
-
writeMemoryData(data: MemoryData, memoryId: string): Promise<void>;
|
|
15076
|
+
writeMemoryData(data: MemoryData, memoryId: string, when: Date): Promise<void>;
|
|
14972
15077
|
/**
|
|
14973
15078
|
* Soft-delete a memory entry. File stays on disk; subsequent reads return null.
|
|
14974
15079
|
*
|
|
@@ -15049,7 +15154,7 @@ declare class PersistMemoryInstance implements IPersistMemoryInstance {
|
|
|
15049
15154
|
* @param memoryId - Memory entry identifier
|
|
15050
15155
|
* @returns Promise that resolves when write is complete
|
|
15051
15156
|
*/
|
|
15052
|
-
writeMemoryData(data: MemoryData, memoryId: string): Promise<void>;
|
|
15157
|
+
writeMemoryData(data: MemoryData, memoryId: string, _when: Date): Promise<void>;
|
|
15053
15158
|
/**
|
|
15054
15159
|
* Soft-deletes a memory entry by writing `removed: true` flag.
|
|
15055
15160
|
*
|
|
@@ -15142,13 +15247,14 @@ declare class PersistMemoryUtils {
|
|
|
15142
15247
|
* Writes a memory entry for the given context.
|
|
15143
15248
|
* Lazily initializes the instance on first access.
|
|
15144
15249
|
*
|
|
15145
|
-
* @param data - Entry data to persist
|
|
15250
|
+
* @param data - Entry data to persist (already carries `data.when`)
|
|
15146
15251
|
* @param signalId - Signal identifier
|
|
15147
15252
|
* @param bucketName - Bucket name
|
|
15148
15253
|
* @param memoryId - Memory entry identifier
|
|
15254
|
+
* @param when - Logical timestamp this entry belongs to (duplicates `data.when` for API consistency)
|
|
15149
15255
|
* @returns Promise that resolves when write is complete
|
|
15150
15256
|
*/
|
|
15151
|
-
writeMemoryData: (data: MemoryData, signalId: string, bucketName: string, memoryId: string) => Promise<void>;
|
|
15257
|
+
writeMemoryData: (data: MemoryData, signalId: string, bucketName: string, memoryId: string, when: Date) => Promise<void>;
|
|
15152
15258
|
/**
|
|
15153
15259
|
* Soft-deletes a memory entry for the given context.
|
|
15154
15260
|
* Lazily initializes the instance on first access.
|
|
@@ -15241,10 +15347,11 @@ interface IPersistRecentInstance {
|
|
|
15241
15347
|
/**
|
|
15242
15348
|
* Write the latest recent signal for this context.
|
|
15243
15349
|
*
|
|
15244
|
-
* @param signalRow - Recent signal data to persist
|
|
15350
|
+
* @param signalRow - Recent signal data to persist (already carries `signalRow.timestamp`)
|
|
15351
|
+
* @param when - Logical timestamp this signal belongs to (duplicates `signalRow.timestamp` for API consistency)
|
|
15245
15352
|
* @returns Promise that resolves when write is complete
|
|
15246
15353
|
*/
|
|
15247
|
-
writeRecentData(signalRow: IPublicSignalRow): Promise<void>;
|
|
15354
|
+
writeRecentData(signalRow: IPublicSignalRow, when: Date): Promise<void>;
|
|
15248
15355
|
}
|
|
15249
15356
|
/**
|
|
15250
15357
|
* Default file-based implementation of IPersistRecentInstance.
|
|
@@ -15299,7 +15406,7 @@ declare class PersistRecentInstance implements IPersistRecentInstance {
|
|
|
15299
15406
|
* @param signalRow - Recent signal data to persist
|
|
15300
15407
|
* @returns Promise that resolves when write is complete
|
|
15301
15408
|
*/
|
|
15302
|
-
writeRecentData(signalRow: IPublicSignalRow): Promise<void>;
|
|
15409
|
+
writeRecentData(signalRow: IPublicSignalRow, _when: Date): Promise<void>;
|
|
15303
15410
|
}
|
|
15304
15411
|
/**
|
|
15305
15412
|
* Constructor type for IPersistRecentInstance.
|
|
@@ -15362,15 +15469,16 @@ declare class PersistRecentUtils {
|
|
|
15362
15469
|
* Writes the latest recent signal for the given context.
|
|
15363
15470
|
* Lazily initializes the instance on first access.
|
|
15364
15471
|
*
|
|
15365
|
-
* @param signalRow - Recent signal data to persist
|
|
15472
|
+
* @param signalRow - Recent signal data to persist (already carries `signalRow.timestamp`)
|
|
15366
15473
|
* @param symbol - Trading pair symbol
|
|
15367
15474
|
* @param strategyName - Strategy identifier
|
|
15368
15475
|
* @param exchangeName - Exchange identifier
|
|
15369
15476
|
* @param frameName - Frame identifier (may be empty)
|
|
15370
15477
|
* @param backtest - True for backtest mode, false for live mode
|
|
15478
|
+
* @param when - Logical timestamp this signal belongs to (duplicates `signalRow.timestamp` for API consistency)
|
|
15371
15479
|
* @returns Promise that resolves when write is complete
|
|
15372
15480
|
*/
|
|
15373
|
-
writeRecentData: (signalRow: IPublicSignalRow, symbol: string, strategyName: StrategyName, exchangeName: ExchangeName, frameName: FrameName, backtest: boolean) => Promise<void>;
|
|
15481
|
+
writeRecentData: (signalRow: IPublicSignalRow, symbol: string, strategyName: StrategyName, exchangeName: ExchangeName, frameName: FrameName, backtest: boolean, when: Date) => Promise<void>;
|
|
15374
15482
|
/**
|
|
15375
15483
|
* Clears the memoized instance cache.
|
|
15376
15484
|
* Call when process.cwd() changes between strategy iterations.
|
|
@@ -15424,10 +15532,11 @@ interface IPersistStateInstance {
|
|
|
15424
15532
|
/**
|
|
15425
15533
|
* Write state for this context.
|
|
15426
15534
|
*
|
|
15427
|
-
* @param data - State data to persist
|
|
15535
|
+
* @param data - State data to persist (already carries `data.when`)
|
|
15536
|
+
* @param when - Logical timestamp this value belongs to (duplicates `data.when` for API consistency)
|
|
15428
15537
|
* @returns Promise that resolves when write is complete
|
|
15429
15538
|
*/
|
|
15430
|
-
writeStateData(data: StateData): Promise<void>;
|
|
15539
|
+
writeStateData(data: StateData, when: Date): Promise<void>;
|
|
15431
15540
|
/**
|
|
15432
15541
|
* Release any resources held by this instance.
|
|
15433
15542
|
* Default implementations may treat this as a no-op.
|
|
@@ -15481,7 +15590,7 @@ declare class PersistStateInstance implements IPersistStateInstance {
|
|
|
15481
15590
|
* @param data - State data to persist
|
|
15482
15591
|
* @returns Promise that resolves when write is complete
|
|
15483
15592
|
*/
|
|
15484
|
-
writeStateData(data: StateData): Promise<void>;
|
|
15593
|
+
writeStateData(data: StateData, _when: Date): Promise<void>;
|
|
15485
15594
|
/**
|
|
15486
15595
|
* No-op for the default file-based implementation.
|
|
15487
15596
|
* Resource cleanup (memo cache invalidation) is handled by PersistStateUtils.dispose().
|
|
@@ -15545,12 +15654,13 @@ declare class PersistStateUtils {
|
|
|
15545
15654
|
* Writes state for the given context.
|
|
15546
15655
|
* Lazily initializes the instance on first access.
|
|
15547
15656
|
*
|
|
15548
|
-
* @param data - State data to persist
|
|
15657
|
+
* @param data - State data to persist (already carries `data.when`)
|
|
15549
15658
|
* @param signalId - Signal identifier
|
|
15550
15659
|
* @param bucketName - Bucket name
|
|
15660
|
+
* @param when - Logical timestamp this value belongs to (duplicates `data.when` for API consistency)
|
|
15551
15661
|
* @returns Promise that resolves when write is complete
|
|
15552
15662
|
*/
|
|
15553
|
-
writeStateData: (data: StateData, signalId: string, bucketName: string) => Promise<void>;
|
|
15663
|
+
writeStateData: (data: StateData, signalId: string, bucketName: string, when: Date) => Promise<void>;
|
|
15554
15664
|
/**
|
|
15555
15665
|
* Switches to PersistStateDummyInstance (all operations are no-ops).
|
|
15556
15666
|
*/
|
|
@@ -15612,10 +15722,11 @@ interface IPersistSessionInstance {
|
|
|
15612
15722
|
/**
|
|
15613
15723
|
* Write session data for this context.
|
|
15614
15724
|
*
|
|
15615
|
-
* @param data - Session data to persist
|
|
15725
|
+
* @param data - Session data to persist (already carries `data.when`)
|
|
15726
|
+
* @param when - Logical timestamp this value belongs to (duplicates `data.when` for API consistency)
|
|
15616
15727
|
* @returns Promise that resolves when write is complete
|
|
15617
15728
|
*/
|
|
15618
|
-
writeSessionData(data: SessionData): Promise<void>;
|
|
15729
|
+
writeSessionData(data: SessionData, when: Date): Promise<void>;
|
|
15619
15730
|
/**
|
|
15620
15731
|
* Release any resources held by this instance.
|
|
15621
15732
|
* Default implementations may treat this as a no-op.
|
|
@@ -15671,7 +15782,7 @@ declare class PersistSessionInstance implements IPersistSessionInstance {
|
|
|
15671
15782
|
* @param data - Session data to persist
|
|
15672
15783
|
* @returns Promise that resolves when write is complete
|
|
15673
15784
|
*/
|
|
15674
|
-
writeSessionData(data: SessionData): Promise<void>;
|
|
15785
|
+
writeSessionData(data: SessionData, _when: Date): Promise<void>;
|
|
15675
15786
|
/**
|
|
15676
15787
|
* No-op for the default file-based implementation.
|
|
15677
15788
|
* Resource cleanup (memo cache invalidation) is handled by PersistSessionUtils.dispose().
|
|
@@ -15738,13 +15849,14 @@ declare class PersistSessionUtils {
|
|
|
15738
15849
|
* Writes session data for the given context.
|
|
15739
15850
|
* Lazily initializes the instance on first access.
|
|
15740
15851
|
*
|
|
15741
|
-
* @param data - Session data to persist
|
|
15852
|
+
* @param data - Session data to persist (already carries `data.when`)
|
|
15742
15853
|
* @param strategyName - Strategy identifier
|
|
15743
15854
|
* @param exchangeName - Exchange identifier
|
|
15744
15855
|
* @param frameName - Frame identifier
|
|
15856
|
+
* @param when - Logical timestamp this value belongs to (duplicates `data.when` for API consistency)
|
|
15745
15857
|
* @returns Promise that resolves when write is complete
|
|
15746
15858
|
*/
|
|
15747
|
-
writeSessionData: (data: SessionData, strategyName: string, exchangeName: string, frameName: string) => Promise<void>;
|
|
15859
|
+
writeSessionData: (data: SessionData, strategyName: string, exchangeName: string, frameName: string, when: Date) => Promise<void>;
|
|
15748
15860
|
/**
|
|
15749
15861
|
* Switches to PersistSessionDummyInstance (all operations are no-ops).
|
|
15750
15862
|
*/
|
|
@@ -28899,6 +29011,7 @@ declare class ClientRisk implements IRisk {
|
|
|
28899
29011
|
strategyName: StrategyName;
|
|
28900
29012
|
riskName: RiskName;
|
|
28901
29013
|
exchangeName: ExchangeName;
|
|
29014
|
+
frameName: string;
|
|
28902
29015
|
}): Promise<void>;
|
|
28903
29016
|
/**
|
|
28904
29017
|
* Checks if a signal should be allowed based on risk limits.
|
|
@@ -29322,9 +29435,7 @@ declare class RiskConnectionService implements TRisk$1 {
|
|
|
29322
29435
|
setLogger: (logger: ILogger) => void;
|
|
29323
29436
|
};
|
|
29324
29437
|
readonly riskSchemaService: RiskSchemaService;
|
|
29325
|
-
readonly
|
|
29326
|
-
readonly context: IExecutionContext;
|
|
29327
|
-
};
|
|
29438
|
+
readonly timeMetaService: TimeMetaService;
|
|
29328
29439
|
/**
|
|
29329
29440
|
* Action core service injected from DI container.
|
|
29330
29441
|
*/
|
|
@@ -29484,6 +29595,7 @@ declare class PartialConnectionService implements IPartial {
|
|
|
29484
29595
|
* Action core service injected from DI container.
|
|
29485
29596
|
*/
|
|
29486
29597
|
readonly actionCoreService: ActionCoreService;
|
|
29598
|
+
readonly timeMetaService: TimeMetaService;
|
|
29487
29599
|
/**
|
|
29488
29600
|
* Memoized factory function for ClientPartial instances.
|
|
29489
29601
|
*
|
|
@@ -29600,6 +29712,7 @@ declare class BreakevenConnectionService implements IBreakeven {
|
|
|
29600
29712
|
* Action core service injected from DI container.
|
|
29601
29713
|
*/
|
|
29602
29714
|
readonly actionCoreService: ActionCoreService;
|
|
29715
|
+
readonly timeMetaService: TimeMetaService;
|
|
29603
29716
|
/**
|
|
29604
29717
|
* Memoized factory function for ClientBreakeven instances.
|
|
29605
29718
|
*
|
|
@@ -29645,95 +29758,6 @@ declare class BreakevenConnectionService implements IBreakeven {
|
|
|
29645
29758
|
clear: (symbol: string, data: IPublicSignalRow, priceClose: number, backtest: boolean) => Promise<void>;
|
|
29646
29759
|
}
|
|
29647
29760
|
|
|
29648
|
-
/**
|
|
29649
|
-
* Service for tracking the latest candle timestamp per symbol-strategy-exchange-frame combination.
|
|
29650
|
-
*
|
|
29651
|
-
* Maintains a memoized BehaviorSubject per unique key that is updated on every strategy tick
|
|
29652
|
-
* by StrategyConnectionService. Consumers can synchronously read the last known timestamp or
|
|
29653
|
-
* await the first value if none has arrived yet.
|
|
29654
|
-
*
|
|
29655
|
-
* Primary use case: providing the current candle time outside of a tick execution context,
|
|
29656
|
-
* e.g., when a command is triggered between ticks.
|
|
29657
|
-
*
|
|
29658
|
-
* Features:
|
|
29659
|
-
* - One BehaviorSubject per (symbol, strategyName, exchangeName, frameName, backtest) key
|
|
29660
|
-
* - Falls back to ExecutionContextService.context.when when called inside an execution context
|
|
29661
|
-
* - Waits up to LISTEN_TIMEOUT ms for the first timestamp if none is cached yet
|
|
29662
|
-
* - clear() disposes the BehaviorSubject for a single key or all keys
|
|
29663
|
-
*
|
|
29664
|
-
* Architecture:
|
|
29665
|
-
* - Registered as singleton in DI container
|
|
29666
|
-
* - Updated by StrategyConnectionService after each tick
|
|
29667
|
-
* - Cleared by Backtest/Live/Walker at strategy start to prevent stale data
|
|
29668
|
-
*
|
|
29669
|
-
* @example
|
|
29670
|
-
* ```typescript
|
|
29671
|
-
* const ts = await backtest.timeMetaService.getTimestamp("BTCUSDT", context, false);
|
|
29672
|
-
* ```
|
|
29673
|
-
*/
|
|
29674
|
-
declare class TimeMetaService {
|
|
29675
|
-
private readonly loggerService;
|
|
29676
|
-
private readonly executionContextService;
|
|
29677
|
-
/**
|
|
29678
|
-
* Memoized factory for BehaviorSubject streams keyed by (symbol, strategyName, exchangeName, frameName, backtest).
|
|
29679
|
-
*
|
|
29680
|
-
* Each subject holds the latest createdAt timestamp emitted by the strategy iterator for that key.
|
|
29681
|
-
* Instances are cached until clear() is called.
|
|
29682
|
-
*/
|
|
29683
|
-
private getSource;
|
|
29684
|
-
/**
|
|
29685
|
-
* Returns the current candle timestamp (in milliseconds) for the given symbol and context.
|
|
29686
|
-
*
|
|
29687
|
-
* When called inside an execution context (i.e., during a signal handler or action),
|
|
29688
|
-
* reads the timestamp directly from ExecutionContextService.context.when.
|
|
29689
|
-
* Otherwise, reads the last value from the cached BehaviorSubject. If no value has
|
|
29690
|
-
* been emitted yet, waits up to LISTEN_TIMEOUT ms for the first tick before throwing.
|
|
29691
|
-
*
|
|
29692
|
-
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
29693
|
-
* @param context - Strategy, exchange, and frame identifiers
|
|
29694
|
-
* @param backtest - True if backtest mode, false if live mode
|
|
29695
|
-
* @returns Unix timestamp in milliseconds of the latest processed candle
|
|
29696
|
-
* @throws When no timestamp arrives within LISTEN_TIMEOUT ms
|
|
29697
|
-
*/
|
|
29698
|
-
getTimestamp: (symbol: string, context: {
|
|
29699
|
-
strategyName: string;
|
|
29700
|
-
exchangeName: string;
|
|
29701
|
-
frameName: string;
|
|
29702
|
-
}, backtest: boolean) => Promise<number>;
|
|
29703
|
-
/**
|
|
29704
|
-
* Pushes a new timestamp value into the BehaviorSubject for the given key.
|
|
29705
|
-
*
|
|
29706
|
-
* Called by StrategyConnectionService after each strategy tick to keep
|
|
29707
|
-
* the cached timestamp up to date.
|
|
29708
|
-
*
|
|
29709
|
-
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
29710
|
-
* @param timestamp - The createdAt timestamp from the tick (milliseconds)
|
|
29711
|
-
* @param context - Strategy, exchange, and frame identifiers
|
|
29712
|
-
* @param backtest - True if backtest mode, false if live mode
|
|
29713
|
-
*/
|
|
29714
|
-
next: (symbol: string, timestamp: number, context: {
|
|
29715
|
-
strategyName: string;
|
|
29716
|
-
exchangeName: string;
|
|
29717
|
-
frameName: string;
|
|
29718
|
-
}, backtest: boolean) => Promise<void>;
|
|
29719
|
-
/**
|
|
29720
|
-
* Disposes cached BehaviorSubject(s) to free memory and prevent stale data.
|
|
29721
|
-
*
|
|
29722
|
-
* When called without arguments, clears all memoized timestamp streams.
|
|
29723
|
-
* When called with a payload, clears only the stream for the specified key.
|
|
29724
|
-
* Should be called at strategy start (Backtest/Live/Walker) to reset state.
|
|
29725
|
-
*
|
|
29726
|
-
* @param payload - Optional key to clear a single stream; omit to clear all
|
|
29727
|
-
*/
|
|
29728
|
-
clear: (payload?: {
|
|
29729
|
-
symbol: string;
|
|
29730
|
-
strategyName: string;
|
|
29731
|
-
exchangeName: string;
|
|
29732
|
-
frameName: string;
|
|
29733
|
-
backtest: boolean;
|
|
29734
|
-
}) => void;
|
|
29735
|
-
}
|
|
29736
|
-
|
|
29737
29761
|
/**
|
|
29738
29762
|
* Service for tracking the latest market price per symbol-strategy-exchange-frame combination.
|
|
29739
29763
|
*
|