aiden-shared-calculations-unified 1.0.44 → 1.0.45

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.
@@ -1,53 +1,91 @@
1
1
  /**
2
- * @fileoverview Calculates the necessary components to determine the statistical variance of PnL for each stock,
3
- * serving as a proxy for crowd volatility.
2
+ * @fileoverview Meta-calculation (Pass 2) to calculate a proxy for the
3
+ * crowd's "Sharpe Ratio" (risk-adjusted return) on a per-asset basis.
4
+ *
5
+ * --- META REFACTOR (v2) ---
6
+ * This calculation is now stateless. It declares its dependencies and
7
+ * expects them to be passed to its `process` method.
4
8
  */
5
- const { loadInstrumentMappings } = require('../../utils/sector_mapping_provider');
6
9
 
7
- class PnlDistributionPerStock {
8
- constructor() {
9
- this.distributionData = {};
10
- this.mappings = null;
10
+ class CrowdSharpeRatioProxy {
11
+
12
+ /**
13
+ * (NEW) Statically declare dependencies.
14
+ */
15
+ static getDependencies() {
16
+ return ['pnl-distribution-per-stock'];
11
17
  }
12
18
 
13
- process(portfolioData, yesterdayPortfolio, userId, context) {
14
- const positions = portfolioData.AggregatedPositions || portfolioData.PublicPositions;
15
- if (!positions) return;
19
+ constructor() {}
16
20
 
17
- for (const position of positions) {
18
- const instrumentId = position.InstrumentID;
19
- const netProfit = position.NetProfit;
21
+ /**
22
+ * REFACTORED PROCESS METHOD
23
+ * @param {string} dateStr The date to run the analysis for (e.g., "2025-10-31").
24
+ * @param {object} dependencies The shared dependencies (db, logger).
25
+ * @param {object} config The computation system configuration.
26
+ * @param {object} fetchedDependencies In-memory results from previous passes.
27
+ * e.g., { 'pnl-distribution-per-stock': ... }
28
+ * @returns {Promise<object|null>} The analysis result or null.
29
+ */
30
+ async process(dateStr, dependencies, config, fetchedDependencies) {
31
+ const { logger } = dependencies;
20
32
 
21
- if (!this.distributionData[instrumentId]) {
22
- this.distributionData[instrumentId] = {
23
- pnl_sum: 0,
24
- pnl_sum_sq: 0,
25
- position_count: 0
26
- };
27
- }
33
+ // 1. Get dependency from in-memory cache
34
+ const data = fetchedDependencies['pnl-distribution-per-stock'];
28
35
 
29
- this.distributionData[instrumentId].pnl_sum += netProfit;
30
- this.distributionData[instrumentId].pnl_sum_sq += netProfit * netProfit;
31
- this.distributionData[instrumentId].position_count++;
36
+ // 2. Handle missing dependency
37
+ if (!data) {
38
+ logger.log('WARN', `[CrowdSharpeRatioProxy] Missing dependency 'pnl-distribution-per-stock' for ${dateStr}. Skipping.`);
39
+ return null;
32
40
  }
33
- }
34
41
 
35
- async getResult() {
36
- if (!this.mappings) {
37
- this.mappings = await loadInstrumentMappings();
42
+ const pnlDistribution = data.pnl_distribution_by_asset;
43
+
44
+ if (!pnlDistribution) {
45
+ logger.log('WARN', `[CrowdSharpeRatioProxy] Dependency data for ${dateStr} is empty. Skipping.`);
46
+ return null;
38
47
  }
39
- const result = {};
40
- for (const instrumentId in this.distributionData) {
41
- const ticker = this.mappings.instrumentToTicker[instrumentId] || instrumentId.toString();
42
- result[ticker] = this.distributionData[instrumentId];
48
+
49
+ const results = {};
50
+
51
+ // 3. Calculate Sharpe Proxy for each asset
52
+ for (const ticker in pnlDistribution) {
53
+ const stats = pnlDistribution[ticker];
54
+ const N = stats.position_count;
55
+
56
+ if (N < 2) continue; // Need at least 2 data points for variance
57
+
58
+ const mean = stats.pnl_sum / N; // E(x)
59
+ const mean_sq = stats.pnl_sum_sq / N; // E(x^2)
60
+
61
+ const variance = mean_sq - (mean * mean);
62
+
63
+ if (variance <= 0) {
64
+ results[ticker] = {
65
+ average_pnl: mean,
66
+ std_dev_pnl: 0,
67
+ sharpe_ratio_proxy: 0,
68
+ position_count: N
69
+ };
70
+ continue;
71
+ }
72
+
73
+ const std_dev = Math.sqrt(variance); // "Risk"
74
+ const sharpe_proxy = mean / std_dev; // Return / Risk
75
+
76
+ results[ticker] = {
77
+ average_pnl: mean,
78
+ std_dev_pnl: std_dev,
79
+ sharpe_ratio_proxy: sharpe_proxy,
80
+ position_count: N
81
+ };
43
82
  }
44
- return { pnl_distribution_by_asset: result };
45
- }
46
83
 
47
- reset() {
48
- this.distributionData = {};
49
- this.mappings = null;
84
+ return results;
50
85
  }
86
+
87
+ async getResult() { return null; }
88
+ reset() {}
51
89
  }
52
90
 
53
- module.exports = PnlDistributionPerStock;
91
+ module.exports = CrowdSharpeRatioProxy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.44",
3
+ "version": "1.0.45",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [