按条件搜索期货品种

'玻璃FG503:连续14天、30天内有22天,在10均线↓方',
'玻璃FG505:连续14天、30天内有22天,在10均线↓方',
'玻璃FG506:连续14天、30天内有21天,在10均线↓方',
'玻璃FG411:连续7天、30天内有25天,在10均线↓方',
'玻璃FG410:连续5天、30天内有25天,在10均线↓方',

 

import axios from 'axios';
import fs from 'fs-extra';
const instance = axios.create();
const api={
    sleep(time){
        return new Promise(function (resolve){
            setTimeout(resolve, time);
        })
    },
    //获取纯碱实时价格
    async getSaRealPrice(){
        const res=await instance.get('https://futsseapi.eastmoney.com/list/variety/115/22?callback=aaa_callback&orderBy=zdf&sort=desc&pageSize=20&pageIndex=0&callbackName=aaa_callback&token=58b2fa8f54638b60b87d69b31969089c&field=dm%2Csc%2Cname%2Cp%2Czsjd%2Czde%2Czdf%2Cf152%2Co%2Ch%2Cl%2Czjsj%2Cvol%2Ccje%2Cwp%2Cnp%2Cccl&blockName=callback&_='+(new Date().getTime()))
        const func=new Function('aaa_callback','return '+res.data)
        const data=func(function(item){return item})
        return data;
    },
    //获取纯碱每日价格
    async getSaDayPrice(code){
        const url=`https://finance.pae.baidu.com/selfselect/getstockquotation?all=1&code=${code}&isIndex=false&isBk=false&isBlock=false&isFutures=true&isStock=false&newFormat=1&ktype=1&market_type=ab&group=quotation_futures_kline&finClientType=pc`
        const res=await instance.get(url)
        return res.data.Result.newMarketData;
    },
    //按条件搜索期货品种
    async searchSrc(){
        const codeMap={
            '纯碱':['SA409','SA410','SA411','SA412','SA501','SA502','SA503','SA504','SA505','SA506','SA507'],
            '甲醇':['MA409','MA410','MA411','MA412','MA501','MA502','MA503','MA504','MA505','MA506','MA507'],
            '锰硅':['SM409','SM410','SM411','SM412','SM501','SM502','SM503','SM504','SM505','SM506','SM507'],
            '对二甲苯':['PX409','PX410','PX411','PX412','PX501','PX502','PX503','PX504','PX505','PX506','PX507'],
            '焦煤':['jm2409','jm2410','jm2411','jm2412','jm2501','jm2502','jm2503','jm2504','jm2505','jm2506','jm2507'],
            '焦炭':['j2409','j2410','j2411','j2412','j2501','j2502','j2503','j2504','j2505','j2506','j2507'],
            '铁矿石':['i2409','i2410','i2411','i2412','i2501','i2502','i2503','i2504','i2505','i2506','i2507'],
            'PTA':['TA409','TA410','TA411','TA412','TA501','TA502','TA503','TA504','TA505','TA506','TA507'],
            'PVC':['v2409','v2410','v2411','v2412','v2501','v2502','v2503','v2504','v2505','v2506','v2507'],
            '菜油':['OI409','OI411','OI501','OI503','OI505','OI507'],
            '豆油':['y2409','y2411','y2501','y2503','y2505','y2507'],
            '玻璃':['FG409','FG410','FG411','FG412','FG501','FG502','FG503','FG504','FG505','FG506','FG507'],
            '烧碱':['SH409','SH410','SH411','SH412','SH501','SH502','SH503','SH504','SH505','SH506','SH507'],
            '沪银':['ag2409','ag2410','ag2411','ag2412','ag2501','ag2502','ag2503','ag2504','ag2505','ag2506','ag2507'],
            '乙二醇':['eg2409','eg2410','eg2411','eg2412','eg2501','eg2502','eg2503','eg2504','eg2505','eg2506','eg2507'],
            '螺纹钢':['rb2409','rb2410','rb2411','rb2412','rb2501','rb2502','rb2503','rb2504','rb2505','rb2506','rb2507'],
            '燃油':['fu2409','fu2410','fu2411','fu2412','fu2501','fu2502','fu2503','fu2504','fu2505','fu2506','fu2507'],
            '低硫燃油':['lu2409','lu2410','lu2411','lu2412','lu2501','lu2502','lu2503','lu2504','lu2505','lu2506','lu2507'],
            '尿素':['UR409','UR410','UR411','UR412','UR501','UR502','UR503','UR504','UR505','UR506','UR507'],
            '菜粕':['RM409','RM411','RM501','RM503','RM505','RM507'],
            '豆粕':['m2409','m2411','m2412','m2501','m2503','m2505','m2507'],
        }
        const nameMap=[]
        const codeLists=[]
        for(let name in codeMap){
            codeMap[name].forEach(function (code){
                nameMap[code]=name
                codeLists.push(code)
            })
        }
        const effectiveTime=24*60*60*1000
        const nTime=Date.now()
        const paiMingList=[]
        let codesLen=codeLists.length
        for(let i=0;i<codesLen;i++){
            const code=codeLists[i];
            const filepath='src/'+code+'.json'
            let data
            if(fs.existsSync(filepath)&&nTime-effectiveTime<fs.statSync(filepath).mtime.getTime()){
                data=fs.readJsonSync(filepath)
            }else{
                data=await this.getSaDayPrice(code)
                console.log(code,data)
                fs.outputJsonSync(filepath,data)
                // await this.sleep(1000)
            }
            const dLen=30;//统计最近30天
            const lists=data.marketData.split(';').map(str=>str.split(','))
            const list20=lists.splice(lists.length-dLen,lists.length).reverse()
            let down=0
            let up=0
            let cLen=0
            list20.forEach((item,i)=>{
                const high=Number(item[data.headers.indexOf('最高')])
                const low=Number(item[data.headers.indexOf('最低')])
                const ma10avgprice=Number(item[data.headers.indexOf('ma10均价')])
                if(low>ma10avgprice){
                    if(up===i){
                        cLen=up+1
                    }
                    up++
                }else if(high<ma10avgprice){
                    if(down===i){
                        cLen=down+1
                    }
                    down++
                }
            })
            paiMingList.push({
                name:nameMap[code]+code,
                dLen,
                cLen,
                order:cLen+Math.max(up,down),
                ma10avg:up>down?up:-down
            })
        }
        paiMingList.sort(function (a,b){
            return b.order-a.order
        })
        const pTextList=paiMingList.map(function (obj){
            return `${obj.name}:连续${obj.cLen}天、${obj.dLen}天内有${Math.abs(obj.ma10avg)}天,在10均线${obj.ma10avg>0?'↑':'↓'}方`
        })
        pTextList.forEach((text)=>{console.log(text)})
    },

}


api.searchSrc()
export default api;

 

posted @ 2024-08-10 19:35  无工时代  阅读(17)  评论(0编辑  收藏  举报