aiden-shared-calculations-unified 1.0.89 → 1.0.91
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,11 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Core Metric (Pass 2)
|
|
3
|
-
* REFACTORED: Buckets
|
|
3
|
+
* REFACTORED: Buckets P&L status by User Profile.
|
|
4
|
+
* - Pivots data to be User-Centric (User -> Winners/Losers).
|
|
5
|
+
* - Removes sharding complexity (fits within 1MB via structure optimization).
|
|
4
6
|
*/
|
|
5
7
|
class AssetPnlStatus {
|
|
6
8
|
constructor() {
|
|
7
|
-
|
|
8
|
-
this.
|
|
9
|
+
// Map<UserId, { winners: Set<String>, losers: Set<String> }>
|
|
10
|
+
this.userStats = new Map();
|
|
9
11
|
}
|
|
10
12
|
|
|
11
13
|
static getMetadata() {
|
|
@@ -21,80 +23,92 @@ class AssetPnlStatus {
|
|
|
21
23
|
static getDependencies() { return []; }
|
|
22
24
|
|
|
23
25
|
static getSchema() {
|
|
24
|
-
const cohortSchema = {
|
|
25
|
-
"type": "object",
|
|
26
|
-
"properties": {
|
|
27
|
-
"winners": { "type": "array", "items": { "type": "string" } },
|
|
28
|
-
"losers": { "type": "array", "items": { "type": "string" } }
|
|
29
|
-
},
|
|
30
|
-
"required": ["winners", "losers"]
|
|
31
|
-
};
|
|
32
26
|
return {
|
|
33
27
|
"type": "object",
|
|
34
28
|
"properties": {
|
|
35
|
-
"
|
|
36
|
-
|
|
29
|
+
"by_user": {
|
|
30
|
+
"type": "object",
|
|
31
|
+
"description": "Map of User IDs to their winning/losing assets and sectors.",
|
|
32
|
+
"patternProperties": {
|
|
33
|
+
"^[0-9]+$": { // Matches User IDs (CIDs)
|
|
34
|
+
"type": "object",
|
|
35
|
+
"properties": {
|
|
36
|
+
"winners": { "type": "array", "items": { "type": "string" } },
|
|
37
|
+
"losers": { "type": "array", "items": { "type": "string" } }
|
|
38
|
+
},
|
|
39
|
+
"required": ["winners", "losers"]
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
37
43
|
}
|
|
38
44
|
};
|
|
39
45
|
}
|
|
40
46
|
|
|
41
|
-
|
|
42
|
-
if (!
|
|
47
|
+
_initUser(userId) {
|
|
48
|
+
if (!this.userStats.has(userId)) {
|
|
49
|
+
this.userStats.set(userId, { winners: new Set(), losers: new Set() });
|
|
50
|
+
}
|
|
51
|
+
return this.userStats.get(userId);
|
|
43
52
|
}
|
|
44
53
|
|
|
45
54
|
process(context) {
|
|
46
55
|
const { extract } = context.math;
|
|
47
56
|
const { mappings, user } = context;
|
|
57
|
+
const userId = user.id;
|
|
48
58
|
|
|
59
|
+
// 1. Get Positions using the standard extractor
|
|
49
60
|
const positions = extract.getPositions(user.portfolio.today, user.type);
|
|
61
|
+
if (!positions || positions.length === 0) return;
|
|
62
|
+
|
|
63
|
+
// 2. Lazy init stats only if needed
|
|
64
|
+
let stats = null;
|
|
50
65
|
|
|
51
66
|
for (const pos of positions) {
|
|
52
67
|
const instId = extract.getInstrumentId(pos);
|
|
53
68
|
if (!instId) continue;
|
|
54
69
|
|
|
55
70
|
const pnl = extract.getNetProfit(pos);
|
|
56
|
-
if (pnl === 0) continue;
|
|
71
|
+
if (pnl === 0) continue; // Ignore flat positions
|
|
57
72
|
|
|
58
73
|
const ticker = mappings.instrumentToTicker[instId];
|
|
59
74
|
const sector = mappings.instrumentToSector[instId];
|
|
60
75
|
const isWinner = pnl > 0;
|
|
61
76
|
|
|
62
|
-
if (ticker) {
|
|
63
|
-
this.
|
|
64
|
-
|
|
65
|
-
if (
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
if (ticker || sector) {
|
|
78
|
+
if (!stats) stats = this._initUser(userId);
|
|
79
|
+
|
|
80
|
+
if (ticker) {
|
|
81
|
+
if (isWinner) stats.winners.add(ticker);
|
|
82
|
+
else stats.losers.add(ticker);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (sector) {
|
|
86
|
+
if (isWinner) stats.winners.add(sector);
|
|
87
|
+
else stats.losers.add(sector);
|
|
88
|
+
}
|
|
74
89
|
}
|
|
75
90
|
}
|
|
76
91
|
}
|
|
77
92
|
|
|
78
93
|
async getResult() {
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
94
|
+
const byUser = {};
|
|
95
|
+
|
|
96
|
+
for (const [userId, stats] of this.userStats) {
|
|
97
|
+
// Only include users who actually have relevant P&L data
|
|
98
|
+
if (stats.winners.size > 0 || stats.losers.size > 0) {
|
|
99
|
+
byUser[userId] = {
|
|
100
|
+
winners: Array.from(stats.winners),
|
|
101
|
+
losers: Array.from(stats.losers)
|
|
86
102
|
};
|
|
87
103
|
}
|
|
88
|
-
}
|
|
104
|
+
}
|
|
89
105
|
|
|
90
|
-
|
|
91
|
-
buildResult(this.sectorBuckets, result.by_sector);
|
|
92
|
-
return result;
|
|
106
|
+
return { by_user: byUser };
|
|
93
107
|
}
|
|
94
108
|
|
|
95
109
|
reset() {
|
|
96
|
-
this.
|
|
97
|
-
this.sectorBuckets.clear();
|
|
110
|
+
this.userStats.clear();
|
|
98
111
|
}
|
|
99
112
|
}
|
|
113
|
+
|
|
100
114
|
module.exports = AssetPnlStatus;
|
|
@@ -87,9 +87,9 @@ class CorePriceMetrics {
|
|
|
87
87
|
|
|
88
88
|
process(context) {
|
|
89
89
|
const { mappings, prices, date } = context;
|
|
90
|
-
|
|
90
|
+
// FIX 1: Use the correct property name 'instrumentToSector'
|
|
91
|
+
const { instrumentToTicker, instrumentToSector } = mappings;
|
|
91
92
|
|
|
92
|
-
// Uses history from context
|
|
93
93
|
const priceData = prices?.history;
|
|
94
94
|
const todayDateStr = date.today;
|
|
95
95
|
|
|
@@ -101,7 +101,8 @@ class CorePriceMetrics {
|
|
|
101
101
|
const by_instrument = {};
|
|
102
102
|
const tickerToInstrument = {};
|
|
103
103
|
|
|
104
|
-
|
|
104
|
+
// FIX 2: Iterate over Object.values() since priceData is a map, not an array
|
|
105
|
+
for (const p of Object.values(priceData)) {
|
|
105
106
|
const ticker = instrumentToTicker[p.instrumentId];
|
|
106
107
|
if (!ticker) continue;
|
|
107
108
|
tickerToInstrument[ticker] = p.instrumentId;
|
|
@@ -125,7 +126,9 @@ class CorePriceMetrics {
|
|
|
125
126
|
const sectorAggs = {};
|
|
126
127
|
for (const ticker in by_instrument) {
|
|
127
128
|
const instId = tickerToInstrument[ticker];
|
|
128
|
-
|
|
129
|
+
// FIX 1 (Usage): Use the corrected variable name
|
|
130
|
+
const sector = instrumentToSector[instId] || "Unknown";
|
|
131
|
+
|
|
129
132
|
if (!sectorAggs[sector]) sectorAggs[sector] = { metrics: {}, counts: {} };
|
|
130
133
|
|
|
131
134
|
const data = by_instrument[ticker];
|