超简单C++ 获取股票历史数据自动缓存本地二进制加速访问,省心 更新至2021-07-09

本文主要是介绍超简单C++ 获取股票历史数据自动缓存本地二进制加速访问,省心 更新至2021-07-09,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目前全网没有一个专门提供C++访问股票接口并专门提供给仅仅使用历史数据的人的资源

本资源付费获取源码,有需要的联系我本人(首页微信)。

2021-07-09最新版本上传到百度云可下载
2019-08-03

增加当日数据获取:

市盈率、市净率

2019-07-21自动缓存本地

2019-09-01

2019-09-04

2019-09-05

2019-09-08(去掉了map)

支持全部历史数据一次性获取:

收盘价, 最高价, 最低价, 开盘价, 前收盘, 涨跌额, 涨跌幅, 换手率, 成交量, 成交金额, 总市值, 流通市值

下载全部历史数据缓存本地用时:4374个记录(个股和指数) 24分钟-> 20分钟 560MB磁盘空间

下载单个个股平均用时:0.7秒

加载全部用时:1分钟->24秒

更新全部并加载:3分钟

加载全部占用内存:1.9GB->1.8GB->1.3GB

加载2019年以后的数据:120MB->90MB

加载2019年以后的数据:4.5秒

2019-09-22

支持全部历史数据一次性获取:收盘价, 最高价, 最低价, 开盘价, 前收盘, 成交量

下载单个个股平均用时:0.4秒

 更新数据的核心逻辑:

std::shared_ptr<CStock> CStockMgr::GetUserStock(const string & id)
{auto itr = m_allUserStock.find(id);if (itr == m_allUserStock.end())//不存在则惰性加载{auto pStock = LoadOneStockFromLocalAndWeb(id, m_needUpdate);//加载优先使用本地缓存二进制文件auto file = Path::GetStockHistoryDataDir() + id;if (!Path::Exist(file))//还没缓存过则就地缓存{SaveOneUserStockToLocalDir(pStock);}}return m_allUserStock.at(id);
}

int main()
{//UpdateStockNameList();auto pStock = CStockMgr::Instance().GetUserStock("sz000858");//"五粮液"PrintStock(pStock.get(), "sz000858");pStock = CStockMgr::Instance().GetUserStock("sh000001");//"上证指数"PrintStock(pStock.get(), "sh000001");pStock = CStockMgr::Instance().GetUserStock("sz399001");//"深圳成指"PrintStock(pStock.get(), "sz399001");return 0;
}

使用二进制读写本地数据


void CStockItem::writeTo(ostream & out) const
{auto& item = *this;out.write(reinterpret_cast<const char*>(&item.m_date), sizeof(item.m_date));//日期out.write(reinterpret_cast<const char*>(&item.m_close), sizeof(item.m_close));//收盘价out.write(reinterpret_cast<const char*>(&item.m_high), sizeof(item.m_high));//最高价out.write(reinterpret_cast<const char*>(&item.m_low), sizeof(item.m_low));//最低价out.write(reinterpret_cast<const char*>(&item.m_open), sizeof(item.m_open));//开盘价out.write(reinterpret_cast<const char*>(&item.m_qianfuquan_close), sizeof(item.m_qianfuquan_close));//前复权out.write(reinterpret_cast<const char*>(&item.m_last_close), sizeof(item.m_last_close));//前收盘out.write(reinterpret_cast<const char*>(&item.m_chg), sizeof(item.m_chg));//涨跌额out.write(reinterpret_cast<const char*>(&item.m_percent_chg), sizeof(item.m_percent_chg));//涨跌幅out.write(reinterpret_cast<const char*>(&item.m_turnover), sizeof(item.m_turnover));//换手率out.write(reinterpret_cast<const char*>(&item.m_volume), sizeof(item.m_volume));//成交量out.write(reinterpret_cast<const char*>(&item.m_amount), sizeof(item.m_amount));//成交额out.write(reinterpret_cast<const char*>(&item.m_tcap), sizeof(item.m_tcap));//总市值out.write(reinterpret_cast<const char*>(&item.m_mcap), sizeof(item.m_mcap));//流通市值
}istream& operator >> (istream& in, CStockItem& item)
{in.read(reinterpret_cast<char*>(&item.m_date), sizeof(item.m_date));//日期in.read(reinterpret_cast<char*>(&item.m_close), sizeof(item.m_close));//收盘价in.read(reinterpret_cast<char*>(&item.m_high), sizeof(item.m_high));//最高价in.read(reinterpret_cast<char*>(&item.m_low), sizeof(item.m_low));//最低价in.read(reinterpret_cast<char*>(&item.m_open), sizeof(item.m_open));//开盘价in.read(reinterpret_cast<char*>(&item.m_qianfuquan_close), sizeof(item.m_qianfuquan_close));//前复权in.read(reinterpret_cast<char*>(&item.m_last_close), sizeof(item.m_last_close));//前收盘in.read(reinterpret_cast<char*>(&item.m_chg), sizeof(item.m_chg));//涨跌额in.read(reinterpret_cast<char*>(&item.m_percent_chg), sizeof(item.m_percent_chg));//涨跌幅in.read(reinterpret_cast<char*>(&item.m_turnover), sizeof(item.m_turnover));//换手率in.read(reinterpret_cast<char*>(&item.m_volume), sizeof(item.m_volume));//成交量in.read(reinterpret_cast<char*>(&item.m_amount), sizeof(item.m_amount));//成交额in.read(reinterpret_cast<char*>(&item.m_tcap), sizeof(item.m_tcap));//总市值in.read(reinterpret_cast<char*>(&item.m_mcap), sizeof(item.m_mcap));//流通市值return in;
}

本地二进制缓存(一个文件才230K左右,900个文件才200MB,所有上市公司连同指数的全部数据也才800MB):

本地缓存数据:

股票名称列表输出到文件

这篇关于超简单C++ 获取股票历史数据自动缓存本地二进制加速访问,省心 更新至2021-07-09的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/423081

相关文章

C++右移运算符的一个小坑及解决

《C++右移运算符的一个小坑及解决》文章指出右移运算符处理负数时左侧补1导致死循环,与除法行为不同,强调需注意补码机制以正确统计二进制1的个数... 目录我遇到了这么一个www.chinasem.cn函数由此可以看到也很好理解总结我遇到了这么一个函数template<typename T>unsigned

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

C++统计函数执行时间的最佳实践

《C++统计函数执行时间的最佳实践》在软件开发过程中,性能分析是优化程序的重要环节,了解函数的执行时间分布对于识别性能瓶颈至关重要,本文将分享一个C++函数执行时间统计工具,希望对大家有所帮助... 目录前言工具特性核心设计1. 数据结构设计2. 单例模式管理器3. RAII自动计时使用方法基本用法高级用法

SpringBoot 获取请求参数的常用注解及用法

《SpringBoot获取请求参数的常用注解及用法》SpringBoot通过@RequestParam、@PathVariable等注解支持从HTTP请求中获取参数,涵盖查询、路径、请求体、头、C... 目录SpringBoot 提供了多种注解来方便地从 HTTP 请求中获取参数以下是主要的注解及其用法:1

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python

深入解析C++ 中std::map内存管理

《深入解析C++中std::map内存管理》文章详解C++std::map内存管理,指出clear()仅删除元素可能不释放底层内存,建议用swap()与空map交换以彻底释放,针对指针类型需手动de... 目录1️、基本清空std::map2️、使用 swap 彻底释放内存3️、map 中存储指针类型的对象

使用Spring Cache本地缓存示例代码

《使用SpringCache本地缓存示例代码》缓存是提高应用程序性能的重要手段,通过将频繁访问的数据存储在内存中,可以减少数据库访问次数,从而加速数据读取,:本文主要介绍使用SpringCac... 目录一、Spring Cache简介核心特点:二、基础配置1. 添加依赖2. 启用缓存3. 缓存配置方案方案

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过