backtest-kit 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/types.d.ts ADDED
@@ -0,0 +1,213 @@
1
+ import * as di_scoped from 'di-scoped';
2
+ import * as functools_kit from 'functools-kit';
3
+
4
+ interface IExecutionContext {
5
+ when: Date;
6
+ backtest: boolean;
7
+ }
8
+ declare const ExecutionContextService: (new () => {
9
+ readonly context: IExecutionContext;
10
+ }) & Omit<{
11
+ new (context: IExecutionContext): {
12
+ readonly context: IExecutionContext;
13
+ };
14
+ }, "prototype"> & di_scoped.IScopedClassRun<[context: IExecutionContext]>;
15
+ type TExecutionContextService = InstanceType<typeof ExecutionContextService>;
16
+
17
+ /**
18
+ * Interface representing a logging mechanism for the swarm system.
19
+ * Provides methods to record messages at different severity levels, used across components like agents, sessions, states, storage, swarms, history, embeddings, completions, and policies.
20
+ * Logs are utilized to track lifecycle events (e.g., initialization, disposal), operational details (e.g., tool calls, message emissions), validation outcomes (e.g., policy checks), and errors (e.g., persistence failures), aiding in debugging, monitoring, and auditing.
21
+ */
22
+ interface ILogger {
23
+ /**
24
+ * Logs a general-purpose message.
25
+ * Used throughout the swarm system to record significant events or state changes, such as agent execution, session connections, or storage updates.
26
+ */
27
+ log(topic: string, ...args: any[]): void;
28
+ /**
29
+ * Logs a debug-level message.
30
+ * Employed for detailed diagnostic information, such as intermediate states during agent tool calls, swarm navigation changes, or embedding creation processes, typically enabled in development or troubleshooting scenarios.
31
+ */
32
+ debug(topic: string, ...args: any[]): void;
33
+ /**
34
+ * Logs an info-level message.
35
+ * Used to record informational updates, such as successful completions, policy validations, or history commits, providing a high-level overview of system activity without excessive detail.
36
+ */
37
+ info(topic: string, ...args: any[]): void;
38
+ }
39
+
40
+ type CandleInterval = "1m" | "3m" | "5m" | "15m" | "30m" | "1h" | "2h" | "4h" | "6h" | "8h";
41
+ interface ICandleData {
42
+ timestamp: number;
43
+ open: number;
44
+ high: number;
45
+ low: number;
46
+ close: number;
47
+ volume: number;
48
+ }
49
+ interface ICandleParams extends ICandleSchema {
50
+ logger: ILogger;
51
+ execution: TExecutionContextService;
52
+ }
53
+ interface ICandleCallbacks {
54
+ onCandleData: (symbol: string, interval: CandleInterval, since: Date, limit: number, data: ICandleData[]) => void;
55
+ }
56
+ interface ICandleSchema {
57
+ getCandles: (symbol: string, interval: CandleInterval, since: Date, limit: number) => Promise<ICandleData[]>;
58
+ callbacks?: Partial<ICandleCallbacks>;
59
+ }
60
+ interface ICandle {
61
+ getCandles: (symbol: string, interval: CandleInterval, limit: number) => Promise<ICandleData[]>;
62
+ getAveragePrice: (symbol: string) => Promise<number>;
63
+ }
64
+
65
+ interface ISignalData {
66
+ id: string;
67
+ position: "long" | "short";
68
+ note: string;
69
+ priceOpen: number;
70
+ priceTakeProfit: number;
71
+ priceStopLoss: number;
72
+ minuteEstimatedTime: number;
73
+ timestamp: number;
74
+ }
75
+ interface IStrategyCallbacks {
76
+ onOpen: (backtest: boolean, symbol: string, data: ISignalData) => void;
77
+ onClose: (backtest: boolean, symbol: string, priceClose: number, data: ISignalData) => void;
78
+ }
79
+ interface IStrategySchema {
80
+ getSignal: (symbol: string) => Promise<ISignalData | null>;
81
+ callbacks?: Partial<IStrategyCallbacks>;
82
+ }
83
+ type StrategyCloseReason = "time_expired" | "take_profit" | "stop_loss";
84
+ interface IStrategyPnL {
85
+ pnlPercentage: number;
86
+ priceOpen: number;
87
+ priceClose: number;
88
+ }
89
+ interface IStrategyTickResultIdle {
90
+ action: "idle";
91
+ signal: null;
92
+ }
93
+ interface IStrategyTickResultOpened {
94
+ action: "opened";
95
+ signal: ISignalData;
96
+ }
97
+ interface IStrategyTickResultActive {
98
+ action: "active";
99
+ signal: ISignalData;
100
+ currentPrice: number;
101
+ }
102
+ interface IStrategyTickResultClosed {
103
+ action: "closed";
104
+ signal: ISignalData;
105
+ currentPrice: number;
106
+ closeReason: StrategyCloseReason;
107
+ pnl: IStrategyPnL;
108
+ }
109
+ type IStrategyTickResult = IStrategyTickResultIdle | IStrategyTickResultOpened | IStrategyTickResultActive | IStrategyTickResultClosed;
110
+ interface IStrategy {
111
+ tick: (symbol: string) => Promise<IStrategyTickResult>;
112
+ }
113
+
114
+ declare function addStrategy(strategySchema: IStrategySchema): void;
115
+ declare function addCandle(candleSchema: ICandleSchema): void;
116
+
117
+ interface IBacktestResult {
118
+ symbol: string;
119
+ results: IStrategyTickResult[];
120
+ }
121
+ declare function runBacktest(symbol: string, timeframes: Date[]): Promise<IBacktestResult>;
122
+ declare function runBacktestGUI(symbol: string, timeframes: Date[]): Promise<void>;
123
+
124
+ interface IReduceResult<T> {
125
+ symbol: string;
126
+ accumulator: T;
127
+ totalTicks: number;
128
+ }
129
+ type ReduceCallback<T> = (accumulator: T, index: number, when: Date, symbol: string) => T | Promise<T>;
130
+ declare function reduce<T>(symbol: string, timeframes: Date[], callback: ReduceCallback<T>, initialValue: T): Promise<IReduceResult<T>>;
131
+
132
+ interface IRunConfig {
133
+ symbol: string;
134
+ interval: number;
135
+ }
136
+ declare function startRun(config: IRunConfig): void;
137
+ declare function stopRun(symbol: string): void;
138
+ declare function stopAll(): void;
139
+
140
+ declare class LoggerService implements ILogger {
141
+ private _commonLogger;
142
+ log: (topic: string, ...args: any[]) => Promise<void>;
143
+ debug: (topic: string, ...args: any[]) => Promise<void>;
144
+ info: (topic: string, ...args: any[]) => Promise<void>;
145
+ setLogger: (logger: ILogger) => void;
146
+ }
147
+
148
+ declare class ClientCandle implements ICandle {
149
+ readonly params: ICandleParams;
150
+ constructor(params: ICandleParams);
151
+ getCandles: (symbol: string, interval: CandleInterval, limit: number) => Promise<ICandleData[]>;
152
+ getAveragePrice: (symbol: string) => Promise<number>;
153
+ }
154
+
155
+ declare class CandleConnectionService implements ICandle {
156
+ private readonly loggerService;
157
+ private readonly executionContextService;
158
+ private readonly candleSchemaService;
159
+ getCandle: ((symbol: string) => ClientCandle) & functools_kit.IClearableMemoize<string> & functools_kit.IControlMemoize<string, ClientCandle>;
160
+ getCandles: (symbol: string, interval: CandleInterval, limit: number) => Promise<ICandleData[]>;
161
+ getAveragePrice: (symbol: string) => Promise<number>;
162
+ }
163
+
164
+ declare class CandleSchemaService {
165
+ private readonly loggerService;
166
+ private _candleSchema;
167
+ getSchema: () => ICandleSchema;
168
+ addSchema: (candleSchema: ICandleSchema) => void;
169
+ }
170
+
171
+ declare class StrategySchemaService {
172
+ private readonly loggerService;
173
+ private _strategySchema;
174
+ getSchema: () => IStrategySchema;
175
+ addSchema: (strategySchema: IStrategySchema) => void;
176
+ }
177
+
178
+ declare class StrategyConnectionService implements IStrategy {
179
+ private readonly loggerService;
180
+ private readonly executionContextService;
181
+ private readonly strategySchemaService;
182
+ private readonly candleConnectionService;
183
+ private getStrategy;
184
+ tick: (symbol: string) => Promise<IStrategyTickResult>;
185
+ }
186
+
187
+ declare class CandlePublicService {
188
+ private readonly loggerService;
189
+ private readonly candleConnectionService;
190
+ getCandles: (symbol: string, interval: CandleInterval, limit: number, when: Date, backtest: boolean) => Promise<ICandleData[]>;
191
+ getAveragePrice: (symbol: string, when: Date, backtest: boolean) => Promise<number>;
192
+ }
193
+
194
+ declare class StrategyPublicService {
195
+ private readonly loggerService;
196
+ private readonly strategyConnectionService;
197
+ tick: (symbol: string, when: Date, backtest: boolean) => Promise<IStrategyTickResult>;
198
+ }
199
+
200
+ declare const backtest: {
201
+ candlePublicService: CandlePublicService;
202
+ strategyPublicService: StrategyPublicService;
203
+ candleSchemaService: CandleSchemaService;
204
+ strategySchemaService: StrategySchemaService;
205
+ candleConnectionService: CandleConnectionService;
206
+ strategyConnectionService: StrategyConnectionService;
207
+ executionContextService: {
208
+ readonly context: IExecutionContext;
209
+ };
210
+ loggerService: LoggerService;
211
+ };
212
+
213
+ export { ExecutionContextService, addCandle, addStrategy, backtest, reduce, runBacktest, runBacktestGUI, startRun, stopAll, stopRun };