aiden-shared-calculations-unified 1.0.5 → 1.0.7
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.
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Correlates social post volume spikes with topics from Gemini analysis.
|
|
3
|
+
* This calculation ONLY uses the social post insights context.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class SocialEventCorrelation {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.todayTickerVolume = {};
|
|
9
|
+
this.yesterdayTickerVolume = {};
|
|
10
|
+
this.todayTopicCounts = {}; // { "TICKER": { "topic": count } }
|
|
11
|
+
this.processed = false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Helper to process a set of posts and populate volume/topic counts.
|
|
16
|
+
* @param {object} socialPostInsights - Map of { [postId]: postData }
|
|
17
|
+
* @param {object} volumeTarget - The object to store volume counts.
|
|
18
|
+
* @param {object} [topicTarget] - Optional. The object to store topic counts.
|
|
19
|
+
*/
|
|
20
|
+
_processPosts(socialPostInsights, volumeTarget, topicTarget = null) {
|
|
21
|
+
if (!socialPostInsights) return;
|
|
22
|
+
const posts = Object.values(socialPostInsights);
|
|
23
|
+
|
|
24
|
+
for (const post of posts) {
|
|
25
|
+
if (!post.tickers || !Array.isArray(post.tickers)) continue;
|
|
26
|
+
|
|
27
|
+
for (const ticker of post.tickers) {
|
|
28
|
+
// 1. Aggregate Volume
|
|
29
|
+
volumeTarget[ticker] = (volumeTarget[ticker] || 0) + 1;
|
|
30
|
+
|
|
31
|
+
// 2. Aggregate Topics (if target provided)
|
|
32
|
+
if (topicTarget) {
|
|
33
|
+
if (!topicTarget[ticker]) {
|
|
34
|
+
topicTarget[ticker] = {};
|
|
35
|
+
}
|
|
36
|
+
// New structure: post.sentiment is { overallSentiment: "...", topics: [...] }
|
|
37
|
+
const topics = post.sentiment?.topics;
|
|
38
|
+
if (topics && Array.isArray(topics)) {
|
|
39
|
+
for (const topic of topics) {
|
|
40
|
+
// Normalize topic to lowercase for consistent counting
|
|
41
|
+
const normalizedTopic = topic.toLowerCase();
|
|
42
|
+
topicTarget[ticker][normalizedTopic] = (topicTarget[ticker][normalizedTopic] || 0) + 1;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @param {null} todayPortfolio - Not used.
|
|
52
|
+
* @param {null} yesterdayPortfolio - Not used.
|
|
53
|
+
* @param {null} userId - Not used.
|
|
54
|
+
* @param {object} context - Shared context (e.g., mappings).
|
|
55
|
+
* @param {object} todayInsights - Daily instrument insights.
|
|
56
|
+
* @param {object} yesterdayInsights - Yesterday's instrument insights.
|
|
57
|
+
* @param {object} todaySocialPostInsights - Map of { [postId]: postData } for today.
|
|
58
|
+
* @param {object} yesterdaySocialPostInsights - Map of { [postId]: postData } for yesterday.
|
|
59
|
+
*/
|
|
60
|
+
async process(todayPortfolio, yesterdayPortfolio, userId, context, todayInsights, yesterdayInsights, todaySocialPostInsights, yesterdaySocialPostInsights) {
|
|
61
|
+
|
|
62
|
+
if (this.processed) {
|
|
63
|
+
return; // Run only once
|
|
64
|
+
}
|
|
65
|
+
this.processed = true;
|
|
66
|
+
|
|
67
|
+
// 1. Process yesterday's posts for baseline volume
|
|
68
|
+
this._processPosts(yesterdaySocialPostInsights, this.yesterdayTickerVolume);
|
|
69
|
+
|
|
70
|
+
// 2. Process today's posts for volume AND topic correlation
|
|
71
|
+
this._processPosts(todaySocialPostInsights, this.todayTickerVolume, this.todayTopicCounts);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getResult() {
|
|
75
|
+
if (!this.processed) return {};
|
|
76
|
+
|
|
77
|
+
const results = {};
|
|
78
|
+
const allTickers = new Set([
|
|
79
|
+
...Object.keys(this.todayTickerVolume),
|
|
80
|
+
...Object.keys(this.yesterdayTickerVolume)
|
|
81
|
+
]);
|
|
82
|
+
|
|
83
|
+
for (const ticker of allTickers) {
|
|
84
|
+
const todayVolume = this.todayTickerVolume[ticker] || 0;
|
|
85
|
+
// Use 1 as baseline if yesterday was 0 to avoid -100% change, but 0 for spike calc
|
|
86
|
+
const yesterdayVolumeForCalc = this.yesterdayTickerVolume[ticker] || 0;
|
|
87
|
+
|
|
88
|
+
let volumeSpikePercent = 0;
|
|
89
|
+
if (yesterdayVolumeForCalc > 0) {
|
|
90
|
+
volumeSpikePercent = ((todayVolume - yesterdayVolumeForCalc) / yesterdayVolumeForCalc) * 100;
|
|
91
|
+
} else if (todayVolume > 0) {
|
|
92
|
+
volumeSpikePercent = todayVolume * 100.0; // e.g., 0 to 5 posts is a 500% increase
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Only report tickers with activity today
|
|
96
|
+
if (todayVolume === 0) continue;
|
|
97
|
+
|
|
98
|
+
// Correlate topics
|
|
99
|
+
const topics = this.todayTopicCounts[ticker] || {};
|
|
100
|
+
const correlatedEvents = Object.entries(topics)
|
|
101
|
+
.map(([topic, postCount]) => ({ topic, postCount }))
|
|
102
|
+
.sort((a, b) => b.postCount - a.postCount); // Sort by count descending
|
|
103
|
+
|
|
104
|
+
results[ticker] = {
|
|
105
|
+
volume: todayVolume,
|
|
106
|
+
volumeSpikePercent: volumeSpikePercent,
|
|
107
|
+
correlatedEvents: correlatedEvents
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return results;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
reset() {
|
|
115
|
+
this.todayTickerVolume = {};
|
|
116
|
+
this.yesterdayTickerVolume = {};
|
|
117
|
+
this.todayTopicCounts = {};
|
|
118
|
+
this.processed = false;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
module.exports = SocialEventCorrelation;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Aggregates sentiment data from social posts.
|
|
3
3
|
* This calculation ONLY uses the social post insights context, not portfolio data.
|
|
4
|
+
* UPDATED: To read sentiment from the `sentiment.overallSentiment` property.
|
|
4
5
|
*/
|
|
5
6
|
|
|
6
7
|
class SocialSentimentAggregation {
|
|
@@ -36,7 +37,9 @@ class SocialSentimentAggregation {
|
|
|
36
37
|
this.totalPosts = posts.length;
|
|
37
38
|
|
|
38
39
|
for (const post of posts) {
|
|
39
|
-
|
|
40
|
+
// --- FIX: Read from the nested overallSentiment property ---
|
|
41
|
+
const sentiment = post.sentiment?.overallSentiment || 'Neutral';
|
|
42
|
+
// --- END FIX ---
|
|
40
43
|
|
|
41
44
|
// 1. Aggregate global sentiment counts
|
|
42
45
|
if (this.sentimentCounts.hasOwnProperty(sentiment)) {
|
|
@@ -109,4 +112,4 @@ class SocialSentimentAggregation {
|
|
|
109
112
|
}
|
|
110
113
|
}
|
|
111
114
|
|
|
112
|
-
module.exports = SocialSentimentAggregation;
|
|
115
|
+
module.exports = SocialSentimentAggregation;
|