aiden-shared-calculations-unified 1.0.28 → 1.0.29
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.
|
@@ -81,64 +81,85 @@ class AssetCrowdFlow {
|
|
|
81
81
|
|
|
82
82
|
async getResult() {
|
|
83
83
|
if (this.user_count === 0 || !this.dates.today) {
|
|
84
|
-
|
|
84
|
+
console.warn('[AssetCrowdFlow] No users processed or dates missing.');
|
|
85
|
+
return {};
|
|
85
86
|
}
|
|
86
87
|
|
|
87
|
-
// Load
|
|
88
|
+
// Load priceMap and mappings if not loaded
|
|
88
89
|
if (!this.priceMap || !this.mappings) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
try {
|
|
91
|
+
const [priceData, mappingData] = await Promise.all([
|
|
92
|
+
loadAllPriceData(),
|
|
93
|
+
loadInstrumentMappings()
|
|
94
|
+
]);
|
|
95
|
+
this.priceMap = priceData;
|
|
96
|
+
this.mappings = mappingData;
|
|
97
|
+
} catch (err) {
|
|
98
|
+
console.error('[AssetCrowdFlow] Failed to load dependencies:', err);
|
|
99
|
+
return {};
|
|
100
|
+
}
|
|
95
101
|
}
|
|
96
|
-
|
|
102
|
+
|
|
97
103
|
const finalResults = {};
|
|
98
104
|
const todayStr = this.dates.today;
|
|
99
105
|
const yesterdayStr = this.dates.yesterday;
|
|
100
106
|
|
|
101
|
-
for (const
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
const avg_day1_value = this.asset_values[
|
|
106
|
-
const avg_day2_value = this.asset_values[
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
107
|
+
for (const rawInstrumentId in this.asset_values) {
|
|
108
|
+
const instrumentId = String(rawInstrumentId); // normalize
|
|
109
|
+
const ticker = this.mappings.instrumentToTicker?.[instrumentId] || `id_${instrumentId}`;
|
|
110
|
+
|
|
111
|
+
const avg_day1_value = this.asset_values[rawInstrumentId].day1_value_sum / this.user_count;
|
|
112
|
+
const avg_day2_value = this.asset_values[rawInstrumentId].day2_value_sum / this.user_count;
|
|
113
|
+
|
|
114
|
+
let priceChangePct = null;
|
|
115
|
+
|
|
116
|
+
// Check priceMap presence
|
|
117
|
+
if (!this.priceMap || !this.priceMap[instrumentId]) {
|
|
118
|
+
console.debug(`[AssetCrowdFlow] Missing priceMap entry for instrumentId ${instrumentId} (${ticker})`);
|
|
119
|
+
} else {
|
|
120
|
+
const priceDay1 = this.priceMap[instrumentId][yesterdayStr];
|
|
121
|
+
const priceDay2 = this.priceMap[instrumentId][todayStr];
|
|
122
|
+
|
|
123
|
+
if (priceDay1 == null) console.debug(`[AssetCrowdFlow] Missing price for ${instrumentId} (${ticker}) on ${yesterdayStr}`);
|
|
124
|
+
if (priceDay2 == null) console.debug(`[AssetCrowdFlow] Missing price for ${instrumentId} (${ticker}) on ${todayStr}`);
|
|
125
|
+
|
|
126
|
+
if (priceDay1 != null && priceDay2 != null && priceDay1 > 0) {
|
|
127
|
+
priceChangePct = (priceDay2 - priceDay1) / priceDay1;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
110
130
|
|
|
111
131
|
if (priceChangePct === null) {
|
|
112
|
-
// Cannot calculate if price data is missing for either day
|
|
113
132
|
finalResults[ticker] = {
|
|
114
133
|
net_crowd_flow_pct: 0,
|
|
115
|
-
|
|
134
|
+
avg_value_day1: avg_day1_value,
|
|
135
|
+
avg_value_day2: avg_day2_value,
|
|
136
|
+
price_change_pct: null,
|
|
137
|
+
user_sample_size: this.user_count,
|
|
138
|
+
warning: 'Missing price data for calculation'
|
|
116
139
|
};
|
|
117
140
|
continue;
|
|
118
141
|
}
|
|
119
142
|
|
|
120
|
-
//
|
|
121
|
-
// We use avg_day1_value as the base. The cash flow proxy calculation
|
|
122
|
-
// uses (avg_value - avg_invested) because it's solving for a different unknown.
|
|
123
|
-
// Here, we are solving for flow *relative to the asset itself*.
|
|
143
|
+
// Calculate expected day2 value from price movement
|
|
124
144
|
const expected_day2_value = avg_day1_value * (1 + priceChangePct);
|
|
125
145
|
|
|
126
|
-
//
|
|
146
|
+
// Net crowd flow = actual minus expected
|
|
127
147
|
const net_crowd_flow_pct = avg_day2_value - expected_day2_value;
|
|
128
148
|
|
|
129
149
|
finalResults[ticker] = {
|
|
130
|
-
net_crowd_flow_pct
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
150
|
+
net_crowd_flow_pct,
|
|
151
|
+
avg_value_day1,
|
|
152
|
+
avg_value_day2,
|
|
153
|
+
expected_day2_value,
|
|
134
154
|
price_change_pct: priceChangePct,
|
|
135
155
|
user_sample_size: this.user_count
|
|
136
156
|
};
|
|
137
157
|
}
|
|
138
|
-
|
|
158
|
+
|
|
139
159
|
return finalResults;
|
|
140
160
|
}
|
|
141
161
|
|
|
162
|
+
|
|
142
163
|
reset() {
|
|
143
164
|
this.asset_values = {};
|
|
144
165
|
this.user_count = 0;
|