aiden-shared-calculations-unified 1.0.2 → 1.0.4
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.
- package/calculations/asset_metrics/asset_dollar_metrics.js +1 -1
- package/calculations/asset_metrics/asset_position_size.js +1 -1
- package/calculations/pnl/asset_pnl_status.js +2 -2
- package/calculations/pnl/profitable_and_unprofitable_status.js +65 -0
- package/calculations/sectors/sector_dollar_metrics.js +1 -1
- package/package.json +1 -1
|
@@ -17,7 +17,7 @@ class AssetDollarMetrics {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
process(portfolioData, userId, context) {
|
|
20
|
+
process(portfolioData, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights) {
|
|
21
21
|
const { instrumentMappings } = context;
|
|
22
22
|
|
|
23
23
|
// FIX: Use the correct portfolio position properties
|
|
@@ -9,7 +9,7 @@ class AssetPositionSize {
|
|
|
9
9
|
this.mappings = null;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
process(portfolioData, userId, context) {
|
|
12
|
+
process(portfolioData, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights) {
|
|
13
13
|
const positions = portfolioData.AggregatedPositions || portfolioData.PublicPositions;
|
|
14
14
|
if (!positions || !Array.isArray(positions)) return;
|
|
15
15
|
|
|
@@ -12,8 +12,8 @@ class AssetPnlStatus {
|
|
|
12
12
|
this.assets[ticker] = { profit_count: 0, loss_count: 0 };
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
process(portfolioData, userId, context) {
|
|
15
|
+
|
|
16
|
+
process(portfolioData, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights) {
|
|
17
17
|
const { instrumentMappings } = context;
|
|
18
18
|
const processedTickers = new Set(); // Ensure one user is only counted once per ticker
|
|
19
19
|
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Counts the number of users whose overall portfolio is in profit vs. loss for the current day.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
class DailyUserPnlStatus {
|
|
6
|
+
constructor() {
|
|
7
|
+
// Initialize counters
|
|
8
|
+
this.profitableUsers = 0;
|
|
9
|
+
this.unprofitableUsers = 0;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Processes a single user's portfolio for the current day.
|
|
14
|
+
* @param {object} todayPortfolio - The portfolio data for the current day.
|
|
15
|
+
* @param {object} yesterdayPortfolio - Not used in this calculation.
|
|
16
|
+
* @param {string} userId - The user's ID.
|
|
17
|
+
* @param {object} context - Shared context data (not used here).
|
|
18
|
+
*/
|
|
19
|
+
process(todayPortfolio, yesterdayPortfolio, userId, context) {
|
|
20
|
+
// Prefer AggregatedPositions as it typically contains NetProfit
|
|
21
|
+
const positions = todayPortfolio?.AggregatedPositions || todayPortfolio?.PublicPositions;
|
|
22
|
+
|
|
23
|
+
// Ensure we have portfolio data and positions
|
|
24
|
+
if (!positions || !Array.isArray(positions) || positions.length === 0) {
|
|
25
|
+
return; // Skip if no positions data for the user today
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Calculate the sum of NetProfit across all positions for the user
|
|
29
|
+
let totalUserPnl = 0;
|
|
30
|
+
for (const position of positions) {
|
|
31
|
+
// Ensure NetProfit exists and is a number, default to 0 otherwise
|
|
32
|
+
totalUserPnl += (typeof position.NetProfit === 'number' ? position.NetProfit : 0);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Increment the appropriate counter based on the total P/L
|
|
36
|
+
if (totalUserPnl > 0) {
|
|
37
|
+
this.profitableUsers++;
|
|
38
|
+
} else if (totalUserPnl < 0) {
|
|
39
|
+
this.unprofitableUsers++;
|
|
40
|
+
}
|
|
41
|
+
// Users with exactly zero P/L are ignored
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Returns the final aggregated counts.
|
|
46
|
+
* @returns {object} Object containing the counts of profitable and unprofitable users.
|
|
47
|
+
*/
|
|
48
|
+
getResult() {
|
|
49
|
+
// Return the raw counts
|
|
50
|
+
return {
|
|
51
|
+
profitable_user_count: this.profitableUsers,
|
|
52
|
+
unprofitable_user_count: this.unprofitableUsers
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Resets the internal counters for the next processing run.
|
|
58
|
+
*/
|
|
59
|
+
reset() {
|
|
60
|
+
this.profitableUsers = 0;
|
|
61
|
+
this.unprofitableUsers = 0;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
module.exports = DailyUserPnlStatus;
|
|
@@ -17,7 +17,7 @@ class SectorDollarMetrics {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
process(portfolioData, userId, context) {
|
|
20
|
+
process(portfolioData, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights) {
|
|
21
21
|
const { sectorMapping } = context; // Assumes sectorMapping is in context
|
|
22
22
|
|
|
23
23
|
// FIX: Use the correct portfolio position properties
|