-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.ts
More file actions
476 lines (457 loc) · 14.3 KB
/
Copy pathsetup.ts
File metadata and controls
476 lines (457 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import {
CandleInterval,
CandleData,
RiskData,
StrategyData,
PartialData,
BreakevenData,
StorageData,
NotificationData,
LogData,
MeasureData,
MemoryData,
IntervalData,
RecentData,
StateData,
SessionData,
ISignalRow,
IScheduledSignalRow,
PersistCandleAdapter,
IPersistCandleInstance,
PersistSignalAdapter,
IPersistSignalInstance,
PersistRiskAdapter,
IPersistRiskInstance,
PersistScheduleAdapter,
IPersistScheduleInstance,
PersistStrategyAdapter,
IPersistStrategyInstance,
PersistPartialAdapter,
IPersistPartialInstance,
PersistBreakevenAdapter,
IPersistBreakevenInstance,
PersistStorageAdapter,
IPersistStorageInstance,
PersistNotificationAdapter,
IPersistNotificationInstance,
PersistLogAdapter,
IPersistLogInstance,
PersistMeasureAdapter,
IPersistMeasureInstance,
PersistIntervalAdapter,
IPersistIntervalInstance,
PersistMemoryAdapter,
IPersistMemoryInstance,
PersistRecentAdapter,
IPersistRecentInstance,
PersistStateAdapter,
IPersistStateInstance,
PersistSessionAdapter,
IPersistSessionInstance,
} from "backtest-kit";
import ioc from "../lib";
import { singleshot } from "functools-kit";
const MS_PER_MINUTE = 60_000;
const INTERVAL_MINUTES: Record<CandleInterval, number> = {
"1m": 1,
"3m": 3,
"5m": 5,
"15m": 15,
"30m": 30,
"1h": 60,
"2h": 120,
"4h": 240,
"6h": 360,
"8h": 480,
"1d": 1440,
};
const waitForInfra = singleshot(
async () => {
await Promise.all([
ioc.postgresService.waitForInit(),
ioc.redisService.waitForInit(),
]);
}
);
PersistCandleAdapter.usePersistCandleAdapter(class implements IPersistCandleInstance {
constructor(
readonly symbol: string,
readonly interval: CandleInterval,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async writeCandlesData(candles: CandleData[]): Promise<void> {
for (const candle of candles) {
await ioc.candleDbService.create({
symbol: this.symbol,
interval: this.interval,
close: candle.close,
high: candle.high,
low: candle.low,
open: candle.open,
timestamp: candle.timestamp,
volume: candle.volume,
});
}
}
async readCandlesData(limit: number, sinceTimestamp: number) {
const stepMs = INTERVAL_MINUTES[this.interval] * MS_PER_MINUTE;
const result: CandleData[] = [];
for (let i = 0; i < limit; i++) {
const ts = sinceTimestamp + i * stepMs;
const row = await ioc.candleDbService.findBySymbolIntervalTimestamp(this.symbol, this.interval, ts);
if (!row) {
return null;
}
result.push({ timestamp: row.timestamp, open: row.open, high: row.high, low: row.low, close: row.close, volume: row.volume });
}
return result;
}
});
PersistSignalAdapter.usePersistSignalAdapter(class implements IPersistSignalInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readSignalData(): Promise<ISignalRow | null> {
const row = await ioc.signalDbService.findByContext(this.symbol, this.strategyName, this.exchangeName);
return row ? row.payload : null;
}
async writeSignalData(signalRow: ISignalRow | null): Promise<void> {
await ioc.signalDbService.upsert(this.symbol, this.strategyName, this.exchangeName, signalRow);
}
});
PersistRiskAdapter.usePersistRiskAdapter(class implements IPersistRiskInstance {
constructor(
readonly riskName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readPositionData(_when: Date): Promise<RiskData> {
const row = await ioc.riskDbService.findByContext(this.riskName, this.exchangeName);
return row ? row.positions : [];
}
async writePositionData(positions: RiskData, when: Date): Promise<void> {
await ioc.riskDbService.upsert(this.riskName, this.exchangeName, positions, when);
}
});
PersistScheduleAdapter.usePersistScheduleAdapter(class implements IPersistScheduleInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readScheduleData(): Promise<IScheduledSignalRow | null> {
const row = await ioc.scheduleDbService.findByContext(this.symbol, this.strategyName, this.exchangeName);
return row ? row.payload : null;
}
async writeScheduleData(scheduleRow: IScheduledSignalRow | null): Promise<void> {
await ioc.scheduleDbService.upsert(this.symbol, this.strategyName, this.exchangeName, scheduleRow);
}
});
PersistStrategyAdapter.usePersistStrategyAdapter(class implements IPersistStrategyInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readStrategyData(): Promise<StrategyData | null> {
const row = await ioc.strategyDbService.findByContext(this.symbol, this.strategyName, this.exchangeName);
return row ? row.payload : null;
}
async writeStrategyData(strategyRow: StrategyData | null): Promise<void> {
await ioc.strategyDbService.upsert(this.symbol, this.strategyName, this.exchangeName, strategyRow);
}
});
PersistPartialAdapter.usePersistPartialAdapter(class implements IPersistPartialInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readPartialData(signalId: string, _when: Date): Promise<PartialData> {
const row = await ioc.partialDbService.findByContext(this.symbol, this.strategyName, this.exchangeName, signalId);
return row ? row.payload : {};
}
async writePartialData(data: PartialData, signalId: string, when: Date): Promise<void> {
await ioc.partialDbService.upsert(this.symbol, this.strategyName, this.exchangeName, signalId, data, when);
}
});
PersistBreakevenAdapter.usePersistBreakevenAdapter(class implements IPersistBreakevenInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readBreakevenData(signalId: string, _when: Date): Promise<BreakevenData> {
const row = await ioc.breakevenDbService.findByContext(this.symbol, this.strategyName, this.exchangeName, signalId);
return row ? row.payload : {};
}
async writeBreakevenData(data: BreakevenData, signalId: string, when: Date): Promise<void> {
await ioc.breakevenDbService.upsert(this.symbol, this.strategyName, this.exchangeName, signalId, data, when);
}
});
PersistStorageAdapter.usePersistStorageAdapter(class implements IPersistStorageInstance {
constructor(readonly backtest: boolean) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readStorageData(): Promise<StorageData> {
const rows = await ioc.storageDbService.listByMode(this.backtest);
return rows.map((row) => row.payload);
}
async writeStorageData(signals: StorageData): Promise<void> {
for (const signal of signals) {
await ioc.storageDbService.upsert(this.backtest, signal.id, signal);
}
}
});
PersistNotificationAdapter.usePersistNotificationAdapter(class implements IPersistNotificationInstance {
constructor(readonly backtest: boolean) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readNotificationData(): Promise<NotificationData> {
const rows = await ioc.notificationDbService.listByMode(this.backtest);
return rows.map((row) => row.payload).reverse();
}
async writeNotificationData(notifications: NotificationData): Promise<void> {
for (const notification of notifications) {
await ioc.notificationDbService.upsert(this.backtest, notification.id, notification);
}
}
});
PersistLogAdapter.usePersistLogAdapter(class implements IPersistLogInstance {
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readLogData(): Promise<LogData> {
const rows = await ioc.logDbService.listAll();
return rows.map((row) => row.payload).reverse();
}
async writeLogData(entries: LogData): Promise<void> {
for (const entry of entries) {
await ioc.logDbService.upsert(entry.id, entry);
}
}
});
PersistMeasureAdapter.usePersistMeasureAdapter(class implements IPersistMeasureInstance {
constructor(readonly bucket: string) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readMeasureData(key: string): Promise<MeasureData | null> {
const row = await ioc.measureDbService.findByKey(this.bucket, key);
if (!row || row.removed) {
return null;
}
return row.payload;
}
async writeMeasureData(data: MeasureData, key: string, _when: Date): Promise<void> {
await ioc.measureDbService.upsert(this.bucket, key, data);
}
async removeMeasureData(key: string): Promise<void> {
await ioc.measureDbService.softRemove(this.bucket, key);
}
async *listMeasureData(): AsyncGenerator<string> {
const keys = await ioc.measureDbService.listKeys(this.bucket);
for (const key of keys) {
yield key;
}
}
});
PersistIntervalAdapter.usePersistIntervalAdapter(class implements IPersistIntervalInstance {
constructor(readonly bucket: string) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readIntervalData(key: string): Promise<IntervalData | null> {
const row = await ioc.intervalDbService.findByKey(this.bucket, key);
if (!row || row.removed) {
return null;
}
return row.payload;
}
async writeIntervalData(data: IntervalData, key: string, when: Date): Promise<void> {
await ioc.intervalDbService.upsert(this.bucket, key, data, when);
}
async removeIntervalData(key: string): Promise<void> {
await ioc.intervalDbService.softRemove(this.bucket, key);
}
async *listIntervalData(): AsyncGenerator<string> {
const keys = await ioc.intervalDbService.listKeys(this.bucket);
for (const key of keys) {
yield key;
}
}
});
PersistMemoryAdapter.usePersistMemoryAdapter(class implements IPersistMemoryInstance {
constructor(
readonly signalId: string,
readonly bucketName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readMemoryData(memoryId: string): Promise<MemoryData | null> {
const row = await ioc.memoryDbService.findByMemoryId(this.signalId, this.bucketName, memoryId);
if (!row || row.removed) {
return null;
}
return row.payload;
}
async hasMemoryData(memoryId: string): Promise<boolean> {
return await ioc.memoryDbService.hasMemoryEntry(this.signalId, this.bucketName, memoryId);
}
async writeMemoryData(data: MemoryData, memoryId: string, when: Date): Promise<void> {
await ioc.memoryDbService.upsert(this.signalId, this.bucketName, memoryId, data, when);
}
async removeMemoryData(memoryId: string): Promise<void> {
await ioc.memoryDbService.softRemove(this.signalId, this.bucketName, memoryId);
}
async *listMemoryData(): AsyncGenerator<{ memoryId: string; data: MemoryData }> {
const rows = await ioc.memoryDbService.listEntries(this.signalId, this.bucketName);
for (const row of rows) {
yield { memoryId: row.memoryId, data: row.payload };
}
}
dispose(): void { void 0; }
});
PersistRecentAdapter.usePersistRecentAdapter(class implements IPersistRecentInstance {
constructor(
readonly symbol: string,
readonly strategyName: string,
readonly exchangeName: string,
readonly frameName: string,
readonly backtest: boolean,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readRecentData(): Promise<RecentData> {
const row = await ioc.recentDbService.findByContext(
this.symbol,
this.strategyName,
this.exchangeName,
this.frameName,
this.backtest,
);
return row ? row.payload : null;
}
async writeRecentData(signalRow: NonNullable<RecentData>, when: Date): Promise<void> {
await ioc.recentDbService.upsert(
this.symbol,
this.strategyName,
this.exchangeName,
this.frameName,
this.backtest,
signalRow,
when,
);
}
});
PersistStateAdapter.usePersistStateAdapter(class implements IPersistStateInstance {
constructor(
readonly signalId: string,
readonly bucketName: string,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readStateData(): Promise<StateData | null> {
const row = await ioc.stateDbService.findByContext(this.signalId, this.bucketName);
return row ? row.payload : null;
}
async writeStateData(data: StateData, when: Date): Promise<void> {
await ioc.stateDbService.upsert(this.signalId, this.bucketName, data, when);
}
dispose(): void { void 0; }
});
PersistSessionAdapter.usePersistSessionAdapter(class implements IPersistSessionInstance {
constructor(
readonly strategyName: string,
readonly exchangeName: string,
readonly frameName: string,
readonly symbol: string,
readonly backtest: boolean,
) {}
async waitForInit(initial: boolean) {
if (!initial) {
return;
}
await waitForInfra();
}
async readSessionData(): Promise<SessionData | null> {
const row = await ioc.sessionDbService.findByContext(this.strategyName, this.exchangeName, this.frameName, this.symbol, this.backtest);
return row ? row.payload : null;
}
async writeSessionData(data: SessionData, when: Date): Promise<void> {
await ioc.sessionDbService.upsert(this.strategyName, this.exchangeName, this.frameName, this.symbol, this.backtest, data, when);
}
dispose(): void { void 0; }
});