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 init(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(n12===1){
            tagLen1++;
            if(n1>n11){
                num1++;
            }
        }else if(n12===-1){
            tagLen2++;
            if(n1>n11){
                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(str2).toString()
    console.log(re1,re2)

}
init('002915')

猜你喜欢

转载自www.cnblogs.com/caoke/p/11305069.html