aiden-shared-calculations-unified 1.0.10 → 1.0.11
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,71 +1,118 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @fileoverview Estimates
|
|
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).
|
|
3
16
|
*/
|
|
4
17
|
|
|
5
|
-
class
|
|
18
|
+
class CrowdCashFlowProxy {
|
|
6
19
|
constructor() {
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
20
|
+
this.total_invested_day1 = 0;
|
|
21
|
+
this.total_value_day1 = 0;
|
|
22
|
+
this.total_invested_day2 = 0;
|
|
23
|
+
this.total_value_day2 = 0;
|
|
24
|
+
this.user_count = 0;
|
|
9
25
|
}
|
|
10
26
|
|
|
11
27
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param {object}
|
|
14
|
-
* @
|
|
28
|
+
* Helper to sum a specific field from an AggregatedPositions array.
|
|
29
|
+
* @param {Array<object>} positions - The AggregatedPositions array.
|
|
30
|
+
* @param {string} field - The field to sum ('Invested' or 'Value').
|
|
31
|
+
* @returns {number} The total sum of that field.
|
|
15
32
|
*/
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return portfolio.AggregatedPositions.reduce((sum, pos) => sum + (pos.NetProfit || 0), 0);
|
|
20
|
-
} else if (portfolio && portfolio.PublicPositions) {
|
|
21
|
-
// PublicPositions might lack NetProfit, adjust if necessary based on your data
|
|
22
|
-
return portfolio.PublicPositions.reduce((sum, pos) => sum + (pos.NetProfit || 0), 0);
|
|
33
|
+
_sumPositions(positions, field) {
|
|
34
|
+
if (!positions || !Array.isArray(positions)) {
|
|
35
|
+
return 0;
|
|
23
36
|
}
|
|
24
|
-
return
|
|
37
|
+
return positions.reduce((sum, pos) => sum + (pos[field] || 0), 0);
|
|
25
38
|
}
|
|
26
39
|
|
|
27
40
|
process(todayPortfolio, yesterdayPortfolio, userId) {
|
|
28
|
-
|
|
29
|
-
|
|
41
|
+
// This calculation requires both days' data to compare
|
|
42
|
+
if (!todayPortfolio || !yesterdayPortfolio || !todayPortfolio.AggregatedPositions || !yesterdayPortfolio.AggregatedPositions) {
|
|
30
43
|
return;
|
|
31
44
|
}
|
|
32
45
|
|
|
33
|
-
const
|
|
46
|
+
const invested_day1 = this._sumPositions(yesterdayPortfolio.AggregatedPositions, 'Invested');
|
|
47
|
+
const value_day1 = this._sumPositions(yesterdayPortfolio.AggregatedPositions, 'Value');
|
|
48
|
+
const invested_day2 = this._sumPositions(todayPortfolio.AggregatedPositions, 'Invested');
|
|
49
|
+
const value_day2 = this._sumPositions(todayPortfolio.AggregatedPositions, 'Value');
|
|
34
50
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
// If PnL can't be calculated for either day, we can't reliably estimate cash flow
|
|
39
|
-
if (todayPnl === null || yesterdayPnl === null) {
|
|
51
|
+
// Only include users who have some form of positions
|
|
52
|
+
if (invested_day1 === 0 && invested_day2 === 0 && value_day1 === 0 && value_day2 === 0) {
|
|
40
53
|
return;
|
|
41
54
|
}
|
|
42
55
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
const depositPercentage = (cashFlow / yesterdayPortfolio.PortfolioValue) * 100;
|
|
49
|
-
this.totalDepositPercentage += depositPercentage;
|
|
50
|
-
this.userCount++;
|
|
51
|
-
}
|
|
56
|
+
this.total_invested_day1 += invested_day1;
|
|
57
|
+
this.total_value_day1 += value_day1;
|
|
58
|
+
this.total_invested_day2 += invested_day2;
|
|
59
|
+
this.total_value_day2 += value_day2;
|
|
60
|
+
this.user_count++;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
63
|
getResult() {
|
|
55
|
-
if (this.
|
|
56
|
-
return {}; //
|
|
64
|
+
if (this.user_count === 0) {
|
|
65
|
+
return {}; // No users processed, return empty.
|
|
57
66
|
}
|
|
58
67
|
|
|
68
|
+
// 1. Calculate the average portfolio for the crowd
|
|
69
|
+
const avg_invested_day1 = this.total_invested_day1 / this.user_count;
|
|
70
|
+
const avg_value_day1 = this.total_value_day1 / this.user_count;
|
|
71
|
+
const avg_invested_day2 = this.total_invested_day2 / this.user_count;
|
|
72
|
+
const avg_value_day2 = this.total_value_day2 / this.user_count;
|
|
73
|
+
|
|
74
|
+
// 2. Isolate the three effects
|
|
75
|
+
const total_value_change = avg_value_day2 - avg_value_day1;
|
|
76
|
+
const pl_effect = avg_value_day1 - avg_invested_day1;
|
|
77
|
+
const trading_effect = avg_invested_day2 - avg_invested_day1;
|
|
78
|
+
|
|
79
|
+
// 3. Solve for the Cash Flow Effect
|
|
80
|
+
// Total_Change = pl_effect + trading_effect + cash_flow_effect
|
|
81
|
+
const cash_flow_effect = total_value_change - pl_effect - trading_effect;
|
|
82
|
+
|
|
59
83
|
return {
|
|
60
|
-
//
|
|
61
|
-
|
|
84
|
+
// The final proxy value.
|
|
85
|
+
// A negative value signals a net DEPOSIT.
|
|
86
|
+
// A positive value signals a net WITHDRAWAL.
|
|
87
|
+
cash_flow_effect_proxy: cash_flow_effect,
|
|
88
|
+
|
|
89
|
+
// Interpretation for the frontend
|
|
90
|
+
interpretation: "A negative value signals a net crowd deposit. A positive value signals a net crowd withdrawal.",
|
|
91
|
+
|
|
92
|
+
// Debug components
|
|
93
|
+
components: {
|
|
94
|
+
total_value_change: total_value_change,
|
|
95
|
+
pl_effect: pl_effect,
|
|
96
|
+
trading_effect: trading_effect
|
|
97
|
+
},
|
|
98
|
+
// Debug averages
|
|
99
|
+
averages: {
|
|
100
|
+
avg_invested_day1: avg_invested_day1,
|
|
101
|
+
avg_value_day1: avg_value_day1,
|
|
102
|
+
avg_invested_day2: avg_invested_day2,
|
|
103
|
+
avg_value_day2: avg_value_day2
|
|
104
|
+
},
|
|
105
|
+
user_sample_size: this.user_count
|
|
62
106
|
};
|
|
63
107
|
}
|
|
64
108
|
|
|
65
109
|
reset() {
|
|
66
|
-
this.
|
|
67
|
-
this.
|
|
110
|
+
this.total_invested_day1 = 0;
|
|
111
|
+
this.total_value_day1 = 0;
|
|
112
|
+
this.total_invested_day2 = 0;
|
|
113
|
+
this.total_value_day2 = 0;
|
|
114
|
+
this.user_count = 0;
|
|
68
115
|
}
|
|
69
116
|
}
|
|
70
117
|
|
|
71
|
-
module.exports =
|
|
118
|
+
module.exports = CrowdCashFlowProxy;
|