-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisplay.js
More file actions
307 lines (274 loc) · 11.3 KB
/
display.js
File metadata and controls
307 lines (274 loc) · 11.3 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
const fs = require('fs');
const path = require('path');
const opn = require('opn');
const ETFPEDPFetcher = require('./etf-pe-dp-fetcher.js');
const { processAllETFs, getETFResultByStock } = require('./sp-pdf-metrics.js');
const HSIIndexScraper = require('./hsi-index-scraper.js');
function readConfig() {
try {
const configPath = path.join(process.cwd(), 'config.json');
const configData = fs.readFileSync(configPath, 'utf8');
return JSON.parse(configData);
} catch (error) {
console.error('Error reading config file:', error);
return { stocks: [] };
}
}
function calculateSizeChanges(stock) {
const fileName = 'data/'+stock+'_data.json';
try {
if (!fs.existsSync(fileName)) {
console.log('No data file found');
return null;
}
const fileContent = fs.readFileSync(fileName, 'utf8');
const data = JSON.parse(fileContent);
if (!Array.isArray(data) || data.length < 2) {
return null;
}
const sortedData = [...data].sort((a, b) => new Date(b.date) - new Date(a.date));
const oneDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[1].size)
};
let threeDayChange = null;
if (data.length >= 4) {
threeDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[3].size)
};
}
let fiveDayChange = null;
if (data.length >= 6) {
fiveDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[5].size)
};
}
let tenDayChange = null;
if (data.length >= 11) {
tenDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[10].size)
};
}
let fityDayChange = null;
if (data.length >= 22) {
fityDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[21].size)
};
}
let thirtyDayChange = null;
if (data.length >= 31) {
thirtyDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[30].size)
};
}
let sixtyDayChange = null;
if (data.length >= 61) {
sixtyDayChange = {
date: sortedData[0].date,
change: parseFloat(sortedData[0].size) - parseFloat(sortedData[60].size)
};
}
return {
oneDayChange,
threeDayChange,
fiveDayChange,
tenDayChange,
thirtyDayChange,
sixtyDayChange
};
} catch (error) {
console.error('Error calculating size changes:', error);
return null;
}
}
function formatNumber(num) {
if (num === 'N/A') return num;
return (parseFloat(num)/10000).toLocaleString('zh-CN', {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
}
function getChangeClass(change) {
if (change === 'N/A') return '';
return change > 0 ? 'negative' : 'positive';
}
async function generateHTML() {
const config = readConfig();
const results = [];
const fetcher = new ETFPEDPFetcher();
// 先获取S&P PDF指标数据
let spPdfResults = [];
try {
spPdfResults = await processAllETFs();
console.log(`✅ S&P PDF指标数据获取完成,共获取 ${spPdfResults.length} 个ETF的数据`);
} catch (error) {
console.error('❌ S&P PDF指标数据获取失败:', error);
}
// 构建Map便于查找
const spPdfMap = new Map();
spPdfResults.forEach(item => {
console.log(item);
if (item.stock && !item.error) {
spPdfMap.set(item.stock, item);
}
});
console.log(spPdfMap);
// 读取恒生指数数据(直接调用HSIFundamentalsScraper.getAllHsidata,内部已处理缓存)
let hsiFundamentalsMap = new Map();
try {
const config = readConfig();
const etfIndexMapping = config.etfIndexMapping || {};
const hsiScraper = new HSIIndexScraper();
const hsiData = await hsiScraper.getAllHsidata(etfIndexMapping);
if (hsiData && Array.isArray(hsiData.results)) {
hsiData.results.forEach(item => {
if (item.etfCode && item.fundamentals && item.success) {
hsiFundamentalsMap.set(item.etfCode, item);
}
});
}
await hsiScraper.close && hsiScraper.close();
} catch (e) {
console.error('自动获取恒生指数数据失败:', e);
}
for (const stock of config.stocks) {
const fileName = 'data/'+stock+'_data.json';
let name = 'N/A';
let latestSize = 'N/A';
let indexName = '-';
let peValue = '-';
let dpValue = '-';
try {
if (fs.existsSync(fileName)) {
const fileContent = fs.readFileSync(fileName, 'utf8');
const data = JSON.parse(fileContent);
if (data && data.length > 0) {
name = data[0].name;
latestSize = data[0].size;
}
}
} catch (error) {
console.error(`Error reading data for stock ${stock}:`, error);
}
// 优先使用恒生指数数据
if (hsiFundamentalsMap.has(stock)) {
const hsiData = hsiFundamentalsMap.get(stock);
indexName = hsiData.indexName || '-';
peValue = hsiData.fundamentals.peRatio !== null && hsiData.fundamentals.peRatio !== undefined ? hsiData.fundamentals.peRatio : '-';
dpValue = hsiData.fundamentals.dividendYield !== null && hsiData.fundamentals.dividendYield !== undefined ? hsiData.fundamentals.dividendYield : '-';
console.log(`📊 从恒生指数数据中获取 ${stock} 的数据: PE=${peValue}, DP=${dpValue}`);
} else if (spPdfMap.has(stock)) {
// 其次使用S&P PDF指标数据
const spData = spPdfMap.get(stock);
indexName = spData.indexName || '-';
peValue = spData["预期市盈率"] !== null && spData["预期市盈率"] !== undefined ? spData["预期市盈率"] : '-';
dpValue = spData["股息率"] !== null && spData["股息率"] !== undefined ? spData["股息率"] : '-';
console.log(`📊 从S&P PDF数据中获取 ${stock} 的数据: PE=${peValue}, DP=${dpValue}`);
} else {
// 如果都没有,使用原有的逻辑
try {
const peDpResult = await fetcher.getETFPEAndDP(stock);
if (peDpResult.success) {
indexName = peDpResult.indexName || '-';
peValue = peDpResult.peValue !== null && peDpResult.peValue !== undefined ? peDpResult.peValue : '-';
dpValue = peDpResult.dpValue !== null && peDpResult.dpValue !== undefined ? peDpResult.dpValue : '-';
}
} catch (error) {
console.error(`Error getting PE/DP data for stock ${stock}:`, error);
}
}
const changes = calculateSizeChanges(stock);
results.push({
stock,
name,
latestSize: latestSize === 'N/A' ? 'N/A' : parseFloat(latestSize),
indexName,
peValue: (peValue !== null && peValue !== undefined && !isNaN(Number(peValue))) ? Number(peValue) : '-',
dpValue: (dpValue !== null && dpValue !== undefined && !isNaN(Number(dpValue))) ? Number(dpValue) : '-',
oneDayChange: changes?.oneDayChange?.change ?? 'N/A',
threeDayChange: changes?.threeDayChange?.change ?? 'N/A',
fiveDayChange: changes?.fiveDayChange?.change ?? 'N/A',
tenDayChange: changes?.tenDayChange?.change ?? 'N/A',
thirtyDayChange: changes?.thirtyDayChange?.change ?? 'N/A',
sixtyDayChange: changes?.sixtyDayChange?.change ?? 'N/A'
});
}
let html = '';
results.forEach(result => {
html += `
<tr>
<td>${result.stock}</td>
<td>${result.name}</td>
<td>${formatNumber(result.latestSize)}</td>
<td class="${getChangeClass(result.oneDayChange)}">${formatNumber(result.oneDayChange)}</td>
<td class="${getChangeClass(result.threeDayChange)}">${formatNumber(result.threeDayChange)}</td>
<td class="${getChangeClass(result.fiveDayChange)}">${formatNumber(result.fiveDayChange)}</td>
<td class="${getChangeClass(result.tenDayChange)}">${formatNumber(result.tenDayChange)}</td>
<td class="${getChangeClass(result.thirtyDayChange)}">${formatNumber(result.thirtyDayChange)}</td>
<td class="${getChangeClass(result.sixtyDayChange)}">${formatNumber(result.sixtyDayChange)}</td>
<td>${result.indexName}</td>
<td>${result.peValue}</td>
<td>${result.dpValue}</td>
</tr>
`;
});
const updateTime = new Date().toLocaleString();
// 新增:将表格数据以JSON形式返回
return {
tableHtml: html,
updateTime: updateTime,
tableData: JSON.stringify(results)
};
}
// Write the HTML content to a file
async function writeHTML() {
// Get today's date string in YYYY-MM-DD format
const today = new Date();
const yyyy = today.getFullYear();
const mm = String(today.getMonth() + 1).padStart(2, '0');
const dd = String(today.getDate()).padStart(2, '0');
const dateStr = `${yyyy}-${mm}-${dd}`;
// Ensure report-data directory exists
const htmlDataDir = path.join(process.cwd(), 'report-data');
if (!fs.existsSync(htmlDataDir)) {
fs.mkdirSync(htmlDataDir);
}
// Set output file path
const outputFileName = `etf-report-${dateStr}.html`;
const outputPath = path.join(htmlDataDir, outputFileName);
// If today's file exists, open it and return
if (fs.existsSync(outputPath)) {
console.log(`Today's HTML file already exists: ${outputPath}`);
opn(outputPath);
return;
}
// Otherwise, generate new HTML
const { tableHtml, updateTime, tableData } = await generateHTML();
const templatePath = path.join(__dirname, 'etf-report-template.html');
let htmlContent = fs.readFileSync(templatePath, 'utf8');
htmlContent = htmlContent.replace('<!-- Data will be inserted here by JavaScript -->', tableHtml + `\n<script id="tableData" type="application/json">${tableData}</script>`);
htmlContent = htmlContent.replace('<span id="updateTime"></span>', updateTime);
fs.writeFileSync(outputPath, htmlContent);
console.log('HTML file has been generated successfully!');
opn(outputPath);
}
// Export the functions
module.exports = { writeHTML, calculateSizeChanges };
// Main function to run the program
async function main() {
try {
await writeHTML();
} catch (error) {
console.error('Error generating HTML:', error);
process.exit(1);
}
}
// Run the main function if this file is executed directly
if (require.main === module) {
main();
}