今日指数项目个股周K线功能实现

个股周K线功能实现

1 个股周K线功能实现功能分析

1)个股周K线功能原型分析

在这里插入图片描述

2)个股周K线功能接口分析

个股周K线数据主要包含:
股票ID、 一周内最高价、 一周内最低价 、周1开盘价、周5的收盘价、
整周均价、以及一周内最大交易日期(一般是周五所对应日期)

接口要求:

功能描述:统计每周内的股票数据信息,信息包含:
股票ID、 一周内最高价、 一周内最低价 、周1开盘价、周5的收盘价、
整周均价、以及一周内最大交易日期(一般是周五所对应日期);
服务路径:/api/quot/stock/screen/weekkline
服务方法:GET
请求参数:code //股票编码

响应数据格式:

{
    "code": 1,
    "data": [
        {
            "avgPrice": 8.574954,//一周内平均价
            "minPrice": 8.56,//一周内最低价
            "openPrice": 8.6,//周一开盘价
            "maxPrice": 8.6,//一周内最高价
            "closePrice": 8.57,//周五收盘价(如果当前日期不到周五,则显示最新价格)
            "mxTime": "2021-12-19 15:00",//一周内最大时间
            "stockCode": "600000"//股票编码
        }
    ]
}

代码实现

1. 表现层
    /**
     * 个股周K线功能
     */
    @ApiOperation(value = "个股周K线功能", notes = "个股周K线功能", httpMethod = "GET")
    @GetMapping("/stock/screen/weekkline")
    public R<List<WeekklineDomain>> getWeekkline(@RequestParam String code){
        return service.getWeekkline(code);
    }

2. 服务层
R<List<WeekklineDomain>> getWeekkline(String code);
    @Override
    public R<List<WeekklineDomain>> getWeekkline(String code) {
        // 获取当前时间点
        DateTime curDate = DateTimeUtil.getLastDate4Stock(DateTime.now());
        // 制造mock数据
        curDate = DateTime.parse("2022-07-07 14:55:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));

        Date endDate = curDate.toDate();
        Date startDate = curDate.minusMonths(3).toDate();

        List<WeekklineDomain> date = stockRtInfoMapper.getWeekkline(startDate,endDate,code);

        return R.ok(date);
    }
3. Dao层
List<WeekklineDomain> getWeekkline(@Param("startDate") Date startDate, @Param("endDate") Date endDate, @Param("code") String code);
<select id="getWeekkline" resultType="com.jixu.stock.pojo.domain.WeekklineDomain">
        CREATE TEMPORARY TABLE sir1
        select
            min(min_price) as minPrice ,
            max(max_price) as maxPrice,
            max(cur_time) as mxTime,
            stock_code as stockCode,
            sum(cur_price)/count(cur_price) as avgPrice,
            YEARWEEK(cur_time) as week
        from stock_rt_info
        where stock_code = #{code}
        group by yearweek(cur_time);



        CREATE TEMPORARY TABLE sir2
        select cur_price ,
               stock_code,
               YEARWEEK(cur_time) as week
        from stock_rt_info
        where  stock_code = #{code} and cur_time in (
            select
                max(cur_time) as mxTime
            from stock_rt_info
            where stock_code = #{code}
            group by yearweek(cur_time)
        ) ;


        CREATE TEMPORARY TABLE sir3
        select open_price ,
               stock_code,
               YEARWEEK(cur_time) as week,
               cur_time
        from stock_rt_info
        where  stock_code = #{code} and cur_time in (
            select
                min(cur_time) as minTime
            from stock_rt_info
            where stock_code = #{code}
            group by yearweek(cur_time)
        )
        ;


        select sir1.stockCode as stockCode,
               sir1.avgPrice as avgPrice ,
               sir1.minPrice as minPrice,
               sir1.mxTime as mxTime,
               sir1.maxPrice as maxPrice,
               sir2.cur_price as closePrice,
               sir3.open_price as openPrice
        from sir1 join sir2 on sir1.stockCode = sir2.stock_code and sir1.week = sir2.week
                  join sir3 on sir3.stock_code=sir2.stock_code and sir2.week=sir3.week
        where sir3.cur_time between #{startDate} and #{endDate}
        ;

    </select>
要爬取东方财富个股k线的振幅,我们需要先知道东方财富的数据接口地址,并找到代表振幅数据的字段。 首先,我们可以在东方财富网站上打开我们需要爬取的个股K线图页面,然后按F12打开开发者工具,在"Network"选项卡下刷新页面,观察XHR(ajax)请求,找到代表K线图数据的请求,可以看到请求链接的格式大致如下: https://pdfm.eastmoney.com/EM_UBG_PDTI_Fast/api/js?id=代码&rtntype=5&start=20191110&end=20201202 其中,id代表股票代码,start和end是起始和截止日期,可以手动修改。 接下来,我们需要对请求链接进行解码,获取返回的数据。我们可以使用Python中的requests和json库进行解码。代码如下: import requests import json url = "https://pdfm.eastmoney.com/EM_UBG_PDTI_Fast/api/js?id=代码&rtntype=5&start=20191110&end=20201202" resp = requests.get(url) data = json.loads(resp.text) 接着,我们需要对返回的数据进行处理,筛选出振幅数据。可以通过循环遍历每一个K线数据,然后计算出振幅: for kline in data: open_price = float(kline[1]) close_price = float(kline[2]) high_price = float(kline[3]) low_price = float(kline[4]) amplitude = round((high_price-low_price)/open_price*100, 2) print(amplitude) 在以上代码中,我们通过对每一个K线数据的开盘价、收盘价、最高价、最低价进行提取,计算出振幅(amplitude)。round函数用来保留两位小数。 综上,通过以上代码,我们可以成功爬取到东方财富个股K线图的振幅数据。需要注意的是,为了确保爬取数据的准确性和可靠性,需要在HTTP请求中加入一些headers信息,以模拟浏览器访问。同时,在请求过程中需要加入适当的错误处理机制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

攒了一袋星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值