backtest-kit 1.4.12 → 1.4.14
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 +130 -100
- package/build/index.mjs +130 -100
- package/package.json +1 -1
- package/types.d.ts +80 -64
package/build/index.mjs
CHANGED
|
@@ -10462,6 +10462,11 @@ const columns = [
|
|
|
10462
10462
|
label: "Symbol",
|
|
10463
10463
|
format: (data) => data.symbol,
|
|
10464
10464
|
},
|
|
10465
|
+
{
|
|
10466
|
+
key: "strategyName",
|
|
10467
|
+
label: "Strategy",
|
|
10468
|
+
format: (data) => data.strategyName,
|
|
10469
|
+
},
|
|
10465
10470
|
{
|
|
10466
10471
|
key: "signalId",
|
|
10467
10472
|
label: "Signal ID",
|
|
@@ -10496,7 +10501,7 @@ const columns = [
|
|
|
10496
10501
|
/** Maximum number of events to store in partial reports */
|
|
10497
10502
|
const MAX_EVENTS = 250;
|
|
10498
10503
|
/**
|
|
10499
|
-
* Storage class for accumulating partial profit/loss events per symbol.
|
|
10504
|
+
* Storage class for accumulating partial profit/loss events per symbol-strategy pair.
|
|
10500
10505
|
* Maintains a chronological list of profit and loss level events.
|
|
10501
10506
|
*/
|
|
10502
10507
|
class ReportStorage {
|
|
@@ -10507,17 +10512,17 @@ class ReportStorage {
|
|
|
10507
10512
|
/**
|
|
10508
10513
|
* Adds a profit event to the storage.
|
|
10509
10514
|
*
|
|
10510
|
-
* @param symbol - Trading pair symbol
|
|
10511
10515
|
* @param data - Signal row data
|
|
10512
10516
|
* @param currentPrice - Current market price
|
|
10513
10517
|
* @param level - Profit level reached
|
|
10514
10518
|
* @param backtest - True if backtest mode
|
|
10515
10519
|
*/
|
|
10516
|
-
addProfitEvent(
|
|
10520
|
+
addProfitEvent(data, currentPrice, level, backtest, timestamp) {
|
|
10517
10521
|
this._eventList.push({
|
|
10518
10522
|
timestamp,
|
|
10519
10523
|
action: "profit",
|
|
10520
|
-
symbol,
|
|
10524
|
+
symbol: data.symbol,
|
|
10525
|
+
strategyName: data.strategyName,
|
|
10521
10526
|
signalId: data.id,
|
|
10522
10527
|
position: data.position,
|
|
10523
10528
|
currentPrice,
|
|
@@ -10532,17 +10537,17 @@ class ReportStorage {
|
|
|
10532
10537
|
/**
|
|
10533
10538
|
* Adds a loss event to the storage.
|
|
10534
10539
|
*
|
|
10535
|
-
* @param symbol - Trading pair symbol
|
|
10536
10540
|
* @param data - Signal row data
|
|
10537
10541
|
* @param currentPrice - Current market price
|
|
10538
10542
|
* @param level - Loss level reached
|
|
10539
10543
|
* @param backtest - True if backtest mode
|
|
10540
10544
|
*/
|
|
10541
|
-
addLossEvent(
|
|
10545
|
+
addLossEvent(data, currentPrice, level, backtest, timestamp) {
|
|
10542
10546
|
this._eventList.push({
|
|
10543
10547
|
timestamp,
|
|
10544
10548
|
action: "loss",
|
|
10545
|
-
symbol,
|
|
10549
|
+
symbol: data.symbol,
|
|
10550
|
+
strategyName: data.strategyName,
|
|
10546
10551
|
signalId: data.id,
|
|
10547
10552
|
position: data.position,
|
|
10548
10553
|
currentPrice,
|
|
@@ -10578,35 +10583,37 @@ class ReportStorage {
|
|
|
10578
10583
|
};
|
|
10579
10584
|
}
|
|
10580
10585
|
/**
|
|
10581
|
-
* Generates markdown report with all partial events for a symbol (View).
|
|
10586
|
+
* Generates markdown report with all partial events for a symbol-strategy pair (View).
|
|
10582
10587
|
*
|
|
10583
10588
|
* @param symbol - Trading pair symbol
|
|
10589
|
+
* @param strategyName - Strategy name
|
|
10584
10590
|
* @returns Markdown formatted report with all events
|
|
10585
10591
|
*/
|
|
10586
|
-
async getReport(symbol) {
|
|
10592
|
+
async getReport(symbol, strategyName) {
|
|
10587
10593
|
const stats = await this.getData();
|
|
10588
10594
|
if (stats.totalEvents === 0) {
|
|
10589
|
-
return str.newline(`# Partial Profit/Loss Report: ${symbol}`, "", "No partial profit/loss events recorded yet.");
|
|
10595
|
+
return str.newline(`# Partial Profit/Loss Report: ${symbol}:${strategyName}`, "", "No partial profit/loss events recorded yet.");
|
|
10590
10596
|
}
|
|
10591
10597
|
const header = columns.map((col) => col.label);
|
|
10592
10598
|
const separator = columns.map(() => "---");
|
|
10593
10599
|
const rows = this._eventList.map((event) => columns.map((col) => col.format(event)));
|
|
10594
10600
|
const tableData = [header, separator, ...rows];
|
|
10595
10601
|
const table = str.newline(tableData.map((row) => `| ${row.join(" | ")} |`));
|
|
10596
|
-
return str.newline(`# Partial Profit/Loss Report: ${symbol}`, "", table, "", `**Total events:** ${stats.totalEvents}`, `**Profit events:** ${stats.totalProfit}`, `**Loss events:** ${stats.totalLoss}`);
|
|
10602
|
+
return str.newline(`# Partial Profit/Loss Report: ${symbol}:${strategyName}`, "", table, "", `**Total events:** ${stats.totalEvents}`, `**Profit events:** ${stats.totalProfit}`, `**Loss events:** ${stats.totalLoss}`);
|
|
10597
10603
|
}
|
|
10598
10604
|
/**
|
|
10599
|
-
* Saves symbol report to disk.
|
|
10605
|
+
* Saves symbol-strategy report to disk.
|
|
10600
10606
|
*
|
|
10601
10607
|
* @param symbol - Trading pair symbol
|
|
10608
|
+
* @param strategyName - Strategy name
|
|
10602
10609
|
* @param path - Directory path to save report (default: "./dump/partial")
|
|
10603
10610
|
*/
|
|
10604
|
-
async dump(symbol, path = "./dump/partial") {
|
|
10605
|
-
const markdown = await this.getReport(symbol);
|
|
10611
|
+
async dump(symbol, strategyName, path = "./dump/partial") {
|
|
10612
|
+
const markdown = await this.getReport(symbol, strategyName);
|
|
10606
10613
|
try {
|
|
10607
10614
|
const dir = join(process.cwd(), path);
|
|
10608
10615
|
await mkdir(dir, { recursive: true });
|
|
10609
|
-
const filename = `${symbol}.md`;
|
|
10616
|
+
const filename = `${symbol}_${strategyName}.md`;
|
|
10610
10617
|
const filepath = join(dir, filename);
|
|
10611
10618
|
await writeFile(filepath, markdown, "utf-8");
|
|
10612
10619
|
console.log(`Partial profit/loss report saved: ${filepath}`);
|
|
@@ -10621,10 +10628,10 @@ class ReportStorage {
|
|
|
10621
10628
|
*
|
|
10622
10629
|
* Features:
|
|
10623
10630
|
* - Listens to partial profit and loss events via partialProfitSubject/partialLossSubject
|
|
10624
|
-
* - Accumulates all events (profit, loss) per symbol
|
|
10631
|
+
* - Accumulates all events (profit, loss) per symbol-strategy pair
|
|
10625
10632
|
* - Generates markdown tables with detailed event information
|
|
10626
10633
|
* - Provides statistics (total profit/loss events)
|
|
10627
|
-
* - Saves reports to disk in dump/partial/{symbol}.md
|
|
10634
|
+
* - Saves reports to disk in dump/partial/{symbol}_{strategyName}.md
|
|
10628
10635
|
*
|
|
10629
10636
|
* @example
|
|
10630
10637
|
* ```typescript
|
|
@@ -10634,7 +10641,7 @@ class ReportStorage {
|
|
|
10634
10641
|
* // No manual callback setup needed
|
|
10635
10642
|
*
|
|
10636
10643
|
* // Later: generate and save report
|
|
10637
|
-
* await service.dump("BTCUSDT");
|
|
10644
|
+
* await service.dump("BTCUSDT", "my-strategy");
|
|
10638
10645
|
* ```
|
|
10639
10646
|
*/
|
|
10640
10647
|
class PartialMarkdownService {
|
|
@@ -10642,10 +10649,10 @@ class PartialMarkdownService {
|
|
|
10642
10649
|
/** Logger service for debug output */
|
|
10643
10650
|
this.loggerService = inject(TYPES.loggerService);
|
|
10644
10651
|
/**
|
|
10645
|
-
* Memoized function to get or create ReportStorage for a symbol.
|
|
10646
|
-
* Each symbol gets its own isolated storage instance.
|
|
10652
|
+
* Memoized function to get or create ReportStorage for a symbol-strategy pair.
|
|
10653
|
+
* Each symbol-strategy combination gets its own isolated storage instance.
|
|
10647
10654
|
*/
|
|
10648
|
-
this.getStorage = memoize(([symbol]) =>
|
|
10655
|
+
this.getStorage = memoize(([symbol, strategyName]) => JSON.stringify([symbol, strategyName]), () => new ReportStorage());
|
|
10649
10656
|
/**
|
|
10650
10657
|
* Processes profit events and accumulates them.
|
|
10651
10658
|
* Should be called from partialProfitSubject subscription.
|
|
@@ -10662,8 +10669,8 @@ class PartialMarkdownService {
|
|
|
10662
10669
|
this.loggerService.log("partialMarkdownService tickProfit", {
|
|
10663
10670
|
data,
|
|
10664
10671
|
});
|
|
10665
|
-
const storage = this.getStorage(data.symbol);
|
|
10666
|
-
storage.addProfitEvent(data.
|
|
10672
|
+
const storage = this.getStorage(data.symbol, data.data.strategyName);
|
|
10673
|
+
storage.addProfitEvent(data.data, data.currentPrice, data.level, data.backtest, data.timestamp);
|
|
10667
10674
|
};
|
|
10668
10675
|
/**
|
|
10669
10676
|
* Processes loss events and accumulates them.
|
|
@@ -10681,101 +10688,113 @@ class PartialMarkdownService {
|
|
|
10681
10688
|
this.loggerService.log("partialMarkdownService tickLoss", {
|
|
10682
10689
|
data,
|
|
10683
10690
|
});
|
|
10684
|
-
const storage = this.getStorage(data.symbol);
|
|
10685
|
-
storage.addLossEvent(data.
|
|
10691
|
+
const storage = this.getStorage(data.symbol, data.data.strategyName);
|
|
10692
|
+
storage.addLossEvent(data.data, data.currentPrice, data.level, data.backtest, data.timestamp);
|
|
10686
10693
|
};
|
|
10687
10694
|
/**
|
|
10688
|
-
* Gets statistical data from all partial profit/loss events for a symbol.
|
|
10695
|
+
* Gets statistical data from all partial profit/loss events for a symbol-strategy pair.
|
|
10689
10696
|
* Delegates to ReportStorage.getData().
|
|
10690
10697
|
*
|
|
10691
10698
|
* @param symbol - Trading pair symbol to get data for
|
|
10699
|
+
* @param strategyName - Strategy name to get data for
|
|
10692
10700
|
* @returns Statistical data object with all metrics
|
|
10693
10701
|
*
|
|
10694
10702
|
* @example
|
|
10695
10703
|
* ```typescript
|
|
10696
10704
|
* const service = new PartialMarkdownService();
|
|
10697
|
-
* const stats = await service.getData("BTCUSDT");
|
|
10705
|
+
* const stats = await service.getData("BTCUSDT", "my-strategy");
|
|
10698
10706
|
* console.log(stats.totalProfit, stats.totalLoss);
|
|
10699
10707
|
* ```
|
|
10700
10708
|
*/
|
|
10701
|
-
this.getData = async (symbol) => {
|
|
10709
|
+
this.getData = async (symbol, strategyName) => {
|
|
10702
10710
|
this.loggerService.log("partialMarkdownService getData", {
|
|
10703
10711
|
symbol,
|
|
10712
|
+
strategyName,
|
|
10704
10713
|
});
|
|
10705
|
-
const storage = this.getStorage(symbol);
|
|
10714
|
+
const storage = this.getStorage(symbol, strategyName);
|
|
10706
10715
|
return storage.getData();
|
|
10707
10716
|
};
|
|
10708
10717
|
/**
|
|
10709
|
-
* Generates markdown report with all partial events for a symbol.
|
|
10718
|
+
* Generates markdown report with all partial events for a symbol-strategy pair.
|
|
10710
10719
|
* Delegates to ReportStorage.getReport().
|
|
10711
10720
|
*
|
|
10712
10721
|
* @param symbol - Trading pair symbol to generate report for
|
|
10722
|
+
* @param strategyName - Strategy name to generate report for
|
|
10713
10723
|
* @returns Markdown formatted report string with table of all events
|
|
10714
10724
|
*
|
|
10715
10725
|
* @example
|
|
10716
10726
|
* ```typescript
|
|
10717
10727
|
* const service = new PartialMarkdownService();
|
|
10718
|
-
* const markdown = await service.getReport("BTCUSDT");
|
|
10728
|
+
* const markdown = await service.getReport("BTCUSDT", "my-strategy");
|
|
10719
10729
|
* console.log(markdown);
|
|
10720
10730
|
* ```
|
|
10721
10731
|
*/
|
|
10722
|
-
this.getReport = async (symbol) => {
|
|
10732
|
+
this.getReport = async (symbol, strategyName) => {
|
|
10723
10733
|
this.loggerService.log("partialMarkdownService getReport", {
|
|
10724
10734
|
symbol,
|
|
10735
|
+
strategyName,
|
|
10725
10736
|
});
|
|
10726
|
-
const storage = this.getStorage(symbol);
|
|
10727
|
-
return storage.getReport(symbol);
|
|
10737
|
+
const storage = this.getStorage(symbol, strategyName);
|
|
10738
|
+
return storage.getReport(symbol, strategyName);
|
|
10728
10739
|
};
|
|
10729
10740
|
/**
|
|
10730
|
-
* Saves symbol report to disk.
|
|
10741
|
+
* Saves symbol-strategy report to disk.
|
|
10731
10742
|
* Creates directory if it doesn't exist.
|
|
10732
10743
|
* Delegates to ReportStorage.dump().
|
|
10733
10744
|
*
|
|
10734
10745
|
* @param symbol - Trading pair symbol to save report for
|
|
10746
|
+
* @param strategyName - Strategy name to save report for
|
|
10735
10747
|
* @param path - Directory path to save report (default: "./dump/partial")
|
|
10736
10748
|
*
|
|
10737
10749
|
* @example
|
|
10738
10750
|
* ```typescript
|
|
10739
10751
|
* const service = new PartialMarkdownService();
|
|
10740
10752
|
*
|
|
10741
|
-
* // Save to default path: ./dump/partial/
|
|
10742
|
-
* await service.dump("BTCUSDT");
|
|
10753
|
+
* // Save to default path: ./dump/partial/BTCUSDT_my-strategy.md
|
|
10754
|
+
* await service.dump("BTCUSDT", "my-strategy");
|
|
10743
10755
|
*
|
|
10744
|
-
* // Save to custom path: ./custom/path/
|
|
10745
|
-
* await service.dump("BTCUSDT", "./custom/path");
|
|
10756
|
+
* // Save to custom path: ./custom/path/BTCUSDT_my-strategy.md
|
|
10757
|
+
* await service.dump("BTCUSDT", "my-strategy", "./custom/path");
|
|
10746
10758
|
* ```
|
|
10747
10759
|
*/
|
|
10748
|
-
this.dump = async (symbol, path = "./dump/partial") => {
|
|
10760
|
+
this.dump = async (symbol, strategyName, path = "./dump/partial") => {
|
|
10749
10761
|
this.loggerService.log("partialMarkdownService dump", {
|
|
10750
10762
|
symbol,
|
|
10763
|
+
strategyName,
|
|
10751
10764
|
path,
|
|
10752
10765
|
});
|
|
10753
|
-
const storage = this.getStorage(symbol);
|
|
10754
|
-
await storage.dump(symbol, path);
|
|
10766
|
+
const storage = this.getStorage(symbol, strategyName);
|
|
10767
|
+
await storage.dump(symbol, strategyName, path);
|
|
10755
10768
|
};
|
|
10756
10769
|
/**
|
|
10757
10770
|
* Clears accumulated event data from storage.
|
|
10758
|
-
* If
|
|
10759
|
-
* If
|
|
10771
|
+
* If ctx is provided, clears only that specific symbol-strategy pair's data.
|
|
10772
|
+
* If nothing is provided, clears all data.
|
|
10760
10773
|
*
|
|
10761
|
-
* @param
|
|
10774
|
+
* @param ctx - Optional context with symbol and strategyName
|
|
10762
10775
|
*
|
|
10763
10776
|
* @example
|
|
10764
10777
|
* ```typescript
|
|
10765
10778
|
* const service = new PartialMarkdownService();
|
|
10766
10779
|
*
|
|
10767
|
-
* // Clear specific symbol
|
|
10768
|
-
* await service.clear("BTCUSDT");
|
|
10780
|
+
* // Clear specific symbol-strategy pair
|
|
10781
|
+
* await service.clear({ symbol: "BTCUSDT", strategyName: "my-strategy" });
|
|
10769
10782
|
*
|
|
10770
|
-
* // Clear all
|
|
10783
|
+
* // Clear all data
|
|
10771
10784
|
* await service.clear();
|
|
10772
10785
|
* ```
|
|
10773
10786
|
*/
|
|
10774
|
-
this.clear = async (
|
|
10787
|
+
this.clear = async (ctx) => {
|
|
10775
10788
|
this.loggerService.log("partialMarkdownService clear", {
|
|
10776
|
-
|
|
10789
|
+
ctx,
|
|
10777
10790
|
});
|
|
10778
|
-
|
|
10791
|
+
if (ctx) {
|
|
10792
|
+
const key = JSON.stringify([ctx.symbol, ctx.strategyName]);
|
|
10793
|
+
this.getStorage.clear(key);
|
|
10794
|
+
}
|
|
10795
|
+
else {
|
|
10796
|
+
this.getStorage.clear();
|
|
10797
|
+
}
|
|
10779
10798
|
};
|
|
10780
10799
|
/**
|
|
10781
10800
|
* Initializes the service by subscribing to partial profit/loss events.
|
|
@@ -13077,24 +13096,26 @@ class BacktestUtils {
|
|
|
13077
13096
|
/**
|
|
13078
13097
|
* Saves strategy report to disk.
|
|
13079
13098
|
*
|
|
13099
|
+
* @param symbol - Trading pair symbol
|
|
13080
13100
|
* @param strategyName - Strategy name to save report for
|
|
13081
13101
|
* @param path - Optional directory path to save report (default: "./dump/backtest")
|
|
13082
13102
|
*
|
|
13083
13103
|
* @example
|
|
13084
13104
|
* ```typescript
|
|
13085
13105
|
* // Save to default path: ./dump/backtest/my-strategy.md
|
|
13086
|
-
* await Backtest.dump("my-strategy");
|
|
13106
|
+
* await Backtest.dump("BTCUSDT", "my-strategy");
|
|
13087
13107
|
*
|
|
13088
13108
|
* // Save to custom path: ./custom/path/my-strategy.md
|
|
13089
|
-
* await Backtest.dump("my-strategy", "./custom/path");
|
|
13109
|
+
* await Backtest.dump("BTCUSDT", "my-strategy", "./custom/path");
|
|
13090
13110
|
* ```
|
|
13091
13111
|
*/
|
|
13092
|
-
this.dump = async (strategyName, path) => {
|
|
13112
|
+
this.dump = async (symbol, strategyName, path) => {
|
|
13093
13113
|
backtest$1.loggerService.info(BACKTEST_METHOD_NAME_DUMP, {
|
|
13114
|
+
symbol,
|
|
13094
13115
|
strategyName,
|
|
13095
13116
|
path,
|
|
13096
13117
|
});
|
|
13097
|
-
await backtest$1.backtestMarkdownService.dump(strategyName, path);
|
|
13118
|
+
await backtest$1.backtestMarkdownService.dump(symbol, strategyName, path);
|
|
13098
13119
|
};
|
|
13099
13120
|
}
|
|
13100
13121
|
}
|
|
@@ -13290,24 +13311,26 @@ class LiveUtils {
|
|
|
13290
13311
|
/**
|
|
13291
13312
|
* Saves strategy report to disk.
|
|
13292
13313
|
*
|
|
13314
|
+
* @param symbol - Trading pair symbol
|
|
13293
13315
|
* @param strategyName - Strategy name to save report for
|
|
13294
13316
|
* @param path - Optional directory path to save report (default: "./dump/live")
|
|
13295
13317
|
*
|
|
13296
13318
|
* @example
|
|
13297
13319
|
* ```typescript
|
|
13298
13320
|
* // Save to default path: ./dump/live/my-strategy.md
|
|
13299
|
-
* await Live.dump("my-strategy");
|
|
13321
|
+
* await Live.dump("BTCUSDT", "my-strategy");
|
|
13300
13322
|
*
|
|
13301
13323
|
* // Save to custom path: ./custom/path/my-strategy.md
|
|
13302
|
-
* await Live.dump("my-strategy", "./custom/path");
|
|
13324
|
+
* await Live.dump("BTCUSDT", "my-strategy", "./custom/path");
|
|
13303
13325
|
* ```
|
|
13304
13326
|
*/
|
|
13305
|
-
this.dump = async (strategyName, path) => {
|
|
13327
|
+
this.dump = async (symbol, strategyName, path) => {
|
|
13306
13328
|
backtest$1.loggerService.info(LIVE_METHOD_NAME_DUMP, {
|
|
13329
|
+
symbol,
|
|
13307
13330
|
strategyName,
|
|
13308
13331
|
path,
|
|
13309
13332
|
});
|
|
13310
|
-
await backtest$1.liveMarkdownService.dump(strategyName, path);
|
|
13333
|
+
await backtest$1.liveMarkdownService.dump(symbol, strategyName, path);
|
|
13311
13334
|
};
|
|
13312
13335
|
}
|
|
13313
13336
|
}
|
|
@@ -13401,24 +13424,26 @@ class ScheduleUtils {
|
|
|
13401
13424
|
/**
|
|
13402
13425
|
* Saves strategy report to disk.
|
|
13403
13426
|
*
|
|
13427
|
+
* @param symbol - Trading pair symbol
|
|
13404
13428
|
* @param strategyName - Strategy name to save report for
|
|
13405
13429
|
* @param path - Optional directory path to save report (default: "./dump/schedule")
|
|
13406
13430
|
*
|
|
13407
13431
|
* @example
|
|
13408
13432
|
* ```typescript
|
|
13409
13433
|
* // Save to default path: ./dump/schedule/my-strategy.md
|
|
13410
|
-
* await Schedule.dump("my-strategy");
|
|
13434
|
+
* await Schedule.dump("BTCUSDT", "my-strategy");
|
|
13411
13435
|
*
|
|
13412
13436
|
* // Save to custom path: ./custom/path/my-strategy.md
|
|
13413
|
-
* await Schedule.dump("my-strategy", "./custom/path");
|
|
13437
|
+
* await Schedule.dump("BTCUSDT", "my-strategy", "./custom/path");
|
|
13414
13438
|
* ```
|
|
13415
13439
|
*/
|
|
13416
|
-
this.dump = async (strategyName, path) => {
|
|
13440
|
+
this.dump = async (symbol, strategyName, path) => {
|
|
13417
13441
|
backtest$1.loggerService.info(SCHEDULE_METHOD_NAME_DUMP, {
|
|
13442
|
+
symbol,
|
|
13418
13443
|
strategyName,
|
|
13419
13444
|
path,
|
|
13420
13445
|
});
|
|
13421
|
-
await backtest$1.scheduleMarkdownService.dump(strategyName, path);
|
|
13446
|
+
await backtest$1.scheduleMarkdownService.dump(symbol, strategyName, path);
|
|
13422
13447
|
};
|
|
13423
13448
|
}
|
|
13424
13449
|
}
|
|
@@ -13530,20 +13555,21 @@ class Performance {
|
|
|
13530
13555
|
* Creates directory if it doesn't exist.
|
|
13531
13556
|
* Default path: ./dump/performance/{strategyName}.md
|
|
13532
13557
|
*
|
|
13558
|
+
* @param symbol - Trading pair symbol
|
|
13533
13559
|
* @param strategyName - Strategy name to save report for
|
|
13534
13560
|
* @param path - Optional custom directory path
|
|
13535
13561
|
*
|
|
13536
13562
|
* @example
|
|
13537
13563
|
* ```typescript
|
|
13538
13564
|
* // Save to default path: ./dump/performance/my-strategy.md
|
|
13539
|
-
* await Performance.dump("my-strategy");
|
|
13565
|
+
* await Performance.dump("BTCUSDT", "my-strategy");
|
|
13540
13566
|
*
|
|
13541
13567
|
* // Save to custom path: ./reports/perf/my-strategy.md
|
|
13542
|
-
* await Performance.dump("my-strategy", "./reports/perf");
|
|
13568
|
+
* await Performance.dump("BTCUSDT", "my-strategy", "./reports/perf");
|
|
13543
13569
|
* ```
|
|
13544
13570
|
*/
|
|
13545
|
-
static async dump(strategyName, path = "./dump/performance") {
|
|
13546
|
-
return backtest$1.performanceMarkdownService.dump(strategyName, path);
|
|
13571
|
+
static async dump(symbol, strategyName, path = "./dump/performance") {
|
|
13572
|
+
return backtest$1.performanceMarkdownService.dump(symbol, strategyName, path);
|
|
13547
13573
|
}
|
|
13548
13574
|
}
|
|
13549
13575
|
|
|
@@ -14138,26 +14164,26 @@ const PARTIAL_METHOD_NAME_DUMP = "PartialUtils.dump";
|
|
|
14138
14164
|
*
|
|
14139
14165
|
* Data source:
|
|
14140
14166
|
* - PartialMarkdownService listens to partialProfitSubject/partialLossSubject
|
|
14141
|
-
* - Accumulates events in ReportStorage (max 250 events per symbol)
|
|
14142
|
-
* - Events include: timestamp, action, symbol, signalId, position, level, price, mode
|
|
14167
|
+
* - Accumulates events in ReportStorage (max 250 events per symbol-strategy pair)
|
|
14168
|
+
* - Events include: timestamp, action, symbol, strategyName, signalId, position, level, price, mode
|
|
14143
14169
|
*
|
|
14144
14170
|
* @example
|
|
14145
14171
|
* ```typescript
|
|
14146
14172
|
* import { Partial } from "./classes/Partial";
|
|
14147
14173
|
*
|
|
14148
|
-
* // Get statistical data for BTCUSDT
|
|
14149
|
-
* const stats = await Partial.getData("BTCUSDT");
|
|
14174
|
+
* // Get statistical data for BTCUSDT:my-strategy
|
|
14175
|
+
* const stats = await Partial.getData("BTCUSDT", "my-strategy");
|
|
14150
14176
|
* console.log(`Total events: ${stats.totalEvents}`);
|
|
14151
14177
|
* console.log(`Profit events: ${stats.totalProfit}`);
|
|
14152
14178
|
* console.log(`Loss events: ${stats.totalLoss}`);
|
|
14153
14179
|
*
|
|
14154
14180
|
* // Generate markdown report
|
|
14155
|
-
* const markdown = await Partial.getReport("BTCUSDT");
|
|
14181
|
+
* const markdown = await Partial.getReport("BTCUSDT", "my-strategy");
|
|
14156
14182
|
* console.log(markdown); // Formatted table with all events
|
|
14157
14183
|
*
|
|
14158
14184
|
* // Export report to file
|
|
14159
|
-
* await Partial.dump("BTCUSDT"); // Saves to ./dump/partial/
|
|
14160
|
-
* await Partial.dump("BTCUSDT", "./custom/path"); // Custom directory
|
|
14185
|
+
* await Partial.dump("BTCUSDT", "my-strategy"); // Saves to ./dump/partial/BTCUSDT_my-strategy.md
|
|
14186
|
+
* await Partial.dump("BTCUSDT", "my-strategy", "./custom/path"); // Custom directory
|
|
14161
14187
|
* ```
|
|
14162
14188
|
*/
|
|
14163
14189
|
class PartialUtils {
|
|
@@ -14169,11 +14195,12 @@ class PartialUtils {
|
|
|
14169
14195
|
* Returns aggregated metrics calculated from all profit and loss events.
|
|
14170
14196
|
*
|
|
14171
14197
|
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
14198
|
+
* @param strategyName - Strategy name (e.g., "my-strategy")
|
|
14172
14199
|
* @returns Promise resolving to PartialStatistics object with counts and event list
|
|
14173
14200
|
*
|
|
14174
14201
|
* @example
|
|
14175
14202
|
* ```typescript
|
|
14176
|
-
* const stats = await Partial.getData("BTCUSDT");
|
|
14203
|
+
* const stats = await Partial.getData("BTCUSDT", "my-strategy");
|
|
14177
14204
|
*
|
|
14178
14205
|
* console.log(`Total events: ${stats.totalEvents}`);
|
|
14179
14206
|
* console.log(`Profit events: ${stats.totalProfit} (${(stats.totalProfit / stats.totalEvents * 100).toFixed(1)}%)`);
|
|
@@ -14185,16 +14212,17 @@ class PartialUtils {
|
|
|
14185
14212
|
* }
|
|
14186
14213
|
* ```
|
|
14187
14214
|
*/
|
|
14188
|
-
this.getData = async (symbol) => {
|
|
14189
|
-
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_GET_DATA, { symbol });
|
|
14190
|
-
return await backtest$1.partialMarkdownService.getData(symbol);
|
|
14215
|
+
this.getData = async (symbol, strategyName) => {
|
|
14216
|
+
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_GET_DATA, { symbol, strategyName });
|
|
14217
|
+
return await backtest$1.partialMarkdownService.getData(symbol, strategyName);
|
|
14191
14218
|
};
|
|
14192
14219
|
/**
|
|
14193
|
-
* Generates markdown report with all partial profit/loss events for a symbol.
|
|
14220
|
+
* Generates markdown report with all partial profit/loss events for a symbol-strategy pair.
|
|
14194
14221
|
*
|
|
14195
14222
|
* Creates formatted table containing:
|
|
14196
14223
|
* - Action (PROFIT/LOSS)
|
|
14197
14224
|
* - Symbol
|
|
14225
|
+
* - Strategy
|
|
14198
14226
|
* - Signal ID
|
|
14199
14227
|
* - Position (LONG/SHORT)
|
|
14200
14228
|
* - Level % (+10%, -20%, etc)
|
|
@@ -14205,35 +14233,36 @@ class PartialUtils {
|
|
|
14205
14233
|
* Also includes summary statistics at the end.
|
|
14206
14234
|
*
|
|
14207
14235
|
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
14236
|
+
* @param strategyName - Strategy name (e.g., "my-strategy")
|
|
14208
14237
|
* @returns Promise resolving to markdown formatted report string
|
|
14209
14238
|
*
|
|
14210
14239
|
* @example
|
|
14211
14240
|
* ```typescript
|
|
14212
|
-
* const markdown = await Partial.getReport("BTCUSDT");
|
|
14241
|
+
* const markdown = await Partial.getReport("BTCUSDT", "my-strategy");
|
|
14213
14242
|
* console.log(markdown);
|
|
14214
14243
|
*
|
|
14215
14244
|
* // Output:
|
|
14216
|
-
* // # Partial Profit/Loss Report: BTCUSDT
|
|
14245
|
+
* // # Partial Profit/Loss Report: BTCUSDT:my-strategy
|
|
14217
14246
|
* //
|
|
14218
|
-
* // | Action | Symbol | Signal ID | Position | Level % | Current Price | Timestamp | Mode |
|
|
14219
|
-
* // | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
14220
|
-
* // | PROFIT | BTCUSDT | abc123 | LONG | +10% | 51500.00000000 USD | 2024-01-15T10:30:00.000Z | Backtest |
|
|
14221
|
-
* // | LOSS | BTCUSDT | abc123 | LONG | -10% | 49000.00000000 USD | 2024-01-15T11:00:00.000Z | Backtest |
|
|
14247
|
+
* // | Action | Symbol | Strategy | Signal ID | Position | Level % | Current Price | Timestamp | Mode |
|
|
14248
|
+
* // | --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
|
14249
|
+
* // | PROFIT | BTCUSDT | my-strategy | abc123 | LONG | +10% | 51500.00000000 USD | 2024-01-15T10:30:00.000Z | Backtest |
|
|
14250
|
+
* // | LOSS | BTCUSDT | my-strategy | abc123 | LONG | -10% | 49000.00000000 USD | 2024-01-15T11:00:00.000Z | Backtest |
|
|
14222
14251
|
* //
|
|
14223
14252
|
* // **Total events:** 2
|
|
14224
14253
|
* // **Profit events:** 1
|
|
14225
14254
|
* // **Loss events:** 1
|
|
14226
14255
|
* ```
|
|
14227
14256
|
*/
|
|
14228
|
-
this.getReport = async (symbol) => {
|
|
14229
|
-
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_GET_REPORT, { symbol });
|
|
14230
|
-
return await backtest$1.partialMarkdownService.getReport(symbol);
|
|
14257
|
+
this.getReport = async (symbol, strategyName) => {
|
|
14258
|
+
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_GET_REPORT, { symbol, strategyName });
|
|
14259
|
+
return await backtest$1.partialMarkdownService.getReport(symbol, strategyName);
|
|
14231
14260
|
};
|
|
14232
14261
|
/**
|
|
14233
14262
|
* Generates and saves markdown report to file.
|
|
14234
14263
|
*
|
|
14235
14264
|
* Creates directory if it doesn't exist.
|
|
14236
|
-
* Filename format: {symbol}.md (e.g., "
|
|
14265
|
+
* Filename format: {symbol}_{strategyName}.md (e.g., "BTCUSDT_my-strategy.md")
|
|
14237
14266
|
*
|
|
14238
14267
|
* Delegates to PartialMarkdownService.dump() which:
|
|
14239
14268
|
* 1. Generates markdown report via getReport()
|
|
@@ -14242,26 +14271,27 @@ class PartialUtils {
|
|
|
14242
14271
|
* 4. Logs success/failure to console
|
|
14243
14272
|
*
|
|
14244
14273
|
* @param symbol - Trading pair symbol (e.g., "BTCUSDT")
|
|
14274
|
+
* @param strategyName - Strategy name (e.g., "my-strategy")
|
|
14245
14275
|
* @param path - Output directory path (default: "./dump/partial")
|
|
14246
14276
|
* @returns Promise that resolves when file is written
|
|
14247
14277
|
*
|
|
14248
14278
|
* @example
|
|
14249
14279
|
* ```typescript
|
|
14250
|
-
* // Save to default path: ./dump/partial/
|
|
14251
|
-
* await Partial.dump("BTCUSDT");
|
|
14280
|
+
* // Save to default path: ./dump/partial/BTCUSDT_my-strategy.md
|
|
14281
|
+
* await Partial.dump("BTCUSDT", "my-strategy");
|
|
14252
14282
|
*
|
|
14253
|
-
* // Save to custom path: ./reports/partial/
|
|
14254
|
-
* await Partial.dump("BTCUSDT", "./reports/partial");
|
|
14283
|
+
* // Save to custom path: ./reports/partial/BTCUSDT_my-strategy.md
|
|
14284
|
+
* await Partial.dump("BTCUSDT", "my-strategy", "./reports/partial");
|
|
14255
14285
|
*
|
|
14256
14286
|
* // After multiple symbols backtested, export all reports
|
|
14257
14287
|
* for (const symbol of ["BTCUSDT", "ETHUSDT", "BNBUSDT"]) {
|
|
14258
|
-
* await Partial.dump(symbol, "./backtest-results");
|
|
14288
|
+
* await Partial.dump(symbol, "my-strategy", "./backtest-results");
|
|
14259
14289
|
* }
|
|
14260
14290
|
* ```
|
|
14261
14291
|
*/
|
|
14262
|
-
this.dump = async (symbol, path) => {
|
|
14263
|
-
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_DUMP, { symbol, path });
|
|
14264
|
-
await backtest$1.partialMarkdownService.dump(symbol, path);
|
|
14292
|
+
this.dump = async (symbol, strategyName, path) => {
|
|
14293
|
+
backtest$1.loggerService.info(PARTIAL_METHOD_NAME_DUMP, { symbol, strategyName, path });
|
|
14294
|
+
await backtest$1.partialMarkdownService.dump(symbol, strategyName, path);
|
|
14265
14295
|
};
|
|
14266
14296
|
}
|
|
14267
14297
|
}
|
|
@@ -14274,9 +14304,9 @@ class PartialUtils {
|
|
|
14274
14304
|
* import { Partial } from "backtest-kit";
|
|
14275
14305
|
*
|
|
14276
14306
|
* // Usage same as PartialUtils methods
|
|
14277
|
-
* const stats = await Partial.getData("BTCUSDT");
|
|
14278
|
-
* const report = await Partial.getReport("BTCUSDT");
|
|
14279
|
-
* await Partial.dump("BTCUSDT");
|
|
14307
|
+
* const stats = await Partial.getData("BTCUSDT", "my-strategy");
|
|
14308
|
+
* const report = await Partial.getReport("BTCUSDT", "my-strategy");
|
|
14309
|
+
* await Partial.dump("BTCUSDT", "my-strategy");
|
|
14280
14310
|
* ```
|
|
14281
14311
|
*/
|
|
14282
14312
|
const Partial = new PartialUtils();
|