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

个股日K线详情功能

一. 什么是个股日K线

1.日K线就是将股票交易流水按天分组,然后统计出每天的交易数据,内容包含:日期、股票编码、名称、最高价、最低价、开盘价、收盘价、前收盘价、交易量;

2.需要注意的是这里的收盘价就是指每天最大交易时间点下对应的价格;
在这里插入图片描述

二. 接口说明

功能描述:查询指定股票每天产生的数据,组装成日K线数据;

​ 如果当大盘尚未收盘,则以最新的交易价格作为当天的收盘价格;

服务路径:/api/quot/stock/screen/dkline
服务方法:GET
前端请求频率:每分钟

请求参数:code

参数说明参数名称是否必须数据类型备注
股票编码codetruestring股票编码

响应数据结构:

{
    "code": 1,
    "data": [
        {
            "date": "2021-12-20 10:20",//日期
            "tradeAmt": 28284252,//交易量(指收盘时的交易量,如果当天未收盘,则显示最新数据)
            "code": "000021",//股票编码
            "lowPrice": 16,//最低价格(指收盘时记录的最低价,如果当天未收盘,则显示最新数据)
            "name": "深科技",//名称
            "highPrice": 16.83,//最高价(指收盘时记录的最高价,如果当天未收盘,则显示最新数据)
            "openPrice": 16.8,//开盘价
            "tradeVol": 459088567.58,//交易金额(指收盘时记录交易量,如果当天未收盘,则显示最新数据)
            "closePrice": 16.81//当前收盘价(指收盘时的价格,如果当天未收盘,则显示最新cur_price)
            "preClosePrice": 16.81//前收盘价
        },
        //......
    ]
}

三. SQL分析

SQL查询要划定一个默认的日期范围,这样可避免大数据量下全表查询而导致慢查询的问题;

在指定的日期范围内以天分组统计出每天日期的最大值(收盘时间);

根据获取的最大日期组,使用in进行条件查询,进而获取日K线相关的数据;

1. 查询每天对应的收盘时间

select max(cur_time)
from stock_rt_info
where stock_code = '600021'
  and cur_time between '2022-01-01 09:30:00' and '2022-06-06 14:25:00'
group by date_format(cur_time, '%Y%m%d');

2. 获取在该时间段内所需要的数据

select sri.cur_time        as date,
       sri.trade_amount    as tradeAmt,
       sri.stock_code      as code,
       sri.min_price       as lowPrice,
       sri.stock_name      as name,
       sri.max_price       as highPrice,
       sri.open_price      as openPrice,
       sri.trade_volume    as tradeVol,
       sri.cur_price       as closePrice,
       sri.pre_close_price as preClosePrice
from stock_rt_info as sri
where cur_time in (select max(cur_time)
                   from stock_rt_info
                   where stock_code = '600021'
                     and cur_time between '2022-01-01 09:30:00' and '2022-06-06 14:25:00'
                   group by date_format(cur_time, '%Y%m%d'))

这里同样也可以将查询时间与获取数据分开为两个Mapper接口, 这样更加方便查询

四. 代码实现

1. 表现层
	/**
     * 个股日K图
     */
    @ApiOperation(value = "个股日K图", notes = "个股日K图", httpMethod = "GET")
    @GetMapping("/stock/screen/dkline")
    public R<List<Stock4EvrDayDomain>> getStockScreenDkline(@RequestParam(value = "code" , required = true) String code){
        return service.getStockScreenDkline(code);
    }
2. 服务层

接口

R<List<Stock4EvrDayDomain>> getStockScreenDkline(String code);

实现类

	/**
     * 个股日K图
     */
    @Override
    public R<List<Stock4EvrDayDomain>> getStockScreenDkline(String code) {
        // 获取截止时间
        DateTime curTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
        curTime = DateTime.parse("2022-06-07 15:00:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss"));
        Date nowDate = curTime.toDate();
        // 获取起始时间
        Date startDate = curTime.minusMonths(3).toDate();
        startDate = DateTime.parse("2022-01-01 09:30:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate();

        // 获取最新数据列表
        List<String> latestChangeTime = stockRtInfoMapper.getLatestChangeTime(startDate, nowDate, code);

        // 查询数据
        List<Stock4EvrDayDomain> data = stockRtInfoMapper.getStockScreenDkline(startDate,nowDate,code,latestChangeTime);
        return R.ok(data);

    }
3.Dao层

mapper接口

// 实现数据查询
List<Stock4EvrDayDomain> getStockScreenDkline(@Param("startDate") Date startDate, @Param("nowDate") Date nowDate, @Param("code") String code , @Param("latestChangeTime") List<String> latestChangeTime);

// 实现查询每天对应的收盘时间
List<String> getLatestChangeTime(@Param("startDay") Date startDay , @Param("latestTime") Date latestTime , @Param("code") String code);

xml代码实现

<select id="getStockScreenDkline" resultType="com.jixu.stock.pojo.domain.Stock4EvrDayDomain">
        select
            date_format(sri2.cur_time,'%Y%m%d') as date,
             sri2.trade_amount as tradeAmt,
             sri2.stock_code as code,
             sri2.min_price as lowPrice,
             sri2.stock_name as name,
             sri2.max_price as highPrice,
             sri2.open_price as openPrice,
             sri2.trade_volume as tradeVol,
             sri2.cur_price as closePrice,
             sri2.pre_close_price as preClosePrice
        from stock_rt_info as sri2
        where sri2.cur_time in
            <foreach collection="latestChangeTime" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
          and sri2.stock_code=#{code}
        order by sri2.cur_time
    </select>
    <select id="getLatestChangeTime" resultType="java.lang.String">
        select max(cur_time) as max_time
        from stock_rt_info
        where stock_code = #{code}
          and cur_time between #{startDay} and #{latestTime}
        group by date_format(cur_time, '%Y%m%d')
    </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、付费专栏及课程。

余额充值