aiden-shared-calculations-unified 1.0.90 → 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;
|