【金融数据分析】计算2023年沪深300行业涨跌幅

本文我们来计算2023年沪深300行业涨跌幅,最后呈现的页面是这样的

先来说后端的代码,计算行业涨跌幅的原理就是将某个行业所有成分股的涨跌幅加起来,然后除以股票数量得到一个平均涨跌幅

    // 查询行业涨跌幅
    public List<CSI300IndustryRankVO> queryIndustryRank() {
        // 首先将所有数据查出来
        List<CSI300RankVO> csi300RankVOList = query2023rank(3, 300);
        // 将沪深300所有成分股都查出来
        List<CSI300Entity> csi300Entities = sqlIteCSI300Dao.queryAllItems();
        // 记录涨跌幅
        Map<String, Integer> numMap = new HashMap<>();
        Map<String, Double> riseMap = new HashMap<>();
        for (int i=0; i<csi300RankVOList.size(); i++) {
            for (int j = 0; j < csi300Entities.size(); j++) {
                if (csi300RankVOList.get(i).getName().equals(csi300Entities.get(j).getName())) {
                    if (!riseMap.containsKey(csi300Entities.get(j).getIndustry())) {
                        riseMap.put(csi300Entities.get(j).getIndustry(), csi300RankVOList.get(i).getRise());
                        numMap.put(csi300Entities.get(j).getIndustry(), 1);
                    } else {
                        Double rise = riseMap.get(csi300Entities.get(j).getIndustry()) + csi300RankVOList.get(i).getRise();
                        riseMap.put(csi300Entities.get(j).getIndustry(), rise);
                        Integer num = numMap.get(csi300Entities.get(j).getIndustry());
                        numMap.put(csi300Entities.get(j).getIndustry(), num + 1);
                    }
                    log.info("name:" + csi300RankVOList.get(i).getName() + " industry:" +
                            csi300Entities.get(j).getIndustry() + " rise:" +
                            csi300RankVOList.get(i).getRise());
                    break;
                }
            }
        }
        List<String> keyList = new ArrayList<>(riseMap.keySet());
        List<CSI300IndustryRankVO> csi300IndustryRankVOList = new ArrayList<>();
        for (int i=0; i<keyList.size(); i++) {
            CSI300IndustryRankVO entity = new CSI300IndustryRankVO();
            entity.setIndustry(keyList.get(i));
            Double rise = riseMap.get(keyList.get(i));
            rise = rise / numMap.get(keyList.get(i));
            String str = String.format("%.2f", rise);
            rise = Double.parseDouble(str);
            log.info("行业:" + keyList.get(i) + " 数量: " + numMap.get(keyList.get(i)) + " 涨幅:" + rise);
            entity.setRise(rise);
            csi300IndustryRankVOList.add(entity);
        }
        // 按照涨幅降序排序
        Collections.sort(csi300IndustryRankVOList, new Comparator<CSI300IndustryRankVO>() {
            @Override
            public int compare(CSI300IndustryRankVO o1, CSI300IndustryRankVO o2) {
                if (o1.getRise() > o2.getRise()) {
                    return -1;
                } else if (o1.getRise() == o2.getRise()) {
                    return 0;
                } else {
                    return 1;
                }
            }
        });
        return csi300IndustryRankVOList;
    }

    // 将查询的数据缓存到内存中
    private List<CSI300RankVO> cache = new ArrayList<>();

    // 查询沪深300成分股2023年涨跌排行榜
    public synchronized List<CSI300RankVO> query2023rank(int type, int limit) {
        // 首先将所有的股票查出来
        List<StockOptionVO> allCode = getAllCode();
        List<CSI300RankVO> csi300RankVOList = new ArrayList<>();
        if (cache.size() == 0) {
            log.info("需要重新计算数据");
            // 根据股票代码查询所有股票的涨跌幅
            for (int i = 0; i < allCode.size(); i++) {
                // 去除掉沪深300指数本身
                if (allCode.get(i).getCode().equals("399300")) {
                    continue;
                }
                List<StockEntity> stockEntities = sqLiteStockDao.queryAllByCodeAndYear(allCode.get(i).getCode(), "2023");
                CSI300RankVO csi300RankVO = new CSI300RankVO();
                csi300RankVO.setCode(allCode.get(i).getCode());
                csi300RankVO.setName(allCode.get(i).getName());
                Double rise = (stockEntities.get(0).getClose_price() - stockEntities.get(stockEntities.size() - 1).getClose_price()) / stockEntities.get(stockEntities.size() - 1).getClose_price();
                rise = rise * 100;
                String str = String.format("%.2f", rise);
                rise = Double.parseDouble(str);
                csi300RankVO.setRise(rise);
                csi300RankVOList.add(csi300RankVO);
            }
            log.info("填充数据");
            // 将数据填充到缓存中
            for (int i=0; i<csi300RankVOList.size(); i++) {
                cache.add(csi300RankVOList.get(i));
            }
        } else {
            log.info("缓存中已存在数据,不需要重新计算");
            for (int i=0; i<cache.size(); i++) {
                csi300RankVOList.add(cache.get(i));
            }
        }
        // type==1查询涨幅
        if (type == 1) {
            // 按照涨幅升序排序
            Collections.sort(csi300RankVOList, new Comparator<CSI300RankVO>() {
                @Override
                public int compare(CSI300RankVO o1, CSI300RankVO o2) {
                    if (o1.getRise() > o2.getRise()) {
                        return -1;
                    } else if (o1.getRise() == o2.getRise()) {
                        return 0;
                    } else {
                        return 1;
                    }
                }
            });
        } else if (type == 2) { // type==2查询跌幅
            Collections.sort(csi300RankVOList, new Comparator<CSI300RankVO>() {
                @Override
                public int compare(CSI300RankVO o1, CSI300RankVO o2) {
                    if (o1.getRise() > o2.getRise()) {
                        return 1;
                    } else if (o1.getRise() == o2.getRise()) {
                        return 0;
                    } else {
                        return -1;
                    }
                }
            });
        }
        // 最后取limit个数据返回
        List<CSI300RankVO> result = new ArrayList<>();
        for (int i=0; i<limit; i++) {
            if (i > csi300RankVOList.size()-1) {
                return result;
            }
            if (type == 1 && csi300RankVOList.get(i).getRise()<0) { // 查询涨幅
                return result;
            }
            if (type == 2 && csi300RankVOList.get(i).getRise()>0) { // 查询跌幅
                return result;
            }
            result.add(csi300RankVOList.get(i));
        }
        return result;
    }

前端页面代码如下

<template>
  <div>
    <el-row class="container">
      <div class="left-grid">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>行业涨跌幅排行榜</span>
            </div>
          </template>
          <el-table
            v-loading="loading1"
            :data="rise_data"
            :show-header="true"
            :max-height="250"
            stripe
          >
            <el-table-column
              type="index"
              label="排名"
              width="65%"
            ></el-table-column>
            <el-table-column prop="industry" label="行业"></el-table-column>
            <el-table-column
              prop="rise"
              label="涨幅"
              :formatter="formatter1"
            ></el-table-column>
          </el-table>
        </el-card>

        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>行业权重</span>
            </div>
          </template>
          <el-table
            v-loading="loading2"
            :data="industry_dist"
            :show-header="true"
            :max-height="250"
            stripe
          >
            <el-table-column
              type="index"
              label="序号"
              width="65%"
            ></el-table-column>
            <el-table-column prop="industry" label="行业"></el-table-column>
            <el-table-column
              prop="weight"
              label="权重"
              :formatter="formatter2"
            ></el-table-column>
          </el-table>
        </el-card>
      </div>

      <div class="right-grid" ref="myChart"></div>
    </el-row>
  </div>
</template>

<script>
import axios from "axios";
import { getCurrentInstance } from "vue";
export default {
  data() {
    return {
      // 涨幅排行榜
      rise_data: [],
      loading1: true,
      // 权重数据
      industry_dist: [],
      loading2: true,
      table_title: "行业涨跌幅排行榜",
      echarts: getCurrentInstance().appContext.config.globalProperties.$echarts,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      var url1 = "http://localhost:9001/stock/queryIndustryRank";
      this.loading1 = true;
      axios
        .get(url1)
        .then((response) => {
          this.rise_data = response.data;
          console.log(response);
          this.loading1 = false;
          this.create_bar();
        })
        .catch((error) => {
          console.log(error);
          this.loading1 = false;
        });
      var url2 = "http://localhost:9001/queryDist";
      this.loading2 = true;
      axios
        .get(url2)
        .then((response) => {
          this.industry_dist = response.data;
          console.log(response);
          this.loading2 = false;
        })
        .catch((error) => {
          console.log(error);
          this.loading2 = false;
        });
    },
    // 绘制柱状图
    create_bar() {
      //3.初始化实例对象 echarts.init(dom容器)
      var data_xAxis = [];
      var data_yAxis = [];
      for (var i = this.rise_data.length - 1; i >= 0; i--) {
        data_xAxis.push(this.rise_data[i].industry);
        data_yAxis.push(this.rise_data[i].rise);
      }
      console.log(data_xAxis);
      console.log(data_yAxis);
      var dom = this.$refs["myChart"]; // 获取dom节点
      var myChart = this.echarts.init(dom);
      //4.指定配置项和数据
      var option = {
        tooltip: {
          trigger: "axis",
          position: function (pt) {
            return [pt[0], "10%"];
          },
        },
        title: {
          left: "center",
          text: this.table_title,
        },
        toolbox: {
          feature: {
            dataZoom: {
              yAxisIndex: "none",
            },
            restore: {},
            saveAsImage: {},
          },
        },
        grid: {
          top: 80,
          bottom: 30,
        },
        xAxis: {
          type: "value",
          position: "top",
          splitLine: {
            lineStyle: {
              type: "dashed",
            },
          },
        },
        yAxis: {
          type: "category",
          axisLine: { show: false },
          axisLabel: { show: false },
          axisTick: { show: false },
          splitLine: { show: false },
          data: data_xAxis,
        },
        series: [
          {
            name: "行业涨跌幅",
            type: "bar",
            stack: "Total",
            label: {
              show: true,
              formatter: "[{b}]=>({c})%",
            },
            data: data_yAxis,
          },
        ],
      };
      //5.将配置项设置给echarts实例对象,使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },

    formatter1(row) {
      return row.rise + "%";
    },
    formatter2(row) {
      return row.weight + "%";
    },
  },
};
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.container {
  display: grid;
  grid-template-columns: 35% 65%;
  width: 100%;
  height: 80vh;
}
.left-grid {
  background-color: #f0f0f0;
  border-radius: 2%;
  padding: 10px;
  height: 95%;
}
.right-grid {
  background-color: #f9ecc3;
  border-radius: 2%;
  padding: 10px;
  height: 650px;
}
</style>

最后计算得到的行业涨跌幅排行榜如下

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/317823.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何将.NET 8.0的ASP.NET Core Web API部署成Windows服务

写在前面 前面写了一篇关于将.NET应用转换成Windows服务的方法&#xff0c;其实真正的目的是为了探索如何将Asp.Net Core Web Api 部署成Windows 服务。基于上一篇的基础&#xff0c;只需把创建 WebApplication 的代码放到 BackgroundService 的ExecuteAsync方法中即可。 其中…

【一】通信协议概述

通信协议概述 简介&#xff1a; 很早之前就思考了要写一下电力系统常用的几种通信协议&#xff0c;一直拖着也没有行动&#xff0c;这次终于下定决心来出一个《通信协议》这样的专栏。电力行业数字化方面资料较少&#xff0c;我理解主要一方面是数字化程度还不高&#xff0c;一…

笔记系统的部署架构

前天给笔记系统打了0.0.3的tag&#xff0c;一个简单的全栈功能闭环基本完成。既然是开源&#xff0c;因此&#xff0c;这里有必要分享一下部署结构&#xff0c;希望能够获得小伙伴们的反馈。 目前整个系统采用docker容器来部署。应用介绍 auth_app: 登录/注册的前端应用 web_ap…

WebRTC入门:基础的核心协议与概念(二十三)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长! 优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏:多媒体系统工程师系列【原创干货持续更新中……】🚀 人生格言: 人生从来没有捷径,只…

Open3D对产生偏差的点云数据进行校正

由于相机安装问题&#xff0c;导致点云数据两边翘起来&#xff0c;为了计算把翘起来的部分拉平整 import time import open3d as o3d; import numpy as np; import matplotlib.pyplot as plt from scipy.signal import find_peaks import pandas as pdOriginalPly o3d.io.rea…

自养号测评:掌握Shopee运营黑科技的必备攻略

虾皮卖家们经常挂在嘴边的“权重”&#xff0c;简而言之&#xff0c;就是商品或店铺在Shopee平台上受到重视的程度。它的存在是为了评估商品或店铺是否满足用户需求&#xff0c;能否助力订单转化&#xff0c;为平台创造更多收益。就像我们给孩子们打分一样&#xff0c;分数越高…

Vue3函数式弹窗实现

要在一些敏感操作进行前要求输入账号和密码&#xff0c;然后将输入的账号和密码加到接口请求的header里面。如果每个页面都去手动导入弹窗组件&#xff0c;在点击按钮后弹出弹窗。再拿到弹窗返回的账号密码后去请求接口也太累了&#xff0c;那么有没有更简单的实现方式呢&#…

python_locust压测

可以在命令行启动&#xff0c;cmd命令框&#xff0c;进入此文件的目录输入&#xff1a; locust -f .\dash.py --host"https://litemall.hogwarts.ceshiren.com" -f&#xff1a; 指定性能测试脚本文件的绝对路径。 –host&#xff1a; 指定被测试应用的URL的地址&…

Qt/QML编程学习之心得:Grid、GridLayout、GridView、Repeater(33)

GRID网格用处非常大,不仅在excel中,在GUI中,也是非常重要的一种控件。 Grid 网格是一种以网格形式定位其子项的类型。网格创建一个足够大的单元格网格,以容纳其所有子项,并将这些项从左到右、从上到下放置在单元格中。每个项目都位于其单元格的左上角,位置为(0,0)。…

【JavaScript】深度理解js的函数(function、Function)

简言 学了这么久的JavaScript&#xff0c;函数在JavaScript中最常用之一&#xff0c;如果你不会函数&#xff0c;你就不会JavaScript。 函数就是Function对象&#xff0c;一个函数是可以通过外部代码调用的一个“子程序”&#xff0c;它是头等&#xff08;first-class&#xf…

微机原理常考简答题(二)

一&#xff0c;简述8086CPU响应可屏蔽中断的条件及过程。 CPU响应可屏蔽中断的条件是有中断请求&#xff0c;中断标志IF1开中断&#xff0c;现行指令执行结束。 CPU响应可屏蔽中断的过程&#xff1a;CPU在INTR引脚上接到一个中断请求信号&#xff0c;如果此时IF1&#xff0c;并…

group by 查询慢的话,如何优化?

1、说明 根据一定的规则&#xff0c;进行分组。 group by可能会慢在哪里&#xff1f;因为它既用到临时表&#xff0c;又默认用到排序。有时候还可能用到磁盘临时表。 如果执行过程中&#xff0c;会发现内存临时表大小到达了上限&#xff08;控制这个上限的参数就是tmp_table…

基于完整熵编码系数组的JPEG图像加密方案

论文题目&#xff1a;JPEG image encryption with grouping coefficients based on entropy coding 期刊&#xff1a;Journal of Visual Communication and Image Representation 分区&#xff1a;中科苑三区&#xff0c;老牌图像处理期刊 文章目录 摘要概要整体架构流程实验结…

mac怎么拼图?Mac拼图技巧分享

mac怎么拼图&#xff1f;在Mac上拼图是一种令人愉悦的创意表达方式&#xff0c;可以让你将多张图片巧妙地融合在一起&#xff0c;创造出令人惊叹的艺术品。本文将向你介绍在Mac上进行拼图的几种方法&#xff0c;帮助你轻松实现这一目标。 一、使用Mac内置的预览功能进行拼图 M…

100个GEO基因表达芯片或转录组数据处理之GSE159676(002)

写在前边 虽然现在是高通量测序的时代&#xff0c;但是GEO、ArrayExpress等数据库储存并公开大量的基因表达芯片数据&#xff0c;还是会有大量的需求去处理芯片数据&#xff0c;并且建模或验证自己所研究基因的表达情况&#xff0c;芯片数据的处理也可能是大部分刚学生信的道友…

物联网协议Coap之Core和NetWork简介

目录 前言 一、Coap的Core包 1、Coap对象 2、Message对象 3、Request对象 4、Response对象 二、Coap的NetWork调试 1、UDP运行模式 2、Network消息接收 3、Sender线程发送数据 三、总结 前言 在之前的博文中&#xff0c;对Californium中Coap的实现进行了简要的介绍&a…

IT从业人员如何养生?

目前&#xff0c;电脑对人体生理和心理方面的负面影响已日益受到人们的重视。为此科学使用电脑&#xff0c;减少电脑和网络的危害是十分必要的。好代码网总结了一些it从业人员的保健知识&#xff0c;分享给大家。 一是要增强自我保健意识 工作间隙注意适当休息&#xff0c;一般…

计算机体系结构----缓存一致性/多处理机

本文严禁转载&#xff0c;仅供学习使用。参考资料来自中国科学院大学计算机体系结构课程PPT以及《Digital Design and Computer Architecture》、《超标量处理器设计》、同济大学张晨曦教授资料。如有侵权&#xff0c;联系本人修改。 本文衔接上文计算机体系结构----存储系统 …

秒懂百科,C++如此简单丨第十二天:ASCLL码

目录 必看信息 Everyday English &#x1f4dd;ASCLL码是什么&#xff1f; &#x1f4dd;ASCLL码表 &#x1f4dd;利用ASCLL码实现大写转小写 &#x1f4dd;小试牛刀 总结 必看信息 ▶本篇文章由爱编程的小芒果原创&#xff0c;未经许可&#xff0c;严禁转载。 ▶本篇文…

ActiveMQ反序列化RCE漏洞复现(CVE-2023-46604)

漏洞名称 Apache ActiveMQ OpenWire 协议反序列化命令执行漏洞 漏洞描述 Apache ActiveMQ 是美国阿帕奇&#xff08;Apache&#xff09;软件基金会所研发的一套开源的消息中间件&#xff0c;它支持Java消息服务、集群、Spring Framework等。 OpenWire协议在ActiveMQ中被用于…
最新文章