1 Star 0 Fork 241

葛老师 / 股票分析

forked from wking / 股票分析 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

说明文档

项目无Wiki,此说明文档包含了项目所有相关内容。包括项目介绍、使用说明等。

前言

一切有为法,如梦幻泡影;如露亦如电,应作如是观。

缘起

  我自己多年来一直使用通达信公式选股。近期越发感觉到通达信公式的局限性(大智慧同花顺公式和通达信都有同样局限性),比如变量无法二次赋值、没有循环、IF语句简陋,且软件自带公式评测功能鸡肋。更重要的是通达信公式都是基于短周期的判断,比如不管是MACD金叉、cross上穿还是创历史新高,皆为“当前周期日”的结果,无法判断类似“此为上涨第几浪,每个浪的高低价各是多少”。而如果用周线周期选股,那只有每周五收盘以后才可以选,会错失先机。月线更是不可能用到。

  因此,我有了两个核心需求:盘中选股以及策略回测。这又延伸出了数据采集、数据加工、行情监视等需求。另外为了隐私考虑,所有第三方导入库、借鉴的代码都需开源,本地化,不云端。

  我本身侧重于研究股票或期货的趋势、逻辑、投机,不做价投,不做超短,可以耐心持股,也可以耐心空仓。

什么是量化

  我的理解,“量化”一词有两层释义。狭义上是指把思路变为计算机代码,包括通达信公式、python代码等;广义上是指在狭义释义的基础上,通过计算机自动完成整个选股和交易的过程。

为什么量化

  然而量化又是投资过程中必须要完成的阶段。你有了一个选股思路,不管是长线投资的还是短线投机、不论是看基本面还是看技术面,总之你想把思路转化为选股策略,把思想转变为选股公式,这一过程本身就是量化。再之后,为了确定策略是否有效、收益率如何,又必然需要做策略回测来验证。最后,为了防止人性对交易过程的干扰,可以考虑使用自动化交易。

量化的优点

  1. 选股。当把选股思路量化为代码后,计算机可以快速准确的帮你选出你想要的股票。
  2. 下单速度。触发买入、卖出条件后,计算机可以在毫秒级别内完成下单操作。人为操作需要“打开交易软件-填股票代码-填金额-填数量-点下单”。这还没有考虑下单前,人们会来回犹豫所浪费的时间。

量化了就能赚钱?

  python、量化过程、量化平台都只是工具,核心是交易策略,是交易思想。交易的目的是赚钱,不是学编程当程序员,也不是沉迷于玩数据中不可自拔,代码写的再高级精炼,不赚钱统统白搭。现在网上很多收费课程、量化平台,都是“卖铲子”的,核心的可以能稳定赚钱的量化策略,绝无一家提供。

  就我个人而言,我不赞同完全把重心放在全自动量化交易上,而忽视基本面和技术面。对于短线来说,不确定性太多。比如,1分钟K线几乎处于“混沌”状态,根本没有逻辑和趋势可言。而周期越大的K线,越能体现出趋势和力度,突然反转的可能性越小。对于长线来说……还未听说过有把程序化交易用于长线的事例。然而就目前我所了解见到的,都是将量化应用于短线的实战。我觉得,对于短线而言,量化的优点是抢先发现买点、抢先下单,先敌制胜。但这个前提是你在做量化之前,就本身已经有了一套“短线不败法宝”,换言之就是你短线本身就一直在赚钱,上量化只不过是为了减少选股和下单时间。如果之前做短线一直亏钱,那上了量化后就赚钱是绝无可能的。对于中长线而言,量化也是侧重于选股方面。下单可以自己手动下单,中长线是不在乎那几分钟下单时间的区别的。

  引用一段网友的话:

交易这门手艺发展了这么多年,流派可谓五花八门,有看基本面搞价值投资的,有看K线搞技术指标的,有学江恩,缠论数波浪画中枢的,有分析资金面的,分析市场情绪的,有结合原始数据做日内波段的,有恨不得把服务器架在交易所对面做高频的,有搞一箱子GPU做automl,深度学习和强化学习的,有搞对冲的,搞多因子的,搞指数增强的,有搞MOM组合管理的,有搞一堆艰深晦涩的微分方程做衍生品套利的,当然,也有靠求神拜佛和拍脑门跺地板的。每种流派都有一些人奉为圭臬,还有一批人弃如敝履,而且时不时的还会冒出几个新的流派出来,令人眼花缭乱,有些摸不到头脑。

不知道哪个著名的人曾说过,如果你没有自己的思想,那你的脑子注定会成为其他人思想的跑马场。上面的这一堆思想和流派,既然能够出现并且流传下来,还能够有一批拥趸和死忠,也就表明它们确实是市场的本质或者圣杯在某个维度的一个映射或投影,但也仅仅只是一个投影而已。学习它们只是为了能够从更多的角度去窥视那个交易的圣杯,进而一点点的深化,完善和验证自己的交易思想和理论体系,最终通过一个承载着自己思想体系的工具来将思想兑换成实际的收益。在这个市面上出现的每一种付费编译的或者免费开源的交易软件都是固定的,即使在不断更新迭代也只是按照开发团队的思路来进行,包括QA在内,不可能有一个软件或者项目能够满足所有可能的交易思想,自然也就无法让你自由的学习,验证,归纳和吸收这些思想中的精华。因此,如果你没有定制化的开发交易工具的能力,而只能使用现有的工具的话,你的思想和自由意志就这样被别人的工具所局限住了。——对QUANTAXIS的设计理念的思考和一些感悟

项目介绍

  • 使用python进行股票历史数据下载和分析选股。除了选股策略以外,其他都可公开。

  • git网站上有很多优秀开源量化平台项目。本项目与其他项目的区别是,本项目侧重于选股、回测所需数据的导入工作。有了历史数据和选股策略,选择哪个量化平台做回测都是很轻松的事情了。

  • 业余编程水平,需求导向。才疏学浅,刚学python几个月时间。git主要作为云端git库使用。无任何解答服务。

  • 力求选择最稳定可靠的数据获取方式。虽然网上有很多数据源平台,但都受制于“积分”、带宽、平台是否更新等,完全是把程序主动权交到了对方手里。因此本项目所有数据依靠本地通达信软件导出提供。感谢通达信,真是个好公司!不止数据容易提取,各种和谐加强版也很好用。

功能截图

软件运行截图

盘中选股截图

盘中选股截图

回测系统截图

回测系统截图

显示策略持仓区域

高亮显示策略历史持仓区域,红赚绿亏

项目进度

已完成功能

  • 读取本机通达信文件,导出未除权日线数据、股本变迁数据、全A股全年份财务报告数据。
  • 财务报告数据自动判断是否需要更新
  • 日线数据本地前复权计算处理。日线数据只追加当日数据,加快数据更新速度。
  • 在当天数据更新后的盘后选股(选股策略你得自己写)
  • 当天盘中选股【2021年2月10日19:03:24完成】
  • 数据存储方式优化。目前使用csv文件,性能略差。但这个也不是特别需要。【2021年2月10日完成,使用pickle格式保存】
  • 回测(是指本项目提供数据和策略,接入本地化的量化回测平台进行回测,使用rqalpha)【2021年3月2日完成】
  • 多进程处理优化【2021年3月5日完成】

待完成功能

需求优先级由高到低排序

  • 暂无

数据来源

基础数据来源:

数据名称 数据来源路径 数据本身更新方式
日线数据 解析本机通达信离线数据/vipdoc/sh(sz)/lday 完整数据通过通达信软件手动更新,当日数据可在通达信设置自动更新
财务报告数据 解析本机通达信离线数据 /vipdoc/cw 代码自动维护更新
股本变迁数据 解析本机通达信离线数据 /T0002/hq_cache/gbbq 运行通达信时,通达信可自动更新

其他数据可以由基础数据本机计算得出。比如前复权、流通市值、市盈率等。

使用说明

初次使用

  1. 项目本身只有几个python文件,不占硬盘。但通达信导出的数据最少需要4G的硬盘空间。并且强烈建议使用固态硬盘存储通达信导出数据,可极大提高性能。
  2. 克隆/下载项目zip包,解压缩。项目无需pip安装
  3. 查看软件架构章节,自行安装支持库
  4. 打开user_config.py,配置好各路径
  5. 参照CeLue模板.py,照猫画虎自己写策略,保存为celue.py
  6. 接着看数据更新章节

数据更新

每天16时以后,通达信才会更新当日日线数据。你可手动更新,或在通达信设置-设置1标签,勾选“定时提示下载日线”并设置为16点。在通达信软件运行的情况下,每天16点通达信自动更新当日数据。

本机通达信软件更新数据后,再进行下面的操作:

  1. 运行readTDX_cw.py,更新财报和股权变迁数据

  2. 运行readTDX_lday.py,自动更新需要更新的数据。使用命令行参数 del 可删除现有数据文件,强制重新生成全部数据。

python readTDX_lday.py del

有了历史数据文件,你也可以利用数据自行发挥你的想象力做任何事情。

本项目导出的数据自动进行了前复权处理,可与通达信软件内显示的价格完全一致,一分钱都不差(你需要在通达信软件的设置1里勾选“复权采用等比方式”,否则很多股票多年前的价格是负的)。

选股

盘中或盘后,都可通过运行xuangu.py选股。xuangu.py会自动调用celue.py的策略选股并显示结果。

盘后选股

盘后(周一~周五16:01~8:59,或周六周日)使用 readTDX_lday.py 生成的本机数据选股。当日16点以后,自己手动控制通达信更新完当日数据,运行xuangu.py进行选股。xuangu.py文件里有日期变量的设置。如果不指定日期,按照当天数据选股。如果指定日期,按照日期区间的数据选股。

盘中选股

盘中(周一~周五,9:00~16:00)使用昨日之前的本机数据 + 当天在线实时数据选股。程序会获取当日实时最新行情,整合到具体股票日线数据中(不保存到文件,只在内存操作),并判断是否需要前复权(前复权的特性是如果今天除权,那么以前的数据全需要变)。

策略信号及回测

保存策略信号

保存策略信号是为了方便查看策略的买卖点,以及方便回测。

生成的celue汇总.csv可以极大的提升回测效率。回测如果不使用 celue汇总.csv 而是直接循环,每天4000多只股票都需要循环一遍,循环5年就需要半个小时多。而使用 celue汇总.csv 只需要1分钟就可获得结果。

  1. 运行 celue_save.py 文件为本机数据文件添加策略买卖信号。并生成一个独立的 celue汇总.csv 文件。

  2. 默认只更新缺少策略信号的交易日,使用命令行参数 del 完全重新生成策略信号(比如当你修改了策略时就需要全部重新生成策略信号)

python celue_save.py del
  1. 默认使用多进程执行,效率高。使用命令行参数 single 单进程执行。
python celue_save.py single

回测

生成策略信号文件之后你就可以拿着 celue汇总.csv 文件去任意一个回测框架回测,只需要在回测框架里导入 celue汇总.csv 内容即可。具体如何导入可借鉴 huice_rq.py 文件。

我自己使用了开源量化回测框架 rqalpha ,运行 huice_rq.py 即可直接进行回测。

使用网页查看策略信号

运行 plot.py 后打开同目录的 pyecharts.html 可以直观查看策略在具体股票历史K线图上的买卖区间。

显示策略持仓区域 高亮显示策略历史持仓区域,红赚绿亏

二次开发

想给日线计算添加自己的数据,在func_TDX.py的make_fq函数,加到下面语句前。

if flag_attach:  # 追加模式,则附加最新处理的数据
data = df_code_original.append(data)

财报文件所有字段释义,查看“util_docs\专业财务文件字段含义对照表.txt”

自动化交易

本项目不包含自动化交易模块。但你可以对接任何有python接口的自动化交易软件。

买入对接

xuangu.py 最后的变量“已选出股票列表”保存的就是策略当天发出买入信号,可买入的股票列表。只需对“已选出股票列表”下达买入命令即可。

卖出对接

缺少获取当前持仓股部分的代码,需要自行编写。

获取持仓股列表后,加工下面的核心代码:

stock_code = '000887'
df_stock = pd.read_csv(ucfg.tdx['csv_lday'] + os.sep + stock_code + '.csv',
                       index_col=None, encoding='gbk', dtype={'code': str})
df_stock['date'] = pd.to_datetime(df_stock['date'], format='%Y-%m-%d')  # 转为时间格式
df_stock.set_index('date', drop=False, inplace=True)  # 时间为索引。方便与另外复权的DF表对齐合并

if '09:00:00' < time.strftime("%H:%M:%S", time.localtime()) < '16:00:00':
    df_today = get_tdx_lastestquote(stock_code)
    df_stock = update_stockquote(stock_code, df_stock, df_today)

celue_sell = 卖策略(df_stock)

软件架构

软件/库 版本
windows 10 20H2
通达信开心果版本 v2020.12
python (anaconda) 3.8.5
pytdx 1.72
pyecharts 1.9.0
RQalpha 4.2.5
ta-lib(安装方法:在cmd上使用“pip install talib”命令一般会报错,正确安装方法是,进入*https://www.lfd.uci.edu/~gohlke/pythonlibs/*,下拉选择TA_Lib-0.4.17-cp37-cp37m-win_amd64.whl(win系统64位,python3.7版本,根据自己系统和python版本选择相应的安装包),将下载包放在当前工作路径中,然后在Anaconda Prompt(或windows的cmd)里面输入命令:pip install TA_Lib-0.4.17-cp27-cp27m-win_amd64.whl) 0.4.19

其他都是辅助库。缺什么库运行时报错自己pip安装一下即可

开发日志

点我打开

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

使用python进行股票分析和选股 展开 收起
Python
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/gray6666/stock-analysis.git
git@gitee.com:gray6666/stock-analysis.git
gray6666
stock-analysis
股票分析
master

搜索帮助