-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (96 loc) · 3.18 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
// @ts-check
const nodemailer = require('nodemailer')
const cron = require('node-cron')
const fs = require('fs')
const { getFearGreedIndex } = require('./lib/fear_greed')
const { getStockRSI } = require('./lib/rsi')
const { getVIXInfo } = require('./lib/vix')
const { fixDigit } = require('./lib/util')
// 读取配置文件
const config = JSON.parse(fs.readFileSync('config.json', 'utf-8'))
// SMTP 邮件发送配置,从配置文件读取
const transporter = nodemailer.createTransport({
host: config.smtp.host,
port: config.smtp.port,
secure: config.smtp.secure,
auth: {
user: config.smtp.auth.user,
pass: config.smtp.auth.pass,
},
})
// 发送邮件函数
async function sendEmail(stockRSIReports) {
const mailOptions = {
from: config.email.from,
to: config.email.to,
subject: 'Stock Alert: RSI Notification',
text: stockRSIReports.join('\n'), // 汇总每个股票的 RSI 信息
}
try {
await transporter.sendMail(mailOptions)
console.log('Email sent successfully.')
} catch (error) {
console.error('Error sending email:', error)
}
}
// 获取多个股票的 RSI 并发送邮件
async function getMultipleStocksRSI() {
const stockRSIReports = []
// 遍历配置文件中列出的每个股票
for (const stock of config.stocks) {
const stockRSI = await getStockRSI(stock)
if (stockRSI.rsi !== null) {
// 检查 RSI 是否小于 30
if (stockRSI.rsi < 32) {
stockRSIReports.push(
`RSI for ${stockRSI.stock} is below 32. Current RSI: ${fixDigit(stockRSI.rsi)}. Consider buying.`
)
} else if (stockRSI.rsi > 68) {
stockRSIReports.push(
`RSI for ${stockRSI.stock} is above 68. Current RSI: ${fixDigit(stockRSI.rsi)}. Consider selling.`
)
} else {
stockRSIReports.push(`RSI for ${stockRSI.stock} is ${fixDigit(stockRSI.rsi)}.`)
}
} else {
stockRSIReports.push(`Failed to retrieve data for ${stockRSI.stock}: ${stockRSI.error}`)
}
}
// 获取 VIX 指数
const vixInfo = await getVIXInfo()
if (vixInfo) {
stockRSIReports.push(`VIX index is ${fixDigit(vixInfo.price)} (MA20: ${fixDigit(vixInfo.ma20)}).`)
}
// 获取 CNN Fear & Greed Index
const fearGreedIndex = await getFearGreedIndex()
// 如果指数为 "Fear" 或 "Extreme Fear",添加到报告中
if (
fearGreedIndex
// (fearGreedIndex.rating.toLowerCase() === 'fear' ||
// fearGreedIndex.rating.toLowerCase() === 'extreme fear')
) {
stockRSIReports.push(
`CNN Fear & Greed Index is in ${fearGreedIndex.rating} state (Score: ${fixDigit(fearGreedIndex.score)}).`
)
}
// 发送邮件,如果检测结果存在
if (stockRSIReports.length > 0) {
await sendEmail(stockRSIReports)
} else {
console.log('No valid data to send.')
}
}
// 使用 cron 每天工作日(周一至周五)定时运行
// cron 格式: '秒 分钟 小时 日 月 星期几'
cron.schedule(
'0 20 * * 1-5',
() => {
console.log('Running stock check...')
getMultipleStocksRSI()
},
{
timezone: 'America/New_York', // 设置时区为美东时间(美国股市常用时区)
}
)
// Export for test.
module.exports = { getMultipleStocksRSI, getStockRSI }