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.
- package/calculations/backtests/strategy-performance.js +39 -46
- package/calculations/behavioural/historical/dumb-cohort-flow.js +29 -29
- package/calculations/behavioural/historical/smart-cohort-flow.js +29 -29
- package/calculations/behavioural/historical/smart_money_flow.js +108 -104
- package/calculations/behavioural/historical/user-investment-profile.js +51 -95
- package/calculations/meta/capital_deployment_strategy.js +25 -15
- package/calculations/meta/capital_liquidation_performance.js +18 -4
- package/calculations/meta/capital_vintage_performance.js +18 -4
- package/calculations/meta/cash-flow-deployment.js +42 -16
- package/calculations/meta/cash-flow-liquidation.js +27 -15
- package/calculations/meta/profit_cohort_divergence.js +22 -9
- package/calculations/meta/smart-dumb-divergence-index.js +19 -9
- package/calculations/meta/social_flow_correlation.js +20 -8
- package/calculations/pnl/pnl_distribution_per_stock.js +75 -37
- package/package.json +1 -1
|
@@ -1,53 +1,91 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview
|
|
3
|
-
*
|
|
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
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
14
|
-
const positions = portfolioData.AggregatedPositions || portfolioData.PublicPositions;
|
|
15
|
-
if (!positions) return;
|
|
19
|
+
constructor() {}
|
|
16
20
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
22
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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
|
-
|
|
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 =
|
|
91
|
+
module.exports = CrowdSharpeRatioProxy;
|