backtest-kit 1.1.5 → 1.1.6
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 +3 -3
- package/build/index.cjs +273 -31
- package/build/index.mjs +273 -31
- package/package.json +1 -1
- package/types.d.ts +441 -256
package/types.d.ts
CHANGED
|
@@ -1178,6 +1178,416 @@ declare function getDate(): Promise<Date>;
|
|
|
1178
1178
|
*/
|
|
1179
1179
|
declare function getMode(): Promise<"backtest" | "live">;
|
|
1180
1180
|
|
|
1181
|
+
/**
|
|
1182
|
+
* Statistical data calculated from backtest results.
|
|
1183
|
+
*
|
|
1184
|
+
* All numeric values are null if calculation is unsafe (NaN, Infinity, etc).
|
|
1185
|
+
* Provides comprehensive metrics for strategy performance analysis.
|
|
1186
|
+
*
|
|
1187
|
+
* @example
|
|
1188
|
+
* ```typescript
|
|
1189
|
+
* const stats = await Backtest.getData("my-strategy");
|
|
1190
|
+
*
|
|
1191
|
+
* console.log(`Total signals: ${stats.totalSignals}`);
|
|
1192
|
+
* console.log(`Win rate: ${stats.winRate}%`);
|
|
1193
|
+
* console.log(`Sharpe Ratio: ${stats.sharpeRatio}`);
|
|
1194
|
+
*
|
|
1195
|
+
* // Access raw signal data
|
|
1196
|
+
* stats.signalList.forEach(signal => {
|
|
1197
|
+
* console.log(`Signal ${signal.signal.id}: ${signal.pnl.pnlPercentage}%`);
|
|
1198
|
+
* });
|
|
1199
|
+
* ```
|
|
1200
|
+
*/
|
|
1201
|
+
interface BacktestStatistics {
|
|
1202
|
+
/** Array of all closed signals with full details (price, PNL, timestamps, etc.) */
|
|
1203
|
+
signalList: IStrategyTickResultClosed[];
|
|
1204
|
+
/** Total number of closed signals */
|
|
1205
|
+
totalSignals: number;
|
|
1206
|
+
/** Number of winning signals (PNL > 0) */
|
|
1207
|
+
winCount: number;
|
|
1208
|
+
/** Number of losing signals (PNL < 0) */
|
|
1209
|
+
lossCount: number;
|
|
1210
|
+
/** Win rate as percentage (0-100), null if unsafe. Higher is better. */
|
|
1211
|
+
winRate: number | null;
|
|
1212
|
+
/** Average PNL per signal as percentage, null if unsafe. Higher is better. */
|
|
1213
|
+
avgPnl: number | null;
|
|
1214
|
+
/** Cumulative PNL across all signals as percentage, null if unsafe. Higher is better. */
|
|
1215
|
+
totalPnl: number | null;
|
|
1216
|
+
/** Standard deviation of returns (volatility metric), null if unsafe. Lower is better. */
|
|
1217
|
+
stdDev: number | null;
|
|
1218
|
+
/** Sharpe Ratio (risk-adjusted return = avgPnl / stdDev), null if unsafe. Higher is better. */
|
|
1219
|
+
sharpeRatio: number | null;
|
|
1220
|
+
/** Annualized Sharpe Ratio (sharpeRatio × √365), null if unsafe. Higher is better. */
|
|
1221
|
+
annualizedSharpeRatio: number | null;
|
|
1222
|
+
/** Certainty Ratio (avgWin / |avgLoss|), null if unsafe. Higher is better. */
|
|
1223
|
+
certaintyRatio: number | null;
|
|
1224
|
+
/** Expected yearly returns based on average trade duration and PNL, null if unsafe. Higher is better. */
|
|
1225
|
+
expectedYearlyReturns: number | null;
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Service for generating and saving backtest markdown reports.
|
|
1229
|
+
*
|
|
1230
|
+
* Features:
|
|
1231
|
+
* - Listens to signal events via onTick callback
|
|
1232
|
+
* - Accumulates closed signals per strategy using memoized storage
|
|
1233
|
+
* - Generates markdown tables with detailed signal information
|
|
1234
|
+
* - Saves reports to disk in logs/backtest/{strategyName}.md
|
|
1235
|
+
*
|
|
1236
|
+
* @example
|
|
1237
|
+
* ```typescript
|
|
1238
|
+
* const service = new BacktestMarkdownService();
|
|
1239
|
+
*
|
|
1240
|
+
* // Add to strategy callbacks
|
|
1241
|
+
* addStrategy({
|
|
1242
|
+
* strategyName: "my-strategy",
|
|
1243
|
+
* callbacks: {
|
|
1244
|
+
* onTick: (symbol, result, backtest) => {
|
|
1245
|
+
* service.tick(result);
|
|
1246
|
+
* }
|
|
1247
|
+
* }
|
|
1248
|
+
* });
|
|
1249
|
+
*
|
|
1250
|
+
* // After backtest, generate and save report
|
|
1251
|
+
* await service.saveReport("my-strategy");
|
|
1252
|
+
* ```
|
|
1253
|
+
*/
|
|
1254
|
+
declare class BacktestMarkdownService {
|
|
1255
|
+
/** Logger service for debug output */
|
|
1256
|
+
private readonly loggerService;
|
|
1257
|
+
/**
|
|
1258
|
+
* Memoized function to get or create ReportStorage for a strategy.
|
|
1259
|
+
* Each strategy gets its own isolated storage instance.
|
|
1260
|
+
*/
|
|
1261
|
+
private getStorage;
|
|
1262
|
+
/**
|
|
1263
|
+
* Processes tick events and accumulates closed signals.
|
|
1264
|
+
* Should be called from IStrategyCallbacks.onTick.
|
|
1265
|
+
*
|
|
1266
|
+
* Only processes closed signals - opened signals are ignored.
|
|
1267
|
+
*
|
|
1268
|
+
* @param data - Tick result from strategy execution (opened or closed)
|
|
1269
|
+
*
|
|
1270
|
+
* @example
|
|
1271
|
+
* ```typescript
|
|
1272
|
+
* const service = new BacktestMarkdownService();
|
|
1273
|
+
*
|
|
1274
|
+
* callbacks: {
|
|
1275
|
+
* onTick: (symbol, result, backtest) => {
|
|
1276
|
+
* service.tick(result);
|
|
1277
|
+
* }
|
|
1278
|
+
* }
|
|
1279
|
+
* ```
|
|
1280
|
+
*/
|
|
1281
|
+
private tick;
|
|
1282
|
+
/**
|
|
1283
|
+
* Gets statistical data from all closed signals for a strategy.
|
|
1284
|
+
* Delegates to ReportStorage.getData().
|
|
1285
|
+
*
|
|
1286
|
+
* @param strategyName - Strategy name to get data for
|
|
1287
|
+
* @returns Statistical data object with all metrics
|
|
1288
|
+
*
|
|
1289
|
+
* @example
|
|
1290
|
+
* ```typescript
|
|
1291
|
+
* const service = new BacktestMarkdownService();
|
|
1292
|
+
* const stats = await service.getData("my-strategy");
|
|
1293
|
+
* console.log(stats.sharpeRatio, stats.winRate);
|
|
1294
|
+
* ```
|
|
1295
|
+
*/
|
|
1296
|
+
getData: (strategyName: StrategyName) => Promise<BacktestStatistics>;
|
|
1297
|
+
/**
|
|
1298
|
+
* Generates markdown report with all closed signals for a strategy.
|
|
1299
|
+
* Delegates to ReportStorage.generateReport().
|
|
1300
|
+
*
|
|
1301
|
+
* @param strategyName - Strategy name to generate report for
|
|
1302
|
+
* @returns Markdown formatted report string with table of all closed signals
|
|
1303
|
+
*
|
|
1304
|
+
* @example
|
|
1305
|
+
* ```typescript
|
|
1306
|
+
* const service = new BacktestMarkdownService();
|
|
1307
|
+
* const markdown = await service.getReport("my-strategy");
|
|
1308
|
+
* console.log(markdown);
|
|
1309
|
+
* ```
|
|
1310
|
+
*/
|
|
1311
|
+
getReport: (strategyName: StrategyName) => Promise<string>;
|
|
1312
|
+
/**
|
|
1313
|
+
* Saves strategy report to disk.
|
|
1314
|
+
* Creates directory if it doesn't exist.
|
|
1315
|
+
* Delegates to ReportStorage.dump().
|
|
1316
|
+
*
|
|
1317
|
+
* @param strategyName - Strategy name to save report for
|
|
1318
|
+
* @param path - Directory path to save report (default: "./logs/backtest")
|
|
1319
|
+
*
|
|
1320
|
+
* @example
|
|
1321
|
+
* ```typescript
|
|
1322
|
+
* const service = new BacktestMarkdownService();
|
|
1323
|
+
*
|
|
1324
|
+
* // Save to default path: ./logs/backtest/my-strategy.md
|
|
1325
|
+
* await service.dump("my-strategy");
|
|
1326
|
+
*
|
|
1327
|
+
* // Save to custom path: ./custom/path/my-strategy.md
|
|
1328
|
+
* await service.dump("my-strategy", "./custom/path");
|
|
1329
|
+
* ```
|
|
1330
|
+
*/
|
|
1331
|
+
dump: (strategyName: StrategyName, path?: string) => Promise<void>;
|
|
1332
|
+
/**
|
|
1333
|
+
* Clears accumulated signal data from storage.
|
|
1334
|
+
* If strategyName is provided, clears only that strategy's data.
|
|
1335
|
+
* If strategyName is omitted, clears all strategies' data.
|
|
1336
|
+
*
|
|
1337
|
+
* @param strategyName - Optional strategy name to clear specific strategy data
|
|
1338
|
+
*
|
|
1339
|
+
* @example
|
|
1340
|
+
* ```typescript
|
|
1341
|
+
* const service = new BacktestMarkdownService();
|
|
1342
|
+
*
|
|
1343
|
+
* // Clear specific strategy data
|
|
1344
|
+
* await service.clear("my-strategy");
|
|
1345
|
+
*
|
|
1346
|
+
* // Clear all strategies' data
|
|
1347
|
+
* await service.clear();
|
|
1348
|
+
* ```
|
|
1349
|
+
*/
|
|
1350
|
+
clear: (strategyName?: StrategyName) => Promise<void>;
|
|
1351
|
+
/**
|
|
1352
|
+
* Initializes the service by subscribing to backtest signal events.
|
|
1353
|
+
* Uses singleshot to ensure initialization happens only once.
|
|
1354
|
+
* Automatically called on first use.
|
|
1355
|
+
*
|
|
1356
|
+
* @example
|
|
1357
|
+
* ```typescript
|
|
1358
|
+
* const service = new BacktestMarkdownService();
|
|
1359
|
+
* await service.init(); // Subscribe to backtest events
|
|
1360
|
+
* ```
|
|
1361
|
+
*/
|
|
1362
|
+
protected init: (() => Promise<void>) & functools_kit.ISingleshotClearable;
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
/**
|
|
1366
|
+
* Unified tick event data for report generation.
|
|
1367
|
+
* Contains all information about a tick event regardless of action type.
|
|
1368
|
+
*/
|
|
1369
|
+
interface TickEvent {
|
|
1370
|
+
/** Event timestamp in milliseconds */
|
|
1371
|
+
timestamp: number;
|
|
1372
|
+
/** Event action type */
|
|
1373
|
+
action: "idle" | "opened" | "active" | "closed";
|
|
1374
|
+
/** Trading pair symbol (only for non-idle events) */
|
|
1375
|
+
symbol?: string;
|
|
1376
|
+
/** Signal ID (only for opened/active/closed) */
|
|
1377
|
+
signalId?: string;
|
|
1378
|
+
/** Position type (only for opened/active/closed) */
|
|
1379
|
+
position?: string;
|
|
1380
|
+
/** Signal note (only for opened/active/closed) */
|
|
1381
|
+
note?: string;
|
|
1382
|
+
/** Current price */
|
|
1383
|
+
currentPrice: number;
|
|
1384
|
+
/** Open price (only for opened/active/closed) */
|
|
1385
|
+
openPrice?: number;
|
|
1386
|
+
/** Take profit price (only for opened/active/closed) */
|
|
1387
|
+
takeProfit?: number;
|
|
1388
|
+
/** Stop loss price (only for opened/active/closed) */
|
|
1389
|
+
stopLoss?: number;
|
|
1390
|
+
/** PNL percentage (only for closed) */
|
|
1391
|
+
pnl?: number;
|
|
1392
|
+
/** Close reason (only for closed) */
|
|
1393
|
+
closeReason?: string;
|
|
1394
|
+
/** Duration in minutes (only for closed) */
|
|
1395
|
+
duration?: number;
|
|
1396
|
+
}
|
|
1397
|
+
/**
|
|
1398
|
+
* Statistical data calculated from live trading results.
|
|
1399
|
+
*
|
|
1400
|
+
* All numeric values are null if calculation is unsafe (NaN, Infinity, etc).
|
|
1401
|
+
* Provides comprehensive metrics for live trading performance analysis.
|
|
1402
|
+
*
|
|
1403
|
+
* @example
|
|
1404
|
+
* ```typescript
|
|
1405
|
+
* const stats = await Live.getData("my-strategy");
|
|
1406
|
+
*
|
|
1407
|
+
* console.log(`Total events: ${stats.totalEvents}`);
|
|
1408
|
+
* console.log(`Closed signals: ${stats.totalClosed}`);
|
|
1409
|
+
* console.log(`Win rate: ${stats.winRate}%`);
|
|
1410
|
+
* console.log(`Sharpe Ratio: ${stats.sharpeRatio}`);
|
|
1411
|
+
*
|
|
1412
|
+
* // Access raw event data (includes idle, opened, active, closed)
|
|
1413
|
+
* stats.eventList.forEach(event => {
|
|
1414
|
+
* if (event.action === "closed") {
|
|
1415
|
+
* console.log(`Closed signal: ${event.pnl}%`);
|
|
1416
|
+
* }
|
|
1417
|
+
* });
|
|
1418
|
+
* ```
|
|
1419
|
+
*/
|
|
1420
|
+
interface LiveStatistics {
|
|
1421
|
+
/** Array of all events (idle, opened, active, closed) with full details */
|
|
1422
|
+
eventList: TickEvent[];
|
|
1423
|
+
/** Total number of all events (includes idle, opened, active, closed) */
|
|
1424
|
+
totalEvents: number;
|
|
1425
|
+
/** Total number of closed signals only */
|
|
1426
|
+
totalClosed: number;
|
|
1427
|
+
/** Number of winning closed signals (PNL > 0) */
|
|
1428
|
+
winCount: number;
|
|
1429
|
+
/** Number of losing closed signals (PNL < 0) */
|
|
1430
|
+
lossCount: number;
|
|
1431
|
+
/** Win rate as percentage (0-100) based on closed signals, null if unsafe. Higher is better. */
|
|
1432
|
+
winRate: number | null;
|
|
1433
|
+
/** Average PNL per closed signal as percentage, null if unsafe. Higher is better. */
|
|
1434
|
+
avgPnl: number | null;
|
|
1435
|
+
/** Cumulative PNL across all closed signals as percentage, null if unsafe. Higher is better. */
|
|
1436
|
+
totalPnl: number | null;
|
|
1437
|
+
/** Standard deviation of returns (volatility metric), null if unsafe. Lower is better. */
|
|
1438
|
+
stdDev: number | null;
|
|
1439
|
+
/** Sharpe Ratio (risk-adjusted return = avgPnl / stdDev), null if unsafe. Higher is better. */
|
|
1440
|
+
sharpeRatio: number | null;
|
|
1441
|
+
/** Annualized Sharpe Ratio (sharpeRatio × √365), null if unsafe. Higher is better. */
|
|
1442
|
+
annualizedSharpeRatio: number | null;
|
|
1443
|
+
/** Certainty Ratio (avgWin / |avgLoss|), null if unsafe. Higher is better. */
|
|
1444
|
+
certaintyRatio: number | null;
|
|
1445
|
+
/** Expected yearly returns based on average trade duration and PNL, null if unsafe. Higher is better. */
|
|
1446
|
+
expectedYearlyReturns: number | null;
|
|
1447
|
+
}
|
|
1448
|
+
/**
|
|
1449
|
+
* Service for generating and saving live trading markdown reports.
|
|
1450
|
+
*
|
|
1451
|
+
* Features:
|
|
1452
|
+
* - Listens to all signal events via onTick callback
|
|
1453
|
+
* - Accumulates all events (idle, opened, active, closed) per strategy
|
|
1454
|
+
* - Generates markdown tables with detailed event information
|
|
1455
|
+
* - Provides trading statistics (win rate, average PNL)
|
|
1456
|
+
* - Saves reports to disk in logs/live/{strategyName}.md
|
|
1457
|
+
*
|
|
1458
|
+
* @example
|
|
1459
|
+
* ```typescript
|
|
1460
|
+
* const service = new LiveMarkdownService();
|
|
1461
|
+
*
|
|
1462
|
+
* // Add to strategy callbacks
|
|
1463
|
+
* addStrategy({
|
|
1464
|
+
* strategyName: "my-strategy",
|
|
1465
|
+
* callbacks: {
|
|
1466
|
+
* onTick: (symbol, result, backtest) => {
|
|
1467
|
+
* if (!backtest) {
|
|
1468
|
+
* service.tick(result);
|
|
1469
|
+
* }
|
|
1470
|
+
* }
|
|
1471
|
+
* }
|
|
1472
|
+
* });
|
|
1473
|
+
*
|
|
1474
|
+
* // Later: generate and save report
|
|
1475
|
+
* await service.dump("my-strategy");
|
|
1476
|
+
* ```
|
|
1477
|
+
*/
|
|
1478
|
+
declare class LiveMarkdownService {
|
|
1479
|
+
/** Logger service for debug output */
|
|
1480
|
+
private readonly loggerService;
|
|
1481
|
+
/**
|
|
1482
|
+
* Memoized function to get or create ReportStorage for a strategy.
|
|
1483
|
+
* Each strategy gets its own isolated storage instance.
|
|
1484
|
+
*/
|
|
1485
|
+
private getStorage;
|
|
1486
|
+
/**
|
|
1487
|
+
* Processes tick events and accumulates all event types.
|
|
1488
|
+
* Should be called from IStrategyCallbacks.onTick.
|
|
1489
|
+
*
|
|
1490
|
+
* Processes all event types: idle, opened, active, closed.
|
|
1491
|
+
*
|
|
1492
|
+
* @param data - Tick result from strategy execution
|
|
1493
|
+
*
|
|
1494
|
+
* @example
|
|
1495
|
+
* ```typescript
|
|
1496
|
+
* const service = new LiveMarkdownService();
|
|
1497
|
+
*
|
|
1498
|
+
* callbacks: {
|
|
1499
|
+
* onTick: (symbol, result, backtest) => {
|
|
1500
|
+
* if (!backtest) {
|
|
1501
|
+
* service.tick(result);
|
|
1502
|
+
* }
|
|
1503
|
+
* }
|
|
1504
|
+
* }
|
|
1505
|
+
* ```
|
|
1506
|
+
*/
|
|
1507
|
+
private tick;
|
|
1508
|
+
/**
|
|
1509
|
+
* Gets statistical data from all live trading events for a strategy.
|
|
1510
|
+
* Delegates to ReportStorage.getData().
|
|
1511
|
+
*
|
|
1512
|
+
* @param strategyName - Strategy name to get data for
|
|
1513
|
+
* @returns Statistical data object with all metrics
|
|
1514
|
+
*
|
|
1515
|
+
* @example
|
|
1516
|
+
* ```typescript
|
|
1517
|
+
* const service = new LiveMarkdownService();
|
|
1518
|
+
* const stats = await service.getData("my-strategy");
|
|
1519
|
+
* console.log(stats.sharpeRatio, stats.winRate);
|
|
1520
|
+
* ```
|
|
1521
|
+
*/
|
|
1522
|
+
getData: (strategyName: StrategyName) => Promise<LiveStatistics>;
|
|
1523
|
+
/**
|
|
1524
|
+
* Generates markdown report with all events for a strategy.
|
|
1525
|
+
* Delegates to ReportStorage.getReport().
|
|
1526
|
+
*
|
|
1527
|
+
* @param strategyName - Strategy name to generate report for
|
|
1528
|
+
* @returns Markdown formatted report string with table of all events
|
|
1529
|
+
*
|
|
1530
|
+
* @example
|
|
1531
|
+
* ```typescript
|
|
1532
|
+
* const service = new LiveMarkdownService();
|
|
1533
|
+
* const markdown = await service.getReport("my-strategy");
|
|
1534
|
+
* console.log(markdown);
|
|
1535
|
+
* ```
|
|
1536
|
+
*/
|
|
1537
|
+
getReport: (strategyName: StrategyName) => Promise<string>;
|
|
1538
|
+
/**
|
|
1539
|
+
* Saves strategy report to disk.
|
|
1540
|
+
* Creates directory if it doesn't exist.
|
|
1541
|
+
* Delegates to ReportStorage.dump().
|
|
1542
|
+
*
|
|
1543
|
+
* @param strategyName - Strategy name to save report for
|
|
1544
|
+
* @param path - Directory path to save report (default: "./logs/live")
|
|
1545
|
+
*
|
|
1546
|
+
* @example
|
|
1547
|
+
* ```typescript
|
|
1548
|
+
* const service = new LiveMarkdownService();
|
|
1549
|
+
*
|
|
1550
|
+
* // Save to default path: ./logs/live/my-strategy.md
|
|
1551
|
+
* await service.dump("my-strategy");
|
|
1552
|
+
*
|
|
1553
|
+
* // Save to custom path: ./custom/path/my-strategy.md
|
|
1554
|
+
* await service.dump("my-strategy", "./custom/path");
|
|
1555
|
+
* ```
|
|
1556
|
+
*/
|
|
1557
|
+
dump: (strategyName: StrategyName, path?: string) => Promise<void>;
|
|
1558
|
+
/**
|
|
1559
|
+
* Clears accumulated event data from storage.
|
|
1560
|
+
* If strategyName is provided, clears only that strategy's data.
|
|
1561
|
+
* If strategyName is omitted, clears all strategies' data.
|
|
1562
|
+
*
|
|
1563
|
+
* @param strategyName - Optional strategy name to clear specific strategy data
|
|
1564
|
+
*
|
|
1565
|
+
* @example
|
|
1566
|
+
* ```typescript
|
|
1567
|
+
* const service = new LiveMarkdownService();
|
|
1568
|
+
*
|
|
1569
|
+
* // Clear specific strategy data
|
|
1570
|
+
* await service.clear("my-strategy");
|
|
1571
|
+
*
|
|
1572
|
+
* // Clear all strategies' data
|
|
1573
|
+
* await service.clear();
|
|
1574
|
+
* ```
|
|
1575
|
+
*/
|
|
1576
|
+
clear: (strategyName?: StrategyName) => Promise<void>;
|
|
1577
|
+
/**
|
|
1578
|
+
* Initializes the service by subscribing to live signal events.
|
|
1579
|
+
* Uses singleshot to ensure initialization happens only once.
|
|
1580
|
+
* Automatically called on first use.
|
|
1581
|
+
*
|
|
1582
|
+
* @example
|
|
1583
|
+
* ```typescript
|
|
1584
|
+
* const service = new LiveMarkdownService();
|
|
1585
|
+
* await service.init(); // Subscribe to live events
|
|
1586
|
+
* ```
|
|
1587
|
+
*/
|
|
1588
|
+
protected init: (() => Promise<void>) & functools_kit.ISingleshotClearable;
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1181
1591
|
declare const BASE_WAIT_FOR_INIT_SYMBOL: unique symbol;
|
|
1182
1592
|
/**
|
|
1183
1593
|
* Signal data stored in persistence layer.
|
|
@@ -1468,6 +1878,19 @@ declare class BacktestUtils {
|
|
|
1468
1878
|
exchangeName: string;
|
|
1469
1879
|
frameName: string;
|
|
1470
1880
|
}) => () => void;
|
|
1881
|
+
/**
|
|
1882
|
+
* Gets statistical data from all closed signals for a strategy.
|
|
1883
|
+
*
|
|
1884
|
+
* @param strategyName - Strategy name to get data for
|
|
1885
|
+
* @returns Promise resolving to statistical data object
|
|
1886
|
+
*
|
|
1887
|
+
* @example
|
|
1888
|
+
* ```typescript
|
|
1889
|
+
* const stats = await Backtest.getData("my-strategy");
|
|
1890
|
+
* console.log(stats.sharpeRatio, stats.winRate);
|
|
1891
|
+
* ```
|
|
1892
|
+
*/
|
|
1893
|
+
getData: (strategyName: StrategyName) => Promise<BacktestStatistics>;
|
|
1471
1894
|
/**
|
|
1472
1895
|
* Generates markdown report with all closed signals for a strategy.
|
|
1473
1896
|
*
|
|
@@ -1583,10 +2006,23 @@ declare class LiveUtils {
|
|
|
1583
2006
|
* });
|
|
1584
2007
|
* ```
|
|
1585
2008
|
*/
|
|
1586
|
-
background: (symbol: string, context: {
|
|
1587
|
-
strategyName: string;
|
|
1588
|
-
exchangeName: string;
|
|
1589
|
-
}) => () => void;
|
|
2009
|
+
background: (symbol: string, context: {
|
|
2010
|
+
strategyName: string;
|
|
2011
|
+
exchangeName: string;
|
|
2012
|
+
}) => () => void;
|
|
2013
|
+
/**
|
|
2014
|
+
* Gets statistical data from all live trading events for a strategy.
|
|
2015
|
+
*
|
|
2016
|
+
* @param strategyName - Strategy name to get data for
|
|
2017
|
+
* @returns Promise resolving to statistical data object
|
|
2018
|
+
*
|
|
2019
|
+
* @example
|
|
2020
|
+
* ```typescript
|
|
2021
|
+
* const stats = await Live.getData("my-strategy");
|
|
2022
|
+
* console.log(stats.sharpeRatio, stats.winRate);
|
|
2023
|
+
* ```
|
|
2024
|
+
*/
|
|
2025
|
+
getData: (strategyName: StrategyName) => Promise<LiveStatistics>;
|
|
1590
2026
|
/**
|
|
1591
2027
|
* Generates markdown report with all events for a strategy.
|
|
1592
2028
|
*
|
|
@@ -2515,257 +2951,6 @@ declare class BacktestGlobalService {
|
|
|
2515
2951
|
}) => AsyncGenerator<IStrategyTickResultClosed, void, unknown>;
|
|
2516
2952
|
}
|
|
2517
2953
|
|
|
2518
|
-
/**
|
|
2519
|
-
* Service for generating and saving backtest markdown reports.
|
|
2520
|
-
*
|
|
2521
|
-
* Features:
|
|
2522
|
-
* - Listens to signal events via onTick callback
|
|
2523
|
-
* - Accumulates closed signals per strategy using memoized storage
|
|
2524
|
-
* - Generates markdown tables with detailed signal information
|
|
2525
|
-
* - Saves reports to disk in logs/backtest/{strategyName}.md
|
|
2526
|
-
*
|
|
2527
|
-
* @example
|
|
2528
|
-
* ```typescript
|
|
2529
|
-
* const service = new BacktestMarkdownService();
|
|
2530
|
-
*
|
|
2531
|
-
* // Add to strategy callbacks
|
|
2532
|
-
* addStrategy({
|
|
2533
|
-
* strategyName: "my-strategy",
|
|
2534
|
-
* callbacks: {
|
|
2535
|
-
* onTick: (symbol, result, backtest) => {
|
|
2536
|
-
* service.tick(result);
|
|
2537
|
-
* }
|
|
2538
|
-
* }
|
|
2539
|
-
* });
|
|
2540
|
-
*
|
|
2541
|
-
* // After backtest, generate and save report
|
|
2542
|
-
* await service.saveReport("my-strategy");
|
|
2543
|
-
* ```
|
|
2544
|
-
*/
|
|
2545
|
-
declare class BacktestMarkdownService {
|
|
2546
|
-
/** Logger service for debug output */
|
|
2547
|
-
private readonly loggerService;
|
|
2548
|
-
/**
|
|
2549
|
-
* Memoized function to get or create ReportStorage for a strategy.
|
|
2550
|
-
* Each strategy gets its own isolated storage instance.
|
|
2551
|
-
*/
|
|
2552
|
-
private getStorage;
|
|
2553
|
-
/**
|
|
2554
|
-
* Processes tick events and accumulates closed signals.
|
|
2555
|
-
* Should be called from IStrategyCallbacks.onTick.
|
|
2556
|
-
*
|
|
2557
|
-
* Only processes closed signals - opened signals are ignored.
|
|
2558
|
-
*
|
|
2559
|
-
* @param data - Tick result from strategy execution (opened or closed)
|
|
2560
|
-
*
|
|
2561
|
-
* @example
|
|
2562
|
-
* ```typescript
|
|
2563
|
-
* const service = new BacktestMarkdownService();
|
|
2564
|
-
*
|
|
2565
|
-
* callbacks: {
|
|
2566
|
-
* onTick: (symbol, result, backtest) => {
|
|
2567
|
-
* service.tick(result);
|
|
2568
|
-
* }
|
|
2569
|
-
* }
|
|
2570
|
-
* ```
|
|
2571
|
-
*/
|
|
2572
|
-
private tick;
|
|
2573
|
-
/**
|
|
2574
|
-
* Generates markdown report with all closed signals for a strategy.
|
|
2575
|
-
* Delegates to ReportStorage.generateReport().
|
|
2576
|
-
*
|
|
2577
|
-
* @param strategyName - Strategy name to generate report for
|
|
2578
|
-
* @returns Markdown formatted report string with table of all closed signals
|
|
2579
|
-
*
|
|
2580
|
-
* @example
|
|
2581
|
-
* ```typescript
|
|
2582
|
-
* const service = new BacktestMarkdownService();
|
|
2583
|
-
* const markdown = service.generateReport("my-strategy");
|
|
2584
|
-
* console.log(markdown);
|
|
2585
|
-
* ```
|
|
2586
|
-
*/
|
|
2587
|
-
getReport: (strategyName: StrategyName) => Promise<string>;
|
|
2588
|
-
/**
|
|
2589
|
-
* Saves strategy report to disk.
|
|
2590
|
-
* Creates directory if it doesn't exist.
|
|
2591
|
-
* Delegates to ReportStorage.dump().
|
|
2592
|
-
*
|
|
2593
|
-
* @param strategyName - Strategy name to save report for
|
|
2594
|
-
* @param path - Directory path to save report (default: "./logs/backtest")
|
|
2595
|
-
*
|
|
2596
|
-
* @example
|
|
2597
|
-
* ```typescript
|
|
2598
|
-
* const service = new BacktestMarkdownService();
|
|
2599
|
-
*
|
|
2600
|
-
* // Save to default path: ./logs/backtest/my-strategy.md
|
|
2601
|
-
* await service.dump("my-strategy");
|
|
2602
|
-
*
|
|
2603
|
-
* // Save to custom path: ./custom/path/my-strategy.md
|
|
2604
|
-
* await service.dump("my-strategy", "./custom/path");
|
|
2605
|
-
* ```
|
|
2606
|
-
*/
|
|
2607
|
-
dump: (strategyName: StrategyName, path?: string) => Promise<void>;
|
|
2608
|
-
/**
|
|
2609
|
-
* Clears accumulated signal data from storage.
|
|
2610
|
-
* If strategyName is provided, clears only that strategy's data.
|
|
2611
|
-
* If strategyName is omitted, clears all strategies' data.
|
|
2612
|
-
*
|
|
2613
|
-
* @param strategyName - Optional strategy name to clear specific strategy data
|
|
2614
|
-
*
|
|
2615
|
-
* @example
|
|
2616
|
-
* ```typescript
|
|
2617
|
-
* const service = new BacktestMarkdownService();
|
|
2618
|
-
*
|
|
2619
|
-
* // Clear specific strategy data
|
|
2620
|
-
* await service.clear("my-strategy");
|
|
2621
|
-
*
|
|
2622
|
-
* // Clear all strategies' data
|
|
2623
|
-
* await service.clear();
|
|
2624
|
-
* ```
|
|
2625
|
-
*/
|
|
2626
|
-
clear: (strategyName?: StrategyName) => Promise<void>;
|
|
2627
|
-
/**
|
|
2628
|
-
* Initializes the service by subscribing to backtest signal events.
|
|
2629
|
-
* Uses singleshot to ensure initialization happens only once.
|
|
2630
|
-
* Automatically called on first use.
|
|
2631
|
-
*
|
|
2632
|
-
* @example
|
|
2633
|
-
* ```typescript
|
|
2634
|
-
* const service = new BacktestMarkdownService();
|
|
2635
|
-
* await service.init(); // Subscribe to backtest events
|
|
2636
|
-
* ```
|
|
2637
|
-
*/
|
|
2638
|
-
protected init: (() => Promise<void>) & functools_kit.ISingleshotClearable;
|
|
2639
|
-
}
|
|
2640
|
-
|
|
2641
|
-
/**
|
|
2642
|
-
* Service for generating and saving live trading markdown reports.
|
|
2643
|
-
*
|
|
2644
|
-
* Features:
|
|
2645
|
-
* - Listens to all signal events via onTick callback
|
|
2646
|
-
* - Accumulates all events (idle, opened, active, closed) per strategy
|
|
2647
|
-
* - Generates markdown tables with detailed event information
|
|
2648
|
-
* - Provides trading statistics (win rate, average PNL)
|
|
2649
|
-
* - Saves reports to disk in logs/live/{strategyName}.md
|
|
2650
|
-
*
|
|
2651
|
-
* @example
|
|
2652
|
-
* ```typescript
|
|
2653
|
-
* const service = new LiveMarkdownService();
|
|
2654
|
-
*
|
|
2655
|
-
* // Add to strategy callbacks
|
|
2656
|
-
* addStrategy({
|
|
2657
|
-
* strategyName: "my-strategy",
|
|
2658
|
-
* callbacks: {
|
|
2659
|
-
* onTick: (symbol, result, backtest) => {
|
|
2660
|
-
* if (!backtest) {
|
|
2661
|
-
* service.tick(result);
|
|
2662
|
-
* }
|
|
2663
|
-
* }
|
|
2664
|
-
* }
|
|
2665
|
-
* });
|
|
2666
|
-
*
|
|
2667
|
-
* // Later: generate and save report
|
|
2668
|
-
* await service.dump("my-strategy");
|
|
2669
|
-
* ```
|
|
2670
|
-
*/
|
|
2671
|
-
declare class LiveMarkdownService {
|
|
2672
|
-
/** Logger service for debug output */
|
|
2673
|
-
private readonly loggerService;
|
|
2674
|
-
/**
|
|
2675
|
-
* Memoized function to get or create ReportStorage for a strategy.
|
|
2676
|
-
* Each strategy gets its own isolated storage instance.
|
|
2677
|
-
*/
|
|
2678
|
-
private getStorage;
|
|
2679
|
-
/**
|
|
2680
|
-
* Processes tick events and accumulates all event types.
|
|
2681
|
-
* Should be called from IStrategyCallbacks.onTick.
|
|
2682
|
-
*
|
|
2683
|
-
* Processes all event types: idle, opened, active, closed.
|
|
2684
|
-
*
|
|
2685
|
-
* @param data - Tick result from strategy execution
|
|
2686
|
-
*
|
|
2687
|
-
* @example
|
|
2688
|
-
* ```typescript
|
|
2689
|
-
* const service = new LiveMarkdownService();
|
|
2690
|
-
*
|
|
2691
|
-
* callbacks: {
|
|
2692
|
-
* onTick: (symbol, result, backtest) => {
|
|
2693
|
-
* if (!backtest) {
|
|
2694
|
-
* service.tick(result);
|
|
2695
|
-
* }
|
|
2696
|
-
* }
|
|
2697
|
-
* }
|
|
2698
|
-
* ```
|
|
2699
|
-
*/
|
|
2700
|
-
private tick;
|
|
2701
|
-
/**
|
|
2702
|
-
* Generates markdown report with all events for a strategy.
|
|
2703
|
-
* Delegates to ReportStorage.getReport().
|
|
2704
|
-
*
|
|
2705
|
-
* @param strategyName - Strategy name to generate report for
|
|
2706
|
-
* @returns Markdown formatted report string with table of all events
|
|
2707
|
-
*
|
|
2708
|
-
* @example
|
|
2709
|
-
* ```typescript
|
|
2710
|
-
* const service = new LiveMarkdownService();
|
|
2711
|
-
* const markdown = await service.getReport("my-strategy");
|
|
2712
|
-
* console.log(markdown);
|
|
2713
|
-
* ```
|
|
2714
|
-
*/
|
|
2715
|
-
getReport: (strategyName: StrategyName) => Promise<string>;
|
|
2716
|
-
/**
|
|
2717
|
-
* Saves strategy report to disk.
|
|
2718
|
-
* Creates directory if it doesn't exist.
|
|
2719
|
-
* Delegates to ReportStorage.dump().
|
|
2720
|
-
*
|
|
2721
|
-
* @param strategyName - Strategy name to save report for
|
|
2722
|
-
* @param path - Directory path to save report (default: "./logs/live")
|
|
2723
|
-
*
|
|
2724
|
-
* @example
|
|
2725
|
-
* ```typescript
|
|
2726
|
-
* const service = new LiveMarkdownService();
|
|
2727
|
-
*
|
|
2728
|
-
* // Save to default path: ./logs/live/my-strategy.md
|
|
2729
|
-
* await service.dump("my-strategy");
|
|
2730
|
-
*
|
|
2731
|
-
* // Save to custom path: ./custom/path/my-strategy.md
|
|
2732
|
-
* await service.dump("my-strategy", "./custom/path");
|
|
2733
|
-
* ```
|
|
2734
|
-
*/
|
|
2735
|
-
dump: (strategyName: StrategyName, path?: string) => Promise<void>;
|
|
2736
|
-
/**
|
|
2737
|
-
* Clears accumulated event data from storage.
|
|
2738
|
-
* If strategyName is provided, clears only that strategy's data.
|
|
2739
|
-
* If strategyName is omitted, clears all strategies' data.
|
|
2740
|
-
*
|
|
2741
|
-
* @param strategyName - Optional strategy name to clear specific strategy data
|
|
2742
|
-
*
|
|
2743
|
-
* @example
|
|
2744
|
-
* ```typescript
|
|
2745
|
-
* const service = new LiveMarkdownService();
|
|
2746
|
-
*
|
|
2747
|
-
* // Clear specific strategy data
|
|
2748
|
-
* await service.clear("my-strategy");
|
|
2749
|
-
*
|
|
2750
|
-
* // Clear all strategies' data
|
|
2751
|
-
* await service.clear();
|
|
2752
|
-
* ```
|
|
2753
|
-
*/
|
|
2754
|
-
clear: (strategyName?: StrategyName) => Promise<void>;
|
|
2755
|
-
/**
|
|
2756
|
-
* Initializes the service by subscribing to live signal events.
|
|
2757
|
-
* Uses singleshot to ensure initialization happens only once.
|
|
2758
|
-
* Automatically called on first use.
|
|
2759
|
-
*
|
|
2760
|
-
* @example
|
|
2761
|
-
* ```typescript
|
|
2762
|
-
* const service = new LiveMarkdownService();
|
|
2763
|
-
* await service.init(); // Subscribe to live events
|
|
2764
|
-
* ```
|
|
2765
|
-
*/
|
|
2766
|
-
protected init: (() => Promise<void>) & functools_kit.ISingleshotClearable;
|
|
2767
|
-
}
|
|
2768
|
-
|
|
2769
2954
|
/**
|
|
2770
2955
|
* @class ExchangeValidationService
|
|
2771
2956
|
* Service for managing and validating exchange configurations
|
|
@@ -2907,4 +3092,4 @@ declare const backtest: {
|
|
|
2907
3092
|
loggerService: LoggerService;
|
|
2908
3093
|
};
|
|
2909
3094
|
|
|
2910
|
-
export { Backtest, type CandleInterval, type DoneContract, type EntityId, ExecutionContextService, type FrameInterval, type ICandleData, type IExchangeSchema, type IFrameSchema, type IPersistBase, type ISignalData, type ISignalDto, type ISignalRow, type IStrategyPnL, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, Live, MethodContextService, PersistBase, PersistSignalAdaper, type ProgressContract, type SignalInterval, type TPersistBase, type TPersistBaseCtor, addExchange, addFrame, addStrategy, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listStrategies, listenDone, listenDoneOnce, listenError, listenProgress, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, setLogger };
|
|
3095
|
+
export { Backtest, type BacktestStatistics, type CandleInterval, type DoneContract, type EntityId, ExecutionContextService, type FrameInterval, type ICandleData, type IExchangeSchema, type IFrameSchema, type IPersistBase, type ISignalData, type ISignalDto, type ISignalRow, type IStrategyPnL, type IStrategySchema, type IStrategyTickResult, type IStrategyTickResultActive, type IStrategyTickResultClosed, type IStrategyTickResultIdle, type IStrategyTickResultOpened, Live, type LiveStatistics, MethodContextService, PersistBase, PersistSignalAdaper, type ProgressContract, type SignalInterval, type TPersistBase, type TPersistBaseCtor, addExchange, addFrame, addStrategy, formatPrice, formatQuantity, getAveragePrice, getCandles, getDate, getMode, backtest as lib, listExchanges, listFrames, listStrategies, listenDone, listenDoneOnce, listenError, listenProgress, listenSignal, listenSignalBacktest, listenSignalBacktestOnce, listenSignalLive, listenSignalLiveOnce, listenSignalOnce, setLogger };
|