backtest-kit 2.2.26 → 2.3.2
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 +98 -3
- package/build/index.cjs +462 -142
- package/build/index.mjs +463 -144
- package/package.json +3 -2
- package/types.d.ts +64 -6
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "backtest-kit",
|
|
3
|
-
"version": "2.2
|
|
3
|
+
"version": "2.3.2",
|
|
4
4
|
"description": "A TypeScript library for trading system backtest",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Petr Tripolsky",
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"@rollup/plugin-typescript": "11.1.6",
|
|
61
61
|
"@types/node": "22.9.0",
|
|
62
62
|
"glob": "11.0.1",
|
|
63
|
+
"plantuml": "0.0.2",
|
|
63
64
|
"rimraf": "6.0.1",
|
|
64
65
|
"rollup": "3.29.5",
|
|
65
66
|
"rollup-plugin-dts": "6.1.1",
|
|
@@ -67,13 +68,13 @@
|
|
|
67
68
|
"ts-morph": "27.0.2",
|
|
68
69
|
"tslib": "2.7.0",
|
|
69
70
|
"typedoc": "0.27.9",
|
|
70
|
-
"plantuml": "0.0.2",
|
|
71
71
|
"worker-testbed": "1.0.12"
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"typescript": "^5.0.0"
|
|
75
75
|
},
|
|
76
76
|
"dependencies": {
|
|
77
|
+
"ccxt": "^4.5.35",
|
|
77
78
|
"di-kit": "^1.0.18",
|
|
78
79
|
"di-scoped": "^1.0.20",
|
|
79
80
|
"functools-kit": "^1.0.95",
|
package/types.d.ts
CHANGED
|
@@ -3893,6 +3893,10 @@ interface BreakevenEvent {
|
|
|
3893
3893
|
partialExecuted?: number;
|
|
3894
3894
|
/** Human-readable description of signal reason */
|
|
3895
3895
|
note?: string;
|
|
3896
|
+
/** Timestamp when position became active (ms) */
|
|
3897
|
+
pendingAt?: number;
|
|
3898
|
+
/** Timestamp when signal was created/scheduled (ms) */
|
|
3899
|
+
scheduledAt?: number;
|
|
3896
3900
|
/** True if backtest mode, false if live mode */
|
|
3897
3901
|
backtest: boolean;
|
|
3898
3902
|
}
|
|
@@ -6529,6 +6533,17 @@ declare function getOrderBook(symbol: string, depth?: number): Promise<IOrderBoo
|
|
|
6529
6533
|
* ```
|
|
6530
6534
|
*/
|
|
6531
6535
|
declare function getRawCandles(symbol: string, interval: CandleInterval, limit?: number, sDate?: number, eDate?: number): Promise<ICandleData[]>;
|
|
6536
|
+
/**
|
|
6537
|
+
* Fetches the set of candles after current time based on execution context.
|
|
6538
|
+
*
|
|
6539
|
+
* Uses the exchange's getNextCandles implementation to retrieve candles
|
|
6540
|
+
* that occur after the current context time.
|
|
6541
|
+
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
6542
|
+
* @param interval - Candle interval ("1m" | "3m" | "5m" | "15m" | "30m" | "1h" | "2h" | "4h" | "6h" | "8h")
|
|
6543
|
+
* @param limit - Number of candles to fetch
|
|
6544
|
+
* @returns Promise resolving to array of candle data
|
|
6545
|
+
*/
|
|
6546
|
+
declare function getNextCandles(symbol: string, interval: CandleInterval, limit: number): Promise<ICandleData[]>;
|
|
6532
6547
|
|
|
6533
6548
|
/**
|
|
6534
6549
|
* Portfolio heatmap statistics for a single symbol.
|
|
@@ -7294,6 +7309,10 @@ interface TickEvent {
|
|
|
7294
7309
|
cancelReason?: string;
|
|
7295
7310
|
/** Duration in minutes (only for closed) */
|
|
7296
7311
|
duration?: number;
|
|
7312
|
+
/** Timestamp when position became active (only for opened/active/closed) */
|
|
7313
|
+
pendingAt?: number;
|
|
7314
|
+
/** Timestamp when signal was created/scheduled (only for scheduled/waiting/opened/active/closed/cancelled) */
|
|
7315
|
+
scheduledAt?: number;
|
|
7297
7316
|
}
|
|
7298
7317
|
/**
|
|
7299
7318
|
* Statistical data calculated from live trading results.
|
|
@@ -7403,6 +7422,10 @@ interface ScheduledEvent {
|
|
|
7403
7422
|
cancelReason?: "timeout" | "price_reject" | "user";
|
|
7404
7423
|
/** Cancellation ID (only for user-initiated cancellations) */
|
|
7405
7424
|
cancelId?: string;
|
|
7425
|
+
/** Timestamp when position became active (only for opened events) */
|
|
7426
|
+
pendingAt?: number;
|
|
7427
|
+
/** Timestamp when signal was created/scheduled (for all events) */
|
|
7428
|
+
scheduledAt?: number;
|
|
7406
7429
|
}
|
|
7407
7430
|
/**
|
|
7408
7431
|
* Statistical data calculated from scheduled signals.
|
|
@@ -7575,6 +7598,10 @@ interface PartialEvent {
|
|
|
7575
7598
|
partialExecuted?: number;
|
|
7576
7599
|
/** Human-readable description of signal reason */
|
|
7577
7600
|
note?: string;
|
|
7601
|
+
/** Timestamp when position became active (ms) */
|
|
7602
|
+
pendingAt?: number;
|
|
7603
|
+
/** Timestamp when signal was created/scheduled (ms) */
|
|
7604
|
+
scheduledAt?: number;
|
|
7578
7605
|
/** True if backtest mode, false if live mode */
|
|
7579
7606
|
backtest: boolean;
|
|
7580
7607
|
}
|
|
@@ -8366,22 +8393,33 @@ declare class PersistCandleUtils {
|
|
|
8366
8393
|
usePersistCandleAdapter(Ctor: TPersistBaseCtor<string, CandleData>): void;
|
|
8367
8394
|
/**
|
|
8368
8395
|
* Reads cached candles for a specific exchange, symbol, and interval.
|
|
8369
|
-
* Returns candles only if cache contains
|
|
8396
|
+
* Returns candles only if cache contains ALL requested candles.
|
|
8397
|
+
*
|
|
8398
|
+
* Algorithm (matches ClientExchange.ts logic):
|
|
8399
|
+
* 1. Calculate expected timestamps: sinceTimestamp, sinceTimestamp + stepMs, ..., sinceTimestamp + (limit-1) * stepMs
|
|
8400
|
+
* 2. Try to read each expected candle by timestamp key
|
|
8401
|
+
* 3. If ANY candle is missing, return null (cache miss)
|
|
8402
|
+
* 4. If all candles found, return them in order
|
|
8370
8403
|
*
|
|
8371
8404
|
* @param symbol - Trading pair symbol
|
|
8372
8405
|
* @param interval - Candle interval
|
|
8373
8406
|
* @param exchangeName - Exchange identifier
|
|
8374
8407
|
* @param limit - Number of candles requested
|
|
8375
|
-
* @param sinceTimestamp -
|
|
8376
|
-
* @param
|
|
8408
|
+
* @param sinceTimestamp - Aligned start timestamp (openTime of first candle)
|
|
8409
|
+
* @param _untilTimestamp - Unused, kept for API compatibility
|
|
8377
8410
|
* @returns Promise resolving to array of candles or null if cache is incomplete
|
|
8378
8411
|
*/
|
|
8379
|
-
readCandlesData: (symbol: string, interval: CandleInterval, exchangeName: ExchangeName, limit: number, sinceTimestamp: number,
|
|
8412
|
+
readCandlesData: (symbol: string, interval: CandleInterval, exchangeName: ExchangeName, limit: number, sinceTimestamp: number, _untilTimestamp: number) => Promise<CandleData[] | null>;
|
|
8380
8413
|
/**
|
|
8381
8414
|
* Writes candles to cache with atomic file writes.
|
|
8382
8415
|
* Each candle is stored as a separate JSON file named by its timestamp.
|
|
8383
8416
|
*
|
|
8384
|
-
*
|
|
8417
|
+
* The candles passed to this function should be validated candles from the adapter:
|
|
8418
|
+
* - First candle.timestamp equals aligned sinceTimestamp (openTime)
|
|
8419
|
+
* - Exact number of candles as requested
|
|
8420
|
+
* - All candles are fully closed (timestamp + stepMs < untilTimestamp)
|
|
8421
|
+
*
|
|
8422
|
+
* @param candles - Array of candle data to cache (validated by the caller)
|
|
8385
8423
|
* @param symbol - Trading pair symbol
|
|
8386
8424
|
* @param interval - Candle interval
|
|
8387
8425
|
* @param exchangeName - Exchange identifier
|
|
@@ -15614,6 +15652,13 @@ declare class ClientExchange implements IExchange {
|
|
|
15614
15652
|
/**
|
|
15615
15653
|
* Fetches historical candles backwards from execution context time.
|
|
15616
15654
|
*
|
|
15655
|
+
* Algorithm:
|
|
15656
|
+
* 1. Align when down to interval boundary (e.g., 00:17 -> 00:15 for 15m)
|
|
15657
|
+
* 2. Calculate since = alignedWhen - limit * step
|
|
15658
|
+
* 3. Fetch candles starting from since
|
|
15659
|
+
* 4. Validate first candle timestamp matches since (adapter must return inclusive data)
|
|
15660
|
+
* 5. Slice to limit
|
|
15661
|
+
*
|
|
15617
15662
|
* @param symbol - Trading pair symbol
|
|
15618
15663
|
* @param interval - Candle interval
|
|
15619
15664
|
* @param limit - Number of candles to fetch
|
|
@@ -15624,6 +15669,13 @@ declare class ClientExchange implements IExchange {
|
|
|
15624
15669
|
* Fetches future candles forwards from execution context time.
|
|
15625
15670
|
* Used in backtest mode to get candles for signal duration.
|
|
15626
15671
|
*
|
|
15672
|
+
* Algorithm:
|
|
15673
|
+
* 1. Align when down to interval boundary (e.g., 00:17 -> 00:15 for 15m)
|
|
15674
|
+
* 2. since = alignedWhen (start from aligned when)
|
|
15675
|
+
* 3. Fetch candles starting from since
|
|
15676
|
+
* 4. Validate first candle timestamp matches since (adapter must return inclusive data)
|
|
15677
|
+
* 5. Slice to limit
|
|
15678
|
+
*
|
|
15627
15679
|
* @param symbol - Trading pair symbol
|
|
15628
15680
|
* @param interval - Candle interval
|
|
15629
15681
|
* @param limit - Number of candles to fetch
|
|
@@ -15667,6 +15719,12 @@ declare class ClientExchange implements IExchange {
|
|
|
15667
15719
|
/**
|
|
15668
15720
|
* Fetches raw candles with flexible date/limit parameters.
|
|
15669
15721
|
*
|
|
15722
|
+
* Algorithm:
|
|
15723
|
+
* 1. Align all timestamps down to interval boundary
|
|
15724
|
+
* 2. Fetch candles starting from aligned since
|
|
15725
|
+
* 3. Validate first candle timestamp matches aligned since (adapter must return inclusive data)
|
|
15726
|
+
* 4. Slice to limit
|
|
15727
|
+
*
|
|
15670
15728
|
* All modes respect execution context and prevent look-ahead bias.
|
|
15671
15729
|
*
|
|
15672
15730
|
* Parameter combinations:
|
|
@@ -20054,4 +20112,4 @@ declare const backtest: {
|
|
|
20054
20112
|
loggerService: LoggerService;
|
|
20055
20113
|
};
|
|
20056
20114
|
|
|
20057
|
-
export { ActionBase, type ActivePingContract, Backtest, type BacktestStatisticsModel, Breakeven, type BreakevenAvailableNotification, type BreakevenCommit, type BreakevenCommitNotification, type BreakevenContract, type BreakevenData, Cache, type CancelScheduledCommit, type CandleData, type CandleInterval, type ClosePendingCommit, type ColumnConfig, type ColumnModel, Constant, type CriticalErrorNotification, type DoneContract, type EntityId, Exchange, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type HeatmapStatisticsModel, type IBidData, type IBreakevenCommitRow, type ICandleData, type ICommitRow, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, type IMarkdownDumpOptions, type IOrderBookData, type IPartialLossCommitRow, type IPartialProfitCommitRow, type IPersistBase, type IPositionSizeATRParams, type IPositionSizeFixedPercentageParams, type IPositionSizeKellyParams, type IPublicSignalRow, type IReportDumpOptions, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskSignalRow, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalCancelRow, type IScheduledSignalRow, type ISignalDto, type ISignalRow, type ISizingCalculateParams, type ISizingCalculateParamsATR, type ISizingCalculateParamsFixedPercentage, type ISizingCalculateParamsKelly, type ISizingParams, type ISizingParamsATR, type ISizingParamsFixedPercentage, type ISizingParamsKelly, type ISizingSchema, type ISizingSchemaATR, type ISizingSchemaFixedPercentage, type ISizingSchemaKelly, type IStorageSignalRow, type IStorageUtils, type IStrategyPnL, type IStrategyResult, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IStrategyTickResultWaiting, type ITrailingStopCommitRow, type ITrailingTakeCommitRow, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, type InfoErrorNotification, Live, type LiveStatisticsModel, Markdown, MarkdownFileBase, MarkdownFolderBase, type MarkdownName, MethodContextService, type MetricStats, Notification, type NotificationModel, Partial$1 as Partial, type PartialData, type PartialEvent, type PartialLossAvailableNotification, type PartialLossCommit, type PartialLossCommitNotification, type PartialLossContract, type PartialProfitAvailableNotification, type PartialProfitCommit, type PartialProfitCommitNotification, type PartialProfitContract, type PartialStatisticsModel, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatisticsModel, PersistBase, PersistBreakevenAdapter, PersistCandleAdapter, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PersistStorageAdapter, PositionSize, type ProgressBacktestContract, type ProgressWalkerContract, Report, ReportBase, type ReportName, Risk, type RiskContract, type RiskData, type RiskEvent, type RiskRejectionNotification, type RiskStatisticsModel, Schedule, type ScheduleData, type SchedulePingContract, type ScheduleStatisticsModel, type ScheduledEvent, type SignalCancelledNotification, type SignalClosedNotification, type SignalData, type SignalInterval, type SignalOpenedNotification, type SignalScheduledNotification, Storage, StorageBacktest, type StorageData, StorageLive, Strategy, type StrategyActionType, type StrategyCancelReason, type StrategyCloseReason, type StrategyCommitContract, type StrategyEvent, type StrategyStatisticsModel, type TMarkdownBase, type TPersistBase, type TPersistBaseCtor, type TReportBase, type TStorageUtilsCtor, type TickEvent, type TrailingStopCommit, type TrailingStopCommitNotification, type TrailingTakeCommit, type TrailingTakeCommitNotification, type ValidationErrorNotification, Walker, type WalkerCompleteContract, type WalkerContract, type WalkerMetric, type SignalData$1 as WalkerSignalData, type WalkerStatisticsModel, addActionSchema, addExchangeSchema, addFrameSchema, addRiskSchema, addSizingSchema, addStrategySchema, addWalkerSchema, commitBreakeven, commitCancelScheduled, commitClosePending, commitPartialLoss, commitPartialProfit, commitTrailingStop, commitTrailingTake, emitters, formatPrice, formatQuantity, get, getActionSchema, getAveragePrice, getBacktestTimeframe, getCandles, getColumns, getConfig, getContext, getDate, getDefaultColumns, getDefaultConfig, getExchangeSchema, getFrameSchema, getMode, getOrderBook, getRawCandles, getRiskSchema, getSizingSchema, getStrategySchema, getSymbol, getWalkerSchema, hasTradeContext, backtest as lib, listExchangeSchema, listFrameSchema, listRiskSchema, listSizingSchema, listStrategySchema, listWalkerSchema, listenActivePing, listenActivePingOnce, listenBacktestProgress, listenBreakevenAvailable, listenBreakevenAvailableOnce, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenExit, listenPartialLossAvailable, listenPartialLossAvailableOnce, listenPartialProfitAvailable, listenPartialProfitAvailableOnce, listenPerformance, listenRisk, listenRiskOnce, listenSchedulePing, listenSchedulePingOnce, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenStrategyCommit, listenStrategyCommitOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, overrideActionSchema, overrideExchangeSchema, overrideFrameSchema, overrideRiskSchema, overrideSizingSchema, overrideStrategySchema, overrideWalkerSchema, parseArgs, roundTicks, set, setColumns, setConfig, setLogger, stopStrategy, validate };
|
|
20115
|
+
export { ActionBase, type ActivePingContract, Backtest, type BacktestStatisticsModel, Breakeven, type BreakevenAvailableNotification, type BreakevenCommit, type BreakevenCommitNotification, type BreakevenContract, type BreakevenData, Cache, type CancelScheduledCommit, type CandleData, type CandleInterval, type ClosePendingCommit, type ColumnConfig, type ColumnModel, Constant, type CriticalErrorNotification, type DoneContract, type EntityId, Exchange, ExecutionContextService, type FrameInterval, type GlobalConfig, Heat, type HeatmapStatisticsModel, type IBidData, type IBreakevenCommitRow, type ICandleData, type ICommitRow, type IExchangeSchema, type IFrameSchema, type IHeatmapRow, type IMarkdownDumpOptions, type IOrderBookData, type IPartialLossCommitRow, type IPartialProfitCommitRow, type IPersistBase, type IPositionSizeATRParams, type IPositionSizeFixedPercentageParams, type IPositionSizeKellyParams, type IPublicSignalRow, type IReportDumpOptions, type IRiskActivePosition, type IRiskCheckArgs, type IRiskSchema, type IRiskSignalRow, type IRiskValidation, type IRiskValidationFn, type IRiskValidationPayload, type IScheduledSignalCancelRow, type IScheduledSignalRow, type ISignalDto, type ISignalRow, type ISizingCalculateParams, type ISizingCalculateParamsATR, type ISizingCalculateParamsFixedPercentage, type ISizingCalculateParamsKelly, type ISizingParams, type ISizingParamsATR, type ISizingParamsFixedPercentage, type ISizingParamsKelly, type ISizingSchema, type ISizingSchemaATR, type ISizingSchemaFixedPercentage, type ISizingSchemaKelly, type IStorageSignalRow, type IStorageUtils, type IStrategyPnL, type IStrategyResult, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultCancelled, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, type IStrategyTickResultScheduled, type IStrategyTickResultWaiting, type ITrailingStopCommitRow, type ITrailingTakeCommitRow, type IWalkerResults, type IWalkerSchema, type IWalkerStrategyResult, type InfoErrorNotification, Live, type LiveStatisticsModel, Markdown, MarkdownFileBase, MarkdownFolderBase, type MarkdownName, MethodContextService, type MetricStats, Notification, type NotificationModel, Partial$1 as Partial, type PartialData, type PartialEvent, type PartialLossAvailableNotification, type PartialLossCommit, type PartialLossCommitNotification, type PartialLossContract, type PartialProfitAvailableNotification, type PartialProfitCommit, type PartialProfitCommitNotification, type PartialProfitContract, type PartialStatisticsModel, Performance, type PerformanceContract, type PerformanceMetricType, type PerformanceStatisticsModel, PersistBase, PersistBreakevenAdapter, PersistCandleAdapter, PersistPartialAdapter, PersistRiskAdapter, PersistScheduleAdapter, PersistSignalAdapter, PersistStorageAdapter, PositionSize, type ProgressBacktestContract, type ProgressWalkerContract, Report, ReportBase, type ReportName, Risk, type RiskContract, type RiskData, type RiskEvent, type RiskRejectionNotification, type RiskStatisticsModel, Schedule, type ScheduleData, type SchedulePingContract, type ScheduleStatisticsModel, type ScheduledEvent, type SignalCancelledNotification, type SignalClosedNotification, type SignalData, type SignalInterval, type SignalOpenedNotification, type SignalScheduledNotification, Storage, StorageBacktest, type StorageData, StorageLive, Strategy, type StrategyActionType, type StrategyCancelReason, type StrategyCloseReason, type StrategyCommitContract, type StrategyEvent, type StrategyStatisticsModel, type TMarkdownBase, type TPersistBase, type TPersistBaseCtor, type TReportBase, type TStorageUtilsCtor, type TickEvent, type TrailingStopCommit, type TrailingStopCommitNotification, type TrailingTakeCommit, type TrailingTakeCommitNotification, type ValidationErrorNotification, Walker, type WalkerCompleteContract, type WalkerContract, type WalkerMetric, type SignalData$1 as WalkerSignalData, type WalkerStatisticsModel, addActionSchema, addExchangeSchema, addFrameSchema, addRiskSchema, addSizingSchema, addStrategySchema, addWalkerSchema, commitBreakeven, commitCancelScheduled, commitClosePending, commitPartialLoss, commitPartialProfit, commitTrailingStop, commitTrailingTake, emitters, formatPrice, formatQuantity, get, getActionSchema, getAveragePrice, getBacktestTimeframe, getCandles, getColumns, getConfig, getContext, getDate, getDefaultColumns, getDefaultConfig, getExchangeSchema, getFrameSchema, getMode, getNextCandles, getOrderBook, getRawCandles, getRiskSchema, getSizingSchema, getStrategySchema, getSymbol, getWalkerSchema, hasTradeContext, backtest as lib, listExchangeSchema, listFrameSchema, listRiskSchema, listSizingSchema, listStrategySchema, listWalkerSchema, listenActivePing, listenActivePingOnce, listenBacktestProgress, listenBreakevenAvailable, listenBreakevenAvailableOnce, listenDoneBacktest, listenDoneBacktestOnce, listenDoneLive, listenDoneLiveOnce, listenDoneWalker, listenDoneWalkerOnce, listenError, listenExit, listenPartialLossAvailable, listenPartialLossAvailableOnce, listenPartialProfitAvailable, listenPartialProfitAvailableOnce, listenPerformance, listenRisk, listenRiskOnce, listenSchedulePing, listenSchedulePingOnce, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, listenStrategyCommit, listenStrategyCommitOnce, listenValidation, listenWalker, listenWalkerComplete, listenWalkerOnce, listenWalkerProgress, overrideActionSchema, overrideExchangeSchema, overrideFrameSchema, overrideRiskSchema, overrideSizingSchema, overrideStrategySchema, overrideWalkerSchema, parseArgs, roundTicks, set, setColumns, setConfig, setLogger, stopStrategy, validate };
|