js预测股票
const getText=require('./utils/getText');
const {Matrix}=require('./utils/math');
//获取股票id的矩阵数据
async function getMat(id) {
const url='http://quotes.money.163.com/trade/lsjysj_'+id+'.html?year=2019&season=3';
const html=await getText(url);
const arr=[];
let Column=0;
let Row=0;
html.replace(/<table.+?"table_bg001[\d\D]+?<thead>([\d\D]+?)<\/thead>([\d\D]+?)<\/table>/gm,function (m,p1,p2) {
p1.replace(/<th[^>]*>(.+?)<\/th>/g,function (m2,item) {
arr.push(item);
})
Column=arr.length;
p2.replace(/<tr[^>]*>(.+?)<\/tr>/g,function (m1,data) {
data.replace(/<td[^>]*>(.+?)<\/td>/g,function (m2,item) {
arr.push(item);
})
})
Row=arr.length/Column;
})
const mat=new Matrix(arr,Row,Column);
const nmat=new Matrix([],Row,Column+2)
nmat.setItem(0,mat.Column,'昨日收盘价')
nmat.setItem(0,mat.Column+1,'tag')
mat.rowEach(function (item,r,c) {
nmat.setItem(r,c,item);
if(r>0&&r+1<mat.Row&&c===4){
const str=mat.getItem(r+1,4)
nmat.setItem(r,mat.Column,str)
const num=parseFloat(item)-parseFloat(str);
nmat.setItem(r,mat.Column+1,num>0?1:-1)
}
})
return nmat;
}
const execMathExpress=require('exec-mathexpress');
async function getData(id){
const mat=await getMat(id);
//分类
let tagLen1=0,tagLen2=0;
let num1=0,num2=0;
for(let r=1;r<mat.Row-1;r++){
const n12=mat.getItem(r,12);
const n1=parseFloat(mat.getItem(r,1).replace(',',''));
const n11=parseFloat(mat.getItem(r,11).replace(',',''));
if(n1>n11){
tagLen1++;
if(n12===1){
num1++;
}
}else{
tagLen2++;
if(n12===1){
num2++;
}
}
}
//贝叶斯公式 P=P1P2/(P1P2+PE1PE2)
const P1=num1+'/'+tagLen1;
const P2=num2+'/'+tagLen2;
// const str1=`${P1}/(${P1}+${P2})`;
// const str2=`(1-${P1})/((1-${P1})+(1-${P2}))`;
// const re1=execMathExpress(str1).toString()
const re2=execMathExpress('(('+P1+')+'+'('+P2+'))/2').toString()
return [re2,P1,P2]
}
async function init(){
const idArr=[
// '603848','好太太',
// '002915','中欣氟材',
// '000666','经纬纺机',
// '000069','华侨城',
// '000333','美的',
// '600559','老白干',
// '000858','五粮液',
// '600600','青岛啤酒',
// '000651','格力电器',
// '600779','水井坊',
// '600520','文一科技',
// '600702','舍得酒业',
// '600903','贵州燃气',
]
const arr=[];
for(let i=0;i<idArr.length;i=i+2){
const data= await getData(idArr[i])
data.push(idArr[i+1])
const fz=execMathExpress(data[0])
data.push((fz.num*100/fz.den).toFixed(0)+'%')
arr.push(data)
}
arr.sort(function (p1,p2) {
if(execMathExpress(p1[0]).lessThan(execMathExpress(p2[0]))){
return 1
}else {
return -1
}
})
console.log(arr)
}
init()
getText.js
const axios = require('axios'); const fs = require('fs'); async function getText(url) { if(/^http/.test(url)){ const res=await axios.get(url) return res.data; }else{ return fs.readFileSync(url).toString(); } } module.exports=getText;
setText.js
const mkdir=require('./mkdir') const fs = require('fs'); async function setText(filepath,buffer) { mkdir(filepath); fs.writeFileSync(filepath,buffer) } module.exports=setText;