aiden-shared-calculations-unified 1.0.3 → 1.0.5
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/sectors/sector_dollar_metrics.js +1 -1
- package/calculations/socialPosts/social_activity_aggregation.js +99 -0
- package/calculations/socialPosts/social_sentiment_aggregation.js +112 -0
- 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
|
|
|
@@ -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
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Aggregates activity data (likes, comments) from social posts.
|
|
3
|
+
* This calculation ONLY uses the social post insights context, not portfolio data.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class SocialActivityAggregation {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.tickerActivity = {};
|
|
9
|
+
this.totalLikes = 0;
|
|
10
|
+
this.totalComments = 0;
|
|
11
|
+
this.processed = false; // Flag to ensure this runs only once
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {null} todayPortfolio - Not used.
|
|
16
|
+
* @param {null} yesterdayPortfolio - Not used.
|
|
17
|
+
* @param {null} userId - Not used.
|
|
18
|
+
* @param {object} context - Shared context (e.g., mappings).
|
|
19
|
+
* @param {object} todayInsights - Daily instrument insights.
|
|
20
|
+
* @param {object} yesterdayInsights - Yesterday's instrument insights.
|
|
21
|
+
* @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
|
|
22
|
+
* @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
|
|
23
|
+
*/
|
|
24
|
+
async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
|
|
25
|
+
// This process should only run once per day
|
|
26
|
+
if (this.processed || !todaySocialPostInsights) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
this.processed = true;
|
|
30
|
+
|
|
31
|
+
const posts = Object.values(todaySocialPostInsights);
|
|
32
|
+
|
|
33
|
+
for (const post of posts) {
|
|
34
|
+
const likeCount = post.likeCount || 0;
|
|
35
|
+
const commentCount = post.commentCount || 0;
|
|
36
|
+
|
|
37
|
+
// 1. Aggregate global counts
|
|
38
|
+
this.totalLikes += likeCount;
|
|
39
|
+
this.totalComments += commentCount;
|
|
40
|
+
|
|
41
|
+
// 2. Aggregate counts per ticker
|
|
42
|
+
if (post.tickers && Array.isArray(post.tickers)) {
|
|
43
|
+
for (const ticker of post.tickers) {
|
|
44
|
+
if (!this.tickerActivity[ticker]) {
|
|
45
|
+
this.tickerActivity[ticker] = {
|
|
46
|
+
totalLikes: 0,
|
|
47
|
+
totalComments: 0,
|
|
48
|
+
totalPosts: 0
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
this.tickerActivity[ticker].totalLikes += likeCount;
|
|
52
|
+
this.tickerActivity[ticker].totalComments += commentCount;
|
|
53
|
+
this.tickerActivity[ticker].totalPosts++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async getResult() {
|
|
60
|
+
if (!this.processed) return {};
|
|
61
|
+
|
|
62
|
+
// Calculate average activity per ticker
|
|
63
|
+
const tickerAverageActivity = {};
|
|
64
|
+
for (const ticker in this.tickerActivity) {
|
|
65
|
+
const data = this.tickerActivity[ticker];
|
|
66
|
+
if (data.totalPosts > 0) {
|
|
67
|
+
tickerAverageActivity[ticker] = {
|
|
68
|
+
avgLikesPerPost: data.totalLikes / data.totalPosts,
|
|
69
|
+
avgCommentsPerPost: data.totalComments / data.totalPosts,
|
|
70
|
+
...data // Include total counts
|
|
71
|
+
};
|
|
72
|
+
} else {
|
|
73
|
+
tickerAverageActivity[ticker] = {
|
|
74
|
+
avgLikesPerPost: 0,
|
|
75
|
+
avgCommentsPerPost: 0,
|
|
76
|
+
...data
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
globalActivity: {
|
|
83
|
+
totalLikes: this.totalLikes,
|
|
84
|
+
totalComments: this.totalComments,
|
|
85
|
+
totalPosts: Object.keys(todaySocialPostInsights || {}).length
|
|
86
|
+
},
|
|
87
|
+
tickerActivity: tickerAverageActivity
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
reset() {
|
|
92
|
+
this.tickerActivity = {};
|
|
93
|
+
this.totalLikes = 0;
|
|
94
|
+
this.totalComments = 0;
|
|
95
|
+
this.processed = false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = SocialActivityAggregation;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Aggregates sentiment data from social posts.
|
|
3
|
+
* This calculation ONLY uses the social post insights context, not portfolio data.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class SocialSentimentAggregation {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.tickerSentiments = {};
|
|
9
|
+
this.totalPosts = 0;
|
|
10
|
+
this.sentimentCounts = {
|
|
11
|
+
Bullish: 0,
|
|
12
|
+
Bearish: 0,
|
|
13
|
+
Neutral: 0
|
|
14
|
+
};
|
|
15
|
+
this.processed = false; // Flag to ensure this runs only once
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {null} todayPortfolio - Not used.
|
|
20
|
+
* @param {null} yesterdayPortfolio - Not used.
|
|
21
|
+
* @param {null} userId - Not used.
|
|
22
|
+
* @param {object} context - Shared context (e.g., mappings).
|
|
23
|
+
* @param {object} todayInsights - Daily instrument insights.
|
|
24
|
+
* @param {object} yesterdayInsights - Yesterday's instrument insights.
|
|
25
|
+
* @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
|
|
26
|
+
* @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
|
|
27
|
+
*/
|
|
28
|
+
async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
|
|
29
|
+
// This process should only run once per day, triggered by the first "user" (which is just a trigger).
|
|
30
|
+
if (this.processed || !todaySocialPostInsights) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
this.processed = true;
|
|
34
|
+
|
|
35
|
+
const posts = Object.values(todaySocialPostInsights);
|
|
36
|
+
this.totalPosts = posts.length;
|
|
37
|
+
|
|
38
|
+
for (const post of posts) {
|
|
39
|
+
const sentiment = post.sentiment || 'Neutral';
|
|
40
|
+
|
|
41
|
+
// 1. Aggregate global sentiment counts
|
|
42
|
+
if (this.sentimentCounts.hasOwnProperty(sentiment)) {
|
|
43
|
+
this.sentimentCounts[sentiment]++;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 2. Aggregate sentiment per ticker
|
|
47
|
+
if (post.tickers && Array.isArray(post.tickers)) {
|
|
48
|
+
for (const ticker of post.tickers) {
|
|
49
|
+
if (!this.tickerSentiments[ticker]) {
|
|
50
|
+
this.tickerSentiments[ticker] = {
|
|
51
|
+
Bullish: 0,
|
|
52
|
+
Bearish: 0,
|
|
53
|
+
Neutral: 0,
|
|
54
|
+
totalPosts: 0
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
if (this.tickerSentiments[ticker].hasOwnProperty(sentiment)) {
|
|
58
|
+
this.tickerSentiments[ticker][sentiment]++;
|
|
59
|
+
}
|
|
60
|
+
this.tickerSentiments[ticker].totalPosts++;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async getResult() {
|
|
67
|
+
if (!this.processed) return {};
|
|
68
|
+
|
|
69
|
+
// Calculate global sentiment ratio
|
|
70
|
+
let globalSentimentRatio = 0;
|
|
71
|
+
const totalSentiments = this.sentimentCounts.Bullish + this.sentimentCounts.Bearish;
|
|
72
|
+
if (totalSentiments > 0) {
|
|
73
|
+
globalSentimentRatio = (this.sentimentCounts.Bullish / totalSentiments) * 100;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Calculate per-ticker sentiment ratios
|
|
77
|
+
const tickerSentimentRatios = {};
|
|
78
|
+
for (const ticker in this.tickerSentiments) {
|
|
79
|
+
const data = this.tickerSentiments[ticker];
|
|
80
|
+
const totalTickerSentiments = data.Bullish + data.Bearish;
|
|
81
|
+
if (totalTickerSentiments > 0) {
|
|
82
|
+
tickerSentimentRatios[ticker] = {
|
|
83
|
+
sentimentRatio: (data.Bullish / totalTickerSentiments) * 100,
|
|
84
|
+
...data // Include counts
|
|
85
|
+
};
|
|
86
|
+
} else {
|
|
87
|
+
tickerSentimentRatios[ticker] = {
|
|
88
|
+
sentimentRatio: 50, // Default to neutral if no clear sentiment
|
|
89
|
+
...data // Include counts
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
globalSentiment: {
|
|
96
|
+
...this.sentimentCounts,
|
|
97
|
+
totalPosts: this.totalPosts,
|
|
98
|
+
sentimentRatio: globalSentimentRatio
|
|
99
|
+
},
|
|
100
|
+
tickerSentiment: tickerSentimentRatios
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
reset() {
|
|
105
|
+
this.tickerSentiments = {};
|
|
106
|
+
this.totalPosts = 0;
|
|
107
|
+
this.sentimentCounts = { Bullish: 0, Bearish: 0, Neutral: 0 };
|
|
108
|
+
this.processed = false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
module.exports = SocialSentimentAggregation;
|