aiden-shared-calculations-unified 1.0.26 → 1.0.28

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.
@@ -95,21 +95,22 @@ class DailyUserActivityTracker {
95
95
 
96
96
  // 3. Check for reallocation (only possible if we have AggregatedPositions for both days)
97
97
  // This checks for changes in the 'Invested' percentage
98
+ // 3. Check for reallocation (only possible if we have AggregatedPositions for both days)
98
99
  if (yHasAgg && tHasAgg) {
99
100
  for (const tId of tIds) {
100
- // We know tId is also in yIds from the checks above
101
101
  const tInvested = tPosMap.get(tId).invested;
102
- const yInvested = yPosMap.get(yId).invested;
102
+ const yInvested = yPosMap.get(tId)?.invested ?? 0;
103
103
 
104
104
  // Check for a meaningful change (e.g., > 0.01% to avoid float noise)
105
105
  if (Math.abs(tInvested - yInvested) > 0.0001) {
106
106
  isActive = true;
107
107
  this.activityEvents.reallocation++;
108
- break; // Found activity
108
+ break;
109
109
  }
110
110
  }
111
111
  }
112
112
 
113
+
113
114
  if (isActive) {
114
115
  this.activeUserIds.add(userId);
115
116
  }
@@ -0,0 +1,122 @@
1
+ /**
2
+ * @fileoverview Estimates a proxy for net crowd cash flow (Deposits vs. Withdrawals)
3
+ * by aggregating portfolio percentage changes across all users.
4
+ *
5
+ * This calculation is based on the formula:
6
+ * Total_Change = P/L_Effect + Trading_Effect + Cash_Flow_Effect
7
+ *
8
+ * Where:
9
+ * - Total_Change = avg_value_day2 - avg_value_day1
10
+ * - P/L_Effect = avg_value_day1 - avg_invested_day1
11
+ * - Trading_Effect = avg_invested_day2 - avg_invested_day1
12
+ *
13
+ * We solve for Cash_Flow_Effect, which serves as our proxy.
14
+ * A negative value indicates a net DEPOSIT (denominator grew, shrinking all %s).
15
+ * A positive value indicates a net WITHDRAWAL (denominator shrank, inflating all %s).
16
+ *
17
+ * NOTE: This file is a logical duplicate of deposit_withdrawal_percentage.js
18
+ * to satisfy the hardcoded 'crowd-cash-flow-proxy' dependency in several
19
+ * meta-calculations.
20
+ */
21
+
22
+ class CrowdCashFlowProxy {
23
+ constructor() {
24
+ this.total_invested_day1 = 0;
25
+ this.total_value_day1 = 0;
26
+ this.total_invested_day2 = 0;
27
+ this.total_value_day2 = 0;
28
+ this.user_count = 0;
29
+ }
30
+
31
+ /**
32
+ * Helper to sum a specific field from an AggregatedPositions array.
33
+ * @param {Array<object>} positions - The AggregatedPositions array.
34
+ * @param {string} field - The field to sum ('Invested' or 'Value').
35
+ * @returns {number} The total sum of that field.
36
+ */
37
+ _sumPositions(positions, field) {
38
+ if (!positions || !Array.isArray(positions)) {
39
+ return 0;
40
+ }
41
+ return positions.reduce((sum, pos) => sum + (pos[field] || 0), 0);
42
+ }
43
+
44
+ process(todayPortfolio, yesterdayPortfolio, userId) {
45
+ // This calculation requires both days' data to compare
46
+ if (!todayPortfolio || !yesterdayPortfolio || !todayPortfolio.AggregatedPositions || !yesterdayPortfolio.AggregatedPositions) {
47
+ return;
48
+ }
49
+
50
+ const invested_day1 = this._sumPositions(yesterdayPortfolio.AggregatedPositions, 'Invested');
51
+ const value_day1 = this._sumPositions(yesterdayPortfolio.AggregatedPositions, 'Value');
52
+ const invested_day2 = this._sumPositions(todayPortfolio.AggregatedPositions, 'Invested');
53
+ const value_day2 = this._sumPositions(todayPortfolio.AggregatedPositions, 'Value');
54
+
55
+ // Only include users who have some form of positions
56
+ if (invested_day1 === 0 && invested_day2 === 0 && value_day1 === 0 && value_day2 === 0) {
57
+ return;
58
+ }
59
+
60
+ this.total_invested_day1 += invested_day1;
61
+ this.total_value_day1 += value_day1;
62
+ this.total_invested_day2 += invested_day2;
63
+ this.total_value_day2 += value_day2;
64
+ this.user_count++;
65
+ }
66
+
67
+ getResult() {
68
+ if (this.user_count === 0) {
69
+ return {}; // No users processed, return empty.
70
+ }
71
+
72
+ // 1. Calculate the average portfolio for the crowd
73
+ const avg_invested_day1 = this.total_invested_day1 / this.user_count;
74
+ const avg_value_day1 = this.total_value_day1 / this.user_count;
75
+ const avg_invested_day2 = this.total_invested_day2 / this.user_count;
76
+ const avg_value_day2 = this.total_value_day2 / this.user_count;
77
+
78
+ // 2. Isolate the three effects
79
+ const total_value_change = avg_value_day2 - avg_value_day1;
80
+ const pl_effect = avg_value_day1 - avg_invested_day1;
81
+ const trading_effect = avg_invested_day2 - avg_invested_day1;
82
+
83
+ // 3. Solve for the Cash Flow Effect
84
+ // Total_Change = pl_effect + trading_effect + cash_flow_effect
85
+ const cash_flow_effect = total_value_change - pl_effect - trading_effect;
86
+
87
+ return {
88
+ // The final proxy value.
89
+ // A negative value signals a net DEPOSIT.
90
+ // A positive value signals a net WITHDRAWAL.
91
+ cash_flow_effect_proxy: cash_flow_effect,
92
+
93
+ // Interpretation for the frontend
94
+ interpretation: "A negative value signals a net crowd deposit. A positive value signals a net crowd withdrawal.",
95
+
96
+ // Debug components
97
+ components: {
98
+ total_value_change: total_value_change,
99
+ pl_effect: pl_effect,
100
+ trading_effect: trading_effect
101
+ },
102
+ // Debug averages
103
+ averages: {
104
+ avg_invested_day1: avg_invested_day1,
105
+ avg_value_day1: avg_value_day1,
106
+ avg_invested_day2: avg_invested_day2,
107
+ avg_value_day2: avg_value_day2
108
+ },
109
+ user_sample_size: this.user_count
110
+ };
111
+ }
112
+
113
+ reset() {
114
+ this.total_invested_day1 = 0;
115
+ this.total_value_day1 = 0;
116
+ this.total_invested_day2 = 0;
117
+ this.total_value_day2 = 0;
118
+ this.user_count = 0;
119
+ }
120
+ }
121
+
122
+ module.exports = CrowdCashFlowProxy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aiden-shared-calculations-unified",
3
- "version": "1.0.26",
3
+ "version": "1.0.28",
4
4
  "description": "Shared calculation modules for the BullTrackers Computation System.",
5
5
  "main": "index.js",
6
6
  "files": [