This repository has been archived by the owner on Nov 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.js
139 lines (123 loc) · 4.66 KB
/
timer.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
'use strict';
const colors = require('colors');
const nodemailer = require('nodemailer');
const fundService = require('./app/services/fund');
const purchaseInfoModel = require('./app/models/purchaseInfo');
const schedule = require('node-schedule');
const Table = require('cli-table');
const co = require('co');
const rtProfitRatePerYearThreshold = {
'000071':15.0,
'000930':13.0,
'002656':15.0,
'160119':15.0,
'202015':15.0,
'202108':13.0,
};
const MININUM_HOLDING_DAYS = 90;
const getTimestampString = () => {
const now = new Date(Date.now());
const hours = now.getHours() < 10 ? '0'+now.getHours() : now.getHours();
const minutes = now.getMinutes() < 10 ? '0'+now.getMinutes() : now.getMinutes();
const seconds = now.getSeconds() < 10 ? '0'+now.getSeconds() : now.getSeconds();
let milliSeconds = now.getMilliseconds()+'';
if(milliSeconds.length === 1){
milliSeconds = '00'+milliSeconds;
}else if(milliSeconds.length === 2){
milliSeconds = '0'+milliSeconds;
}
return now.getFullYear()+'-'+(now.getMonth()+1)+'-'+now.getDate()+' '+hours+':'+minutes+':'+seconds+'.'+milliSeconds;
};
const getFundIds = () => {
return purchaseInfoModel.getFundIds();
};
const sendEmail = (content) => {
const transporter = nodemailer.createTransport('smtps://15606617716@163.com:yy123456@smtp.163.com');
const mailOptions = {
from: '"网易邮箱 👥" <15606617716@163.com>',
to: '394062113@qq.com',
subject: '基金收益率达标 ✔',
text: content, // plaintext body
html: content // html body
};
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
};
const renderEmail = (mailData) => {
let renderResult = '';
for(let elem of mailData){
renderResult += '['+elem.id+' '+elem.name+'] 已经达到收益预期,实时年化收益率为'+elem.rtProfitRatePerYear.toFixed(2)+'%<br/><br/>';
}
return renderResult;
};
const process = () => {
const now = new Date(Date.now());
console.log(getTimestampString().black.bgWhite);
let ids = getFundIds();
let ifSendMail = false;
let mailData = [];
let consoleResultData = [];
var table = new Table({
head: ['ID'.white, 'PPY'.white,'RTPPY'.white, 'Name'.white],
colWidths: [8, 10, 10, 26]
});
co(function*(){
for (let id of ids){
const chartData = yield fundService.getChartDataById(id);
let profitPerYear = chartData.profitsRatesPerYear[chartData.profitsRatesPerYear.length-1]
profitPerYear = (profitPerYear<0?profitPerYear.toFixed(2).green:profitPerYear.toFixed(2).red) + ' %';
let consoleResultItem = [id,profitPerYear,chartData.overview.fundName];
table.push(consoleResultItem);
consoleResultData.push(consoleResultItem);
if(!chartData.overview.rtInfoValid){
console.log(id,chartData.overview.fundName,'无法获取实时信息');
consoleResultItem.splice(2, 0, 'NRT');
continue;
}
let day = chartData.overview.rtTimeStamp.split(' ')[0].split('-')[2];
if(parseInt(now.getDate()) !== parseInt(day)){
console.log(id,chartData.overview.fundName,'当前时间为非交易时间');
consoleResultItem.splice(2, 0, 'NT');
continue;
}
let displayRtProfitRatePerYear = chartData.overview.rtProfitRatePerYear<0?chartData.overview.rtProfitRatePerYear.toFixed(2).green:chartData.overview.rtProfitRatePerYear.toFixed(2).red;
console.log(id,chartData.overview.fundName,'当前实时年化收益率: ',displayRtProfitRatePerYear,'%');
consoleResultItem.splice(2, 0, displayRtProfitRatePerYear+' %');
if(chartData.dates[0]){
const [year,month,day] = chartData.dates[0].split('-');
const firstDate = new Date(parseInt(year),parseInt(month)-1,parseInt(day)+1);
const holdingDays = parseInt((now - firstDate)/1000/3600/24);
if(holdingDays < MININUM_HOLDING_DAYS){
console.log('持有时间未达到'+MININUM_HOLDING_DAYS+'天');
continue;
}
}
if(chartData.overview.rtProfitRatePerYear > rtProfitRatePerYearThreshold[id]){
console.log('收益率达到阈值'.red);
ifSendMail = true;
mailData.push({
id: id,
name: chartData.overview.fundName,
rtProfitRatePerYear: chartData.overview.rtProfitRatePerYear
});
}
}
console.log(table.toString());
if(ifSendMail){
const renderedMail = renderEmail(mailData);
console.log(renderedMail);
sendEmail(renderedMail);
}
});
};
process();
let rule = new schedule.RecurrenceRule();
rule.hour = 14;
rule.minute = 45;
schedule.scheduleJob(rule, function(){
process();
});