generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
170 lines (153 loc) · 5.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import axios from "axios";
import config from "./config.json" assert { type: "json" };
import { SentimentIntensityAnalyzer } from "vader-sentiment";
const getTopHeadlines = async () => {
try {
const response = await axios.get("https://newsapi.org/v2/top-headlines", {
params: {
apiKey: config.NEWS_API_KEY,
category: "business",
language: "en",
pageSize: 100,
},
});
return response.data.articles;
} catch (error) {
console.error("Error fetching top headlines:", error.message);
return [];
}
};
const analyzeSentiment = async (articleTitle) => {
let result = "";
const prompt = `Act as best trader in the world and analyze the following article title: "${articleTitle}". Is the sentiment of the article neutral, bullish, or bearish? After that, specify if the article is related to cryptocurrency, forex, or stocks. You need to determine which market the article is primarily related to. Do not state that the article has no relationship to any of these markets. Write in lowercase.`;
try {
const response = await axios.post(
"https://api.openai.com/v1/completions",
{
prompt: prompt.trim(),
model: "text-davinci-003",
max_tokens: 250,
temperature: 0.7,
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.OPENAI_API_KEY}`,
},
}
);
result = response.data.choices[0].text;
} catch (error) {
console.error("Error analyzing sentiment:", error.message);
return "";
}
if (config.SENTIMENT_ANALYSIS_ALGORITHM === "vader") {
let vaderResult = SentimentIntensityAnalyzer.polarity_scores(articleTitle);
// Sort object by value descending
vaderResult = Object.fromEntries(
Object.entries(vaderResult).sort(([, a], [, b]) => b - a)
);
const highestSentiment = Object.keys(vaderResult)[0];
switch (highestSentiment) {
case "pos":
vaderResult = "bullish";
break;
case "neg":
vaderResult = "bearish";
break;
default:
vaderResult = "neutral";
break;
}
// Replace neutral, bullish or bearish in chatgpt response with vader sentiment
if (result.match(/neutral|bullish|bearish/g)?.length > 0) {
result = result.replace("bullish", vaderResult);
}
return result;
}
};
const incrementSentiment = (report, market, sentiment) => {
report[market][sentiment]++;
};
const printReport = (report) => {
console.log("Sentiment Report:");
console.log("Stocks:");
console.log(`Bullish: ${report.stocks.bullish}`);
console.log(`Bearish: ${report.stocks.bearish}`);
console.log(`Neutral: ${report.stocks.neutral}`);
console.log("Forex:");
console.log(`Bullish: ${report.forex.bullish}`);
console.log(`Bearish: ${report.forex.bearish}`);
console.log(`Neutral: ${report.forex.neutral}`);
console.log("Cryptocurrency:");
console.log(`Bullish: ${report.cryptocurrency.bullish}`);
console.log(`Bearish: ${report.cryptocurrency.bearish}`);
console.log(`Neutral: ${report.cryptocurrency.neutral}`);
};
const crawlFinanceNews = async () => {
try {
const articles = await getTopHeadlines();
const report = {
stocks: {
bullish: 0,
bearish: 0,
neutral: 0,
},
forex: {
bullish: 0,
bearish: 0,
neutral: 0,
},
cryptocurrency: {
bullish: 0,
bearish: 0,
neutral: 0,
},
};
for (const article of articles) {
const articleTitle = article.title;
const sentiment = await analyzeSentiment(articleTitle);
if (sentiment.includes("bullish")) {
if (sentiment.includes("crypto")) {
console.log("Bullish article related to crypto:", articleTitle);
incrementSentiment(report, "cryptocurrency", "bullish");
} else if (sentiment.includes("stock")) {
console.log("Bullish article related to stocks:", articleTitle);
incrementSentiment(report, "stocks", "bullish");
} else if (sentiment.includes("forex")) {
console.log("Bullish article related to forex:", articleTitle);
incrementSentiment(report, "forex", "bullish");
}
} else if (sentiment.includes("bearish")) {
if (sentiment.includes("crypto")) {
console.log("Bearish article related to crypto:", articleTitle);
incrementSentiment(report, "cryptocurrency", "bearish");
} else if (sentiment.includes("stock")) {
console.log("Bearish article related to stocks:", articleTitle);
incrementSentiment(report, "stocks", "bearish");
} else if (sentiment.includes("forex")) {
console.log("Bearish article related to forex:", articleTitle);
incrementSentiment(report, "forex", "bearish");
}
} else if (sentiment.includes("neutral")) {
if (sentiment.includes("crypto")) {
console.log("Neutral article related to crypto:", articleTitle);
incrementSentiment(report, "cryptocurrency", "neutral");
} else if (sentiment.includes("stock")) {
console.log("Neutral article related to stocks:", articleTitle);
incrementSentiment(report, "stocks", "neutral");
} else if (sentiment.includes("forex")) {
console.log("Neutral article related to forex:", articleTitle);
incrementSentiment(report, "forex", "neutral");
}
}
}
return report
} catch (error) {
console.error("Error crawling finance news:", error.message);
}
};
(async () => {
const report = await crawlFinanceNews();
if (report) printReport(report);
})()