涨停跌停数据统计功能
1 涨跌停数据统计业务分析
1.1 涨跌停原型效果
说明:
A股市场有涨幅±10%限制;
股票是否涨停和跌停并不以我们的统计结果为基准,而是由证券交易所来确定,可能真实情况是涨幅超过10%或者低于-10%;
1.2 涨停跌停接口说明
功能描述:统计沪深两市T日(当前股票交易日)每分钟达到涨跌停股票的数据
注意:如果不在股票的交易日内,则统计最近的股票交易日下的数据
服务路径:/api/quot/stock/updown/count
服务方法:GET
前端请求频率:每分钟
请求参数:无
响应数据格式:
{
"code": 1,
"data": {
"upList": [
{
"count": 1,//涨停数量
"time": "202112311412"//当天分时
},
{
"count": 3,//涨停数量
"time": "202112311413"//当天分时
},
//省略......
],
"downList": [
{
"count": 2,//跌停数量
"time": "202112310925"//当天分时
},
//省略......
]
}
}
总之,业务要求获取最新交易日下每分钟达到涨跌停数股票的数量;
关于SQL日期函数,详见:今日指数资料\V3\day03-股票数据报表与导出\资料\预习基础知识点\SQL日期函数.md
2 T日涨跌停统计SQL分析
2.1 SQL分析思路
# 1.以统计当前股票交易日下,每分钟对应的涨停数量为例
# 思考:涨停与涨幅有关,但是我们的股票流水表中没有涨幅的数据,需要自己去就是那
# 1.先统计指定日期下(开盘时间点到最新时间点)涨幅达到涨停的数据
# 查询后的条件过滤,使用关键字:having
select
(sri.cur_price-sri.pre_close_price)/sri.pre_close_price as ud,
sri.cur_time as time
from stock_rt_info sri
where sri.cur_time BETWEEN '2022-01-06 09:30:00' and '2022-01-06 14:25:00'
having ud>=0.1;
# 2.将上述结果作为一张表,然后根据time时间分组,统计出每分钟对应的数量,而这个数量就是涨停的数量
select
tmp.time,
count(*) as count
from () as tmp
group by tmp.time;
# 填充sql
select
tmp.time,
count(*) as count
from (select
(sri.cur_price-sri.pre_close_price)/sri.pre_close_price as ud,
sri.cur_time as time
from stock_rt_info sri
where sri.cur_time BETWEEN '2022-01-06 09:30:00' and '2022-01-06 14:25:00'
having ud>=0.1) as tmp
group by tmp.time
order by tmp.time asc;
# 跌停
select
date_format(tmp.time,'%Y%m%d%H%i') as time ,
count(*) as count
from (select
(sri.cur_price-sri.pre_close_price)/sri.pre_close_price as ud,
sri.cur_time as time
from stock_rt_info sri
where sri.cur_time BETWEEN '2022-01-06 09:30:00' and '2022-01-06 14:25:00'
having ud<=-0.1) as tmp
group by tmp.time
order by tmp.time asc;
2.2 查询数据组装思路
- 涨跌停数据包含了涨停统计数据和跌停统计数据,而每个数据组中的元素又仅仅包含时间和数量,这些数据是高度聚合得出的结果,所以我们可以把每组数据封装到map下,数据类型为:Map<String,List
- 涨停统计SQL和跌停统计的SQL除了条件外,结构是一致的,我们可定义一个flag标识,mapper中传入时,0代表涨停,1代表跌停;
3 T日涨跌停统计功能实现
3.1 定义访问接口
/**
* 统计最新交易日下股票每分钟涨跌停的数量
* @return
*/
@GetMapping("/stock/updown/count")
public R<Map> getStockUpdownCount(){
return stockService.getStockUpdownCount();
}
3.2 定义服务接口方法与实现
服务接口定义:
/**
* 统计最新交易日下股票每分钟涨跌停的数量
* @return
*/
R<Map> getStockUpdownCount();
方法实现:
/**
* 统计最新交易日下股票每分钟涨跌停的数量
* @return
*/
@Override
public R<Map> getStockUpdownCount() {
//1.获取最新的交易时间范围 openTime curTime
//1.1 获取最新股票交易时间点
DateTime curDateTime = DateTimeUtil.getLastDate4Stock(DateTime.now());
Date curTime = curDateTime.toDate();
//TODO
curTime= DateTime.parse("2022-01-06 14:25:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate();
//1.2 获取最新交易时间对应的开盘时间
DateTime openDate = DateTimeUtil.getOpenDate(curDateTime);
Date openTime = openDate.toDate();
//TODO
openTime= DateTime.parse("2022-01-06 09:30:00", DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss")).toDate();
//2.查询涨停数据
//约定mapper中flag入参: 1-》涨停数据 0:跌停
List<Map> upCounts=stockRtInfoMapper.getStockUpdownCount(openTime,curTime,1);
//3.查询跌停数据
List<Map> dwCounts=stockRtInfoMapper.getStockUpdownCount(openTime,curTime,0);
//4.组装数据
HashMap<String, List> mapInfo = new HashMap<>();
mapInfo.put("upList",upCounts);
mapInfo.put("downList",dwCounts);
//5.返回结果
return R.ok(mapInfo);
}
3.3 mapper接口方法定义与xml
Mapper接口定义:
/**
* 查询指定时间范围内每分钟涨停或者跌停的数量
* @param openTime 开始时间
* @param curTime 结束时间 一般开始时间和结束时间在同一天
* @param flag 约定:1->涨停 0:->跌停
* @return
*/
List<Map> getStockUpdownCount(@Param("openTime") Date openTime, @Param("curTime") Date curTime, @Param("flag") int flag);
xml定义:
<select id="getStockUpdownCount" resultType="map">
select
date_format(tmp.time,'%Y%m%d%H%i') as time ,
count(*) as count
from (select
(sri.cur_price-sri.pre_close_price)/sri.pre_close_price as ud,
sri.cur_time as time
from stock_rt_info sri
where sri.cur_time BETWEEN #{openTime} and #{curTime}
having ud
<if test="flag==1">
>=0.1
</if>
<if test="flag==0">
<=-0.1
</if>
)
as tmp
group by tmp.time
order by tmp.time asc
</select>
3.4 web接口测试
- postman测试地址:http://localhost:8091/api/quot//stock/updown/count