Repository: pythonstock/stock Branch: master Commit: 6bc0264e69ba Files: 189 Total size: 417.3 KB Directory structure: gitextract_pteu13qr/ ├── .gitignore ├── LICENSE ├── README.md ├── backend/ │ ├── docs/ │ │ ├── 1-todo-2020-12-06.md │ │ └── git-push-tag.md │ ├── jobs/ │ │ ├── 18h_daily_job.py │ │ ├── README.txt │ │ ├── aps_job.py │ │ ├── basic_job.py │ │ ├── cron.daily/ │ │ │ └── run_daily │ │ ├── cron.hourly/ │ │ │ └── run_hourly │ │ ├── cron.minutely/ │ │ │ └── run_1minute │ │ ├── cron.monthly/ │ │ │ └── run_monthly │ │ ├── crontab │ │ ├── daily_job.py │ │ ├── guess_indicators_daily_buy_job.py │ │ ├── guess_indicators_daily_job.py │ │ ├── guess_indicators_daily_sell_job.py │ │ ├── guess_rsrs_daily_job.py │ │ ├── quarter_job.py │ │ ├── restart_mnist_serving.sh │ │ ├── restart_web.sh │ │ ├── run_cron.sh │ │ ├── run_init.sh │ │ ├── run_jupyter.sh │ │ ├── run_web.sh │ │ ├── start_mariadb.sh │ │ └── test_akshare/ │ │ ├── test_stock_zh_a_daily.py │ │ ├── test_stock_zh_a_spot.py │ │ └── test_stock_zh_index_spot.py │ ├── libs/ │ │ ├── common.py │ │ ├── stock_web_dic.py │ │ └── stock_web_dic.py.bk │ ├── old_jobs/ │ │ ├── README.md │ │ ├── guess_indicators_lite_buy_daily_job.py │ │ ├── guess_indicators_lite_sell_daily_job.py │ │ ├── guess_period_daily_job.py │ │ ├── guess_return_daily_job.py │ │ └── guess_sklearn_ma_daily_job.py │ ├── supervisor/ │ │ ├── example_supervisord_conf │ │ └── supervisord.conf │ └── web/ │ ├── README.md │ ├── base.py │ ├── chartHandler.py │ ├── dataEditorHandler.py │ ├── dataIndicatorsHandler.py │ ├── dataTableHandler.py │ ├── demo-chart.py │ ├── main.py │ ├── minstServingHandler.py │ ├── static/ │ │ ├── css/ │ │ │ └── fonts.googleapis.com.css │ │ ├── js/ │ │ │ ├── bootbox.js │ │ │ ├── bootstrap-datepicker.zh-CN.js │ │ │ ├── datatables.Chinese.json │ │ │ ├── draw.js │ │ │ └── grid.locale-en.js │ │ └── update_bokeh.sh │ ├── templates/ │ │ ├── bokeh_embed.html │ │ ├── common/ │ │ │ ├── footer.html │ │ │ ├── header.html │ │ │ ├── left_menu.html │ │ │ └── meta.html │ │ ├── data_editor.html │ │ ├── index.html │ │ ├── layout/ │ │ │ ├── default.html │ │ │ ├── indicators-main.html │ │ │ ├── indicators.html │ │ │ ├── main.html │ │ │ ├── single_default.html │ │ │ └── single_main.html │ │ ├── minst_serving.html │ │ ├── stock_chart.html │ │ ├── stock_indicators.html │ │ ├── stock_web.html │ │ ├── test.html │ │ └── test2.html │ ├── test_thread.py │ ├── test_thread_v2.py │ └── tornado_bokeh_embed.py ├── docker-compose/ │ ├── .gitignore │ ├── LICENSE │ ├── README.md │ ├── build_stock.sh │ ├── dev-docker-compose-restart.sh │ ├── dev-docker-compose.yml │ ├── docker/ │ │ ├── DevBackendDockerfile │ │ ├── DevFrontendDockerfile │ │ ├── Dockerfile │ │ ├── ProdBackendDockerfile │ │ ├── ProdFrontendDockerfile │ │ ├── README.md │ │ └── build.sh │ ├── docker-compose.yml │ ├── mysql/ │ │ ├── init.sql │ │ └── my.cnf │ ├── nginx/ │ │ └── nginx.conf │ └── nginx.conf └── frontend/ ├── .eslintignore ├── .gitignore ├── LICENSE ├── README.md ├── babel.config.js ├── docker-build.sh ├── docker-entrypoint.sh ├── jest.config.js ├── jsconfig.json ├── mock/ │ ├── index.js │ ├── mock-server.js │ ├── table.js │ ├── user.js │ └── utils.js ├── package.json ├── postcss.config.js ├── public/ │ ├── 40x.html │ ├── 50x.html │ └── index.html ├── src/ │ ├── App.vue │ ├── api/ │ │ ├── article.js │ │ ├── menu.js │ │ ├── package.js │ │ ├── table.js │ │ └── user.js │ ├── components/ │ │ ├── Breadcrumb/ │ │ │ └── index.vue │ │ ├── Hamburger/ │ │ │ └── index.vue │ │ ├── Pagination/ │ │ │ └── index.vue │ │ └── SvgIcon/ │ │ └── index.vue │ ├── directive/ │ │ ├── el-table/ │ │ │ ├── adaptive.js │ │ │ └── index.js │ │ └── waves/ │ │ ├── index.js │ │ ├── waves.css │ │ └── waves.js │ ├── icons/ │ │ ├── index.js │ │ └── svgo.yml │ ├── layout/ │ │ ├── components/ │ │ │ ├── AppMain.vue │ │ │ ├── Navbar.vue │ │ │ ├── Sidebar/ │ │ │ │ ├── FixiOSBug.js │ │ │ │ ├── Item.vue │ │ │ │ ├── Link.vue │ │ │ │ ├── Logo.vue │ │ │ │ ├── SidebarItem.vue │ │ │ │ └── index.vue │ │ │ └── index.js │ │ ├── index.vue │ │ └── mixin/ │ │ └── ResizeHandler.js │ ├── main.js │ ├── permission.js │ ├── router/ │ │ └── index.js │ ├── settings.js │ ├── store/ │ │ ├── getters.js │ │ ├── index.js │ │ └── modules/ │ │ ├── app.js │ │ ├── settings.js │ │ └── user.js │ ├── styles/ │ │ ├── element-ui.scss │ │ ├── index.scss │ │ ├── mixin.scss │ │ ├── sidebar.scss │ │ ├── transition.scss │ │ └── variables.scss │ ├── utils/ │ │ ├── auth.js │ │ ├── get-page-title.js │ │ ├── index.js │ │ ├── request.js │ │ ├── scroll-to.js │ │ └── validate.js │ ├── vendor/ │ │ └── Export2Excel.js │ └── views/ │ ├── 404.vue │ ├── dashboard/ │ │ └── index.vue │ ├── form/ │ │ └── index.vue │ ├── login/ │ │ └── index.vue │ ├── nested/ │ │ ├── menu1/ │ │ │ ├── index.vue │ │ │ ├── menu1-1/ │ │ │ │ └── index.vue │ │ │ ├── menu1-2/ │ │ │ │ ├── index.vue │ │ │ │ ├── menu1-2-1/ │ │ │ │ │ └── index.vue │ │ │ │ └── menu1-2-2/ │ │ │ │ └── index.vue │ │ │ └── menu1-3/ │ │ │ └── index.vue │ │ └── menu2/ │ │ └── index.vue │ ├── table/ │ │ ├── complex-table.vue │ │ └── index.vue │ └── tree/ │ └── index.vue ├── tests/ │ └── unit/ │ ├── .eslintrc.js │ ├── components/ │ │ ├── Breadcrumb.spec.js │ │ ├── Hamburger.spec.js │ │ └── SvgIcon.spec.js │ └── utils/ │ ├── formatTime.spec.js │ ├── param2Obj.spec.js │ ├── parseTime.spec.js │ └── validate.spec.js └── vue.config.js ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] *$py.class # C extensions *.so data # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg notebooks nohup.out # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test_akshare / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *.cover .hypothesis/ # Translations *.mo *.pot # Django stuff: *.log local_settings.py # Flask stuff: instance/ .webassets-cache # Scrapy stuff: .scrapy # Sphinx documentation docs/_build/ # PyBuilder target/ # Jupyter Notebook .ipynb_checkpoints # pyenv .python-version # celery beat schedule file celerybeat-schedule # SageMath parsed files *.sage.py # dotenv .env # virtualenv .venv venv/ ENV/ # Spyder project settings .spyderproject .spyproject # Rope project settings .ropeproject # mkdocs documentation /site # mypy .mypy_cache/ .idea *.iml .DS_Store *.zip *.log *.pyc doc /bin pkg *.tmp ================================================ FILE: LICENSE ================================================ 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. ================================================ FILE: README.md ================================================ ### pythonstock V3.0 项目简介,2025.02.28更新 **特别说明:股市有风险投资需谨慎,本项目只能用于Python代码学习,股票分析,投资失败亏钱不负责,不算BUG。** **github/gitee是项目地址** github地址: https://github.com/pythonstock/stock gitee地址: https://gitee.com/pythonstock/stock **视频地址:** https://space.bilibili.com/52280367/lists/1923758?type=season **相关博客资料:** https://blog.csdn.net/freewebsys/category_9285317.html 数据分析清洗使用pandas,numpy。 http://pandas.pydata.org/ 数据存储到磁盘上,使用Mysql数据库。存储股票数据。 https://pypi.python.org/pypi/mysqlclient web框架使用tornado http://www.tornadoweb.org/en/stable/ tornado web系统 http://docs.pythontab.com/tornado/introduction-to-tornado/ ``` PythonStock V3.0 是基于Python的pandas,akshare,bokeh,tornado,stockstats,ta-lib等框架开发的全栈股票系统。 项目创建于2017年7月17日,每月不定期更新。 1)可以直接使用docker直接本地部署运行,整个项目在docker hub上压缩后200MB,本地占用500MB磁盘空间。 2)使用Docker解决了Python库安装问题,使用Mariadb(MySQL)存储数据。借助akshare抓取数据。 3)使用cron做定时任务,每天进行数据抓取计算,每天18点开始进行数据计算,计算当日数据,使用300天数据进行计算,大约需要15分钟计算完毕。 4)股票数据接口防止被封,按天进行数据缓存,储存最近3天数据,每天定时清除,同时使用read_pickle to_pickle 的gzip压缩模式存储。 5)使用tornado开发web系统,支持每日股票数据-东财,龙虎榜-个股上榜-新浪,数据中心-大宗交易行情等。 6)数据展示系统,是通用数据展示系统,配置字典模板之后,页面自动加载数据,并完成数据展示,后续自己开发的指标数据可以加入进去。 7)增加曲线数据分析,在查看股票中,可以直接跳转到东方财富页面查看相关信息,点击指标之后使用Bokeh将多达 17 个指标的数据绘图,进行图表展示。 8)2.0 最大的更新在于替换tushare库(因部分库不能使用),使用akshare进行数据抓取。 9)3.0 主要做的是项目整合,前端使用vue开发了,后端使用API,使用docker-compose开发部署。 基础库版本 1,pandas使用【 2.2.3 】版本, 2,numpy使用【 2.2.1 】版本, 3,sqlalchemy使用【 2.0.36 】版本, 4,akshare使用【 1.15.59 】版本, 5,bokeh使用【 3.6.2 】版本, 6,stockstats使用【 0.3.2 】版本, ``` 版本3.0 说明 ![image](https://gitee.com/pythonstock/stock/raw/master/frontend/public/stock-001.png) ![image](https://gitee.com/pythonstock/stock/raw/master/frontend/public/stock-002.png) ![image](https://gitee.com/pythonstock/stock/raw/master/frontend/public/stock-003.png) 然后根据3个指标进行股票数据计算: ``` KDJ: 1,超买区:K值在80以上,D值在70以上,J值大于90时为超买。一般情况下,股价有可能下跌。投资者应谨慎行事,局外人不应再追涨,局内人应适时卖出。 2,超卖区:K值在20以下,D值在30以下为超卖区。一般情况下,股价有可能上涨,反弹的可能性增大。局内人不应轻易抛出股票,局外人可寻机入场。 RSI: 1.当六日指标上升到达80时,表示股市已有超买现象,如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 2.当六日强弱指标下降至20时,表示股市有超卖现象,如果一旦继续下降至10以下时则表示已到严重超卖区域,股价极可能有止跌回升的机会。 购买条件结果表:guess_indicators_lite_buy_daily 购买条件结果表:guess_indicators_lite_sell_daily ``` 每日股票指标数据计算17个指标如下(数据表 guess_indicators_daily): | 计算指标 | 说明 | |---------- |------------------------------------------| | 1,交易量delta指标分析 | The Volume Delta (Vol ∆) | | 2,计算n天差 | 可以计算,向前n天,和向后n天的差。 | | 3,n天涨跌百分百计算 | 可以看到,-n天数据和今天数据的百分比。 | | 4, CR指标 | http://wiki.mbalib.com/wiki/CR%E6%8C%87%E6%A0%87 价格动量指标 CR跌穿a、b、c、d四条线,再由低点向上爬升160时,为短线获利的一个良机,应适当卖出股票。 CR跌至40以下时,是建仓良机。而CR高于300~400时,应注意适当减仓。 | | 5,最大值,最小值 | 计算区间最大值 volume max of three days ago, yesterday and two days later stock["volume_-3,2,-1_max"] volume min between 3 days ago and tomorrow stock["volume_-3~1_min"] 实际使用的时候使用 -2~2 可计算出5天的最大,最小值。 | | 6, KDJ指标 | http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、 最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV, 然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度, 以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 | | 7,SMA指标 | http://wiki.mbalib.com/wiki/Sma 简单移动平均线(Simple Moving Average,SMA) 可以动态输入参数,获得几天的移动平均。 | | 8, MACD指标 | http://wiki.mbalib.com/wiki/MACD 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 | | 9, BOLL指标 | http://wiki.mbalib.com/wiki/BOLL 布林线指标(Bollinger Bands) | | 10, RSI指标 | http://wiki.mbalib.com/wiki/RSI 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象,如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 | | 11, W%R指标 | http://wiki.mbalib.com/wiki/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A0%87 威廉指数(Williams%Rate)该指数是利用摆动点来度量市场的超买超卖现象。 | | 14, TR、ATR指标 | http://wiki.mbalib.com/wiki/%E5%9D%87%E5%B9%85%E6%8C%87%E6%A0%87 均幅指标(Average True Ranger,ATR)均幅指标(ATR)是取一定时间周期内的股价波动幅度的移动平均值,主要用于研判买卖时机。 | | 14, DMA指标 | http://wiki.mbalib.com/wiki/DMA DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。 DMA, difference of 10 and 50 moving average stock[‘dma’] | | 15, DMI,+DI,-DI,DX,ADX,ADXR指标 | http://wiki.mbalib.com/wiki/DMI 动向指数Directional Movement Index,DMI) http://wiki.mbalib.com/wiki/ADX 平均趋向指标(Average Directional Indicator,简称ADX) http://wiki.mbalib.com/wiki/%E5%B9%B3%E5%9D%87%E6%96%B9%E5%90%91%E6%8C%87%E6%95%B0%E8%AF%84%E4%BC%B0 平均方向指数评估(ADXR)实际是今日ADX与前面某一日的ADX的平均值。ADXR在高位与ADX同步下滑,可以增加对ADX已经调头的尽早确认。 ADXR是ADX的附属产品,只能发出一种辅助和肯定的讯号,并非入市的指标,而只需同时配合动向指标(DMI)的趋势才可作出买卖策略。 在应用时,应以ADX为主,ADXR为辅。 | | 16, TRIX,MATRIX指标 | http://wiki.mbalib.com/wiki/TRIX TRIX指标又叫三重指数平滑移动平均指标(Triple Exponentially Smoothed Average) | | 17, VR,MAVR指标 | http://wiki.mbalib.com/wiki/%E6%88%90%E4%BA%A4%E9%87%8F%E6%AF%94%E7%8E%87 成交量比率(Volumn Ratio,VR)(简称VR),是一项通过分析股价上升日成交额(或成交量,下同)与股价下降日成交额比值, 从而掌握市场买卖气势的中期技术指标。 | ### 项目部署放到docker-compose ```bash # 下载 docker curl -fsSL https://get.docker.com -o get-docker.sh sh get-docker.sh # curl -L "https://www.ghproxy.cn/https://github.com/docker/compose/releases/download/v2.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose ``` ```bash # 生产环境,编译前端部署: docker-compose up -d # 开发环境,node dev 方式部署: docker-compose -f dev-docker-compose.yml up -d ``` 进入镜像: ```bash docker exec -it stock bash sh /data/stock/jobs/cron.daily/run_daily ``` 说明,启动容器后,会调用。run_init.sh 进行数据初始化,同时第一次执行后台执行当日数据。 以后每日18点(只有18点左右才有今日的数据)进行股票数据抓取并计算。 ### 本地访问端口 > http://localhost:8080 股票系统前端地址 > http://localhost:9090 股票系统后端地址 ### 架构设计 全系使用python实现。因为都是python的类库,互相之间调用方便。 从数据抓取,数据处理,到数据展示数据运算都是python实现。 最终的数据都到前端展示出来。主要分为4个文件夹。 > jobs 抓取数据并存储实现类。 > > libs 通用工具类。 > > web 前端展示框架。 > > supervisor 进程管理工具。 ### 应用部署 需要mysql数据库启动。项目放到/data/stock 目录。 ``` CREATE DATABASE IF NOT EXISTS `stock_data` CHARACTER SET utf8 COLLATE utf8_general_ci; ``` 使用 : http://docs.sqlalchemy.org/en/latest/core/reflection.html ## 更新日志 ### 18 修改bug,前端使用编译nginx方式部署,修改数据库字段,解决定时任务BUG 2025-02-28 存储数据格式为 double 方便进行排序,decimal 类型转换出问题。 拆分生产部署,切换成nginx,前端进行编译构建。提高前端加载速度。 nginx的html影射到./data/html目录,前端编译完成需要拷贝文件到html,等待完成即可。 解决定时任务问题,需要设置权限,才可以。 ### 17 v3.0发布,前端分离,项目和部署整合到一起 2025-01-10 修改接口展示空。修改数据库脚本。 解决预测数据买和卖的脚本。 进行缩减、计算相关添加操作。 修改启动脚本、接口路径及配置。 设置分页数据。增加联合主键判断。 解决分页问题并执行查询语句。 增加日期查询方法。 进行数据搜索相关添加操作。 添加配置。修改路由地址。 解决端口映射问题及修改端口测试。 拆分前后端,用 dockerfile 构建镜像并解决前端编译问题。 修改开发者模式,解决本地开发启动问题。 增加地址。增加前端开发者模式启动。 增加每天数据跑批。进行猜工作相关添加操作。 增加日志跑数据。升级增加展示。 架构升级并使用 vue 的 ui 开发。 增加 install docker 说明。 添加 vue api。 ### 16 更新发布 2.1 版本进行镜像升级 2023-06-03 使用新方式打包镜像,镜像大小从本地的 852MB 缩小到 597MB。 为了支持更多 AKShare 特性,请尽快升级 Python 到 3.8 以上版本 1,numpy从【 1.21.5 】升级到了【 1.21.6 】版本 2,akshare从【 1.3.50 】升级到了【 1.10.5 】版本 3,bokeh从【 2.4.2 】升级到了【 2.4.3 】版本 根据 https://www.akshare.xyz/changelog.html 修改方法: 1.7.99 替换 stock_sina_lhb_ggtj 成:stock_lhb_ggtj_sina ### 15 发布一个 2.0 的版本 - 2021-10-11 构建基础版本 pythonstock/pythonstock:base-2021-09 在这个镜像的基础上使用 akshare 1.1.9 折腾几个月,终于把2.0 弄好了,为啥弄2.0 因为之前发现 tushare的数据不能抓取了。需要注册成 pro 版本,但是pro 还有积分限制。 诸多不便吧,于是换成了 akshare 库了,大改了,需要找到相关的新库。然后在些代码。 删除掉了 ta-lib 安装了之后从来没有用到,jupyter 也是没有用。占空间影响下载心情。将镜像进一步减小。 ### 14 bokeh 升级到 2.4.0 版本 目录 /usr/local/lib/python3.7/site-packages 使用脚本进行升级。 ### 13 升级ak到v1.0.80 做好每日东方财经数据 https://www.akshare.xyz/zh_CN/latest/data/stock/stock.html#id1 限量: 单次返回所有 A 股上市公司的实时行情数据 600开头的股票是上证A股,属于大盘股,其中6006开头的股票是最早上市的股票, 6016开头的股票为大盘蓝筹股;900开头的股票是上证B股; 000开头的股票是深证A股,001、002开头的股票也都属于深证A股, 其中002开头的股票是深证A股中小企业股票;200开头的股票是深证B股; 300开头的股票是创业板股票;400开头的股票是三板市场股票。 过滤包括:600,6006,601,000,001,002,且不包括ST的股票数据。 增加数据库utf8 参数 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci ### 12 升级基础镜像到3.7 python,保障 akshare 0.6.10 以上版本支持 发现 akshare 要求升级python 3.7 以上版本才可以,需要升级基础镜像。 然后 akshare 就可以升级到 0.9.65 的最新版本了。 新版本就可以按照日期进行查询,解决 TypeError: stock_zh_a_daily() got an unexpected keyword argument 'start_date' 这个问题了。 ### 11 使用 akshare 做相关股票数据抓取 中国的股市开盘时间为:每周一至周五的上午9:30——11:30, 下午13:00——15:00。中国股市收盘时间为:每周一至周五的下午3点。 实时行情数据 接口: stock_zh_a_spot 目标地址: http://vip.stock.finance.sina.com.cn/mkt/#hs_a 描述: A 股数据是从新浪财经获取的数据, 重复运行本函数会被新浪暂时封 IP, 建议增加时间间隔 限量: 单次返回所有 A 股上市公司的实时行情数据 历史行情数据 日频率 接口: stock_zh_a_daily 目标地址: https://finance.sina.com.cn/realstock/company/sh600006/nc.shtml(示例) 描述: A 股数据是从新浪财经获取的数据, 历史数据按日频率更新; 注意其中的 sh689009 为 CDR, 请 通过 stock_zh_a_cdr_daily 接口获取 限量: 单次返回指定 A 股上市公司指定日期间的历史行情日频率数据 ### 10 增加东方财经弹窗窗口、增加指标计算弹窗窗口 发现了一个东方财富的页面,是给pc端用的。 可以做个弹出框放到系统中。不进行调整了,长宽高可以做的小点。使用iframe引入界面。否则有跨域和样式问题。 修改指标页面,改成窗口弹窗,做页面适配,方便查看。 ### 9,增加日历 ``` 古老的jquery 代码: $( ".date-picker" ).datepicker({ language: 'zh-CN', //设置语言 format:"yyyymmdd", showOtherMonths: true, selectOtherMonths: false, autoclose: true, todayHighlight: true }); 针对日期类型的搜索条件增加日历 ``` https://www.bootcss.com/p/bootstrap-datetimepicker/ 不是使用jQuery的时间。 ### 8,发现MariaDb 版本不兼容问题,最后切换成mysql,使用 mysql:5.7 镜像 相关数据执行只支持到10.5.4,版本可以使用,但是10.5.8 就有问题了。 限制死了版本。看来软件也不能瞎升级,都用最新的有问题。可以解决数据问题。 使用 mysql:5.7 镜像,更通用些,不折腾mariaDb了。 ### 7,解决 Bokeh JS兼容问题。 > 升级 bokeh 到 2.1.1 版本 > > https://pypi.org/project/bokeh/#files > > 升级JS,因为 lib 包升级导致问题。 ### 6,升级 bokeh 到 2.1.1 版本 ``` https://pypi.org/project/bokeh/#files ``` ### 5,解决日志打印问题 ``` 配置 main.py tornado.options.parse_command_line() 然后启动配置参数: /usr/local/bin/python3 /data/stock/web/main.py -log_file_prefix=/data/logs/web.log ``` ### 4,解决跑数据问题 ``` # 通过数据库链接 engine。 def conn(): try: db = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB, charset="utf8") # db.autocommit = True except Exception as e: print("conn error :", e) db.autocommit(on=True) return db.cursor() ``` 之前升级过代码,造成 db.cursor() 问题。 ### 3,增加多字段排序 > 1,点击是单个字段进行排序。 > > 2,按照【shift】,点击多个,即可完成多字段排序。 > > 3,服务端分页排序。 > > 4,按照多个字段进行筛选查询。 ### 2,使用pandas处理重复数据 https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop_duplicates.html ```python data = get_data(year, quarter) # 处理重复数据,保存最新一条数据。 data.drop_duplicates(subset="code", keep="last") ``` ### 1,web使用datatable显示报表 通用数据配置,在 libs/stock_web_dic.py 配置数据之后,可以实现动态加载菜单,根据数据库表的行列显示数据。 不用一个表一个表进行开发,通用数据展示。 ================================================ FILE: backend/docs/1-todo-2020-12-06.md ================================================ ## 切换到了mysql 数据库 5.7 的版本 ```log $ docker logs mysqldb 2020-12-06 23:01:40+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.32-1debian10 started. 2020-12-06 23:01:41+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql' 2020-12-06 23:01:41+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.32-1debian10 started. 2020-12-06 23:01:41+08:00 [Note] [Entrypoint]: Initializing database files 2020-12-06T15:01:41.637316Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-06T15:01:43.872609Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-12-06T15:01:44.535591Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-12-06T15:01:44.961598Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f4ccb5f1-37d3-11eb-8fd1-0242ac180002. 2020-12-06T15:01:45.054324Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-12-06T15:01:45.604908Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-06T15:01:45.765331Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option. 2020-12-06 23:02:35+08:00 [Note] [Entrypoint]: Database files initialized 2020-12-06 23:02:35+08:00 [Note] [Entrypoint]: Starting temporary server 2020-12-06 23:02:35+08:00 [Note] [Entrypoint]: Waiting for server startup 2020-12-06T15:02:35.990607Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-06T15:02:35.992115Z 0 [Note] mysqld (mysqld 5.7.32) starting as process 81 ... 2020-12-06T15:02:35.995048Z 0 [Note] InnoDB: PUNCH HOLE support available 2020-12-06T15:02:35.995066Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2020-12-06T15:02:35.995070Z 0 [Note] InnoDB: Uses event mutexes 2020-12-06T15:02:35.995075Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2020-12-06T15:02:35.995078Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2020-12-06T15:02:35.995082Z 0 [Note] InnoDB: Using Linux native AIO 2020-12-06T15:02:35.995320Z 0 [Note] InnoDB: Number of pools: 1 2020-12-06T15:02:35.995429Z 0 [Note] InnoDB: Using CPU crc32 instructions 2020-12-06T15:02:35.996774Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2020-12-06T15:02:36.007354Z 0 [Note] InnoDB: Completed initialization of buffer pool 2020-12-06T15:02:36.009348Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2020-12-06T15:02:36.021712Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2020-12-06T15:02:36.153467Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2020-12-06T15:02:36.153634Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2020-12-06T15:02:37.782713Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 2020-12-06T15:02:37.784862Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2020-12-06T15:02:37.784894Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2020-12-06T15:02:37.785844Z 0 [Note] InnoDB: 5.7.32 started; log sequence number 2748463 2020-12-06T15:02:37.786241Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 2020-12-06T15:02:37.786817Z 0 [Note] Plugin 'FEDERATED' is disabled. 2020-12-06T15:02:37.790246Z 0 [Note] InnoDB: Buffer pool(s) load completed at 201206 23:02:37 2020-12-06T15:02:37.803947Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them. 2020-12-06T15:02:37.803988Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory. 2020-12-06T15:02:37.806098Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-06T15:02:37.806183Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory. 2020-12-06T15:02:37.950541Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. 2020-12-06T15:02:37.973492Z 0 [Note] Event Scheduler: Loaded 0 events 2020-12-06T15:02:37.974048Z 0 [Note] mysqld: ready for connections. Version: '5.7.32' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL) 2020-12-06 23:02:38+08:00 [Note] [Entrypoint]: Temporary server started. Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it. Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it. 2020-12-06 23:02:54+08:00 [Note] [Entrypoint]: Creating database stock_data 2020-12-06 23:02:54+08:00 [Note] [Entrypoint]: Stopping temporary server 2020-12-06T15:02:54.825959Z 0 [Note] Giving 0 client threads a chance to die gracefully 2020-12-06T15:02:54.825987Z 0 [Note] Shutting down slave threads 2020-12-06T15:02:54.825992Z 0 [Note] Forcefully disconnecting 0 remaining clients 2020-12-06T15:02:54.825998Z 0 [Note] Event Scheduler: Purging the queue. 0 events 2020-12-06T15:02:54.826139Z 0 [Note] Binlog end 2020-12-06T15:02:54.826658Z 0 [Note] Shutting down plugin 'ngram' 2020-12-06T15:02:54.826669Z 0 [Note] Shutting down plugin 'partition' 2020-12-06T15:02:54.826673Z 0 [Note] Shutting down plugin 'BLACKHOLE' 2020-12-06T15:02:54.826677Z 0 [Note] Shutting down plugin 'ARCHIVE' 2020-12-06T15:02:54.826682Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA' 2020-12-06T15:02:54.826714Z 0 [Note] Shutting down plugin 'MRG_MYISAM' 2020-12-06T15:02:54.826723Z 0 [Note] Shutting down plugin 'MyISAM' 2020-12-06T15:02:54.826732Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL' 2020-12-06T15:02:54.826737Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES' 2020-12-06T15:02:54.826741Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES' 2020-12-06T15:02:54.826746Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS' 2020-12-06T15:02:54.826749Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN' 2020-12-06T15:02:54.826786Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS' 2020-12-06T15:02:54.826791Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS' 2020-12-06T15:02:54.826795Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES' 2020-12-06T15:02:54.826800Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS' 2020-12-06T15:02:54.826804Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES' 2020-12-06T15:02:54.826808Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE' 2020-12-06T15:02:54.826811Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE' 2020-12-06T15:02:54.826813Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG' 2020-12-06T15:02:54.826816Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED' 2020-12-06T15:02:54.826819Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED' 2020-12-06T15:02:54.826822Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD' 2020-12-06T15:02:54.826825Z 0 [Note] Shutting down plugin 'INNODB_METRICS' 2020-12-06T15:02:54.826828Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO' 2020-12-06T15:02:54.826831Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS' 2020-12-06T15:02:54.826834Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU' 2020-12-06T15:02:54.826836Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE' 2020-12-06T15:02:54.826839Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET' 2020-12-06T15:02:54.826842Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX' 2020-12-06T15:02:54.826845Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET' 2020-12-06T15:02:54.826848Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM' 2020-12-06T15:02:54.826851Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET' 2020-12-06T15:02:54.826854Z 0 [Note] Shutting down plugin 'INNODB_CMP' 2020-12-06T15:02:54.826857Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS' 2020-12-06T15:02:54.826859Z 0 [Note] Shutting down plugin 'INNODB_LOCKS' 2020-12-06T15:02:54.826862Z 0 [Note] Shutting down plugin 'INNODB_TRX' 2020-12-06T15:02:54.826910Z 0 [Note] Shutting down plugin 'InnoDB' 2020-12-06T15:02:54.826953Z 0 [Note] InnoDB: FTS optimize thread exiting. 2020-12-06T15:02:54.827046Z 0 [Note] InnoDB: Starting shutdown... 2020-12-06T15:02:54.927339Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool 2020-12-06T15:02:54.993267Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 201206 23:02:54 2020-12-06T15:02:57.668186Z 0 [Note] InnoDB: Shutdown completed; log sequence number 12619636 2020-12-06T15:02:57.673193Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1" 2020-12-06T15:02:57.673260Z 0 [Note] Shutting down plugin 'MEMORY' 2020-12-06T15:02:57.673278Z 0 [Note] Shutting down plugin 'CSV' 2020-12-06T15:02:57.673291Z 0 [Note] Shutting down plugin 'sha256_password' 2020-12-06T15:02:57.673302Z 0 [Note] Shutting down plugin 'mysql_native_password' 2020-12-06T15:02:57.673866Z 0 [Note] Shutting down plugin 'binlog' 2020-12-06T15:02:57.677294Z 0 [Note] mysqld: Shutdown complete 2020-12-06 23:02:57+08:00 [Note] [Entrypoint]: Temporary server stopped 2020-12-06 23:02:57+08:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up. 2020-12-06T15:02:58.038797Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-12-06T15:02:58.040198Z 0 [Note] mysqld (mysqld 5.7.32) starting as process 1 ... 2020-12-06T15:02:58.043137Z 0 [Note] InnoDB: PUNCH HOLE support available 2020-12-06T15:02:58.043152Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2020-12-06T15:02:58.043155Z 0 [Note] InnoDB: Uses event mutexes 2020-12-06T15:02:58.043158Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2020-12-06T15:02:58.043161Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11 2020-12-06T15:02:58.043163Z 0 [Note] InnoDB: Using Linux native AIO 2020-12-06T15:02:58.043392Z 0 [Note] InnoDB: Number of pools: 1 2020-12-06T15:02:58.043486Z 0 [Note] InnoDB: Using CPU crc32 instructions 2020-12-06T15:02:58.044796Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2020-12-06T15:02:58.055082Z 0 [Note] InnoDB: Completed initialization of buffer pool 2020-12-06T15:02:58.057154Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2020-12-06T15:02:58.068627Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2020-12-06T15:02:58.191412Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2020-12-06T15:02:58.191760Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2020-12-06T15:02:58.636078Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 2020-12-06T15:02:58.638452Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2020-12-06T15:02:58.638497Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2020-12-06T15:02:58.639548Z 0 [Note] InnoDB: Waiting for purge to start 2020-12-06T15:02:58.689910Z 0 [Note] InnoDB: 5.7.32 started; log sequence number 12619636 2020-12-06T15:02:58.690442Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool 2020-12-06T15:02:58.691219Z 0 [Note] Plugin 'FEDERATED' is disabled. 2020-12-06T15:02:58.702492Z 0 [Note] InnoDB: Buffer pool(s) load completed at 201206 23:02:58 2020-12-06T15:02:58.715442Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them. 2020-12-06T15:02:58.715904Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory. 2020-12-06T15:02:58.717982Z 0 [Warning] CA certificate ca.pem is self signed. 2020-12-06T15:02:58.718051Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory. 2020-12-06T15:02:58.719045Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 2020-12-06T15:02:58.720034Z 0 [Note] IPv6 is available. 2020-12-06T15:02:58.720077Z 0 [Note] - '::' resolves to '::'; 2020-12-06T15:02:58.720116Z 0 [Note] Server socket created on IP: '::'. 2020-12-06T15:02:59.182050Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory. 2020-12-06T15:02:59.217680Z 0 [Note] Event Scheduler: Loaded 0 events 2020-12-06T15:02:59.218060Z 0 [Note] mysqld: ready for connections. Version: '5.7.32' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL) ``` 要是第一次启动,没有数据,启动会比较慢,需要1分多钟,为了保险,定2分钟后执行初始化任务。 ## 2,初始化数据接口被停用 ``` 本接口即将停止更新,请尽快使用Pro版接口:https://tushare.pro/document/2 error : HTTP Error 404: Not Found ``` ================================================ FILE: backend/docs/git-push-tag.md ================================================ ## 创建 tag 并发布到 github 上 git tag -a v2.0 -m "v2.0" git push origin --tags ================================================ FILE: backend/jobs/18h_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import sys import time import pandas as pd import numpy as np from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import datetime import akshare as ak import traceback import MySQLdb # 600开头的股票是上证A股,属于大盘股 # 600开头的股票是上证A股,属于大盘股,其中6006开头的股票是最早上市的股票, # 6016开头的股票为大盘蓝筹股;900开头的股票是上证B股; # 000开头的股票是深证A股,001、002开头的股票也都属于深证A股, # 其中002开头的股票是深证A股中小企业股票; # 200开头的股票是深证B股; # 300开头的股票是创业板股票;400开头的股票是三板市场股票。 def stock_a(code): # print(code) # print(type(code)) # 上证A股 # 深证A股 if code.startswith('600') or code.startswith('6006') or code.startswith('601') or code.startswith('000') or code.startswith('001') or code.startswith('002'): return True else: return False # 过滤掉 st 股票。 def stock_a_filter_st(name): # print(code) # print(type(code)) # 上证A股 # 深证A股 if name.find("ST") == -1: return True else: return False # 过滤价格,如果没有基本上是退市了。 def stock_a_filter_price(latest_price): # float 在 pandas 里面判断 空。 if np.isnan(latest_price): return False else: return True ####### 3.pdf 方法。宏观经济数据 # 接口全部有错误。只专注股票数据。 def stat_all(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 股票列表 try: data = ak.stock_zh_a_spot_em() # print(data.index) # 解决ESP 小数问题。 # data["esp"] = data["esp"].round(2) # 数据保留2位小数 data.columns = ['index', 'code', 'name', 'last_price', 'change_percent', 'change_amount', 'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed', 'change_5min', 'change_ercent_60day','ytd_change_percent'] data = data.loc[data["code"].apply(stock_a)].loc[data["name"].apply(stock_a_filter_st)].loc[ data["last_price"].apply(stock_a_filter_price)] print(data) data['date'] = datetime_int # 修改时间成为int类型。 # 删除老数据。 del_sql = " DELETE FROM `stock_zh_a_spot_em` where `date` = '%s' " % datetime_int common.insert(del_sql) data.set_index('code', inplace=True) data.drop('index', axis=1, inplace=True) print(data) # 删除index,然后和原始数据合并。 common.insert_db(data, "stock_zh_a_spot_em", True, "`date`,`code`") except Exception as e: print("error :", e) traceback.print_exc() # 龙虎榜-个股上榜统计 # 接口: stock_lhb_ggtj_sina # # 目标地址: http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/ggtj/index.phtml # # 描述: 获取新浪财经-龙虎榜-个股上榜统计 # try: stock_lhb_ggtj_sina = ak.stock_lhb_ggtj_sina(symbol="5") print(stock_lhb_ggtj_sina) stock_lhb_ggtj_sina.columns = ['code', 'name', 'ranking_times', 'sum_buy', 'sum_sell', 'net_amount', 'buy_seat', 'sell_seat'] stock_lhb_ggtj_sina = stock_lhb_ggtj_sina.loc[stock_lhb_ggtj_sina["code"].apply(stock_a)].loc[ stock_lhb_ggtj_sina["name"].apply(stock_a_filter_st)] stock_lhb_ggtj_sina.set_index('code', inplace=True) # data_sina_lhb.drop('index', axis=1, inplace=True) # 删除老数据。 stock_lhb_ggtj_sina['date'] = datetime_int # 修改时间成为int类型。 # 删除老数据。 del_sql = " DELETE FROM `stock_lhb_ggtj_sina` where `date` = '%s' " % datetime_int common.insert(del_sql) common.insert_db(stock_lhb_ggtj_sina, "stock_lhb_ggtj_sina", True, "`date`,`code`") except Exception as e: print("error :", e) traceback.print_exc() # 每日统计 # 接口: stock_dzjy_mrtj # # 目标地址: http://data.eastmoney.com/dzjy/dzjy_mrtj.aspx # # 描述: 获取东方财富网-数据中心-大宗交易-每日统计 # https://akshare.akfamily.xyz/data/stock/stock.html#id318 # import akshare as ak # stock_dzjy_mrtj_df = ak.stock_dzjy_mrtj(start_date='20220105', end_date='20220105') # print(stock_dzjy_mrtj_df) try: print("################ tmp_datetime : " + datetime_int) # 格式要 int类型日期 stock_dzjy_mrtj = ak.stock_dzjy_mrtj(start_date=datetime_int, end_date=datetime_int) print(stock_dzjy_mrtj) stock_dzjy_mrtj.columns = ['index', 'trade_date', 'code', 'name', 'quote_change', 'close_price', 'average_price', 'overflow_rate', 'trade_number', 'sum_volume', 'sum_turnover', 'turnover_market_rate'] stock_dzjy_mrtj.set_index('code', inplace=True) # data_sina_lhb.drop('index', axis=1, inplace=True) # 删除老数据。 stock_dzjy_mrtj['date'] = datetime_int # 修改时间成为int类型。 stock_dzjy_mrtj.drop('trade_date', axis=1, inplace=True) stock_dzjy_mrtj.drop('index', axis=1, inplace=True) # 数据保留2位小数 try: stock_dzjy_mrtj = stock_dzjy_mrtj.loc[stock_dzjy_mrtj["code"].apply(stock_a)].loc[ stock_dzjy_mrtj["name"].apply(stock_a_filter_st)] stock_dzjy_mrtj["average_price"] = stock_dzjy_mrtj["average_price"].round(2) stock_dzjy_mrtj["overflow_rate"] = stock_dzjy_mrtj["overflow_rate"].round(4) stock_dzjy_mrtj["turnover_market_rate"] = stock_dzjy_mrtj["turnover_market_rate"].round(6) except Exception as e: print("round error :", e) traceback.print_exc() # 删除老数据。 del_sql = " DELETE FROM `stock_dzjy_mrtj` where `date` = '%s' " % datetime_int common.insert(del_sql) print(stock_dzjy_mrtj) common.insert_db(stock_dzjy_mrtj, "stock_dzjy_mrtj", True, "`date`,`code`") except Exception as e: print("error :", e) traceback.print_exc() # main函数入口 if __name__ == '__main__': # 执行数据初始化。 # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all) ================================================ FILE: backend/jobs/README.txt ================================================ 1,计算每日买全部推荐买。 2,计算每日全部推荐卖数据。 3,设置个人账号,设置购买和卖的数据。进行关联查询。 4,最重要的沪深300,中正500数据。进行大盘股分析。 ================================================ FILE: backend/jobs/aps_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- from pytz import utc from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from apscheduler.schedulers.blocking import BlockingScheduler from apscheduler.executors.pool import ProcessPoolExecutor import libs.common as common # doc : http://apscheduler.readthedocs.io/en/latest/modules/jobstores/sqlalchemy.html jobstores = { 'default': SQLAlchemyJobStore(url=common.MYSQL_CONN_URL, tablename='apscheduler_jobs') } executors = { 'default': {'type': 'threadpool', 'max_workers': 20}, 'processpool': ProcessPoolExecutor(max_workers=5) } job_defaults = { 'coalesce': False, 'max_instances': 3 } scheduler = BlockingScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc) scheduler.start() print("start ...") ================================================ FILE: backend/jobs/basic_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import MySQLdb # 创建新数据库。 def create_new_database(): with MySQLdb.connect(common.MYSQL_HOST, common.MYSQL_USER, common.MYSQL_PWD, "mysql", charset="utf8") as db: try: create_sql = " CREATE DATABASE IF NOT EXISTS %s CHARACTER SET utf8 COLLATE utf8_general_ci " % common.MYSQL_DB print(create_sql) db.autocommit(on=True) db.cursor().execute(create_sql) except Exception as e: print("error CREATE DATABASE :", e) # main函数入口 if __name__ == '__main__': # 检查,如果执行 select 1 失败,说明数据库不存在,然后创建一个新的数据库。 try: with MySQLdb.connect(common.MYSQL_HOST, common.MYSQL_USER, common.MYSQL_PWD, common.MYSQL_DB, charset="utf8") as db: db.autocommit(on=True) db.cursor().execute(" select 1 ") print("########### db exists ###########") except Exception as e: print("check MYSQL_DB error and create new one :", e) # 检查数据库失败, create_new_database() # 执行数据初始化。 ================================================ FILE: backend/jobs/cron.daily/run_daily ================================================ #!/bin/sh mkdir -p /data/logs DATETIME=`date +%Y-%m-%d:%H:%M:%S` DATE=`date +%Y-%m-%d` export PYTHONIOENCODING=utf-8 export LANG=zh_CN.UTF-8 export PYTHONPATH=/data/stock export LC_CTYPE=zh_CN.UTF-8 echo "###################"$DATETIME"###################" >> /data/logs/daily.${DATE}.log #增加获得今日全部数据和大盘数据 /usr/local/bin/python3 /data/stock/jobs/18h_daily_job.py >> /data/logs/daily.${DATE}.log echo "###################"$DATETIME"###################" >> /data/logs/daily.${DATE}.log #使用股票指标预测。 /usr/local/bin/python3 /data/stock/jobs/guess_indicators_daily_job.py >> /data/logs/daily.${DATE}.log /usr/local/bin/python3 /data/stock/jobs/guess_indicators_daily_buy_job.py >> /data/logs/daily.${DATE}.log #清除前3天数据。 DATE_20=`date -d '-20 days' +%Y-%m-%d` MONTH_20=`date -d '-20 days' +%Y-%m` echo "rm -f /data/cache/hist_data_cache/${MONTH_20}/${DATETIME_20}" rm -f /data/cache/hist_data_cache/${MONTH_20}/${DATETIME_20} ================================================ FILE: backend/jobs/cron.hourly/run_hourly ================================================ #!/bin/sh mkdir -p /data/logs DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE >> /data/logs/hourly.log ================================================ FILE: backend/jobs/cron.minutely/run_1minute ================================================ #!/bin/bash mkdir -p /data/logs DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE >> /data/logs/1min.log ================================================ FILE: backend/jobs/cron.monthly/run_monthly ================================================ #!/bin/sh mkdir -p /data/logs DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE >> /data/logs/monthly.log ================================================ FILE: backend/jobs/crontab ================================================ SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin */1 * * * * /bin/run-parts /etc/cron.minutely 10 * * * * /bin/run-parts /etc/cron.hourly 30 16 * * * /bin/run-parts /etc/cron.daily 30 17 1,10,20 * * /bin/run-parts /etc/cron.monthly ================================================ FILE: backend/jobs/daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import sys import os import time import pandas as pd import tushare as ts from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import datetime import shutil ####### 使用 5.pdf,先做 基本面数据 的数据,然后在做交易数据。 # def stat_all(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") cache_dir = common.bash_stock_tmp % (datetime_str[0:7], datetime_str) if os.path.exists(cache_dir): shutil.rmtree(cache_dir) print("remove cache dir force :", cache_dir) print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) data = ts.top_list(datetime_str) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 # if not data is None and len(data) > 0: # 插入数据库。 # del data["reason"] data["date"] = datetime_int # 修改时间成为int类型。 data = data.drop_duplicates(subset="code", keep="last") data.head(n=1) common.insert_db(data, "ts_top_list", False, "`date`,`code`") else: print("no data .") print(datetime_str) # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all) ================================================ FILE: backend/jobs/guess_indicators_daily_buy_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import stockstats from sqlalchemy import text ### 对每日指标数据,进行筛选。将符合条件的。二次筛选出来。 ### 只是做简单筛选 def stat_all_lite_buy(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 查询参数 params = {"datetime": datetime_int} sql_kdjk = text(" SELECT avg(`kdjk`) as avg_kdjk FROM guess_indicators_daily ") data_kdjk = pd.read_sql(sql=sql_kdjk, con=common.engine(), params=params) kdjk = data_kdjk["avg_kdjk"][0] sql_kdjd = text(" SELECT avg(`kdjd`) as avg_kdjd FROM guess_indicators_daily ") data_kdjd = pd.read_sql(sql=sql_kdjd, con=common.engine(), params=params) kdjd = data_kdjd["avg_kdjd"][0] sql_kdjj = text(" SELECT avg(`kdjj`) as avg_kdjj FROM guess_indicators_daily ") data_kdjj = pd.read_sql(sql=sql_kdjj, con=common.engine(), params=params) kdjj = data_kdjj["avg_kdjj"][0] # K值在80以上,D值在70以上,J值大于90时为超买。 # J大于100时为超买,小于10时为超卖。 # 当六日指标上升到达80时,表示股市已有超买现象 # 当CCI>﹢100 时,表明股价已经进入非常态区间——超买区间,股价的异动现象应多加关注。 params_1 = {"datetime": datetime_int, "kdjk": kdjk, "kdjd": kdjd, "kdjj": kdjj} sql_1 = text(""" SELECT `date`,`code`,`name`,`last_price`,`change_percent`,`change_amount`,`volume`,`turnover`, `amplitude`,`high`,`low`,`open`,`closed`,`volume_ratio`,`turnover_rate`, `pe_ratio`,`pb_ratio`,`market_cap`,`circulating_market_cap`,`rise_speed`, `change_5min`,`change_ercent_60day`,`ytd_change_percent`, `boll`, `boll_lb`, `boll_ub`, `kdjd`, `kdjj`, `kdjk`, `macd`, `macdh`, `macds`, `pdi`,`trix`, `trix_9_sma`, `vr`, `vr_6_sma`, `wr_10`, `wr_6` FROM stock_data.guess_indicators_daily WHERE `date` = :datetime and kdjk >= :kdjk and kdjd >= :kdjd and kdjj >= :kdjj """) # and kdjj > 100 and rsi_6 > 80 and cci > 100 # 调整参数,提前获得股票增长。 try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_lite_buy_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) print(f"sql_1 : {sql_1}") data = pd.read_sql(sql=sql_1, con=common.engine(), params=params_1) data = data.drop_duplicates(subset="code", keep="last") print("######## stat_all_lite_buy len data ########:", len(data)) try: common.insert_db(data, "guess_indicators_lite_buy_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # main函数入口 if __name__ == '__main__': # 使用方法传递。 # 二次筛选数据。直接计算买卖股票数据。 tmp_datetime = common.run_with_args(stat_all_lite_buy) ================================================ FILE: backend/jobs/guess_indicators_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import stockstats # 批处理数据。 def stat_all_batch(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) try: # 删除老数据。 del_sql = " DELETE FROM `guess_indicators_daily` WHERE `date`= %s " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) sql_count = """ SELECT count(1) FROM stock_zh_a_spot_em WHERE `date` = %s and `open` > 0 """ # 修改逻辑,增加中小板块计算。 中小板:002,创业板:300 。已经是经过筛选的数据了。 count = common.select_count(sql_count, params=[datetime_int]) print("count :", count) batch_size = 100 end = int(math.ceil(float(count) / batch_size) * batch_size) print(end) for i in range(0, end, batch_size): print("loop :", i) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`,`code`,`name`,`last_price`,`change_percent`,`change_amount`,`volume`,`turnover`, `amplitude`,`high`,`low`,`open`,`closed`,`volume_ratio`,`turnover_rate`, `pe_ratio`,`pb_ratio`,`market_cap`,`circulating_market_cap`,`rise_speed`, `change_5min`,`change_ercent_60day`,`ytd_change_percent` FROM stock_zh_a_spot_em WHERE `date` = %s and `open` > 0 limit %s , %s """ sql_2 = sql_1 % (datetime_int, i, batch_size) print(sql_2) # data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%', i, batch_size]) data = pd.read_sql(sql=sql_2, con=common.engine()) data = data.drop_duplicates(subset="code", keep="last") print("########data[last_price]########:", len(data)) stat_index_all(data, i) # 分批执行。 def stat_index_all(data, idx): # print(data["last_price"]) # 1), n天涨跌百分百计算 # open price change (in percent) between today and the day before yesterday ‘r’ stands for rate. # stock[‘close_-2_r’] # 可以看到,-n天数据和今天数据的百分比。 # 2), CR指标 # http://wiki.mbalib.com/wiki/CR%E6%8C%87%E6%A0%87 价格动量指标 # CR跌穿a、b、c、d四条线,再由低点向上爬升160时,为短线获利的一个良机,应适当卖出股票。 # CR跌至40以下时,是建仓良机。而CR高于300~400时,应注意适当减仓。 # 3), KDJ指标 # http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 # 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、 # 最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV, # 然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 # (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度, # 以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 # 4), MACD指标 # http://wiki.mbalib.com/wiki/MACD # 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 # MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 # 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 # 5), BOLL指标 # http://wiki.mbalib.com/wiki/BOLL # 布林线指标(Bollinger Bands) # 6), RSI指标 # http://wiki.mbalib.com/wiki/RSI # 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 # (2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 # (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象, # 如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 # 7), W%R指标 # http://wiki.mbalib.com/wiki/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A0%87 # 威廉指数(Williams%Rate)该指数是利用摆动点来度量市场的超买超卖现象。 # 8), CCI指标 # http://wiki.mbalib.com/wiki/%E9%A1%BA%E5%8A%BF%E6%8C%87%E6%A0%87 # 顺势指标又叫CCI指标,其英文全称为“Commodity Channel Index”, # 是由美国股市分析家唐纳德·蓝伯特(Donald Lambert)所创造的,是一种重点研判股价偏离度的股市分析工具。 # 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, # 中短线应及时买入,如果有比较大的成交量配合,买入信号则更为可靠。 #   2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, # 将进入一个比较长的寻底过程,投资者应以持币观望为主。 # CCI, default to 14 days # 9), TR、ATR指标 # http://wiki.mbalib.com/wiki/%E5%9D%87%E5%B9%85%E6%8C%87%E6%A0%87 # 均幅指标(Average True Ranger,ATR) # 均幅指标(ATR)是取一定时间周期内的股价波动幅度的移动平均值,主要用于研判买卖时机。 # 10), DMA指标 # http://wiki.mbalib.com/wiki/DMA #  DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。 # DMA, difference of 10 and 50 moving average # stock[‘dma’] # 11), DMI,+DI,-DI,DX,ADX,ADXR指标 # http://wiki.mbalib.com/wiki/DMI # 动向指数Directional Movement Index,DMI) # http://wiki.mbalib.com/wiki/ADX # 平均趋向指标(Average Directional Indicator,简称ADX) # http://wiki.mbalib.com/wiki/%E5%B9%B3%E5%9D%87%E6%96%B9%E5%90%91%E6%8C%87%E6%95%B0%E8%AF%84%E4%BC%B0 # 平均方向指数评估(ADXR)实际是今日ADX与前面某一日的ADX的平均值。ADXR在高位与ADX同步下滑,可以增加对ADX已经调头的尽早确认。 # ADXR是ADX的附属产品,只能发出一种辅助和肯定的讯号,并非入市的指标,而只需同时配合动向指标(DMI)的趋势才可作出买卖策略。 # 在应用时,应以ADX为主,ADXR为辅。 # 12), TRIX,MATRIX指标 # http://wiki.mbalib.com/wiki/TRIX # TRIX指标又叫三重指数平滑移动平均指标(Triple Exponentially Smoothed Average) # 13), VR,MAVR指标 # http://wiki.mbalib.com/wiki/%E6%88%90%E4%BA%A4%E9%87%8F%E6%AF%94%E7%8E%87 # 成交量比率(Volumn Ratio,VR)(简称VR),是一项通过分析股价上升日成交额(或成交量,下同)与股价下降日成交额比值, # 从而掌握市场买卖气势的中期技术指标。 #stock_column = ['adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', # 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', # 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi', # 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] stock_column = ['date','code', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] # code cr cr-ma1 cr-ma2 cr-ma3 date data_new = concat_guess_data(stock_column, data) data_new = data_new.round(2) # 数据保留2位小数 # print(data_new.head()) print("########insert db guess_indicators_daily idx :########:", idx) try: common.insert_db(data_new, "guess_indicators_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # 链接guess 数据。 def concat_guess_data(stock_column, data): # 使用 trade 填充数据 print("stock_column:", stock_column) tmp_dic = {} # 循环增加临时数据。如果要是date,和code, for col in stock_column: if col == 'date': tmp_dic[col] = data["date"] elif col == 'code': tmp_dic[col] = data["code"] else: tmp_dic[col] = data["last_price"] # print("##########tmp_dic: ", tmp_dic) print("########################## BEGIN ##########################") stock_guess = pd.DataFrame(tmp_dic, index=data.index.values) print(stock_guess.columns.values) # print(stock_guess.head()) stock_guess = stock_guess.apply(apply_guess, stock_column=stock_column, axis=1) # , axis=1) print(stock_guess.head()) # stock_guess.astype('float32', copy=False) stock_guess.drop('date', axis=1, inplace=True) # 删除日期字段,然后和原始数据合并。 # print(stock_guess["5d"]) data_new = pd.merge(data, stock_guess, on=['code'], how='left') print("#############") return data_new # 带参数透传。 def apply_guess(tmp, stock_column): # print("apply_guess columns args:", stock_column) # print("apply_guess data :", type(tmp)) date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-100)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") # print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 设置返回数组。 stock_data_list = [] stock_name_list = [] print(f"stock_column : {stock_column}") # 增加空判断,如果是空返回 0 数据。 if stock is None: for col in stock_column: if col == 'date': stock_data_list.append(date) stock_name_list.append('date') elif col == 'code': stock_data_list.append(code) stock_name_list.append('code') else: stock_data_list.append(0) stock_name_list.append(col) return pd.Series(stock_data_list, index=stock_name_list) # print(stock.head()) # open high close low volume # stock = pd.DataFrame({"close": stock["close"]}, index=stock.index.values) # stock = stock.sort_index(0) # 将数据按照日期排序下。 stock["date"] = stock.index.values # 增加日期列。 print(f"stock: {stock}") # stock = stock.sort_index(0) # 将数据按照日期排序下。 # print(stock) [186 rows x 14 columns] # 初始化统计类 # stockStat = stockstats.StockDataFrame.retype(pd.read_csv('002032.csv')) stockStat = stockstats.StockDataFrame.retype(stock) print(f"stockStat : {stockStat}") print("########################## print result ##########################") for col in stock_column: if col == 'date': stock_data_list.append(date) stock_name_list.append('date') elif col == 'code': stock_data_list.append(code) stock_name_list.append('code') else: # 将数据的最后一个返回。 print(col) print(stockStat[col]) print(stockStat[col].values[1]) #print(stockStat[col].head(1)) tmp_val = stockStat[col].values[1] if np.isinf(tmp_val): # 解决值中存在INF问题。 tmp_val = 0 if np.isnan(tmp_val): # 解决值中存在NaN问题。 tmp_val = 0 # print("col name : ", col, tmp_val) stock_data_list.append(tmp_val) stock_name_list.append(col) # print(stock_data_list) return pd.Series(stock_data_list, index=stock_name_list) # print(stock["mov_vol"].tail()) # print(stock["return"].tail()) # print("stock[10d].tail(1)", stock["10d"].tail(1).values[0]) # 10d 20d 5-10d 5-20d 5d 60d code date mov_vol return # tmp = list([stock["10d"].tail(1).values[0], stock["20d"].tail(1).values[0], stock["5-10d"].tail(1).values[0], # stock["5-20d"].tail(1).values[0], stock["5d"].tail(1).values[0], stock["60d"].tail(1).values[0], # code, date, stock["mov_vol"].tail(1).values[0], stock["return"].tail(1).values[0]]) # # print(tmp) # return tmp # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all_batch) # 二次筛选数据。直接计算买卖股票数据。 ================================================ FILE: backend/jobs/guess_indicators_daily_sell_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import stockstats from sqlalchemy import text # 设置卖出数据。 def stat_all_lite_sell(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 超卖区:K值在20以下,D值在30以下为超卖区。一般情况下,股价有可能上涨,反弹的可能性增大。局内人不应轻易抛出股票,局外人可寻机入场。 # J大于100时为超买,小于10时为超卖。 # 当六日强弱指标下降至20时,表示股市有超卖现象 # 当CCI<﹣100时,表明股价已经进入另一个非常态区间——超卖区间,投资者可以逢低吸纳股票。 sql_1 = text(""" SELECT `date`,`code`,`name`,`last_price`,`change_percent`,`change_amount`,`volume`,`turnover`, `amplitude`,`high`,`low`,`open`,`closed`,`volume_ratio`,`turnover_rate`, `pe_ratio`,`pb_ratio`,`market_cap`,`circulating_market_cap`,`rise_speed`, `change_5min`,`change_ercent_60day`,`ytd_change_percent`, `boll`, `boll_lb`, `boll_ub`, `kdjd`, `kdjj`, `kdjk`, `macd`, `macdh`, `macds`, `pdi`,`trix`, `trix_9_sma`, `vr`, `vr_6_sma`, `wr_10`, `wr_6` FROM stock_data.guess_indicators_daily WHERE `date` = :datetime and kdjk <= 20 and kdjd <= 30 and kdjj <= 10 """) try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_lite_sell_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) # 查询参数 params = {"datetime": datetime_int} print(sql_1) data = pd.read_sql(sql=sql_1, con=common.engine(), params=params) data = data.drop_duplicates(subset="code", keep="last") print("######## stat_all_lite_sell len data ########:", len(data)) try: common.insert_db(data, "guess_indicators_lite_sell_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # main函数入口 if __name__ == '__main__': # 使用方法传递。 # 二次筛选数据。直接计算买卖股票数据。 tmp_datetime = common.run_with_args(stat_all_lite_sell) ================================================ FILE: backend/jobs/guess_rsrs_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import stockstats ### 对每日指标数据,进行筛选。将符合条件的。二次筛选出来。 ### 只是做简单筛选 def stat_all_lite_buy(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # K值在80以上,D值在70以上,J值大于90时为超买。 # J大于100时为超买,小于10时为超卖。 # 当六日指标上升到达80时,表示股市已有超买现象 # 当CCI>﹢100 时,表明股价已经进入非常态区间——超买区间,股价的异动现象应多加关注。 sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` ,`kdjj`,`rsi_6`,`cci` FROM stock_data.guess_indicators_daily WHERE `date` = %s and kdjk >= 80 and kdjd >= 70 and kdjj >= 100 and rsi_6 >= 80 and cci >= 100 """ # and kdjj > 100 and rsi_6 > 80 and cci > 100 # 调整参数,提前获得股票增长。 try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_lite_buy_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int]) data = data.drop_duplicates(subset="code", keep="last") print("######## len data ########:", len(data)) try: common.insert_db(data, "guess_indicators_lite_buy_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # 设置卖出数据。 def stat_all_lite_sell(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 超卖区:K值在20以下,D值在30以下为超卖区。一般情况下,股价有可能上涨,反弹的可能性增大。局内人不应轻易抛出股票,局外人可寻机入场。 # J大于100时为超买,小于10时为超卖。 # 当六日强弱指标下降至20时,表示股市有超卖现象 # 当CCI<﹣100时,表明股价已经进入另一个非常态区间——超卖区间,投资者可以逢低吸纳股票。 sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` ,`kdjj`,`rsi_6`,`cci` FROM stock_data.guess_indicators_daily WHERE `date` = %s and kdjk <= 20 and kdjd <= 30 and kdjj <= 10 and rsi_6 <= 20 and cci <= -100 """ try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_lite_sell_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int]) data = data.drop_duplicates(subset="code", keep="last") print("######## len data ########:", len(data)) try: common.insert_db(data, "guess_indicators_lite_sell_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # 批处理数据。 def stat_all_batch(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_daily` WHERE `date`= %s " % datetime_int common.insert(del_sql) except Exception as e: print("error :", e) sql_count = """ SELECT count(1) FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `name` not like %s """ # 修改逻辑,增加中小板块计算。 中小板:002,创业板:300 。and `code` not like %s and `code` not like %s and `name` not like %s # count = common.select_count(sql_count, params=[datetime_int, '002%', '300%', '%st%']) count = common.select_count(sql_count, params=[datetime_int, '300%', '%st%']) print("count :", count) batch_size = 100 end = int(math.ceil(float(count) / batch_size) * batch_size) print(end) for i in range(0, end, batch_size): print("loop :", i) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `name` not like %s limit %s , %s """ print(sql_1) # data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%', i, batch_size]) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '300%', '%st%', i, batch_size]) data = data.drop_duplicates(subset="code", keep="last") print("########data[trade]########:", len(data)) stat_index_all(data, i) # 分批执行。 def stat_index_all(data, idx): # print(data["trade"]) # 1), n天涨跌百分百计算 # open price change (in percent) between today and the day before yesterday ‘r’ stands for rate. # stock[‘close_-2_r’] # 可以看到,-n天数据和今天数据的百分比。 # 2), CR指标 # http://wiki.mbalib.com/wiki/CR%E6%8C%87%E6%A0%87 价格动量指标 # CR跌穿a、b、c、d四条线,再由低点向上爬升160时,为短线获利的一个良机,应适当卖出股票。 # CR跌至40以下时,是建仓良机。而CR高于300~400时,应注意适当减仓。 # 3), KDJ指标 # http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 # 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、 # 最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV, # 然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 # (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度, # 以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 # 4), MACD指标 # http://wiki.mbalib.com/wiki/MACD # 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 # MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 # 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 # 5), BOLL指标 # http://wiki.mbalib.com/wiki/BOLL # 布林线指标(Bollinger Bands) # 6), RSI指标 # http://wiki.mbalib.com/wiki/RSI # 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 # (2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 # (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象, # 如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 # 7), W%R指标 # http://wiki.mbalib.com/wiki/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A0%87 # 威廉指数(Williams%Rate)该指数是利用摆动点来度量市场的超买超卖现象。 # 8), CCI指标 # http://wiki.mbalib.com/wiki/%E9%A1%BA%E5%8A%BF%E6%8C%87%E6%A0%87 # 顺势指标又叫CCI指标,其英文全称为“Commodity Channel Index”, # 是由美国股市分析家唐纳德·蓝伯特(Donald Lambert)所创造的,是一种重点研判股价偏离度的股市分析工具。 # 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, # 中短线应及时买入,如果有比较大的成交量配合,买入信号则更为可靠。 #   2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, # 将进入一个比较长的寻底过程,投资者应以持币观望为主。 # CCI, default to 14 days # 9), TR、ATR指标 # http://wiki.mbalib.com/wiki/%E5%9D%87%E5%B9%85%E6%8C%87%E6%A0%87 # 均幅指标(Average True Ranger,ATR) # 均幅指标(ATR)是取一定时间周期内的股价波动幅度的移动平均值,主要用于研判买卖时机。 # 10), DMA指标 # http://wiki.mbalib.com/wiki/DMA #  DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。 # DMA, difference of 10 and 50 moving average # stock[‘dma’] # 11), DMI,+DI,-DI,DX,ADX,ADXR指标 # http://wiki.mbalib.com/wiki/DMI # 动向指数Directional Movement Index,DMI) # http://wiki.mbalib.com/wiki/ADX # 平均趋向指标(Average Directional Indicator,简称ADX) # http://wiki.mbalib.com/wiki/%E5%B9%B3%E5%9D%87%E6%96%B9%E5%90%91%E6%8C%87%E6%95%B0%E8%AF%84%E4%BC%B0 # 平均方向指数评估(ADXR)实际是今日ADX与前面某一日的ADX的平均值。ADXR在高位与ADX同步下滑,可以增加对ADX已经调头的尽早确认。 # ADXR是ADX的附属产品,只能发出一种辅助和肯定的讯号,并非入市的指标,而只需同时配合动向指标(DMI)的趋势才可作出买卖策略。 # 在应用时,应以ADX为主,ADXR为辅。 # 12), TRIX,MATRIX指标 # http://wiki.mbalib.com/wiki/TRIX # TRIX指标又叫三重指数平滑移动平均指标(Triple Exponentially Smoothed Average) # 13), VR,MAVR指标 # http://wiki.mbalib.com/wiki/%E6%88%90%E4%BA%A4%E9%87%8F%E6%AF%94%E7%8E%87 # 成交量比率(Volumn Ratio,VR)(简称VR),是一项通过分析股价上升日成交额(或成交量,下同)与股价下降日成交额比值, # 从而掌握市场买卖气势的中期技术指标。 stock_column = ['adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'mdi', 'pdi', 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] # code cr cr-ma1 cr-ma2 cr-ma3 date data_new = concat_guess_data(stock_column, data) data_new = data_new.round(2) # 数据保留2位小数 # print(data_new.head()) print("########insert db guess_indicators_daily idx :########:", idx) try: common.insert_db(data_new, "guess_indicators_daily", False, "`date`,`code`") except Exception as e: print("error :", e) # 链接guess 数据。 def concat_guess_data(stock_column, data): # 使用 trade 填充数据 print("stock_column:", stock_column) tmp_dic = {} # 循环增加临时数据。如果要是date,和code, for col in stock_column: if col == 'date': tmp_dic[col] = data["date"] elif col == 'code': tmp_dic[col] = data["code"] else: tmp_dic[col] = data["trade"] # print("##########tmp_dic: ", tmp_dic) print("########################## BEGIN ##########################") stock_guess = pd.DataFrame(tmp_dic, index=data.index.values) print(stock_guess.columns.values) # print(stock_guess.head()) stock_guess = stock_guess.apply(apply_guess, stock_column=stock_column, axis=1) # , axis=1) print(stock_guess.head()) # stock_guess.astype('float32', copy=False) stock_guess.drop('date', axis=1, inplace=True) # 删除日期字段,然后和原始数据合并。 # print(stock_guess["5d"]) data_new = pd.merge(data, stock_guess, on=['code'], how='left') print("#############") return data_new # 带参数透传。 def apply_guess(tmp, stock_column): # print("apply_guess columns args:", stock_column) # print("apply_guess data :", type(tmp)) date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-100)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") # print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 设置返回数组。 stock_data_list = [] stock_name_list = [] # 增加空判断,如果是空返回 0 数据。 if stock is None: for col in stock_column: if col == 'date': stock_data_list.append(date) stock_name_list.append('date') elif col == 'code': stock_data_list.append(code) stock_name_list.append('code') else: stock_data_list.append(0) stock_name_list.append(col) return pd.Series(stock_data_list, index=stock_name_list) # print(stock.head()) # open high close low volume # stock = pd.DataFrame({"close": stock["close"]}, index=stock.index.values) stock = stock.sort_index(0) # 将数据按照日期排序下。 stock["date"] = stock.index.values # 增加日期列。 stock = stock.sort_index(0) # 将数据按照日期排序下。 # print(stock) [186 rows x 14 columns] # 初始化统计类 # stockStat = stockstats.StockDataFrame.retype(pd.read_csv('002032.csv')) stockStat = stockstats.StockDataFrame.retype(stock) print("########################## print result ##########################") for col in stock_column: if col == 'date': stock_data_list.append(date) stock_name_list.append('date') elif col == 'code': stock_data_list.append(code) stock_name_list.append('code') else: # 将数据的最后一个返回。 tmp_val = stockStat[col].tail(1).values[0] if np.isinf(tmp_val): # 解决值中存在INF问题。 tmp_val = 0 if np.isnan(tmp_val): # 解决值中存在NaN问题。 tmp_val = 0 # print("col name : ", col, tmp_val) stock_data_list.append(tmp_val) stock_name_list.append(col) # print(stock_data_list) return pd.Series(stock_data_list, index=stock_name_list) # print(stock["mov_vol"].tail()) # print(stock["return"].tail()) # print("stock[10d].tail(1)", stock["10d"].tail(1).values[0]) # 10d 20d 5-10d 5-20d 5d 60d code date mov_vol return # tmp = list([stock["10d"].tail(1).values[0], stock["20d"].tail(1).values[0], stock["5-10d"].tail(1).values[0], # stock["5-20d"].tail(1).values[0], stock["5d"].tail(1).values[0], stock["60d"].tail(1).values[0], # code, date, stock["mov_vol"].tail(1).values[0], stock["return"].tail(1).values[0]]) # # print(tmp) # return tmp # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all_batch) # 二次筛选数据。直接计算买卖股票数据。 tmp_datetime = common.run_with_args(stat_all_lite_buy) tmp_datetime = common.run_with_args(stat_all_lite_sell) ####################### 老方法,弃用了。####################### def stat_index_all_no_use(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `code` not like %s and `name` not like %s """ print(sql_1) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%']) data = data.drop_duplicates(subset="code", keep="last") print("########data[trade]########:", len(data)) # print(data["trade"]) # 1), n天涨跌百分百计算 # open price change (in percent) between today and the day before yesterday ‘r’ stands for rate. # stock[‘close_-2_r’] # 可以看到,-n天数据和今天数据的百分比。 stock_column = ['close_-1_r', 'close_-2_r', 'code', 'date'] # close_-1_r close_-2_r code date data_new = concat_guess_data(stock_column, data) # 2), CR指标 # http://wiki.mbalib.com/wiki/CR%E6%8C%87%E6%A0%87 价格动量指标 # CR跌穿a、b、c、d四条线,再由低点向上爬升160时,为短线获利的一个良机,应适当卖出股票。 # CR跌至40以下时,是建仓良机。而CR高于300~400时,应注意适当减仓。 stock_column = ['code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date'] # code cr cr-ma1 cr-ma2 cr-ma3 date data_new = concat_guess_data(stock_column, data_new) # 3), KDJ指标 # http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 # 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、 # 最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV, # 然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 # (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度, # 以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 stock_column = ['code', 'date', 'kdjd', 'kdjj', 'kdjk'] # code date kdjd kdjj kdjk data_new = concat_guess_data(stock_column, data_new) # 4), MACD指标 # http://wiki.mbalib.com/wiki/MACD # 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 # MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 # 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 stock_column = ['code', 'date', 'macd', 'macdh', 'macds'] # code date macd macdh macds data_new = concat_guess_data(stock_column, data_new) # 5), BOLL指标 # http://wiki.mbalib.com/wiki/BOLL # 布林线指标(Bollinger Bands) stock_column = ['boll', 'boll_lb', 'boll_ub', 'code', 'date'] # boll boll_lb boll_ub code date data_new = concat_guess_data(stock_column, data_new) # 6), RSI指标 # http://wiki.mbalib.com/wiki/RSI # 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 # (2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 # (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象, # 如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 stock_column = ['code', 'date', 'rsi_12', 'rsi_6'] # code date rsi_12 rsi_6 data_new = concat_guess_data(stock_column, data_new) # 7), W%R指标 # http://wiki.mbalib.com/wiki/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A0%87 # 威廉指数(Williams%Rate)该指数是利用摆动点来度量市场的超买超卖现象。 stock_column = ['code', 'date', 'wr_10', 'wr_6'] # code date wr_10 wr_6 data_new = concat_guess_data(stock_column, data_new) # 8), CCI指标 # http://wiki.mbalib.com/wiki/%E9%A1%BA%E5%8A%BF%E6%8C%87%E6%A0%87 # 顺势指标又叫CCI指标,其英文全称为“Commodity Channel Index”, # 是由美国股市分析家唐纳德·蓝伯特(Donald Lambert)所创造的,是一种重点研判股价偏离度的股市分析工具。 # 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, # 中短线应及时买入,如果有比较大的成交量配合,买入信号则更为可靠。 #   2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, # 将进入一个比较长的寻底过程,投资者应以持币观望为主。 # CCI, default to 14 days stock_column = ['cci', 'cci_20', 'code', 'date'] # cci cci_20 code date data_new = concat_guess_data(stock_column, data_new) # 9), TR、ATR指标 # http://wiki.mbalib.com/wiki/%E5%9D%87%E5%B9%85%E6%8C%87%E6%A0%87 # 均幅指标(Average True Ranger,ATR) # 均幅指标(ATR)是取一定时间周期内的股价波动幅度的移动平均值,主要用于研判买卖时机。 stock_column = ['cci', 'cci_20', 'code', 'date'] # cci cci_20 code date data_new = concat_guess_data(stock_column, data_new) # 10), DMA指标 # http://wiki.mbalib.com/wiki/DMA #  DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。 # DMA, difference of 10 and 50 moving average # stock[‘dma’] stock_column = ['code', 'date', 'dma'] # code date dma data_new = concat_guess_data(stock_column, data_new) # 11), DMI,+DI,-DI,DX,ADX,ADXR指标 # http://wiki.mbalib.com/wiki/DMI # 动向指数Directional Movement Index,DMI) # http://wiki.mbalib.com/wiki/ADX # 平均趋向指标(Average Directional Indicator,简称ADX) # http://wiki.mbalib.com/wiki/%E5%B9%B3%E5%9D%87%E6%96%B9%E5%90%91%E6%8C%87%E6%95%B0%E8%AF%84%E4%BC%B0 # 平均方向指数评估(ADXR)实际是今日ADX与前面某一日的ADX的平均值。ADXR在高位与ADX同步下滑,可以增加对ADX已经调头的尽早确认。 # ADXR是ADX的附属产品,只能发出一种辅助和肯定的讯号,并非入市的指标,而只需同时配合动向指标(DMI)的趋势才可作出买卖策略。 # 在应用时,应以ADX为主,ADXR为辅。 stock_column = ['adx', 'adxr', 'code', 'date', 'dx', 'mdi', 'pdi'] # adx adxr code date dx mdi pdi data_new = concat_guess_data(stock_column, data_new) # 12), TRIX,MATRIX指标 # http://wiki.mbalib.com/wiki/TRIX # TRIX指标又叫三重指数平滑移动平均指标(Triple Exponentially Smoothed Average) stock_column = ['code', 'date', 'trix', 'trix_9_sma'] # code date trix trix_9_sma data_new = concat_guess_data(stock_column, data_new) # 13), VR,MAVR指标 # http://wiki.mbalib.com/wiki/%E6%88%90%E4%BA%A4%E9%87%8F%E6%AF%94%E7%8E%87 # 成交量比率(Volumn Ratio,VR)(简称VR),是一项通过分析股价上升日成交额(或成交量,下同)与股价下降日成交额比值, # 从而掌握市场买卖气势的中期技术指标。 stock_column = ['code', 'date', 'vr', 'vr_6_sma'] # code date vr vr_6_sma data_new = concat_guess_data(stock_column, data_new) data_new = data_new.round(2) # 数据保留2位小数 # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_indicators_daily` WHERE `date`= %s " % datetime_int common.insert(del_sql) # print(data_new.head()) # data_new["down_rate"] = (data_new["trade"] - data_new["wave_mean"]) / data_new["wave_base"] common.insert_db(data_new, "guess_indicators_daily", False, "`date`,`code`") # 进行左连接. # tmp = pd.merge(tmp, tmp2, on=['company_id'], how='left') ================================================ FILE: backend/jobs/quarter_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import sys import time import pandas as pd import tushare as ts from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import datetime # 增加一个新quarter列,用来存储季度信息。 def concat_quarter(year, quarter, data_array): print(len(data_array)) quarter_str = str(year) + str("%02d" % quarter) # 格式化季度数据。2位。 # 增加到列。 quarter_col = pd.DataFrame([quarter_str for _ in range(len(data_array))], columns=["quarter"]) return pd.concat([quarter_col, data_array], axis=1) #############################基本面数据 http://tushare.org/fundamental.html def stat_all(tmp_datetime): # 返回 31 天前的数据,做上个季度数据统计。 tmp_datetime_1month = tmp_datetime + datetime.timedelta(days=-31) year = int((tmp_datetime_1month).strftime("%Y")) quarter = int(pd.Timestamp(tmp_datetime_1month).quarter) # 获得上个季度的数据。 print("############ year %d, quarter %d", year, quarter) # 业绩报告(主表) data = ts.get_report_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_report_data", False, "`quarter`,`code`") # 盈利能力 data = ts.get_profit_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_profit_data", False, "`quarter`,`code`") # 营运能力 data = ts.get_operation_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_operation_data", False, "`quarter`,`code`") # 成长能力 data = ts.get_growth_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_growth_data", False, "`quarter`,`code`") # 偿债能力 data = ts.get_debtpaying_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_debtpaying_data", False, "`quarter`,`code`") # 现金流量 data = ts.get_cashflow_data(year, quarter) # 增加季度字段。 data = concat_quarter(year, quarter, data) # 处理重复数据,保存最新一条数据。最后一步处理,否则concat有问题。 data = data.drop_duplicates(subset="code", keep="last") # 插入数据库。 common.insert_db(data, "ts_cashflow_data", False, "`quarter`,`code`") # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all) ================================================ FILE: backend/jobs/restart_mnist_serving.sh ================================================ #!/bin/sh ps -ef | grep 'tensorflow_model_server' | grep -v grep | awk '{print$2}' | xargs kill -9 echo "" > /data/logs/mnist_serving.log nohup tensorflow_model_server --model_name=mnist --model_base_path=/data/mnist_model >> /data/logs/mnist_serving.log & ================================================ FILE: backend/jobs/restart_web.sh ================================================ #!/bin/sh ps -ef | grep python3 | grep '/data/stock/web/main.py' | awk '{print$2}' | xargs kill -9 echo "restart web ... " > /data/logs/tornado.log ================================================ FILE: backend/jobs/run_cron.sh ================================================ #!/bin/sh export PYTHONIOENCODING=utf-8 export LANG=zh_CN.UTF-8 export PYTHONPATH=/data/stock export LC_CTYPE=zh_CN.UTF-8 mkdir -p /data/logs/tensorflow DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE >> /data/logs/run_cron.log # 解决定时任务不启动问题,因为权限导致 chmod 755 /etc/cron.minutely/* && chmod 755 /etc/cron.hourly/* chmod 755 /etc/cron.daily/* && chmod 755 /etc/cron.monthly/* # 配置文件每次都设置权限 chmod 600 /var/spool/cron/crontabs/root chown root:root /var/spool/cron/crontabs/root #启动cron服务。在前台 /usr/sbin/cron -f ================================================ FILE: backend/jobs/run_init.sh ================================================ #!/bin/sh export PYTHONIOENCODING=utf-8 export LANG=zh_CN.UTF-8 export PYTHONPATH=/data/stock export LC_CTYPE=zh_CN.UTF-8 mkdir -p /data/logs/tensorflow DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE >> /data/logs/run_init.log echo "wait 120 second , mysqldb is starting ." >> /data/logs/run_init.log sleep 120 /usr/local/bin/python3 /data/stock/jobs/basic_job.py >> /data/logs/run_init.log # https://stackoverflow.com/questions/27771781/how-can-i-access-docker-set-environment-variables-from-a-cron-job # 解决环境变量输出问题。 printenv | grep -v "no_proxy" >> /etc/environment # 第一次后台执行日数据。 nohup bash /data/stock/jobs/cron.daily/run_daily & #防止 supervisor 重复执行 sleep 999999d ================================================ FILE: backend/jobs/run_jupyter.sh ================================================ #!/bin/sh mkdir -p /data/notebooks /usr/local/bin/jupyter notebook --NotebookApp.notebook_dir='/data/notebooks' --ip=0.0.0.0 \ --allow-root >> /data/logs/jupyter-notebook.log ================================================ FILE: backend/jobs/run_web.sh ================================================ #!/bin/bash export PYTHONIOENCODING=utf-8 export LANG=zh_CN.UTF-8 export PYTHONPATH=/data/stock export LC_CTYPE=zh_CN.UTF-8 echo "" > /data/logs/web.log /usr/local/bin/python3 /data/stock/web/main.py -log_file_prefix=/data/logs/web.log ================================================ FILE: backend/jobs/start_mariadb.sh ================================================ #!/bin/sh DATE=`date +%Y-%m-%d:%H:%M:%S` echo $DATE if [ ! -d "/data/mariadb" ]; then mkdir -p /data/mariadb /usr/bin/mysql_install_db fi /usr/bin/mysqld_safe >> /data/logs/start_mariadb.log ================================================ FILE: backend/jobs/test_akshare/test_stock_zh_a_daily.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import akshare as ak import libs.common as common print(ak.__version__) # 历史行情数据 # 日频率 # 接口: stock_zh_a_daily # 目标地址: https://finance.sina.com.cn/realstock/company/sh600006/nc.shtml(示例) # 描述: A 股数据是从新浪财经获取的数据, 历史数据按日频率更新; 注意其中的 sh689009 为 CDR, 请 通过 stock_zh_a_cdr_daily 接口获取 # 限量: 单次返回指定 A 股上市公司指定日期间的历史行情日频率数据 # adjust=""; 默认为空: 返回不复权的数据; qfq: 返回前复权后的数据; hfq: 返回后复权后的数据; stock_zh_a_daily_qfq_df = ak.stock_zh_a_daily(symbol="sz000002", adjust="") print(stock_zh_a_daily_qfq_df) stock_zh_a_daily_qfq_df = ak.stock_zh_a_daily(symbol="sz000002", start_date="20200101", end_date="20210101", adjust="") print(stock_zh_a_daily_qfq_df) # 插入到 MySQL 数据库中 common.insert_db(stock_zh_a_daily_qfq_df, "stock_zh_a_daily", True, "`symbol`") ================================================ FILE: backend/jobs/test_akshare/test_stock_zh_a_spot.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import akshare as ak import libs.common as common print(ak.__version__) # 实时行情数据 # 接口: stock_zh_a_spot # 目标地址: http://vip.stock.finance.sina.com.cn/mkt/#hs_a # 描述: A 股数据是从新浪财经获取的数据, 重复运行本函数会被新浪暂时封 IP, 建议增加时间间隔 # 限量: 单次返回所有 A 股上市公司的实时行情数据 stock_zh_a_spot_df = ak.stock_zh_a_spot() print(stock_zh_a_spot_df) # 插入到 MySQL 数据库中 common.insert_db(stock_zh_a_spot_df, "stock_zh_a_spot", True, "`symbol`") ================================================ FILE: backend/jobs/test_akshare/test_stock_zh_index_spot.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import akshare as ak import libs.common as common print(ak.__version__) #stock_sse_summary_df = ak.stock_sse_summary() #print(stock_sse_summary_df) # 接口: stock_zh_index_spot # 目标地址: http://vip.stock.finance.sina.com.cn/mkt/#hs_s # 描述: 中国股票指数数据, 注意该股票指数指新浪提供的国内股票指数 # 限量: 单次返回所有指数的实时行情数据 stock_zh_index_spot_df = ak.stock_zh_index_spot() print(stock_zh_index_spot_df) # 插入到 MySQL 数据库中 common.insert_db(stock_zh_index_spot_df, "stock_zh_index_spot_df", True, "`symbol`") ================================================ FILE: backend/libs/common.py ================================================ #!/usr/local/bin/python # -*- coding: utf-8 -*- # apk add py-mysqldb or import platform import datetime import time import sys import os import MySQLdb from sqlalchemy import create_engine from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import pandas as pd import traceback import akshare as ak # 使用环境变量获得数据库。兼容开发模式可docker模式。 MYSQL_HOST = os.environ.get('MYSQL_HOST') if (os.environ.get('MYSQL_HOST') != None) else "mysqldb" MYSQL_USER = os.environ.get('MYSQL_USER') if (os.environ.get('MYSQL_USER') != None) else "root" MYSQL_PWD = os.environ.get('MYSQL_PWD') if (os.environ.get('MYSQL_PWD') != None) else "mysqldb" MYSQL_DB = os.environ.get('MYSQL_DB') if (os.environ.get('MYSQL_DB') != None) else "stock_data" MYSQL_PORT = os.environ.get('MYSQL_PORT') if (os.environ.get('MYSQL_PORT') != None) else "3306" print("MYSQL_HOST :", MYSQL_HOST, ",MYSQL_USER :", MYSQL_USER, ",MYSQL_DB :", MYSQL_DB) MYSQL_CONN_URL = "mysql+mysqldb://" + MYSQL_USER + ":" + MYSQL_PWD + "@" + MYSQL_HOST + ":" + MYSQL_PORT + "/" + MYSQL_DB + "?charset=utf8mb4" print("MYSQL_CONN_URL :", MYSQL_CONN_URL) __version__ = "2.0.0" # 每次发布时候更新。 # https://docs.sqlalchemy.org/en/20/errors.html#error-e3q8 # def engine(): engine = create_engine(MYSQL_CONN_URL, pool_size=10, max_overflow=20) #encoding='utf8', convert_unicode=True) return engine def engine_to_db(to_db): MYSQL_CONN_URL_NEW = "mysql+mysqldb://" + MYSQL_USER + ":" + MYSQL_PWD + "@" + MYSQL_HOST + ":" + MYSQL_PORT + "/" + to_db + "?charset=utf8mb4" engine = create_engine(MYSQL_CONN_URL_NEW, pool_size=10, max_overflow=20) #encoding='utf8', convert_unicode=True) return engine # 通过数据库链接 engine。 def conn(): try: db = MySQLdb.connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB, charset="utf8") # db.autocommit = True except Exception as e: print("conn error :", e) db.autocommit(on=True) return db.cursor() # 定义通用方法函数,插入数据库表,并创建数据库主键,保证重跑数据的时候索引唯一。 def insert_db(data, table_name, write_index, primary_keys): # 插入默认的数据库。 insert_other_db(MYSQL_DB, data, table_name, write_index, primary_keys) # 增加一个插入到其他数据库的方法。 def insert_other_db(to_db, data, table_name, write_index, primary_keys): # 定义engine engine_mysql = engine_to_db(to_db) # 使用 http://docs.sqlalchemy.org/en/latest/core/reflection.html # 使用检查检查数据库表是否有主键。 insp = inspect(engine_mysql) col_name_list = data.columns.tolist() # 如果有索引,把索引增加到varchar上面。 if write_index: # 插入到第一个位置: col_name_list.insert(0, data.index.name) print(col_name_list) data.to_sql(name=table_name, con=engine_mysql, schema=to_db, if_exists='append', dtype={col_name: NVARCHAR(length=255) for col_name in col_name_list}, index=write_index) # print(insp.get_pk_constraint(table_name)) # print() # print(type(insp)) # 判断是否存在主键 if insp.get_pk_constraint(table_name)['constrained_columns'] == []: with engine_mysql.connect() as con: # 执行数据库插入数据。 try: con.execute('ALTER TABLE `%s` ADD PRIMARY KEY (%s);' % (table_name, primary_keys)) except Exception as e: print("################## ADD PRIMARY KEY ERROR :", e) # 插入数据。 def insert(sql, params=()): with conn() as db: print("insert sql:" + sql) try: db.execute(sql, params) except Exception as e: print("error :", e) # 查询数据 def select(sql, params=()): with conn() as db: print("select sql:" + sql) try: db.execute(sql, params) except Exception as e: print("error :", e) result = db.fetchall() return result # 计算数量 def select_count(sql, params=()): with conn() as db: print("select sql:" + sql) try: db.execute(sql, params) except Exception as e: print("error :", e) result = db.fetchall() # 只有一个数组中的第一个数据 if len(result) == 1: return int(result[0][0]) else: return 0 # 通用函数。获得日期参数。 def run_with_args(run_fun): tmp_datetime_show = datetime.datetime.now() # 修改成默认是当日执行 + datetime.timedelta() tmp_hour_int = int(tmp_datetime_show.strftime("%H")) if tmp_hour_int < 12 : # 判断如果是每天 中午 12 点之前运行,跑昨天的数据。 tmp_datetime_show = (tmp_datetime_show + datetime.timedelta(days=-1)) tmp_datetime_str = tmp_datetime_show.strftime("%Y-%m-%d %H:%M:%S.%f") print("\n######################### hour_int %d " % tmp_hour_int) str_db = "MYSQL_HOST :" + MYSQL_HOST + ", MYSQL_USER :" + MYSQL_USER + ", MYSQL_DB :" + MYSQL_DB print("\n######################### " + str_db + " ######################### ") print("\n######################### begin run %s %s #########################" % (run_fun, tmp_datetime_str)) start = time.time() # 要支持数据重跑机制,将日期传入。循环次数 if len(sys.argv) == 3: # python xxx.py 2017-07-01 10 tmp_year, tmp_month, tmp_day = sys.argv[1].split("-") loop = int(sys.argv[2]) tmp_datetime = datetime.datetime(int(tmp_year), int(tmp_month), int(tmp_day)) for i in range(0, loop): # 循环插入多次数据,重复跑历史数据使用。 # time.sleep(5) tmp_datetime_new = tmp_datetime + datetime.timedelta(days=i) try: run_fun(tmp_datetime_new) except Exception as e: print("error :", e) traceback.print_exc() elif len(sys.argv) == 2: # python xxx.py 2017-07-01 tmp_year, tmp_month, tmp_day = sys.argv[1].split("-") tmp_datetime = datetime.datetime(int(tmp_year), int(tmp_month), int(tmp_day)) try: run_fun(tmp_datetime) except Exception as e: print("error :", e) traceback.print_exc() else: # tmp_datetime = datetime.datetime.now() + datetime.timedelta(days=-1) try: run_fun(tmp_datetime_show) # 使用当前时间 except Exception as e: print("error :", e) traceback.print_exc() print("######################### finish %s , use time: %s #########################" % ( tmp_datetime_str, time.time() - start)) # 设置基础目录,每次加载使用。 bash_stock_tmp = "/data/cache/hist_data_cache/%s/%s/" if not os.path.exists(bash_stock_tmp): os.makedirs(bash_stock_tmp) # 创建多个文件夹结构。 print("######################### init tmp dir #########################") # 增加读取股票缓存方法。加快处理速度。 def get_hist_data_cache(code, date_start, date_end): cache_dir = bash_stock_tmp % (date_end[0:7], date_end) # 如果没有文件夹创建一个。月文件夹和日文件夹。方便删除。 # print("cache_dir:", cache_dir) if not os.path.exists(cache_dir): os.makedirs(cache_dir) cache_file = cache_dir + "%s^%s.gzip.pickle" % (date_end, code) # 如果缓存存在就直接返回缓存数据。压缩方式。 if os.path.isfile(cache_file): print("######### read from cache #########", cache_file) return pd.read_pickle(cache_file, compression="gzip") else: # https://akshare.akfamily.xyz/data/index/index.html#id4 # 获取历史行情,em #stock = ak.stock_zh_a_hist(symbol= code, start_date=date_start, # end_date=date_end, adjust="") code = gp_type_szsh(code)+ code print("######### get data, write cache #########", code, date_start, date_end) stock = ak.stock_zh_index_daily_em(symbol= code, start_date=date_start.replace("-", ""), end_date=date_end.replace("-", "")) print(stock) if stock is None or stock.empty: return None stock.columns = ['date', 'open', 'close', 'high', 'low', 'volume', 'amount'] # 数据返回的是带 0 列是索引,第一列是 date 日期 # date open close high low volume amount # 0 2024-09-20 9.81 9.90 9.90 9.78 797297 7.851212e+08 stock.set_index('date', inplace=True) #stock = stock.sort_index(0) # 将数据按照日期排序下。 print(stock) stock.to_pickle(cache_file, compression="gzip") return stock # 沪市股票包含上证主板和科创板和B股:沪市主板股票代码是60开头、科创板股票代码是688开头、B股代码900开头。 # 深市股票包含主板、中小板、创业板和B股:深市主板股票代码是000开头、中小板股票代码002开头、创业板300开头、B股代码200开头 # print(gp_type_szsh('002340')) # def gp_type_szsh(gp): if gp.find('60',0,3)==0: gp_type='sh' elif gp.find('688',0,4)==0: gp_type='sh' elif gp.find('900',0,4)==0: gp_type='sh' elif gp.find('00',0,3)==0: gp_type='sz' elif gp.find('300',0,4)==0: gp_type='sz' elif gp.find('200',0,4)==0: gp_type='sz' return gp_type ================================================ FILE: backend/libs/stock_web_dic.py ================================================ #!/usr/local/bin/python # -*- coding: utf-8 -*- class StockWebData: def __init__(self, mode, type, name, table_name, columns, column_names, primary_key, order_by): self.mode = mode # 模式,query,editor 查询和编辑模式 self.type = type self.name = name self.table_name = table_name self.columns = columns self.column_names = column_names self.primary_key = primary_key self.order_by = order_by if mode == "query": self.url = "/stock/data?table_name=" + self.table_name elif mode == "editor": self.url = "/data/editor?table_name=" + self.table_name STOCK_WEB_DATA_LIST = [] # https://www.akshare.xyz/zh_CN/latest/data/stock/stock.html#id1 # 限量: 单次返回所有 A 股上市公司的实时行情数据 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="1,股票基本数据", name="每日股票数据-东财", table_name="stock_zh_a_spot_em", columns= ['date', 'code', 'name', 'last_price', 'change_percent', 'change_amount', 'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed', 'change_5min', 'change_ercent_60day','ytd_change_percent'] , column_names=['日期','代码','名称','最新价','涨跌幅','涨跌额','成交量','成交额', '振幅','最高','最低','今开','昨收','量比','换手率','动态市盈率', '市净率', '总市值', '流通市值', '涨速', '5分钟涨跌', '60日涨跌幅', '年初至今涨跌幅'], primary_key=[], order_by=" code asc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="1,股票基本数据", name="龙虎榜-个股上榜-新浪", table_name="stock_lhb_ggtj_sina", columns= ['date','code','name','ranking_times','sum_buy','sum_sell','net_amount','buy_seat','sell_seat'], column_names=['日期','代码', '名称', '上榜次数', '累积购买额', '累积卖出额', '净额', '买入席位数', '卖出席位数'], primary_key=[], order_by=" code asc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="1,股票基本数据", name="数据中心-大宗交易", table_name="stock_dzjy_mrtj", columns= ['date', 'code', 'name', 'quote_change', 'close_price', 'average_price', 'overflow_rate', 'trade_number', 'sum_volume', 'sum_turnover', 'turnover_market_rate'], column_names=['日期', '代码', '名称', '涨跌幅', '收盘价', '成交均价', '折溢率', '成交笔数', '成交总量', '成交总额', '成交总额/流通市值'], primary_key=[], order_by=" code asc " ) ) # 每日股票指标lite猜想买入。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="2,每日数据猜想", name="股票指标lite猜想买入", table_name="guess_indicators_lite_buy_daily", # columns=['date', 'code', 'name', 'latest_price', 'quote_change', 'ups_downs', 'volume', 'turnover', # 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_dynamic', 'pb', # 'kdjj', 'rsi_6', 'cci'], # column_names=['日期', '代码', '名称', '最新价', '涨跌幅', '涨跌额', '成交量', '成交额', # '振幅', '最高', '最低', '今开', '昨收', '量比', '换手率', '动态市盈率', '市净率', # 'kdjj', 'rsi_6', 'cci'], columns= ['date', 'code', 'name', 'last_price', 'change_percent', 'change_amount', 'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed', 'change_5min', 'change_ercent_60day','ytd_change_percent', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi','trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] , # 中文说明前面和 1 数据一致。 column_names=['日期','代码','名称','最新价','涨跌幅','涨跌额','成交量','成交额', '振幅','最高','最低','今开','昨收','量比','换手率','动态市盈率', '市净率', '总市值', '流通市值', '涨速', '5分钟涨跌', '60日涨跌幅', '年初至今涨跌幅', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], primary_key=[], order_by=" buy_date desc " ) ) # 每日股票指标lite猜想卖出。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="2,每日数据猜想", name="股票指标lite猜想卖出", table_name="guess_indicators_lite_sell_daily", # columns=['date', 'code', 'name', 'latest_price', 'quote_change', 'ups_downs', 'volume', 'turnover', # 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_dynamic', 'pb', # 'kdjj', 'rsi_6', 'cci'], # column_names=['日期', '代码', '名称', '最新价', '涨跌幅', '涨跌额', '成交量', '成交额', # '振幅', '最高', '最低', '今开', '昨收', '量比', '换手率', '动态市盈率', '市净率', # 'kdjj', 'rsi_6', 'cci'], columns= ['date', 'code', 'name', 'last_price', 'change_percent', 'change_amount', 'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed', 'change_5min', 'change_ercent_60day','ytd_change_percent', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi','trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] , # 中文说明前面和 1 数据一致。 column_names=['日期','代码','名称','最新价','涨跌幅','涨跌额','成交量','成交额', '振幅','最高','最低','今开','昨收','量比','换手率','动态市盈率', '市净率', '总市值', '流通市值', '涨速', '5分钟涨跌', '60日涨跌幅', '年初至今涨跌幅', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], primary_key=[], order_by=" buy_date desc " ) ) # 每日股票指标lite猜想。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="2,每日数据猜想", name="股票指标猜想原始数据", table_name="guess_indicators_daily", columns= ['date', 'code', 'name', 'last_price', 'change_percent', 'change_amount', 'volume', 'turnover', 'amplitude', 'high', 'low', 'open', 'closed', 'volume_ratio', 'turnover_rate', 'pe_ratio','pb_ratio', 'market_cap','circulating_market_cap','rise_speed', 'change_5min', 'change_ercent_60day','ytd_change_percent', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi','trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'] , # 中文说明前面和 1 数据一致。 column_names=['日期','代码','名称','最新价','涨跌幅','涨跌额','成交量','成交额', '振幅','最高','最低','今开','昨收','量比','换手率','动态市盈率', '市净率', '总市值', '流通市值', '涨速', '5分钟涨跌', '60日涨跌幅', '年初至今涨跌幅', 'boll', 'boll_lb', 'boll_ub', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'pdi', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], # columns=['date','code','name','latest_price','quote_change','ups_downs', # 'adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', # 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', # 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'mdi', 'pdi', # 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], # column_names=['日期','代码','名称','最新价','涨跌幅','涨跌额', # 'adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', # 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', # 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'mdi', 'pdi', # 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], primary_key=[], order_by=' date desc ' ) ) # "code", "name: pchange", "amount", "buy", "bratio", "sell", "sratio", "reason", "date" # 代码 名称 当日涨跌幅 龙虎榜成交额(万) 买入额(万) 买入占总成交比例 卖出额(万) 卖出占总成交比例 上榜原因 日期 STOCK_WEB_DATA_MAP = {} WEB_EASTMONEY_URL = "http://quote.eastmoney.com/%s.html" # 再拼接成Map使用。 for tmp in STOCK_WEB_DATA_LIST: # try: # # 增加columns 字段中的【查看股票】 # tmp_idx = tmp.columns.index("code") # tmp.column_names.insert(tmp_idx + 1, "查看股票") # except Exception as e: # print("error :", e) STOCK_WEB_DATA_MAP[tmp.table_name] = tmp if len(tmp.columns) != len(tmp.column_names): print(u"error:", tmp.table_name, ",columns:", len(tmp.columns), ",column_names:", len(tmp.column_names)) ================================================ FILE: backend/libs/stock_web_dic.py.bk ================================================ #!/usr/local/bin/python # -*- coding: utf-8 -*- class StockWebData: def __init__(self, mode, type, name, table_name, columns, column_names, primary_key, order_by): self.mode = mode # 模式,query,editor 查询和编辑模式 self.type = type self.name = name self.table_name = table_name self.columns = columns self.column_names = column_names self.primary_key = primary_key self.order_by = order_by if mode == "query": self.url = "/stock/data?table_name=" + self.table_name elif mode == "editor": self.url = "/data/editor?table_name=" + self.table_name STOCK_WEB_DATA_LIST = [] STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="宏观经济数据", name="存款利率", table_name="ts_deposit_rate", columns=["date", "deposit_type", "rate"], column_names=["日期", "存款类型", "存款利率"], primary_key=[], order_by=" date desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="宏观经济数据", name="贷款利率", table_name="ts_loan_rate", columns=["date", "loan_type", "rate"], column_names=["日期", "贷款类型", "存款利率"], primary_key=[], order_by=" date desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="宏观经济数据", name="存款准备金率", table_name="ts_rrr", columns=["date", "before", "now", "changed"], column_names=["变动日期", "调整前存款准备金率(%)", "调整后存款准备金率(%)", "调整幅度(%)"], primary_key=[], order_by=" date desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="宏观经济数据", name="货币供应量", table_name="ts_money_supply", columns=["month", "m2", "m2_yoy", "m1", "m1_yoy", "m0", "m0_yoy", "cd", "cd_yoy", "qm", "qm_yoy", "ftd", "ftd_yoy", "sd", "sd_yoy", "rests", "rests_yoy"], column_names=["统计时间", "货币和准货币(广义货币M2)(亿元)", "货币和准货币(广义货币M2)同比增长(%)", "货币(狭义货币M1)(亿元)", "货币(狭义货币M1)同比增长(%)", "流通中现金(M0)(亿元)", "流通中现金(M0)同比增长(%)", "活期存款(亿元)", "活期存款同比增长(%)", "准货币(亿元)", "准货币同比增长(%)", "定期存款(亿元)", "定期存款同比增长(%)", "储蓄存款(亿元)", "储蓄存款同比增长(%)", "其他存款(亿元)", "其他存款同比增长(%)" ], primary_key=[], order_by=" month desc " ) ) # http://tushare.org/fundamental.html # 参考官网网站的文档,是最全的。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="基本面数据", name="股票列表", table_name="ts_stock_basics", columns=["code", "name", "industry", "area", "pe", "outstanding", "totals", "totalAssets", "liquidAssets", "fixedAssets", "reserved", "reservedPerShare", "esp", "bvps", "pb", "timeToMarket", "undp", "perundp", "rev", "profit", "gpr", "npr", "holders"], column_names=["代码", "名称", "所属行业", "地区", "市盈率", "流通股本(亿)", "总股本(亿)", "总资产(万)", "流动资产", "固定资产", "公积金", "每股公积金", "每股收益", "每股净资", "市净率", "上市日期", "未分利润", "每股未分配", "收入同比(%)", "利润同比(%)", "毛利率(%)", "净利润率(%)", "股东人数" ], primary_key=[], order_by=" code asc " ) ) # 业绩报告(主表) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="基本面数据", name="业绩报告(主表)", table_name="ts_report_data", columns=["quarter", "code", "name", "eps", "eps_yoy", "bvps", "roe", "epcf", "net_profits", "profits_yoy", "distrib", "report_date"], column_names=["季度", "代码", "名称", "每股收益", "每股收益同比(%)", "每股净资产", "净资产收益率(%)", "每股现金流量(元)", ",净利润(万元)", "净利润同比(%)", "分配方案", "发布日期" ], primary_key=[], order_by=" quarter desc " ) ) # 盈利能力 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="基本面数据", name="盈利能力", table_name="ts_profit_data", columns=["quarter", "code", "name", "roe", "net_profit_ratio", "gross_profit_rate", "net_profits", "eps", "business_income", "bips"], column_names=["季度", "代码", "名称", "净资产收益率(%)", "净利率(%)", "毛利率(%)", "净利润(万元)", "每股收益", "营业收入(百万元)", "每股主营业务收入(元)"], primary_key=[], order_by=" quarter desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="基本面数据", name="营运能力", table_name="ts_operation_data", columns=["quarter", "code", "name", "arturnover", "arturndays", "inventory_turnover", "inventory_days", "currentasset_turnover", "currentasset_days"], column_names=["季度", "代码", "名称", "应收账款周转率(次)", "应收账款周转天数(天)", "存货周转率(次)", "存货周转天数(天)", "流动资产周转率(次)", "流动资产周转天数(天)" ], primary_key=[], order_by=" quarter desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="基本面数据", name="成长能力", table_name="ts_growth_data", columns=["quarter", "code", "name", "mbrg", "nprg", "nav", "targ", "epsg", "seg"], column_names=["季度", "代码", "名称", "主营业务收入增长率(%)", "净利润增长率(%)", "净资产增长率", "总资产增长率", "每股收益增长率", "股东权益增长率"], primary_key=[], order_by=" quarter desc " ) ) # "code", "name: pchange", "amount", "buy", "bratio", "sell", "sratio", "reason", "date" # 代码 名称 当日涨跌幅 龙虎榜成交额(万) 买入额(万) 买入占总成交比例 卖出额(万) 卖出占总成交比例 上榜原因 日期 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据", name="龙虎榜", table_name="ts_top_list", columns=["date", "code", "name", "pchange", "amount", "buy", "bratio", "sell", "sratio", "reason"], column_names=["日期", "代码", "名称", "当日涨跌幅", "龙虎榜成交额(万)", "买入额(万)", "买入占总成交比例", "卖出额(万)", "卖出占总成交比例", "上榜原因"], primary_key=[], order_by=" date desc " ) ) # 实时行情 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据", name="每日股票数据", table_name="ts_today_all", columns=["date", "code", "name", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "amount", "per", "pb", "mktcap", "nmc"], column_names=["日期", "代码", "名称", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "成交金额", "市盈率", "市净率", "总市值", "流通市值"], primary_key=[], order_by=" date desc " ) ) # 大盘指数行情列表 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据", name="每日大盘指数行情", table_name="ts_index_all", columns=["date", "code", "name", "change", "open", "preclose", "close", "high", "low", "volume", "amount"], column_names=["日期", "代码", "名称", "涨跌幅", "开盘点位", "昨日收盘点位", "收盘点位", "最高点位", "最低点位", "成交量(手)", "成交金额(亿元)"], primary_key=[], order_by=" date desc " ) ) # 每日波峰波谷猜想 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日波峰波谷猜想", table_name="guess_period_daily", columns=["date", "code", "name", "wave_base", "wave_crest", "wave_mean", "up_rate", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "amount", "per", "pb", "mktcap", "nmc"], column_names=["日期", "代码", "名称", "5波峰平均", "5波谷平均", "价格平均", "上涨率猜想%", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "成交金额", "市盈率", "市净率", "总市值", "流通市值"], primary_key=[], order_by=" date desc " ) ) # 每日收益率猜想。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日收益率猜想", table_name="guess_return_daily", columns=["date", "code", "name", "5d", "10d", "20d", "60d", "5-10d", "5-20d", "mov_vol", "return", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "amount", "per", "pb", "mktcap", "nmc"], column_names=["日期", "代码", "名称", "5周线", "10半月线", "20月线", "60季度线", "5-10日差%", "5-20日差%", "收益", "收益率移动标准差", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "成交金额", "市盈率", "市净率", "总市值", "流通市值"], primary_key=[], order_by=" date desc " ) ) # 每日股票指标lite猜想。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日股票指标lite猜想", table_name="guess_indicators_lite_daily", columns=["date", "code", "name", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "amount", "per", "pb", "mktcap", "nmc", "kdjj", "rsi_6", "cci"], column_names=["日期", "代码", "名称", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "成交金额", "市盈率", "市净率", "总市值", "流通市值", "kdjj", "rsi_6", "cci"], primary_key=[], order_by=" date desc " ) ) # 每日股票指标lite猜想买入。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日股票指标lite猜想买入", table_name="guess_indicators_lite_buy_daily", columns=["buy_date", "code", "name", "changepercent", "trade", "turnoverratio", "pb", "kdjj", "rsi_6", "cci", "wave_base", "wave_crest", "wave_mean", "up_rate", "buy", "sell", "today_trade", "income"], column_names=["购买日期", "代码", "名称", "涨跌幅", "现价", "换手率%", "市净率%", "买入kdjj", "买入rsi_6", "买入cci", "波谷", "波峰", "波平均", "上涨率%", "买入", "卖出", "今日价格", "收益"], primary_key=[], order_by=" buy_date desc " ) ) # 每日股票指标lite猜想卖出。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日股票指标lite猜想卖出", table_name="guess_indicators_lite_sell_daily", columns=["date", "buy_date", "code", "name", "changepercent", "trade", "turnoverratio", "pb", "kdjj", "rsi_6", "cci", "wave_base", "wave_crest", "wave_mean", "up_rate", "buy", "sell", "today_trade", "income", "sell_cci", "sell_kdjj", "sell_rsi_6"], column_names=["日期", "购买日期", "代码", "名称", "涨跌幅", "现价", "换手率%", "市净率%", "买入kdjj", "买入rsi_6", "买入cci", "波谷", "波峰", "波平均", "上涨率%", "买入", "卖出", "今日价格", "收益", "卖出kdjj", "卖出rsi_6", "卖出cci", ], primary_key=[], order_by=" buy_date desc " ) ) # 每日股票指标猜想。 STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据猜想", name="每日股票指标All猜想", table_name="guess_indicators_daily", columns=["date", "code", "name", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "amount", "per", "pb", "mktcap", "nmc", 'adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'mdi', 'pdi', 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], column_names=["日期", "代码", "名称", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "成交金额", "市盈率", "市净率", "总市值", "流通市值", 'adx', 'adxr', 'boll', 'boll_lb', 'boll_ub', 'cci', 'cci_20', 'close_-1_r', 'close_-2_r', 'code', 'cr', 'cr-ma1', 'cr-ma2', 'cr-ma3', 'date', 'dma', 'dx', 'kdjd', 'kdjj', 'kdjk', 'macd', 'macdh', 'macds', 'mdi', 'pdi', 'rsi_12', 'rsi_6', 'trix', 'trix_9_sma', 'vr', 'vr_6_sma', 'wr_10', 'wr_6'], primary_key=[], order_by=" date desc " ) ) STOCK_WEB_DATA_LIST.append( StockWebData( mode="query", type="每日数据Keras猜想", name="每日股票数据Keras猜想", table_name="guess_sklearn_ma_daily", columns=["date", "code", "name", "changepercent", "trade", "open", "high", "low", "settlement", "volume", "turnoverratio", "next_close", "sklearn_score", "up_rate"], column_names=["日期", "代码", "名称", "涨跌幅", "现价", "开盘价", "最高价", "最低价", "昨日收盘价", "成交量", "换手率", "预测收盘价", "sk概率", "预测上涨率"], primary_key=[], order_by=" date desc " ) ) STOCK_WEB_DATA_MAP = {} WEB_EASTMONEY_URL = "http://quote.eastmoney.com/%s.html" # 再拼接成Map使用。 for tmp in STOCK_WEB_DATA_LIST: try: # 增加columns 字段中的【东方财富】 tmp_idx = tmp.columns.index("code") tmp.column_names.insert(tmp_idx + 1, "东方财富") except Exception as e: print("error :", e) STOCK_WEB_DATA_MAP[tmp.table_name] = tmp if len(tmp.columns) != len(tmp.column_names): print(u"error:", tmp.table_name, ",columns:", len(tmp.columns), ",column_names:", len(tmp.column_names)) ================================================ FILE: backend/old_jobs/README.md ================================================ ## 说明 之前测试使用的脚本。执行了一段时间,只是用来进行练习使用的。 ================================================ FILE: backend/old_jobs/guess_indicators_lite_buy_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import heapq ### 对每日指标数据,进行筛选。将符合条件的。二次筛选出来。 def stat_all_lite(tmp_datetime): # 要操作的数据库表名称。 table_name = "guess_indicators_lite_buy_daily" datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # try: # # 删除老数据。guess_indicators_lite_buy_daily 是一张单表,没有日期字段。 # del_sql = " DELETE FROM `stock_data`.`%s` WHERE `date`= '%s' " % (table_name, datetime_int) # print("del_sql:", del_sql) # common.insert(del_sql) # print("del_sql") # except Exception as e: # print("error :", e) sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`,`turnoverratio`, `pb` ,`kdjj`,`rsi_6`,`cci` FROM stock_data.guess_indicators_lite_daily WHERE `date` = %s and `changepercent` > 2 and `pb` > 0 """ # and `changepercent` > 2 and `pb` > 0 and `turnoverratio` > 5 去除掉换手率参数。 data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int]) data = data.drop_duplicates(subset="code", keep="last") print("######## len data ########:", len(data)) # del data["name"] # print(data) data["trade_float32"] = data["trade"].astype('float32', copy=True) # 输入 date 用作历史数据查询。 stock_merge = pd.DataFrame({ "date": data["date"], "code": data["code"], "wave_mean": data["trade"], "wave_crest": data["trade"], "wave_base": data["trade"]}, index=data.index.values) print(stock_merge.head(1)) stock_merge = stock_merge.apply(apply_merge, axis=1) # , axis=1) del stock_merge["date"] # 合并前删除 date 字段。 # 合并数据 data_new = pd.merge(data, stock_merge, on=['code'], how='left') # 使用 trade_float32 参加计算。 data_new = data_new[data_new["trade_float32"] > data_new["wave_base"]] # 交易价格大于波谷价格。 data_new = data_new[data_new["trade_float32"] < data_new["wave_crest"]] # 小于波峰价格 # wave_base wave_crest wave_mean data_new["wave_base"] = data_new["wave_base"].round(2) # 数据保留2位小数 data_new["wave_crest"] = data_new["wave_crest"].round(2) # 数据保留2位小数 data_new["wave_mean"] = data_new["wave_mean"].round(2) # 数据保留2位小数 data_new["up_rate"] = (data_new["wave_mean"].sub(data_new["trade_float32"])).div(data_new["wave_crest"]).mul(100) data_new["up_rate"] = data_new["up_rate"].round(2) # 数据保留2位小数 data_new["buy"] = 1 data_new["sell"] = 0 data_new["today_trade"] = data_new["trade"] data_new["income"] = 0 # 重命名 date data_new.columns.values[0] = "buy_date" del data_new["trade_float32"] try: common.insert_db(data_new, table_name, False, "`code`") print("insert_db") except Exception as e: print("error :", e) # 重命名 del data_new["name"] print(data_new) def apply_merge(tmp): date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock is None: return list([code, date, 0, 0, 0]) stock = pd.DataFrame({"close": stock["close"]}, index=stock.index.values) stock = stock.sort_index(0) # 将数据按照日期排序下。 # print(stock.head(10)) arr = pd.Series(stock["close"].values) # print(df_arr) wave_mean = arr.mean() max_point = 3 # 获得最高的几个采样点。 # 计算股票的波峰值。 wave_crest = heapq.nlargest(max_point, enumerate(arr), key=lambda x: x[1]) wave_crest_mean = pd.DataFrame(wave_crest).mean() # 输出元祖第一个元素是index,第二元素是比较的数值 计算数据的波谷值 wave_base = heapq.nsmallest(max_point, enumerate(arr), key=lambda x: x[1]) wave_base_mean = pd.DataFrame(wave_base).mean() # 输出数据 print("##############", len(stock)) if len(stock) > 180: # code date wave_base wave_crest wave_mean 顺序必须一致。返回的是行数据,然后填充。 return list([code, date, wave_base_mean[1], wave_crest_mean[1], wave_mean]) else: return list([code, date, 0, 0, 0]) # main函数入口 if __name__ == '__main__': # 二次筛选数据。 tmp_datetime = common.run_with_args(stat_all_lite) ================================================ FILE: backend/old_jobs/guess_indicators_lite_sell_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import heapq import stockstats # code date today_trade def apply_merge(tmp): date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock is None: return list([code, date, 0.0]) print("########") # print(stock.tail(1)) close = stock.tail(1)["close"].values[0] print("close: ", close) print("########") return list([code, date, close]) # buy code date sell sell_cci sell_kdjj sell_rsi_6 def apply_merge_sell(tmp): date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock is None: return list([1, code, date, 0, 0, 0, 0]) print("########") # J大于100时为超买,小于10时为超卖。 # 强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 # 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, # 2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, stockStat = stockstats.StockDataFrame.retype(stock) kdjj = int(stockStat["kdjj"].tail(1).values[0]) rsi_6 = int(stockStat["rsi_6"].tail(1).values[0]) cci = int(stockStat["cci"].tail(1).values[0]) print("kdjj:", kdjj, "rsi_6:", rsi_6, "cci:", cci) # and kdjj > 80 and rsi_6 > 55 and cci > 100 判断卖出时刻。也就是买入时刻的反面。发现有波动就卖了。 # if kdjj <= 10 and rsi_6 <= 50 and cci <= 100: old if kdjj <= 80 or rsi_6 <= 55 or cci <= 100: return list([0, code, date, 1, cci, kdjj, rsi_6]) else: return list([1, code, date, 0, cci, kdjj, rsi_6]) # 增加 收益计算。 def stat_index_calculate(tmp_datetime): # 要操作的数据库表名称。 table_name = "guess_indicators_lite_sell_daily" datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) sql_1 = """ SELECT `buy_date`, `code`, `name`, `changepercent`, `trade`, `turnoverratio`, `pb`, `kdjj`, `rsi_6`, `cci`, `wave_base`, `wave_crest`, `wave_mean`, `up_rate` FROM guess_indicators_lite_buy_daily where `buy_date` <= """ + datetime_int print(sql_1) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[]) data = data.drop_duplicates(subset="code", keep="last") print(data["trade"]) data["trade_float32"] = data["trade"].astype('float32', copy=False) print(len(data)) data["date"] = datetime_int stock_merge = pd.DataFrame({ "date": data["date"], "code": data["code"], "today_trade": data["trade"]}, index=data.index.values) print(stock_merge.head(1)) stock_merge = stock_merge.apply(apply_merge, axis=1) # , axis=1) del stock_merge["date"] # 合并前删除 date 字段。 # 合并数据 data_new = pd.merge(data, stock_merge, on=['code'], how='left') data_new["income"] = (data_new["today_trade"] - data_new["trade_float32"]) * 100 data_new["income"] = data_new["income"].round(4) # 保留4位小数。 # 增加售出列。看看是否需要卖出。 stock_sell_merge = pd.DataFrame({ "date": data["date"], "code": data["code"], "sell": 0, "buy": 0, "sell_kdjj": 0, "sell_rsi_6": 0, "sell_cci": 0}, index=data.index.values) print(stock_sell_merge.head(1)) merge_sell_data = stock_sell_merge.apply(apply_merge_sell, axis=1) # , axis=1) # 重命名 del merge_sell_data["date"] # 合并前删除 date 字段。 # 合并数据 data_new = pd.merge(data_new, merge_sell_data, on=['code'], how='left') # 删除老数据。 try: del_sql = " DELETE FROM `stock_data`.`" + table_name + "` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) print("insert_db") except Exception as e: print("error :", e) del data_new["trade_float32"] try: common.insert_db(data_new, table_name, False, "`date`,`code`") print("insert_db") except Exception as e: print("error :", e) # 重命名 del data_new["name"] print(data_new) # main函数入口 if __name__ == '__main__': # 计算买卖。 tmp_datetime = common.run_with_args(stat_index_calculate) ================================================ FILE: backend/old_jobs/guess_period_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import sys import time import pandas as pd import tushare as ts from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import datetime import heapq """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all where `date` = 20171106 and trade > 0 and trade <= 20 and `code` not like '002%' and `code` not like '300%' and `name` not like '%st%' """ def stat_index_all(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `code` not like %s and `name` not like %s """ print(sql_1) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%']) print(type(data)) data = data.drop_duplicates(subset="code", keep="last") print(data["trade"]) data["trade_float32"] = data["trade"].astype('float32', copy=False) print(len(data)) print("########data[trade]########:") print(data["trade"]) # 使用 trade 填充数据 stock_guess = pd.DataFrame({ "date": data["date"], "code": data["code"], "wave_mean": data["trade"], "wave_crest": data["trade"], "wave_base": data["trade"]}, index=data.index.values) print(stock_guess.head()) stock_guess = stock_guess.apply(apply_guess, axis=1) # , axis=1) print(stock_guess.head()) # stock_guess.astype('float32', copy=False) stock_guess.drop('date', axis=1, inplace=True) # 删除日期字段,然后和原始数据合并。 stock_guess = stock_guess.round(2) # 数据保留2位小数 print(stock_guess["wave_base"]) data_new = pd.merge(data, stock_guess, on=['code'], how='left') print("#############") # 使用pandas 函数 : https://pandas.pydata.org/pandas-docs/stable/api.html#id4 data_new["up_rate"] = (data_new["trade_float32"].sub(data_new["wave_mean"])).div(data_new["wave_crest"]).mul(100) data_new["up_rate"] = data_new["up_rate"].round(2) # 数据保留2位小数 data_new.drop('trade_float32', axis=1, inplace=True) # 删除计算字段。 # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_period_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) # print(data_new.head()) # data_new["down_rate"] = (data_new["trade"] - data_new["wave_mean"]) / data_new["wave_base"] common.insert_db(data_new, "guess_period_daily", False, "`date`,`code`") # 进行左连接. # tmp = pd.merge(tmp, tmp2, on=['company_id'], how='left') def apply_guess(tmp): date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock is None: return pd.Series([date, code, 0.0, 0.0, 0.0], index=['date', 'code', 'wave_mean', 'wave_crest', 'wave_base']) stock = pd.DataFrame({"close": stock["close"]}, index=stock.index.values) stock = stock.sort_index(0) # 将数据按照日期排序下。 # print(stock.head(10)) arr = pd.Series(stock["close"].values) # print(df_arr) wave_mean = arr.mean() # 计算股票的波峰值。 wave_crest = heapq.nlargest(5, enumerate(arr), key=lambda x: x[1]) wave_crest_mean = pd.DataFrame(wave_crest).mean() # 输出元祖第一个元素是index,第二元素是比较的数值 计算数据的波谷值 wave_base = heapq.nsmallest(5, enumerate(arr), key=lambda x: x[1]) wave_base_mean = pd.DataFrame(wave_base).mean() # 输出数据 # print("##############") # code date wave_base wave_crest wave_mean 顺序必须一致。返回的是行数据,然后填充。 return pd.Series([date, code, wave_base_mean[1], wave_crest_mean[1], wave_mean], index=['date','code','wave_mean','wave_crest','wave_base']) # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_index_all) ================================================ FILE: backend/old_jobs/guess_return_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import sys import time import pandas as pd import numpy as np import math import tushare as ts from sqlalchemy.types import NVARCHAR from sqlalchemy import inspect import datetime import heapq """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all where `date` = 20171106 and trade > 0 and trade <= 20 and `code` not like '002%' and `code` not like '300%' and `name` not like '%st%' """ def stat_index_all(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `code` not like %s and `name` not like %s """ print(sql_1) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%']) data = data.drop_duplicates(subset="code", keep="last") print("########data[trade]########:") # print(data["trade"]) # 使用 trade 填充数据 stock_guess = pd.DataFrame({ "date": data["date"], "code": data["code"], "5d": data["trade"], "10d": data["trade"], "20d": data["trade"], "60d": data["trade"], "5-10d": data["trade"], "5-20d": data["trade"], "return": data["trade"], "mov_vol": data["trade"] }, index=data.index.values) stock_guess = stock_guess.apply(apply_guess, axis=1) # , axis=1) # print(stock_guess.head()) # stock_guess.astype('float32', copy=False) stock_guess.drop('date', axis=1, inplace=True) # 删除日期字段,然后和原始数据合并。 # print(stock_guess["5d"]) data_new = pd.merge(data, stock_guess, on=['code'], how='left') print("#############") # 使用pandas 函数 : https://pandas.pydata.org/pandas-docs/stable/api.html#id4 data_new["return"] = data_new["return"].mul(100) # 扩大100 倍方便观察 data_new["mov_vol"] = data_new["mov_vol"].mul(100) data_new = data_new.round(2) # 数据保留2位小数 # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`guess_return_daily` WHERE `date`= '%s' " % datetime_int common.insert(del_sql) # data_new["down_rate"] = (data_new["trade"] - data_new["wave_mean"]) / data_new["wave_base"] common.insert_db(data_new, "guess_return_daily", False, "`date`,`code`") # 进行左连接. # tmp = pd.merge(tmp, tmp2, on=['company_id'], how='left') def apply_guess(tmp): date = tmp["date"] code = tmp["code"] date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock is None: return pd.Series([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, code, date, 0.0, 0.0], index=['10d', '20d', '5-10d', '5-20d', '5d', '60d', 'code', 'date', 'mov_vol', 'return']) stock = pd.DataFrame({"close": stock["close"]}, index=stock.index.values) stock = stock.sort_index(0) # 将数据按照日期排序下。 # print(stock.head(10)) # 5周期、10周期、20周期和60周期 # 周线、半月线、月线和季度线 stock["5d"] = stock["close"].rolling(window=5).mean() # 周线 stock["10d"] = stock["close"].rolling(window=10).mean() # 半月线 stock["20d"] = stock["close"].rolling(window=20).mean() # 月线 stock["60d"] = stock["close"].rolling(window=60).mean() # 季度线 # 计算日期差。 stock["5-10d"] = (stock["5d"] - stock["10d"]) * 100 / stock["10d"] # 周-半月线差 stock["5-20d"] = (stock["5d"] - stock["20d"]) * 100 / stock["20d"] # 周-月线差 # 计算股票的收益价格 stock["return"] = np.log(stock["close"] / stock["close"].shift(1)) # print(stock["return"]) # 计算股票的【收益率的移动历史标准差】 mov_day = int(len(stock) / 20) # print("mov_day:", mov_day, len(stock)) stock["mov_vol"] = stock["return"].rolling(window=mov_day).std() * math.sqrt(mov_day) # print(stock["mov_vol"].tail()) # print(stock["return"].tail()) # print("stock[10d].tail(1)", stock["10d"].tail(1).values[0]) # 10d 20d 5-10d 5-20d 5d 60d code date mov_vol return tmp = pd.Series([stock["10d"].tail(1).values[0], stock["20d"].tail(1).values[0], stock["5-10d"].tail(1).values[0], stock["5-20d"].tail(1).values[0], stock["5d"].tail(1).values[0], stock["60d"].tail(1).values[0], code, date, stock["mov_vol"].tail(1).values[0], stock["return"].tail(1).values[0]], index=['10d', '20d', '5-10d', '5-20d', '5d', '60d', 'code', 'date', 'mov_vol', 'return']) # print(tmp) return tmp # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_index_all) ================================================ FILE: backend/old_jobs/guess_sklearn_ma_daily_job.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import libs.common as common import pandas as pd import numpy as np import math import datetime import sklearn as skl from sklearn import datasets, linear_model # https://github.com/udacity/machine-learning/issues/202 # sklearn.cross_validation 这个包不推荐使用了。 from sklearn.model_selection import train_test_split, cross_val_score from sklearn.neighbors import KNeighborsClassifier # 要操作的数据库表名称。 table_name = "guess_sklearn_ma_daily" # 批处理数据。 def stat_all_batch(tmp_datetime): datetime_str = (tmp_datetime).strftime("%Y-%m-%d") datetime_int = (tmp_datetime).strftime("%Y%m%d") print("datetime_str:", datetime_str) print("datetime_int:", datetime_int) try: # 删除老数据。 del_sql = " DELETE FROM `stock_data`.`%s` WHERE `date`= %s " % (table_name, datetime_int) print("del_sql:", del_sql) common.insert(del_sql) except Exception as e: print("error :", e) sql_count = """ SELECT count(1) FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `name` not like %s """ # 修改逻辑,增加中小板块计算。 中小板:002,创业板:300 。and `code` not like %s and `code` not like %s and `name` not like %s # count = common.select_count(sql_count, params=[datetime_int, '002%', '300%', '%st%']) count = common.select_count(sql_count, params=[datetime_int, '300%', '%st%']) print("count :", count) batch_size = 100 end = int(math.ceil(float(count) / batch_size) * batch_size) print(end) # for i in range(0, end, batch_size): for i in range(0, end, batch_size): print("loop :", i) # 查询今日满足股票数据。剔除数据:创业板股票数据,中小板股票数据,所有st股票 # #`code` not like '002%' and `code` not like '300%' and `name` not like '%st%' sql_1 = """ SELECT `date`, `code`, `name`, `changepercent`, `trade`, `open`, `high`, `low`, `settlement`, `volume`, `turnoverratio`, `amount`, `per`, `pb`, `mktcap`, `nmc` FROM stock_data.ts_today_all WHERE `date` = %s and `trade` > 0 and `open` > 0 and trade <= 20 and `code` not like %s and `name` not like %s limit %s , %s """ print(sql_1) # data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '002%', '300%', '%st%', i, batch_size]) data = pd.read_sql(sql=sql_1, con=common.engine(), params=[datetime_int, '300%', '%st%', i, batch_size]) data = data.drop_duplicates(subset="code", keep="last") print("########data[trade]########:", len(data)) # 使用 trade 填充数据 stock_sklearn = pd.DataFrame({ "date": data["date"], "code": data["code"], "next_close": data["trade"], "sklearn_score": data["trade"]}, index=data.index.values) print(stock_sklearn.head()) stock_sklearn_apply = stock_sklearn.apply(apply_sklearn, axis=1) # , axis=1) # 重命名 del stock_sklearn_apply["date"] # 合并前删除 date 字段。 # 合并数据 data_new = pd.merge(data, stock_sklearn_apply, on=['code'], how='left') # for index, row in data.iterrows(): # next_stock, score = stat_index_all(row, i) # print(next_stock, score) data_new["next_close"] = data_new["next_close"].round(2) # 数据保留4位小数 data_new["sklearn_score"] = data_new["sklearn_score"].round(2) # 数据保留2位小数 data_new["trade_float32"] = data["trade"].astype('float32', copy=False) data_new["up_rate"] = (data_new["next_close"] - data_new["trade_float32"]) * 100 / data_new["trade_float32"] data_new["up_rate"] = data_new["up_rate"].round(2) # 数据保留2位小数 del data_new["trade_float32"] try: common.insert_db(data_new, table_name, False, "`date`,`code`") print("insert_db") except Exception as e: print("error :", e) # 重命名 del data_new["name"] print(data_new) # code date next_close sklearn_score def apply_sklearn(data): # 要操作的数据库表名称。 print("########stat_index_all########:", len(data)) date = data["date"] code = data["code"] print(date, code) date_end = datetime.datetime.strptime(date, "%Y%m%d") date_start = (date_end + datetime.timedelta(days=-300)).strftime("%Y-%m-%d") date_end = date_end.strftime("%Y-%m-%d") print(code, date_start, date_end) # open high close low volume price_change p_change ma5 ma10 ma20 v_ma5 v_ma10 v_ma20 turnover stock_X = common.get_hist_data_cache(code, date_start, date_end) # 增加空判断,如果是空返回 0 数据。 if stock_X is None: return list([code, date, 0.0, 0.0]) stock_X = stock_X.sort_index(0) # 将数据按照日期排序下。 stock_y = pd.Series(stock_X["close"].values) # 标签 stock_X_next = stock_X.iloc[len(stock_X) - 1] print("########################### stock_X_next date:", stock_X_next) # 使用今天的交易价格,13 个指标预测明天的价格。偏移股票数据,今天的数据,目标是明天的价格。 stock_X = stock_X.drop(stock_X.index[len(stock_X) - 1]) # 删除最后一条数据 stock_y = stock_y.drop(stock_y.index[0]) # 删除第一条数据 # print("########################### stock_X date:", stock_X) # 删除掉close 也就是收盘价格。 del stock_X["close"] del stock_X_next["close"] model = linear_model.LinearRegression() # model = KNeighborsClassifier() model.fit(stock_X.values, stock_y) # print("############## test_akshare & target #############") # print("############## coef_ & intercept_ #############") # print(model.coef_) # 系数 # print(model.intercept_) # 截断 next_close = model.predict([stock_X_next.values]) if len(next_close) == 1: next_close = next_close[0] sklearn_score = model.score(stock_X.values, stock_y) print("score:", sklearn_score) # 评分 return list([code, date, next_close, sklearn_score * 100]) # main函数入口 if __name__ == '__main__': # 使用方法传递。 tmp_datetime = common.run_with_args(stat_all_batch) ================================================ FILE: backend/supervisor/example_supervisord_conf ================================================ ; Sample supervisor config file. ; ; For more information on the config file, please see: ; http://supervisord.org/configuration.html ; ; Notes: ; - Shell expansion ("~" or "$HOME") is not supported. Environment ; variables can be expanded using this syntax: "%(ENV_HOME)s". ; - Quotes around values are not supported, except in the case of ; the environment= options as shown below. ; - Comments must have a leading space: "a=b ;comment" not "a=b;comment". ; - Command will be truncated if it looks like a config file comment, e.g. ; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ". [unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file ;chmod=0700 ; socket file mode (default 0700) ;chown=nobody:nogroup ; socket file uid:gid owner ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server) ;[inet_http_server] ; inet (TCP) server disabled by default ;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server) [supervisord] logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid nodaemon=false ; start in foreground if true; default false minfds=1024 ; min. avail startup file descriptors; default 1024 minprocs=200 ; min. avail process descriptors;default 200 ;umask=022 ; process file creation umask; default 022 ;user=chrism ; default is current user, required if root ;identifier=supervisor ; supervisord identifier, default is 'supervisor' ;directory=/tmp ; default is not to cd during start ;nocleanup=true ; don't clean up tempfiles at start; default false ;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP ;environment=KEY="value" ; key value pairs to add to environment ;strip_ansi=false ; strip ansi escape codes in logs; def. false ; The rpcinterface:supervisor section must remain in the config file for ; RPC (supervisorctl/web interface) to work. Additional interfaces may be ; added by defining them in separate [rpcinterface:x] sections. [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface ; The supervisorctl section configures how supervisorctl will connect to ; supervisord. configure it match the settings in either the unix_http_server ; or inet_http_server section. [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket ;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket ;username=chris ; should be same as in [*_http_server] if set ;password=123 ; should be same as in [*_http_server] if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor") ;history_file=~/.sc_history ; use readline history if available ; The sample program section below shows all possible program subsection values. ; Create one or more 'real' program: sections to be able to control them under ; supervisor. ;[program:theprogramname] ;command=/bin/cat ; the program (relative uses PATH, can take args) ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) ;numprocs=1 ; number of processes copies to start (def 1) ;directory=/tmp ; directory to cwd to before exec (def no cwd) ;umask=022 ; umask for process (default None) ;priority=999 ; the relative start priority (default 999) ;autostart=true ; start at supervisord start (default: true) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; when to restart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;user=chrism ; setuid to this UNIX account to run the program ;redirect_stderr=true ; redirect proc stderr to stdout (default false) ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) ;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10) ;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;environment=A="1",B="2" ; process environment additions (def no adds) ;serverurl=AUTO ; override serverurl computation (childutils) ; The sample eventlistener section below shows all possible eventlistener ; subsection values. Create one or more 'real' eventlistener: sections to be ; able to handle event notifications sent by supervisord. ;[eventlistener:theeventlistenername] ;command=/bin/eventlistener ; the program (relative uses PATH, can take args) ;process_name=%(program_name)s ; process_name expr (default %(program_name)s) ;numprocs=1 ; number of processes copies to start (def 1) ;events=EVENT ; event notif. types to subscribe to (req'd) ;buffer_size=10 ; event buffer queue size (default 10) ;directory=/tmp ; directory to cwd to before exec (def no cwd) ;umask=022 ; umask for process (default None) ;priority=-1 ; the relative start priority (default -1) ;autostart=true ; start at supervisord start (default: true) ;startsecs=1 ; # of secs prog must stay up to be running (def. 1) ;startretries=3 ; max # of serial start failures when starting (default 3) ;autorestart=unexpected ; autorestart if exited after running (def: unexpected) ;exitcodes=0,2 ; 'expected' exit codes used with autorestart (default 0,2) ;stopsignal=QUIT ; signal used to kill process (default TERM) ;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10) ;stopasgroup=false ; send stop signal to the UNIX process group (default false) ;killasgroup=false ; SIGKILL the UNIX process group (def false) ;user=chrism ; setuid to this UNIX account to run the program ;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners ;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO ;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10) ;stdout_events_enabled=false ; emit events on stdout writes (default false) ;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO ;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB) ;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10) ;stderr_events_enabled=false ; emit events on stderr writes (default false) ;environment=A="1",B="2" ; process environment additions ;serverurl=AUTO ; override serverurl computation (childutils) ; The sample group section below shows all possible group values. Create one ; or more 'real' group: sections to create "heterogeneous" process groups. ;[group:thegroupname] ;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions ;priority=999 ; the relative start priority (default 999) ; The [include] section can just contain the "files" setting. This ; setting can list multiple files (separated by whitespace or ; newlines). It can also contain wildcards. The filenames are ; interpreted as relative to this file. Included files *cannot* ; include files themselves. ;[include] ;files = relative/directory/*.ini ================================================ FILE: backend/supervisor/supervisord.conf ================================================ [unix_http_server] file=/tmp/supervisor.sock ; the path to the socket file [inet_http_server] ; inet (TCP) server disabled by default port=*:9001 ; ip_address:port specifier, *:port for all iface ;username=user ; default is no username (open server) ;password=123 ; default is no password (open server) [supervisord] logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB logfile_backups=10 ; # of main logfile backups; 0 means none, default 10 loglevel=info ; log level; default info; others: debug,warn,trace pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid nodaemon=false ; start in foreground if true; default false minfds=1024 ; min. avail startup file descriptors; default 1024 minprocs=200 ; min. avail process descriptors;default 200 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket [program:init] command=/data/stock/jobs/run_init.sh autostart=true autorestart=true startsecs=20 priority=1 stopasgroup=true killasgroup=true [program:cron] command=/data/stock/jobs/run_cron.sh autostart=true autorestart=true startsecs=20 priority=1 stopasgroup=true killasgroup=true [program:stock-web] command=/data/stock/jobs/run_web.sh autostart=true autorestart=true startsecs=20 priority=1 stopasgroup=true killasgroup=true ================================================ FILE: backend/web/README.md ================================================ ================================================ FILE: backend/web/base.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import tornado.web import libs.stock_web_dic as stock_web_dic import libs.common as common import logging #基础handler,主要负责检查mysql的数据库链接。 class BaseHandler(tornado.web.RequestHandler): def set_default_headers(self): headers = self.request.headers # logging.info('head的类型:',type(headers)) origin = headers.get('origin',None) logging.info("######################## BaseHandler ########################") logging.info(origin) if origin != None and origin.find("localhost") > 0: self.set_header("Access-Control-Allow-Credentials", "true") self.set_header("Access-Control-Allow-Origin",origin) self.set_header("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") self.set_header("Access-Control-Allow-Headers", "x-token, authorization, Authorization, Content-Type, Access-Control-Allow-Origin, Access-Control-Allow-Headers, X-Requested-By, Access-Control-Allow-Methods") self.set_header("Access-Control-Expose-Headers", "Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma") # 同时定义一个option方法 def options(self): self.set_status(204) self.finish() @property def db(self): try: # check every time。 self.application.db.query("SELECT 1 ") except Exception as e: print(e) self.application.db.reconnect() return self.application.db class LeftMenu: def __init__(self, url): self.leftMenuList = stock_web_dic.STOCK_WEB_DATA_LIST self.current_url = url # 获得左菜单。 def GetLeftMenu(url): return LeftMenu(url) ================================================ FILE: backend/web/chartHandler.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- from tornado import gen import libs.stock_web_dic as stock_web_dic import web.base as webBase import libs.common as common import logging import tornado.web import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import numpy as np import io def GenImage(freq): t = np.linspace(0, 10, 500) y = np.sin(t * freq * 2 * 3.141) fig1 = plt.figure() plt.plot(t, y) plt.xlabel('Time [s]') memdata = io.BytesIO() plt.grid(True) plt.savefig(memdata, format='png') image = memdata.getvalue() return image class ImageHandler(tornado.web.RequestHandler): @gen.coroutine def get(self): image = GenImage(0.5) self.set_header('Content-type', 'image/png') self.set_header('Content-length', len(image)) self.write(image) # 获得页面数据。 class GetChartHtmlHandler(webBase.BaseHandler): @gen.coroutine def get(self): name = self.get_argument("table_name", default=None, strip=False) #stockWeb = stock_web_dic.STOCK_WEB_DATA_MAP[name] # self.uri_ = ("self.request.url:", self.request.uri) # print self.uri_ logging.info("chart...") self.render("stock_chart.html", entries="", pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) ================================================ FILE: backend/web/dataEditorHandler.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- from tornado import gen # import sys # import os # sys.path.append(os.path.abspath('/data/stock/libs')) import libs.stock_web_dic as stock_web_dic import web.base as webBase import libs.common as common import logging import re # 获得页面数据。 class GetEditorHtmlHandler(webBase.BaseHandler): @gen.coroutine def get(self): name = self.get_argument("table_name", default=None, strip=False) stockWeb = stock_web_dic.STOCK_WEB_DATA_MAP[name] # self.uri_ = ("self.request.url:", self.request.uri) # print self.uri_ self.render("data_editor.html", stockWeb=stockWeb, pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) # 拼接sql,将value的key 和 value 放到一起。 def genSql(primary_key, param_map, join_string): tmp_sql = "" idx = 0 for tmp_key in primary_key: tmp_val = param_map[tmp_key] if idx == 0: tmp_sql = " `%s` = '%s' " % (tmp_key, tmp_val) else: tmp_sql += join_string + (" `%s` = '%s' " % (tmp_key, tmp_val)) idx += 1 return tmp_sql # 获得页面数据。 class SaveEditorHandler(webBase.BaseHandler): @gen.coroutine def post(self): action = self.get_argument("action", default=None, strip=False) logging.info(action) table_name = self.get_argument("table_name", default=None, strip=False) stockWeb = stock_web_dic.STOCK_WEB_DATA_MAP[table_name] # 临时map数组。 param_map = {} # 支持多排序。使用shift+鼠标左键。 for item, val in self.request.arguments.items(): # 正则查找 data[1112][code] 里面的code字段 item_key = re.search(r"\]\[(.*?)\]", item) if item_key: tmp_1 = item_key.group() if tmp_1: tmp_1 = tmp_1.replace("][", "").replace("]", "") param_map[tmp_1] = val[0].decode("utf-8") #logging.info(param_map) if action == "create": logging.info("###########################create") # 拼接where 和 update 语句。 tmp_columns = "`, `".join(stockWeb.columns) tmp_values = [] for tmp_key in stockWeb.columns: tmp_values.append(param_map[tmp_key]) # 更新sql。 tmp_values2 = "', '".join(tmp_values) insert_sql = " INSERT INTO %s (`%s`) VALUES('%s'); " % (stockWeb.table_name, tmp_columns, tmp_values2) logging.info(insert_sql) try: self.db.execute(insert_sql) except Exception as e: err = {"error": str(e)} logging.info(err) self.write(err) return elif action == "edit": logging.info("###########################edit") # 拼接where 和 update 语句。 tmp_update = genSql(stockWeb.columns, param_map, ",") tmp_where = genSql(stockWeb.primary_key, param_map, "and") # 更新sql。 update_sql = " UPDATE %s SET %s WHERE %s " % (stockWeb.table_name, tmp_update, tmp_where) logging.info(update_sql) try: self.db.execute(update_sql) except Exception as e: err = {"error": str(e)} logging.info(err) self.write(err) return elif action == "remove": logging.info("###########################remove") # 拼接where 语句。 tmp_where = genSql(stockWeb.primary_key, param_map, "and") # 更新sql。 delete_sql = " DELETE FROM %s WHERE %s " % (stockWeb.table_name, tmp_where) logging.info(delete_sql) try: self.db.execute(delete_sql) except Exception as e: err = {"error": str(e)} logging.info(err) self.write(err) return self.write("{\"data\":[{}]}") ================================================ FILE: backend/web/dataIndicatorsHandler.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- from tornado import gen import web.base as webBase import logging # 首映 bokeh 画图。 from bokeh.plotting import figure from bokeh.embed import components import datetime import libs.common as common import stockstats import numpy as np import pandas as pd from bokeh.layouts import gridplot from bokeh.palettes import Category20 from math import radians from bokeh.models import DatetimeTickFormatter # 获得页面数据。 class GetDataIndicatorsHandler(webBase.BaseHandler): @gen.coroutine def get(self): code = self.get_argument("code", default=None, strip=False) logging.info(code) # self.uri_ = ("self.request.url:", self.request.uri) # print self.uri_ comp_list = [] try: date_now = datetime.datetime.now() date_end = date_now.strftime("%Y-%m-%d") date_start = (date_now + datetime.timedelta(days=-100)).strftime("%Y-%m-%d") print(code, date_start, date_end) # open, high, close, low, volume, price_change, p_change, ma5, ma10, ma20, v_ma5, v_ma10, v_ma20, turnover # 使用缓存方法。加快计算速度。 stock = common.get_hist_data_cache(code, date_start, date_end) logging.info(stock.head(1)) # print(stock) [186 rows x 14 columns] # 初始化统计类 # stockStat = stockstats.StockDataFrame.retype(pd.read_csv("002032.csv")) stockStat = stockstats.StockDataFrame.retype(stock) batch_add(comp_list, stockStat) except Exception as e: logging.info("error :", e) logging.info("#################### GetStockHtmlHandlerEnd ####################") self.render("stock_indicators.html", comp_list=comp_list, pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) # 全部指标数据汇总 indicators_all_dic = [ { "title": "1,交易量delta指标分析", "desc": "The Volume Delta (Vol ∆) ", "dic": ["volume", "volume_delta"] }, { "title": "2,计算n天差", "desc": "可以计算,向前n天,和向后n天的差。", "dic": ["close", "close_1_d", "close_2_d", "close_-1_d", "close_-2_d"] }, { "title": "3,n天涨跌百分百计算", "desc": "可以看到,-n天数据和今天数据的百分比。", "dic": ["close", "close_-1_r", "close_-2_r"] }, { "title": "4,CR指标", "desc": """ http://wiki.mbalib.com/wiki/CR%E6%8C%87%E6%A0%87 价格动量指标 4. CR跌穿a、b、c、d四条线,再由低点向上爬升160时,为短线获利的一个良机,应适当卖出股票。 5. CR跌至40以下时,是建仓良机。而CR高于300~400时,应注意适当减仓。 """, "dic": ["close","cr","cr-ma1","cr-ma2","cr-ma3"] }, { "title": "5,最大值,最小值", "desc": """ 计算区间最大值 volume max of three days ago, yesterday and two days later stock["volume_-3,2,-1_max"] volume min between 3 days ago and tomorrow stock["volume_-3~1_min"] 实际使用的时候使用 -2~2 可计算出5天的最大,最小值。 """, "dic": ["volume","volume_-2~2_max","volume_-2~2_min"] }, { "title": "6,KDJ指标", "desc": """ http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV,然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度,以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 """, "dic": ["close","kdjk","kdjd","kdjj"] }, { "title": "7,SMA指标", "desc": """ http://wiki.mbalib.com/wiki/Sma 简单移动平均线(Simple Moving Average,SMA) 可以动态输入参数,获得几天的移动平均。 """, "dic": ["close","close_5_sma","close_10_sma"] }, { "title": "8,MACD指标", "desc": """ http://wiki.mbalib.com/wiki/MACD 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 MACD stock["macd"] MACD signal line stock["macds"] MACD histogram stock["macdh"] MACD技术分析,运用DIF线与MACD线之相交型态及直线棒高低点与背离现象,作为买卖讯号,尤其当市场股价走势呈一较为明确波段趋势时, MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 """, "dic": ["close","macd","macds","macdh"] }, { "title": "9,BOLL指标", "desc": """ http://wiki.mbalib.com/wiki/BOLL 布林线指标(Bollinger Bands) bolling, including upper band and lower band stock["boll"] stock["boll_ub"] stock["boll_lb"] 1、当布林线开口向上后,只要股价K线始终运行在布林线的中轨上方的时候,说明股价一直处在一个中长期上升轨道之中,这是BOLL指标发出的持股待涨信号,如果TRIX指标也是发出持股信号时,这种信号更加准确。此时,投资者应坚决持股待涨。 2、当布林线开口向下后,只要股价K线始终运行在布林线的中轨下方的时候,说明股价一直处在一个中长期下降轨道之中,这是BOLL指标发出的持币观望信号,如果TRIX指标也是发出持币信号时,这种信号更加准确。此时,投资者应坚决持币观望。 """, "dic": ["close","boll","boll_ub","boll_lb"] }, { "title": "10,RSI指标", "desc": """ http://wiki.mbalib.com/wiki/RSI 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 6 days RSI stock["rsi_6"] 12 days RSI stock["rsi_12"] (2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象,如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 (4)当六日强弱指标下降至20时,表示股市有超卖现象,如果一旦继续下降至10以下时则表示已到严重超卖区域,股价极可能有止跌回升的机会。 """, "dic": ["close","rsi_6","rsi_12"] }, { "title": "11,WR指标", "desc": """ http://wiki.mbalib.com/wiki/%E5%A8%81%E5%BB%89%E6%8C%87%E6%A0%87 威廉指数(Williams%Rate)该指数是利用摆动点来度量市场的超买超卖现象。 10 days WR stock["wr_10"] 6 days WR stock["wr_6"] """, "dic": ["close","wr_10","wr_6"] }, { "title": "12,CCI指标", "desc": """ http://wiki.mbalib.com/wiki/%E9%A1%BA%E5%8A%BF%E6%8C%87%E6%A0%87 顺势指标又叫CCI指标,其英文全称为“Commodity Channel Index”, 是由美国股市分析家唐纳德·蓝伯特(Donald Lambert)所创造的,是一种重点研判股价偏离度的股市分析工具。 CCI, default to 14 days stock["cci"] 20 days CCI stock["cci_20"] 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, 中短线应及时买入,如果有比较大的成交量配合,买入信号则更为可靠。 2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, 将进入一个比较长的寻底过程,投资者应以持币观望为主。 """, "dic": ["close","cci","cci_20"] }, { "title": "13,TR、ATR指标", "desc": """ http://wiki.mbalib.com/wiki/%E5%9D%87%E5%B9%85%E6%8C%87%E6%A0%87 均幅指标(Average True Ranger,ATR) 均幅指标(ATR)是取一定时间周期内的股价波动幅度的移动平均值,主要用于研判买卖时机。 TR (true range) stock["tr"] ATR (Average True Range) stock["atr"] 均幅指标无论是从下向上穿越移动平均线,还是从上向下穿越移动平均线时,都是一种研判信号。 """, "dic": ["close","tr","atr"] }, { "title": "14,DMA指标", "desc": """ http://wiki.mbalib.com/wiki/DMA DMA指标(Different of Moving Average)又叫平行线差指标,是目前股市分析技术指标中的一种中短期指标,它常用于大盘指数和个股的研判。 DMA, difference of 10 and 50 moving average stock["dma"] """, "dic": ["close","dma"] }, { "title": "15,DMI,+DI,-DI,DX,ADX,ADXR指标", "desc": """ http://wiki.mbalib.com/wiki/DMI 动向指数Directional Movement Index,DMI) http://wiki.mbalib.com/wiki/ADX 平均趋向指标(Average Directional Indicator,简称ADX) http://wiki.mbalib.com/wiki/%E5%B9%B3%E5%9D%87%E6%96%B9%E5%90%91%E6%8C%87%E6%95%B0%E8%AF%84%E4%BC%B0 平均方向指数评估(ADXR)实际是今日ADX与前面某一日的ADX的平均值。ADXR在高位与ADX同步下滑,可以增加对ADX已经调头的尽早确认。 ADXR是ADX的附属产品,只能发出一种辅助和肯定的讯号,并非入市的指标,而只需同时配合动向指标(DMI)的趋势才可作出买卖策略。 在应用时,应以ADX为主,ADXR为辅。 """, "dic": ["close","pdi","mdi","dx","adx","adxr"] }, { "title": "16,TRIX,MATRIX指标", "desc": """ http://wiki.mbalib.com/wiki/TRIX TRIX指标又叫三重指数平滑移动平均指标(Triple Exponentially Smoothed Average) """, "dic": ["close","trix","trix_9_sma"] }, { "title": "17,VR,MAVR指标", "desc": """ http://wiki.mbalib.com/wiki/%E6%88%90%E4%BA%A4%E9%87%8F%E6%AF%94%E7%8E%87 成交量比率(Volumn Ratio,VR)(简称VR),是一项通过分析股价上升日成交额(或成交量,下同)与股价下降日成交额比值, 从而掌握市场买卖气势的中期技术指标。 VR, default to 26 days stock["vr"] MAVR is the simple moving average of VR stock["vr_6_sma"] """, "dic": ["close","vr","vr_6_sma"] } ] # 配置数据 indicators_dic = [ { "title": "6,KDJ指标", "desc": """ http://wiki.mbalib.com/wiki/%E9%9A%8F%E6%9C%BA%E6%8C%87%E6%A0%87 随机指标(KDJ)一般是根据统计学的原理,通过一个特定的周期(常为9日、9周等)内出现过的最高价、最低价及最后一个计算周期的收盘价及这三者之间的比例关系,来计算最后一个计算周期的未成熟随机值RSV,然后根据平滑移动平均线的方法来计算K值、D值与J值,并绘成曲线图来研判股票走势。 (3)在使用中,常有J线的指标,即3乘以K值减2乘以D值(3K-2D=J),其目的是求出K值与D值的最大乖离程度,以领先KD值找出底部和头部。J大于100时为超买,小于10时为超卖。 """, "dic": ["close","kdjk","kdjd","kdjj"] }, { "title": "7,SMA指标", "desc": """ http://wiki.mbalib.com/wiki/Sma 简单移动平均线(Simple Moving Average,SMA) 可以动态输入参数,获得几天的移动平均。 """, "dic": ["close","close_5_sma","close_10_sma"] }, { "title": "8,MACD指标", "desc": """ http://wiki.mbalib.com/wiki/MACD 平滑异同移动平均线(Moving Average Convergence Divergence,简称MACD指标),也称移动平均聚散指标 MACD stock["macd"] MACD signal line stock["macds"] MACD histogram stock["macdh"] MACD技术分析,运用DIF线与MACD线之相交型态及直线棒高低点与背离现象,作为买卖讯号,尤其当市场股价走势呈一较为明确波段趋势时, MACD 则可发挥其应有的功能,但当市场呈牛皮盘整格局,股价不上不下时,MACD买卖讯号较不明显。 当用MACD作分析时,亦可运用其他的技术分析指标如短期 K,D图形作为辅助工具,而且也可对买卖讯号作双重的确认。 """, "dic": ["close","macd","macds","macdh"] }, { "title": "9,BOLL指标", "desc": """ http://wiki.mbalib.com/wiki/BOLL 布林线指标(Bollinger Bands) bolling, including upper band and lower band stock["boll"] stock["boll_ub"] stock["boll_lb"] 1、当布林线开口向上后,只要股价K线始终运行在布林线的中轨上方的时候,说明股价一直处在一个中长期上升轨道之中,这是BOLL指标发出的持股待涨信号,如果TRIX指标也是发出持股信号时,这种信号更加准确。此时,投资者应坚决持股待涨。 2、当布林线开口向下后,只要股价K线始终运行在布林线的中轨下方的时候,说明股价一直处在一个中长期下降轨道之中,这是BOLL指标发出的持币观望信号,如果TRIX指标也是发出持币信号时,这种信号更加准确。此时,投资者应坚决持币观望。 """, "dic": ["close","boll","boll_ub","boll_lb"] }, { "title": "10,RSI指标", "desc": """ http://wiki.mbalib.com/wiki/RSI 相对强弱指标(Relative Strength Index,简称RSI),也称相对强弱指数、相对力度指数 6 days RSI stock["rsi_6"] 12 days RSI stock["rsi_12"] (2)强弱指标保持高于50表示为强势市场,反之低于50表示为弱势市场。 (3)强弱指标多在70与30之间波动。当六日指标上升到达80时,表示股市已有超买现象,如果一旦继续上升,超过90以上时,则表示已到严重超买的警戒区,股价已形成头部,极可能在短期内反转回转。 (4)当六日强弱指标下降至20时,表示股市有超卖现象,如果一旦继续下降至10以下时则表示已到严重超卖区域,股价极可能有止跌回升的机会。 """, "dic": ["close","rsi_6","rsi_12"] },{ "title": "12,CCI指标", "desc": """ http://wiki.mbalib.com/wiki/%E9%A1%BA%E5%8A%BF%E6%8C%87%E6%A0%87 顺势指标又叫CCI指标,其英文全称为“Commodity Channel Index”, 是由美国股市分析家唐纳德·蓝伯特(Donald Lambert)所创造的,是一种重点研判股价偏离度的股市分析工具。 CCI, default to 14 days stock["cci"] 20 days CCI stock["cci_20"] 1、当CCI指标从下向上突破﹢100线而进入非常态区间时,表明股价脱离常态而进入异常波动阶段, 中短线应及时买入,如果有比较大的成交量配合,买入信号则更为可靠。 2、当CCI指标从上向下突破﹣100线而进入另一个非常态区间时,表明股价的盘整阶段已经结束, 将进入一个比较长的寻底过程,投资者应以持币观望为主。 """, "dic": ["close","cci","cci_20"] } ] # 批量添加数据。 def batch_add(comp_list, stockStat): for conf in indicators_dic: logging.info(conf) comp_list.append(add_plot(stockStat, conf)) # 增加画图方法 def add_plot(stockStat, conf): p_list = [] logging.info("############################", type(conf["dic"])) # 循环 多个line 信息。 for key, val in enumerate(conf["dic"]): logging.info(key) logging.info(val) p1 = figure(width=1000, height=150, x_axis_type="datetime") # add renderers stockStat["date"] = pd.to_datetime(stockStat.index.values) # ["volume","volume_delta"] # 设置20个颜色循环,显示0 2 4 6 号序列。 p1.line(stockStat["date"], stockStat[val], color=Category20[20][key * 2]) # Set date format for x axis 格式化。 p1.xaxis.formatter = DatetimeTickFormatter( hours=["%Y-%m-%d"], days=["%Y-%m-%d"], months=["%Y-%m-%d"], years=["%Y-%m-%d"]) # p1.xaxis.major_label_orientation = radians(30) #可以旋转一个角度。 p_list.append([p1]) gp = gridplot(p_list) script, div = components(gp) return { "script": script, "div": div, "title": conf["title"], "desc": conf["desc"] } ================================================ FILE: backend/web/dataTableHandler.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import json from tornado import gen import libs.common as common import libs.stock_web_dic as stock_web_dic import web.base as webBase import logging import datetime # info 蓝色 云财经 # success 绿色 # danger 红色 东方财富 # warning 黄色 WEB_EASTMONEY_URL = u""" 东财 指标 东研 """ # 和在dic中的字符串一致。字符串前面都不特别声明是u"" eastmoney_name = "查看股票" # 获得页面数据,进入页面中。 class GetStockHtmlHandler(webBase.BaseHandler): @gen.coroutine def get(self): name = self.get_argument("table_name", default=None, strip=False) tableInfo = stock_web_dic.STOCK_WEB_DATA_MAP[name] # self.uri_ = ("self.request.url:", self.request.uri) # print self.uri_ date_now = datetime.datetime.now() date_now_str = date_now.strftime("%Y%m%d") # 每天的 16 点前显示昨天数据。 if date_now.hour < 16: date_now_str = (date_now + datetime.timedelta(days=-1)).strftime("%Y%m%d") # try: # # 增加columns 字段中的【查看股票 东方财富】 # logging.info(eastmoney_name in tableInfo.column_names) # if eastmoney_name in tableInfo.column_names: # tmp_idx = tableInfo.column_names.index(eastmoney_name) # logging.info(tmp_idx) # try: # # 防止重复插入数据。可能会报错。 # tableInfo.columns.remove("eastmoney_url") # except Exception as e: # print("error :", e) # tableInfo.columns.insert(tmp_idx, "eastmoney_url") # except Exception as e: # print("error :", e) logging.info("####################GetStockHtmlHandlerEnd") self.render("tableInfo.html", tableInfo=tableInfo, date_now=date_now_str, pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) # 获得股票数据内容。 class GetStockDataHandler(webBase.BaseHandler): def get(self): self.set_header('Content-Type', 'application/json;charset=UTF-8') logging.info("######################## GetStockDataHandler ########################") # 获得分页参数。 page_param = self.get_argument("page", default=0, strip=False) limit_param = self.get_argument("limit", default=10, strip=False) name_param = self.get_argument("name", default="stock_zh_ah_name", strip=False) type_param = self.get_argument("type", default=None, strip=False) date_param = self.get_argument("date", default=None, strip=False) code_param = self.get_argument("code", default=None, strip=False) logging.info(f"page param: {page_param}, {limit_param}, {type_param}, {date_param}, {code_param}") if name_param == ":tableName": obj = { "code": 20000, "message": "success", "draw": 0, "data": [] } # logging.info("####################") # logging.info(obj) self.write(json.dumps(obj)) return tableInfo = stock_web_dic.STOCK_WEB_DATA_MAP[name_param] order_by_column = [] order_by_dir = [] # 支持多排序。使用shift+鼠标左键。 for item, val in self.request.arguments.items(): # logging.info("item: %s, val: %s" % (item, val) ) if str(item).startswith("order["): print("order:", item, ",val:", val[0]) if str(item).startswith("order[") and str(item).endswith("[column]"): order_by_column.append(int(val[0])) if str(item).startswith("order[") and str(item).endswith("[dir]"): order_by_dir.append(val[0].decode("utf-8")) # bytes转换字符串 search_by_column = [] search_by_data = [] # 返回search字段。 for item, val in self.request.arguments.items(): # logging.info("item: %s, val: %s" % (item, val)) if str(item).startswith("columns[") and str(item).endswith("[search][value]"): logging.info("item: %s, val: %s" % (item, val)) str_idx = item.replace("columns[", "").replace("][search][value]", "") int_idx = int(str_idx) # 找到字符串 str_val = val[0].decode("utf-8") if str_val != "": # 字符串。 search_by_column.append(tableInfo.columns[int_idx]) search_by_data.append(val[0].decode("utf-8")) # bytes转换字符串 # 打印日志。 search_sql = "" search_idx = 0 logging.info("################# search_by_column #################") logging.info(search_by_column) logging.info(search_by_data) for item in search_by_column: val = search_by_data[search_idx] logging.info("idx: %s, column: %s, value: %s " % (search_idx, item, val)) # 查询sql if search_idx == 0: search_sql = " WHERE `%s` = '%s' " % (item, val) else: search_sql = search_sql + " AND `%s` = '%s' " % (item, val) search_idx = search_idx + 1 if date_param: if "WHERE" not in search_sql: search_sql += f" WHERE `date` = '{date_param}' " else: search_sql += f" AND `date` = '{date_param}' " if code_param: if "WHERE" not in search_sql: search_sql += f" WHERE `code` = '{code_param}' " else: search_sql += f" AND `code` = '{code_param}' " # print("tableInfo :", stock_web) order_by_sql = "" # 增加排序。 if len(order_by_column) != 0 and len(order_by_dir) != 0: order_by_sql = " ORDER BY " idx = 0 for key in order_by_column: # 找到排序字段和dir。 col_tmp = tableInfo.columns[key] dir_tmp = order_by_dir[idx] if idx != 0: order_by_sql += " , %s %s" % (col_tmp, dir_tmp) else: order_by_sql += " %s %s" % (col_tmp, dir_tmp) idx += 1 # 查询数据库。 limit_sql = "" if int(limit_param) > 0: start = ( int(page_param) - 1 ) * int(limit_param) limit_sql = f" LIMIT {start} , {limit_param} " sql = " SELECT * FROM `%s` %s %s %s " % ( tableInfo.table_name, search_sql, order_by_sql, limit_sql) count_sql = " SELECT count(1) as num FROM `%s` %s " % (tableInfo.table_name, search_sql) logging.info("select sql : " + sql) logging.info("count sql : " + count_sql) stock_web_list = self.db.query(sql) stock_web_size = self.db.query(count_sql) logging.info("tableInfoList size : %s " % stock_web_size) # 动态表格展示: table_columns = [] try: tmp_len = len(tableInfo.columns) logging.info("ableInfo.columns tmp_len : %s " % tmp_len) # 循环数据,转换成对象,放入到数组中,方便前端 vue table 循环使用。 for tmp_idx in range(0, tmp_len): logging.info(tmp_idx) column = tableInfo.columns[tmp_idx] column_name = tableInfo.column_names[tmp_idx] tpm_column_obj = { "column": column, "columnName" : column_name } table_columns.append(tpm_column_obj) except Exception as e: print("error :", e) obj = { "code": 20000, "message": "success", "draw": 0, "tableName" : tableInfo.name, "tableColumns": table_columns, "total": stock_web_size[0]["num"], "recordsTotal": stock_web_size[0]["num"], "recordsFiltered": stock_web_size[0]["num"], "data": stock_web_list } # logging.info("####################") # logging.info(obj) self.write(json.dumps(obj)) ================================================ FILE: backend/web/demo-chart.py ================================================ """ This example demonstrates how to embed matplotlib WebAgg interactive plotting in your own web application and framework. It is not necessary to do all this if you merely want to display a plot in a browser or use matplotlib's built-in Tornado-based server "on the side". The framework being used must support web sockets. """ import io try: import tornado except ImportError: raise RuntimeError("This example requires tornado.") import tornado.web import tornado.httpserver import tornado.ioloop import tornado.websocket from matplotlib.backends.backend_webagg_core import ( FigureManagerWebAgg, new_figure_manager_given_figure) from matplotlib.figure import Figure import numpy as np import json def create_figure(): """ Creates a simple example figure. """ fig = Figure() a = fig.add_subplot(111) t = np.arange(0.0, 3.0, 0.01) s = np.sin(2 * np.pi * t) a.plot(t, s) return fig # The following is the content of the web page. You would normally # generate this using some sort of template facility in your web # framework, but here we just use Python string formatting. html_content = """ matplotlib
""" class MyApplication(tornado.web.Application): class MainPage(tornado.web.RequestHandler): """ Serves the main HTML page. """ def get(self): manager = self.application.manager ws_uri = "ws://{req.host}/".format(req=self.request) content = html_content % { "ws_uri": ws_uri, "fig_id": manager.num} self.write(content) class MplJs(tornado.web.RequestHandler): """ Serves the generated matplotlib javascript file. The content is dynamically generated based on which toolbar functions the user has defined. Call `FigureManagerWebAgg` to get its content. """ def get(self): self.set_header('Content-Type', 'application/javascript') js_content = FigureManagerWebAgg.get_javascript() self.write(js_content) class Download(tornado.web.RequestHandler): """ Handles downloading of the figure in various file formats. """ def get(self, fmt): manager = self.application.manager mimetypes = { 'ps': 'application/postscript', 'eps': 'application/postscript', 'pdf': 'application/pdf', 'svg': 'image/svg+xml', 'png': 'image/png', 'jpeg': 'image/jpeg', 'tif': 'image/tiff', 'emf': 'application/emf' } self.set_header('Content-Type', mimetypes.get(fmt, 'binary')) buff = io.BytesIO() manager.canvas.print_figure(buff, format=fmt) self.write(buff.getvalue()) class WebSocket(tornado.websocket.WebSocketHandler): """ A websocket for interactive communication between the plot in the browser and the server. In addition to the methods required by tornado, it is required to have two callback methods: - ``send_json(json_content)`` is called by matplotlib when it needs to send json to the browser. `json_content` is a JSON tree (Python dictionary), and it is the responsibility of this implementation to encode it as a string to send over the socket. - ``send_binary(blob)`` is called to send binary image data to the browser. """ supports_binary = True def open(self): # Register the websocket with the FigureManager. manager = self.application.manager manager.add_web_socket(self) if hasattr(self, 'set_nodelay'): self.set_nodelay(True) def on_close(self): # When the socket is closed, deregister the websocket with # the FigureManager. manager = self.application.manager manager.remove_web_socket(self) def on_message(self, message): # The 'supports_binary' message is relevant to the # websocket itself. The other messages get passed along # to matplotlib as-is. # Every message has a "type" and a "figure_id". message = json.loads(message) if message['type'] == 'supports_binary': self.supports_binary = message['value'] else: manager = self.application.manager manager.handle_json(message) def send_json(self, content): self.write_message(json.dumps(content)) def send_binary(self, blob): if self.supports_binary: self.write_message(blob, binary=True) else: data_uri = "data:image/png;base64,{0}".format( blob.encode('base64').replace('\n', '')) self.write_message(data_uri) def __init__(self, figure): self.figure = figure self.manager = new_figure_manager_given_figure( id(figure), figure) super(MyApplication, self).__init__([ # Static files for the CSS and JS (r'/_static/(.*)', tornado.web.StaticFileHandler, {'path': FigureManagerWebAgg.get_static_file_path()}), # The page that contains all of the pieces ('/', self.MainPage), ('/mpl.js', self.MplJs), # Sends images and events to the browser, and receives # events from the browser ('/ws', self.WebSocket), # Handles the downloading (i.e., saving) of static images (r'/download.([a-z0-9.]+)', self.Download), ], debug=True) if __name__ == "__main__": figure = create_figure() application = MyApplication(figure) http_server = tornado.httpserver.HTTPServer(application) http_server.listen(9999) print("http://127.0.0.1:9090/") print("Press Ctrl+C to quit") tornado.ioloop.IOLoop.instance().start() ================================================ FILE: backend/web/main.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import os.path import torndb import tornado.escape from tornado import gen import tornado.httpserver import tornado.ioloop import tornado.options import libs.common as common import libs.stock_web_dic as stock_web_dic import web.dataTableHandler as dataTableHandler import web.dataEditorHandler as dataEditorHandler import web.dataIndicatorsHandler as dataIndicatorsHandler import web.base as webBase import pandas as pd import numpy as np import akshare as ak import bokeh as bh import sqlalchemy import json class Application(tornado.web.Application): def __init__(self): handlers = [ # 设置路由 (r"/", HomeHandler), (r"/stock/", HomeHandler), (r"/api/v1/package_verison", PackageVersionHandler),# 包版本 (r"/api/v1/menu_list", MenuListHandler), # 菜单接口 (r"/test_akshare", TestHandler),# 测试页面,做写js 测试。 (r"/test2", Test2Handler),# 测试页面,做写js 测试。 # 使用datatable 展示报表数据模块。 (r"/api/v1/api_data", dataTableHandler.GetStockDataHandler), (r"/stock/data", dataTableHandler.GetStockHtmlHandler), # 数据修改dataEditor。 (r"/data/editor", dataEditorHandler.GetEditorHtmlHandler), (r"/data/editor/save", dataEditorHandler.SaveEditorHandler), # 获得股票指标数据。 (r"/api/v1/data/indicators", dataIndicatorsHandler.GetDataIndicatorsHandler), ] settings = dict( # 配置 template_path=os.path.join(os.path.dirname(__file__), "templates"), static_path=os.path.join(os.path.dirname(__file__), "static"), xsrf_cookies=False, # True, # cookie加密 cookie_secret="027bb1b670eddf0392cdda8709268a17b58b7", debug=True, default_encoding="utf-8", ) super(Application, self).__init__(handlers, **settings) # Have one global connection to the blog DB across all handlers self.db = torndb.Connection( charset="utf8", max_idle_time=3600, connect_timeout=1000, host=common.MYSQL_HOST, database=common.MYSQL_DB, user=common.MYSQL_USER, password=common.MYSQL_PWD) # 获得包版本 handler。 class PackageVersionHandler(webBase.BaseHandler): @gen.coroutine def get(self): pandasVersion = pd.__version__ numpyVersion = np.__version__ sqlalchemyVersion = sqlalchemy.__version__ akshareVersion = ak.__version__ bokehVersion = bh.__version__ # 返回包的版本信息。 obj = { "code": 20000, "message": "success", "pandasVersion" : pandasVersion, "numpyVersion" : numpyVersion, "sqlalchemyVersion" : sqlalchemyVersion, "akshareVersion" : akshareVersion, "bokehVersion" : bokehVersion, "stockstatsVersion": "0.3.2" } # logging.info("####################") # logging.info(obj) self.write(json.dumps(obj)) # 获得菜单列表数据 handler。 class MenuListHandler(webBase.BaseHandler): @gen.coroutine def get(self): leftMenuList = stock_web_dic.STOCK_WEB_DATA_LIST out_data = [] menu_name = '' menu_children = [] index = 0 for table_info in leftMenuList: print(table_info.name) index = index + 1 # 使用 children 作为二级菜单。 tmp_menu = { "name": table_info.name, "path": "/stock/table/" + table_info.table_name } menu_children.append(tmp_menu) # 使用 type作为 一级目录 if menu_name != table_info.type or index == len(leftMenuList): # 进行数据循环 if menu_name != '' : if index != len(leftMenuList): menu_children.pop() # 删除当前的节点信息。 tmp_children = list(menu_children) tmp_menu2 = { "name": menu_name, "path": "#", "children": tmp_children } # 下一个数据清空和放置。 menu_children = [] menu_children.append(tmp_menu) out_data.append(tmp_menu2) menu_name = table_info.type obj = { "code": 20000, "message": "success", "data": out_data } print(out_data) # self.write(json.dumps(o self.write(json.dumps(obj)) # 首页handler。 class HomeHandler(webBase.BaseHandler): @gen.coroutine def get(self): print("################## index.html ##################") pandasVersion = pd.__version__ numpyVersion = np.__version__ akshareVersion = ak.__version__ bokehVersion = bh.__version__ #stockstatsVersion = ss.__version__ # 没有这个函数,但是好久不更新了 # https://github.com/jealous/stockstats self.render("index.html", pandasVersion=pandasVersion, numpyVersion=numpyVersion, akshareVersion=akshareVersion, bokehVersion=bokehVersion, stockstatsVersion="0.3.2", pythonStockVersion = common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) class TestHandler(webBase.BaseHandler): @gen.coroutine def get(self): self.render("test_akshare.html", entries="hello", pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) class Test2Handler(webBase.BaseHandler): @gen.coroutine def get(self): self.render("test2.html", entries="hello", pythonStockVersion=common.__version__, leftMenu=webBase.GetLeftMenu(self.request.uri)) def main(): tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(Application()) port = 9090 http_server.listen(port) # tornado.options.options.logging = "debug" tornado.options.parse_command_line() tornado.ioloop.IOLoop.current().start() if __name__ == "__main__": main() ================================================ FILE: backend/web/minstServingHandler.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import os.path import json import subprocess import torndb import tornado.escape from tornado import gen import tornado.httpserver import tornado.ioloop import tornado.options import tornado.web import web.base as webBase import logging import numpy as np from PIL import Image from PIL import ImageOps import base64 import io #python2 import StringIO work_dir = "/data/stock/tf/minst_serving/input_data" out_dir = "/static/img/minst_serving/%s.bmp" # 获得页面数据。 class GetMinstServingHtmlHandler(webBase.BaseHandler): @gen.coroutine def get(self): # print self.uri_ arr = np.arange(30) image_array = [] for idx in arr: out_file = out_dir % ("%05d" % idx) print(out_file) image_array.append(out_file) self.render("minst_serving.html", image_array=image_array) # 获得股票数据内容。 class GetPredictionDataHandler(webBase.BaseHandler): def get(self): # 获得分页参数。 img_url = self.get_argument("img_url", default=0, strip=False) print(img_url) img_obj = Image.open("/data/stock/web" + img_url) print("img_obj", img_obj) server = "0.0.0.0:8500" prediction = do_inference(server, img_obj) print('######### prediction : ', prediction) self.write(json.dumps(prediction)) # 获得股票数据内容。 class GetPrediction2DataHandler(webBase.BaseHandler): def post(self): # 获得分页参数。 imgStr = self.get_argument("txt", default="", strip=False) # imgStr.replace(" ", "+") imgStr = base64.b64decode(imgStr) print("imgStr:", type(imgStr)) image = Image.open(io.StringIO(imgStr)) image.thumbnail((28, 28), Image.ANTIALIAS) image = image.convert('L') image = ImageOps.invert(image) image.save(work_dir + "/web-tmp.bmp", format="BMP") #保存看看,是否 #print(image) # img_url = self.get_argument("img_url", default=0, strip=False) # print(img_url) server = "0.0.0.0:8500" prediction = do_inference(server, image) print('######### prediction : ', prediction) self.write(json.dumps(prediction)) # 调用 grpc 代码,将图片转换成数组,让后放到 grpc 调用。 def do_inference(hostport, img_obj): print("############", hostport) ================================================ FILE: backend/web/static/css/fonts.googleapis.com.css ================================================ @font-face { font-family: 'Open Sans'; font-style: normal; font-weight: 300; src: local('Open Sans Light'), local('OpenSans-Light'), url(/static/font-awesome/opensans/v13/DXI1ORHCpsQm3Vp6mXoaTXhCUOGz7vYGh680lGh-uXM.woff) format('woff'); } @font-face { font-family: 'Open Sans'; font-style: normal; font-weight: 400; src: local('Open Sans'), local('OpenSans'), url(/static/font-awesome/opensans/v13/cJZKeOuBrn4kERxqtaUH3T8E0i7KZn-EPnyo3HZu7kw.woff) format('woff'); } ================================================ FILE: backend/web/static/js/bootbox.js ================================================ /** * bootbox.js [v4.4.0] * * http://bootboxjs.com/license.txt */ !function(a,b){"use strict";"function"==typeof define&&define.amd?define(["jquery"],b):"object"==typeof exports?module.exports=b(require("jquery")):a.bootbox=b(a.jQuery)}(this,function a(b,c){"use strict";function d(a){var b=q[o.locale];return b?b[a]:q.en[a]}function e(a,c,d){a.stopPropagation(),a.preventDefault();var e=b.isFunction(d)&&d.call(c,a)===!1;e||c.modal("hide")}function f(a){var b,c=0;for(b in a)c++;return c}function g(a,c){var d=0;b.each(a,function(a,b){c(a,b,d++)})}function h(a){var c,d;if("object"!=typeof a)throw new Error("Please supply an object of options");if(!a.message)throw new Error("Please specify a message");return a=b.extend({},o,a),a.buttons||(a.buttons={}),c=a.buttons,d=f(c),g(c,function(a,e,f){if(b.isFunction(e)&&(e=c[a]={callback:e}),"object"!==b.type(e))throw new Error("button with key "+a+" must be an object");e.label||(e.label=a),e.className||(2>=d&&f===d-1?e.className="btn-primary":e.className="btn-default")}),a}function i(a,b){var c=a.length,d={};if(1>c||c>2)throw new Error("Invalid argument length");return 2===c||"string"==typeof a[0]?(d[b[0]]=a[0],d[b[1]]=a[1]):d=a[0],d}function j(a,c,d){return b.extend(!0,{},a,i(c,d))}function k(a,b,c,d){var e={className:"bootbox-"+a,buttons:l.apply(null,b)};return m(j(e,d,c),b)}function l(){for(var a={},b=0,c=arguments.length;c>b;b++){var e=arguments[b],f=e.toLowerCase(),g=e.toUpperCase();a[f]={label:d(g)}}return a}function m(a,b){var d={};return g(b,function(a,b){d[b]=!0}),g(a.buttons,function(a){if(d[a]===c)throw new Error("button key "+a+" is not allowed (options are "+b.join("\n")+")")}),a}var n={dialog:"",header:"",footer:"",closeButton:"",form:"
",inputs:{text:"",textarea:"",email:"",select:"",checkbox:"
",date:"",time:"",number:"",password:""}},o={locale:"en",backdrop:"static",animate:!0,className:null,closeButton:!0,show:!0,container:"body"},p={};p.alert=function(){var a;if(a=k("alert",["ok"],["message","callback"],arguments),a.callback&&!b.isFunction(a.callback))throw new Error("alert requires callback property to be a function when provided");return a.buttons.ok.callback=a.onEscape=function(){return b.isFunction(a.callback)?a.callback.call(this):!0},p.dialog(a)},p.confirm=function(){var a;if(a=k("confirm",["cancel","confirm"],["message","callback"],arguments),a.buttons.cancel.callback=a.onEscape=function(){return a.callback.call(this,!1)},a.buttons.confirm.callback=function(){return a.callback.call(this,!0)},!b.isFunction(a.callback))throw new Error("confirm requires a callback");return p.dialog(a)},p.prompt=function(){var a,d,e,f,h,i,k;if(f=b(n.form),d={className:"bootbox-prompt",buttons:l("cancel","confirm"),value:"",inputType:"text"},a=m(j(d,arguments,["title","callback"]),["cancel","confirm"]),i=a.show===c?!0:a.show,a.message=f,a.buttons.cancel.callback=a.onEscape=function(){return a.callback.call(this,null)},a.buttons.confirm.callback=function(){var c;switch(a.inputType){case"text":case"textarea":case"email":case"select":case"date":case"time":case"number":case"password":c=h.val();break;case"checkbox":var d=h.find("input:checked");c=[],g(d,function(a,d){c.push(b(d).val())})}return a.callback.call(this,c)},a.show=!1,!a.title)throw new Error("prompt requires a title");if(!b.isFunction(a.callback))throw new Error("prompt requires a callback");if(!n.inputs[a.inputType])throw new Error("invalid prompt type");switch(h=b(n.inputs[a.inputType]),a.inputType){case"text":case"textarea":case"email":case"date":case"time":case"number":case"password":h.val(a.value);break;case"select":var o={};if(k=a.inputOptions||[],!b.isArray(k))throw new Error("Please pass an array of input options");if(!k.length)throw new Error("prompt with select requires options");g(k,function(a,d){var e=h;if(d.value===c||d.text===c)throw new Error("given options in wrong format");d.group&&(o[d.group]||(o[d.group]=b("").attr("label",d.group)),e=o[d.group]),e.append("")}),g(o,function(a,b){h.append(b)}),h.val(a.value);break;case"checkbox":var q=b.isArray(a.value)?a.value:[a.value];if(k=a.inputOptions||[],!k.length)throw new Error("prompt with checkbox requires options");if(!k[0].value||!k[0].text)throw new Error("given options in wrong format");h=b("
"),g(k,function(c,d){var e=b(n.inputs[a.inputType]);e.find("input").attr("value",d.value),e.find("label").append(d.text),g(q,function(a,b){b===d.value&&e.find("input").prop("checked",!0)}),h.append(e)})}return a.placeholder&&h.attr("placeholder",a.placeholder),a.pattern&&h.attr("pattern",a.pattern),a.maxlength&&h.attr("maxlength",a.maxlength),f.append(h),f.on("submit",function(a){a.preventDefault(),a.stopPropagation(),e.find(".btn-primary").click()}),e=p.dialog(a),e.off("shown.bs.modal"),e.on("shown.bs.modal",function(){h.focus()}),i===!0&&e.modal("show"),e},p.dialog=function(a){a=h(a);var d=b(n.dialog),f=d.find(".modal-dialog"),i=d.find(".modal-body"),j=a.buttons,k="",l={onEscape:a.onEscape};if(b.fn.modal===c)throw new Error("$.fn.modal is not defined; please double check you have included the Bootstrap JavaScript library. See http://getbootstrap.com/javascript/ for more details.");if(g(j,function(a,b){k+="",l[a]=b.callback}),i.find(".bootbox-body").html(a.message),a.animate===!0&&d.addClass("fade"),a.className&&d.addClass(a.className),"large"===a.size?f.addClass("modal-lg"):"small"===a.size&&f.addClass("modal-sm"),a.title&&i.before(n.header),a.closeButton){var m=b(n.closeButton);a.title?d.find(".modal-header").prepend(m):m.css("margin-top","-10px").prependTo(i)}return a.title&&d.find(".modal-title").html(a.title),k.length&&(i.after(n.footer),d.find(".modal-footer").html(k)),d.on("hidden.bs.modal",function(a){a.target===this&&d.remove()}),d.on("shown.bs.modal",function(){d.find(".btn-primary:first").focus()}),"static"!==a.backdrop&&d.on("click.dismiss.bs.modal",function(a){d.children(".modal-backdrop").length&&(a.currentTarget=d.children(".modal-backdrop").get(0)),a.target===a.currentTarget&&d.trigger("escape.close.bb")}),d.on("escape.close.bb",function(a){l.onEscape&&e(a,d,l.onEscape)}),d.on("click",".modal-footer button",function(a){var c=b(this).data("bb-handler");e(a,d,l[c])}),d.on("click",".bootbox-close-button",function(a){e(a,d,l.onEscape)}),d.on("keyup",function(a){27===a.which&&d.trigger("escape.close.bb")}),b(a.container).append(d),d.modal({backdrop:a.backdrop?"static":!1,keyboard:!1,show:!1}),a.show&&d.modal("show"),d},p.setDefaults=function(){var a={};2===arguments.length?a[arguments[0]]=arguments[1]:a=arguments[0],b.extend(o,a)},p.hideAll=function(){return b(".bootbox").modal("hide"),p};var q={bg_BG:{OK:"Ок",CANCEL:"Отказ",CONFIRM:"Потвърждавам"},br:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Sim"},cs:{OK:"OK",CANCEL:"Zrušit",CONFIRM:"Potvrdit"},da:{OK:"OK",CANCEL:"Annuller",CONFIRM:"Accepter"},de:{OK:"OK",CANCEL:"Abbrechen",CONFIRM:"Akzeptieren"},el:{OK:"Εντάξει",CANCEL:"Ακύρωση",CONFIRM:"Επιβεβαίωση"},en:{OK:"OK",CANCEL:"Cancel",CONFIRM:"OK"},es:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Aceptar"},et:{OK:"OK",CANCEL:"Katkesta",CONFIRM:"OK"},fa:{OK:"قبول",CANCEL:"لغو",CONFIRM:"تایید"},fi:{OK:"OK",CANCEL:"Peruuta",CONFIRM:"OK"},fr:{OK:"OK",CANCEL:"Annuler",CONFIRM:"D'accord"},he:{OK:"אישור",CANCEL:"ביטול",CONFIRM:"אישור"},hu:{OK:"OK",CANCEL:"Mégsem",CONFIRM:"Megerősít"},hr:{OK:"OK",CANCEL:"Odustani",CONFIRM:"Potvrdi"},id:{OK:"OK",CANCEL:"Batal",CONFIRM:"OK"},it:{OK:"OK",CANCEL:"Annulla",CONFIRM:"Conferma"},ja:{OK:"OK",CANCEL:"キャンセル",CONFIRM:"確認"},lt:{OK:"Gerai",CANCEL:"Atšaukti",CONFIRM:"Patvirtinti"},lv:{OK:"Labi",CANCEL:"Atcelt",CONFIRM:"Apstiprināt"},nl:{OK:"OK",CANCEL:"Annuleren",CONFIRM:"Accepteren"},no:{OK:"OK",CANCEL:"Avbryt",CONFIRM:"OK"},pl:{OK:"OK",CANCEL:"Anuluj",CONFIRM:"Potwierdź"},pt:{OK:"OK",CANCEL:"Cancelar",CONFIRM:"Confirmar"},ru:{OK:"OK",CANCEL:"Отмена",CONFIRM:"Применить"},sq:{OK:"OK",CANCEL:"Anulo",CONFIRM:"Prano"},sv:{OK:"OK",CANCEL:"Avbryt",CONFIRM:"OK"},th:{OK:"ตกลง",CANCEL:"ยกเลิก",CONFIRM:"ยืนยัน"},tr:{OK:"Tamam",CANCEL:"İptal",CONFIRM:"Onayla"},zh_CN:{OK:"OK",CANCEL:"取消",CONFIRM:"确认"},zh_TW:{OK:"OK",CANCEL:"取消",CONFIRM:"確認"}};return p.addLocale=function(a,c){return b.each(["OK","CANCEL","CONFIRM"],function(a,b){if(!c[b])throw new Error("Please supply a translation for '"+b+"'")}),q[a]={OK:c.OK,CANCEL:c.CANCEL,CONFIRM:c.CONFIRM},p},p.removeLocale=function(a){return delete q[a],p},p.setLocale=function(a){return p.setDefaults("locale",a)},p.init=function(c){return a(c||b)},p}); ================================================ FILE: backend/web/static/js/bootstrap-datepicker.zh-CN.js ================================================ /** * Simplified Chinese translation for bootstrap-datepicker * Yuan Cheung */ ;(function($){ $.fn.datepicker.dates['zh-CN'] = { days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"], daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"], daysMin: ["日", "一", "二", "三", "四", "五", "六"], months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"], monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"], today: "今日", clear: "清除", format: "yyyy年mm月dd日", titleFormat: "yyyy年mm月", weekStart: 1 }; }(jQuery)); ================================================ FILE: backend/web/static/js/datatables.Chinese.json ================================================ { "sProcessing": "处理中...", "sLengthMenu": "显示 _MENU_ 项结果", "sZeroRecords": "没有匹配结果", "sInfo": "显示第 _START_ 至 _END_ 项结果,共 _TOTAL_ 项", "sInfoEmpty": "显示第 0 至 0 项结果,共 0 项", "sInfoFiltered": "(由 _MAX_ 项结果过滤)", "sInfoPostFix": "", "sSearch": "搜索:", "sUrl": "", "sEmptyTable": "表中数据为空", "sLoadingRecords": "载入中...", "sInfoThousands": ",", "oPaginate": { "sFirst": "首页", "sPrevious": "上页", "sNext": "下页", "sLast": "末页" }, "oAria": { "sSortAscending": ": 以升序排列此列", "sSortDescending": ": 以降序排列此列" } } ================================================ FILE: backend/web/static/js/draw.js ================================================ var drawing = false; var context; var offset_left = 0; var offset_top = 0; function start_canvas () { var scribbler = document.getElementById ("the_stage"); context = scribbler.getContext ("2d"); scribbler.onmousedown = function (event) {mousedown(event)}; scribbler.onmousemove = function (event) {mousemove(event)}; scribbler.onmouseup = function (event) {mouseup(event)}; for (var o = scribbler; o ; o = o.offsetParent) { offset_left += (o.offsetLeft - o.scrollLeft); offset_top += (o.offsetTop - o.scrollTop); } draw(); } function getPosition(evt) { evt = (evt) ? evt : ((event) ? event : null); var left = 0; var top = 0; var scribbler = document.getElementById("the_stage"); if (evt.pageX) { left = evt.pageX; top = evt.pageY; } else if (document.documentElement.scrollLeft) { left = evt.clientX + document.documentElement.scrollLeft; top = evt.clientY + document.documentElement.scrollTop; } else { left = evt.clientX + document.body.scrollLeft; top = evt.clientY + document.body.scrollTop; } left -= offset_left; top -= offset_top; return {x : left, y : top}; } function mousedown(event) { drawing = true; var location = getPosition(event); context.lineWidth = 20.0; context.strokeStyle="#000000"; context.beginPath(); context.moveTo(location.x,location.y); } function mousemove(event) { if (!drawing) return; var location = getPosition(event); context.lineTo(location.x,location.y); context.stroke(); } function mouseup(event) { if (!drawing) return; mousemove(event); drawing = false; } function draw() { context.fillStyle = '#ffffff'; context.fillRect(0, 0, 400, 400); } function clearCanvas() { context.clearRect (0, 0, 400, 400); draw(); document.getElementById("rec_result").innerHTML = ""; } onload = start_canvas; ================================================ FILE: backend/web/static/js/grid.locale-en.js ================================================ !function(a){"use strict";"function"==typeof define&&define.amd?define(["jquery","../grid.base"],a):a(jQuery)}(function(a){a.jgrid=a.jgrid||{},a.jgrid.hasOwnProperty("regional")||(a.jgrid.regional=[]),a.jgrid.regional.en={defaults:{recordtext:"View {0} - {1} of {2}",emptyrecords:"No records to view",loadtext:"Loading...",savetext:"Saving...",pgtext:"Page {0} of {1}",pgfirst:"First Page",pglast:"Last Page",pgnext:"Next Page",pgprev:"Previous Page",pgrecs:"Records per Page",showhide:"Toggle Expand Collapse Grid",pagerCaption:"Grid::Page Settings",pageText:"Page:",recordPage:"Records per Page",nomorerecs:"No more records...",scrollPullup:"Pull up to load more...",scrollPulldown:"Pull down to refresh...",scrollRefresh:"Release to refresh..."},search:{caption:"Search...",Find:"Find",Reset:"Reset",odata:[{oper:"eq",text:"equal"},{oper:"ne",text:"not equal"},{oper:"lt",text:"less"},{oper:"le",text:"less or equal"},{oper:"gt",text:"greater"},{oper:"ge",text:"greater or equal"},{oper:"bw",text:"begins with"},{oper:"bn",text:"does not begin with"},{oper:"in",text:"is in"},{oper:"ni",text:"is not in"},{oper:"ew",text:"ends with"},{oper:"en",text:"does not end with"},{oper:"cn",text:"contains"},{oper:"nc",text:"does not contain"},{oper:"nu",text:"is null"},{oper:"nn",text:"is not null"}],groupOps:[{op:"AND",text:"all"},{op:"OR",text:"any"}],operandTitle:"Click to select search operation.",resetTitle:"Reset Search Value"},edit:{addCaption:"Add Record",editCaption:"Edit Record",bSubmit:"Submit",bCancel:"Cancel",bClose:"Close",saveData:"Data has been changed! Save changes?",bYes:"Yes",bNo:"No",bExit:"Cancel",msg:{required:"Field is required",number:"Please, enter valid number",minValue:"value must be greater than or equal to ",maxValue:"value must be less than or equal to",email:"is not a valid e-mail",integer:"Please, enter valid integer value",date:"Please, enter valid date value",url:"is not a valid URL. Prefix required ('http://' or 'https://')",nodefined:" is not defined!",novalue:" return value is required!",customarray:"Custom function should return array!",customfcheck:"Custom function should be present in case of custom checking!"}},view:{caption:"View Record",bClose:"Close"},del:{caption:"Delete",msg:"Delete selected record(s)?",bSubmit:"Delete",bCancel:"Cancel"},nav:{edittext:"",edittitle:"Edit selected row",addtext:"",addtitle:"Add new row",deltext:"",deltitle:"Delete selected row",searchtext:"",searchtitle:"Find records",refreshtext:"",refreshtitle:"Reload Grid",alertcap:"Warning",alerttext:"Please, select row",viewtext:"",viewtitle:"View selected row",savetext:"",savetitle:"Save row",canceltext:"",canceltitle:"Cancel row editing",selectcaption:"Actions..."},col:{caption:"Select columns",bSubmit:"Ok",bCancel:"Cancel"},errors:{errcap:"Error",nourl:"No url is set",norecords:"No records to process",model:"Length of colNames <> colModel!"},formatter:{integer:{thousandsSeparator:",",defaultValue:"0"},number:{decimalSeparator:".",thousandsSeparator:",",decimalPlaces:2,defaultValue:"0.00"},currency:{decimalSeparator:".",thousandsSeparator:",",decimalPlaces:2,prefix:"",suffix:"",defaultValue:"0.00"},date:{dayNames:["Sun","Mon","Tue","Wed","Thr","Fri","Sat","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],monthNames:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec","January","February","March","April","May","June","July","August","September","October","November","December"],AmPm:["am","pm","AM","PM"],S:function(a){return 11>a||a>13?["st","nd","rd","th"][Math.min((a-1)%10,3)]:"th"},srcformat:"Y-m-d",newformat:"n/j/Y",parseRe:/[#%\\\/:_;.,\t\s-]/,masks:{ISO8601Long:"Y-m-d H:i:s",ISO8601Short:"Y-m-d",ShortDate:"n/j/Y",LongDate:"l, F d, Y",FullDateTime:"l, F d, Y g:i:s A",MonthDay:"F d",ShortTime:"g:i A",LongTime:"g:i:s A",SortableDateTime:"Y-m-d\\TH:i:s",UniversalSortableDateTime:"Y-m-d H:i:sO",YearMonth:"F, Y"},reformatAfterEdit:!1,userLocalTime:!1},baseLinkUrl:"",showAction:"",target:"",checkbox:{disabled:!0},idName:"id"}}}); ================================================ FILE: backend/web/static/update_bokeh.sh ================================================ rm -f ./js/bokeh.min.js rm -f ./js/bokeh-api.min.js rm -f ./js/bokeh-gl.min.js rm -f ./js/bokeh-tables.min.js rm -f ./js/bokeh-widgets.min.js cp /usr/local/lib/python3.7/site-packages/bokeh/server/static/js/bokeh.min.js ./js/ cp /usr/local/lib/python3.7/site-packages/bokeh/server/static/js/bokeh-api.min.js ./js/ cp /usr/local/lib/python3.7/site-packages/bokeh/server/static/js/bokeh-gl.min.js ./js/ cp /usr/local/lib/python3.7/site-packages/bokeh/server/static/js/bokeh-tables.min.js ./js/ cp /usr/local/lib/python3.7/site-packages/bokeh/server/static/js/bokeh-widgets.min.js ./js/ ================================================ FILE: backend/web/templates/bokeh_embed.html ================================================ Embedding a Bokeh Server With {{ framework }}
This Bokeh app below served by a Bokeh server that has been embedded in another web app framework. For more information see the section Embedding Bokeh Server as a Library in the User's Guide.
{{ script|safe }} ================================================ FILE: backend/web/templates/common/footer.html ================================================ {% block footer %} {% end %} ================================================ FILE: backend/web/templates/common/header.html ================================================ {% block header %} {% end %} ================================================ FILE: backend/web/templates/common/left_menu.html ================================================ {% block left_menu %} {% end %} ================================================ FILE: backend/web/templates/common/meta.html ================================================ {% block meta %} {% end %} ================================================ FILE: backend/web/templates/data_editor.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

{{ stockWeb.name }}

{{ stockWeb.name }}
 
{% for index,element in enumerate(stockWeb.columns) %}
{{ stockWeb.column_names[index] }}  
{% end %}
{% for column_name in stockWeb.column_names %} {% end %}
{{ column_name }}
{% end %} ================================================ FILE: backend/web/templates/index.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

开源Python全栈股票系统,数据抓取、统计分析、报表展示。

基础库版本

1,pandas使用【 {{ pandasVersion }} 】版本, 中文文档

2,numpy使用【 {{ numpyVersion }} 】版本, 中文文档

3,akshare使用【 {{ akshareVersion }} 】版本, 中文文档

4,bokeh使用【 {{ bokehVersion }} 】版本, 官方文档

5,stockstats使用【 {{ stockstatsVersion }} 】版本, 官方文档

相关资料信息

1,github项目地址。pythonstock

2,博客地址。博客地址

2021年9月20日更新,发布2.0版本

1,修复bokeh的版本升级问题,可以显示趋势数据了。

2,使用 stock_zh_ah_name 做每日数据。

3,AkShare 升级到 1.1.9 版本。

2021年8月31日更新job服务

1,过滤包括:600,6006,601,000,001,002,且不包括ST的股票数据。。

2,使用 stock_zh_ah_name 做每日数据。

3,AkShare 升级到 1.0.80 版本。

2021年6月3日使用 AkShare 做数据抓取服务

1,使用 stock_zh_a_spot 做实时行情数据。

2,使用 stock_zh_a_daily 做历史数据统计。

3,升级基础镜像使用python3.7,AkShare 的 0.9.65 版本。

{% end %} ================================================ FILE: backend/web/templates/layout/default.html ================================================ {% extends "../common/meta.html" %} {% extends "../common/header.html" %} {% extends "../common/footer.html" %} {% extends "../common/left_menu.html" %} {% extends "main.html" %} ================================================ FILE: backend/web/templates/layout/indicators-main.html ================================================ 股票系统 {% block meta %}{% end %}
{% block main_content %}{% end %}
================================================ FILE: backend/web/templates/layout/indicators.html ================================================ {% extends "../common/meta.html" %} {% extends "indicators-main.html" %} ================================================ FILE: backend/web/templates/layout/main.html ================================================ 股票系统 {{ pythonStockVersion }} {% block meta %}{% end %} {% block header %}{% end %}
{% block left_menu %}{% end %}
{% block main_content %}{% end %}
================================================ FILE: backend/web/templates/layout/single_default.html ================================================ {% extends "../common/meta.html" %} {% extends "../common/header.html" %} {% extends "../common/footer.html" %} {% extends "single_main.html" %} ================================================ FILE: backend/web/templates/layout/single_main.html ================================================ {% block meta %}{% end %}
{% block main_content %}{% end %}
================================================ FILE: backend/web/templates/minst_serving.html ================================================ {% extends "layout/single_default.html" %} {% block main_content %}

手写图片识别演示

your browser don't support canvas!

result:

测试图片识别演示

{% for image in image_array %}
{% end %}

结果:

"-1"

{% end %} ================================================ FILE: backend/web/templates/stock_chart.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

欢迎使用股票系统。

{% end %} ================================================ FILE: backend/web/templates/stock_indicators.html ================================================ {% extends "layout/indicators.html" %} {% block main_content %} {% for index,element in enumerate(comp_list) %}

{{ element["title"] }}

{{ element["desc"] }}
{% raw element["div"] %} {% raw element["script"] %}
{% end %} {% end %} ================================================ FILE: backend/web/templates/stock_web.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

{{ stockWeb.name }}

{{ stockWeb.name }}
 
{% for index,element in enumerate(stockWeb.columns) %} {% if index < 15 %} {% if element != 'eastmoney_url' %}
{{ stockWeb.column_names[index] }}  
{% end %} {% end %}{% end %}
{% for column_name in stockWeb.column_names %} {% end %}
{{ column_name }}
{% end %} ================================================ FILE: backend/web/templates/test.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

欢迎使用股票系统。

{% end %} ================================================ FILE: backend/web/templates/test2.html ================================================ {% extends "layout/default.html" %} {% block main_content %}

欢迎使用股票系统。

{% end %} ================================================ FILE: backend/web/test_thread.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import time from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, asynchronous, RequestHandler from multiprocessing.pool import ThreadPool from multiprocessing.pool import ApplyResult from tornado import gen # https://gist.github.com/methane/2185380 参考 html_content = """

任务测试


""" class MainPage(RequestHandler): def get(self): self.write(html_content) _workers = ThreadPool(10) _result = {} # 后台任务。 def blocking_task(n, tid): time.sleep(n) print(tid) _result[tid] = {"finish"} class AddJobHandler(RequestHandler): @gen.coroutine def get(self): tid = str(int(time.time() * 10000)) _workers.apply_async(blocking_task, (10, tid)) # 传递参数 10 秒。 self.write(tid) self.finish() # 先finish 掉,然后在后台执行。 class JobCheckHandler(RequestHandler): def get(self): tid = self.get_argument("tid") if tid in _result.keys(): out = _result[tid] # 结果 del _result[tid] # 删除tid的数据。 self.write(str(out)) else: self.write("") # main 启动。 if __name__ == "__main__": HTTPServer(Application([ ("/", MainPage), ("/add_job", AddJobHandler), ("/job_check", JobCheckHandler) ], debug=True)).listen(9090) print("start web .") IOLoop.instance().start() ================================================ FILE: backend/web/test_thread_v2.py ================================================ #!/usr/local/bin/python3 # -*- coding: utf-8 -*- import time from tornado.httpserver import HTTPServer from tornado.ioloop import IOLoop from tornado.web import Application, asynchronous, RequestHandler from tornado import gen from tornado.concurrent import run_on_executor from concurrent.futures import ThreadPoolExecutor # `pip install futures` for python2 # https://gist.github.com/methane/2185380 参考 html_content = """

任务测试


""" class MainPage(RequestHandler): def get(self): self.write(html_content) MAX_WORKERS = 4 _result = {} class AddJobHandler(RequestHandler): # 必须定义一个executor的属性,然后run_on_executor 注解才管用。 executor = ThreadPoolExecutor(max_workers=MAX_WORKERS) @run_on_executor # 标记成后台程序执行。 def background_task(self, tid): time.sleep(10) # 传递参数 10 秒。 _result[tid] = {"finish"} @gen.coroutine def get(self): tid = str(int(time.time() * 10000)) self.background_task(tid) self.write(tid) class JobCheckHandler(RequestHandler): def get(self): tid = self.get_argument("tid") if tid in _result.keys(): out = _result[tid] # 结果 del _result[tid] # 删除tid的数据。 self.write(str(out)) else: self.write("") # main 启动。 if __name__ == "__main__": HTTPServer(Application([ ("/", MainPage), ("/add_job", AddJobHandler), ("/job_check", JobCheckHandler) ], debug=True)).listen(9999) print("start web .") IOLoop.instance().start() ================================================ FILE: backend/web/tornado_bokeh_embed.py ================================================ from jinja2 import Environment, FileSystemLoader from tornado.web import RequestHandler from bokeh.application import Application from bokeh.application.handlers import FunctionHandler from bokeh.embed import server_document from bokeh.layouts import column from bokeh.models import ColumnDataSource, Slider from bokeh.plotting import figure from bokeh.server.server import Server from bokeh.themes import Theme from bokeh.sampledata.sea_surface_temperature import sea_surface_temperature env = Environment(loader=FileSystemLoader('templates')) server_url = "http://localhost:9090/" class IndexHandler(RequestHandler): def get(self): print("index ...") template = env.get_template('bokeh_embed.html') script = server_document(server_url + 'bkapp') print(script) self.write(template.render(script=script, template="Tornado")) # self.write(html_content) def modify_doc(doc): df = sea_surface_temperature.copy() source = ColumnDataSource(data=df) plot = figure(x_axis_type='datetime', y_range=(0, 25), y_axis_label='Temperature (Celsius)', title="Sea Surface Temperature at 43.18, -70.43") plot.line('time', 'temperature', source=source) def callback(attr, old, new): if new == 0: data = df else: data = df.rolling('{0}D'.format(new)).mean() source.data = ColumnDataSource(data=data).data slider = Slider(start=0, end=30, value=0, step=1, title="Smoothing by N Days") slider.on_change('value', callback) doc.add_root(column(slider, plot)) # doc.theme = Theme(filename="theme.yaml") bokeh_app = Application(FunctionHandler(modify_doc)) # Setting num_procs here means we can't touch the IOLoop before now, we must # let Server handle that. If you need to explicitly handle IOLoops then you # will need to use the lower level BaseServer class. server = Server( {'/bkapp': bokeh_app}, num_procs=1, port=9999, extra_patterns=[('/', IndexHandler)] ) server.start() if __name__ == '__main__': from bokeh.util.browser import view print('Opening Tornado app with embedded Bokeh application on ' + server_url) server.io_loop.add_callback(view, server_url) server.io_loop.start() ================================================ FILE: docker-compose/.gitignore ================================================ # C extensions *.so data .idea *.iml .DS_Store *.zip *.log *.pyc doc /bin pkg *.tmp ================================================ FILE: docker-compose/LICENSE ================================================ 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. ================================================ FILE: docker-compose/README.md ================================================ ## 镜像仓库选择 https://github.com/DaoCloud/public-image-mirror ```bash # python使用镜像 docker.m.daocloud.io/library/python:3.11-slim-bullseye # mysql使用: docker.m.daocloud.io/library/mysql:8 ``` ## 本地部署方法 ``` git clone git@gitee.com:pythonstock/docker-compose.git cd docker-compose docker-compose up -d ``` ## 访问地址 http://localhost:9090/ ## 查看日至,进入项目代码 ``` # 查看启动日志: docker logs -f stock # 进入stock容器 docker exec -it stock bash ``` ## 开发模式,映射stock 代码方法 直接使用 dev yml 即可,会映射stock到/data/stock 然后在外部修改代码容器中运行即可。 ```bash docker-compose -f dev-docker-compose.yml up -d ``` ```bash docker-compose -f docker-compose-v2.0.yml up -d ``` ## 老镜像还保存一个版本 ``` pythonstock/pythonstock:v2021 ``` ================================================ FILE: docker-compose/build_stock.sh ================================================ #!/bin/sh cd ../stock NOW_MONTH=$(date "+%Y-%m") DOCKER_TAG=pythonstock/pythonstock:latest DOCKER_TAG_MONTH=pythonstock/pythonstock:stock-${NOW_MONTH} echo " docker build -f Dockerfile -t ${DOCKER_TAG} ." docker build -f Dockerfile -t ${DOCKER_TAG} . echo " docker build tag xxx ${DOCKER_TAG_MONTH} " echo "#################################################################" echo " docker push ${DOCKER_TAG} " ================================================ FILE: docker-compose/dev-docker-compose-restart.sh ================================================ #!/bin/sh git pull sleep 1 docker-compose -f dev-docker-compose.yml down sleep 1 docker-compose -f dev-docker-compose.yml up -d echo "restart dev-docker-compose" ================================================ FILE: docker-compose/dev-docker-compose.yml ================================================ networks: stock-dev-network: driver: bridge version: "3" services: frontend: image: pythonstock/frontend-dev:latest build: context: . dockerfile: docker/DevFrontendDockerfile container_name: frontend ports: - "8080:8080" volumes: # 设置开发目录,方便开发调试 - "../frontend:/usr/src/app" environment: LANG: zh_CN.UTF-8 LC_CTYPE: zh_CN.UTF-8 restart: always networks: stock-dev-network: {} # 入口写死,手动启动应用。 entrypoint: /usr/src/app/docker-entrypoint.sh depends_on: - backend backend: image: pythonstock/backend-dev:latest build: context: . dockerfile: docker/DevBackendDockerfile container_name: backend ports: - "8888:8888" - "9090:9090" volumes: # 设置开发目录,方便开发调试 - "../backend/jobs/crontab:/var/spool/cron/crontabs/root" - "../backend/jobs/cron.minutely:/etc/cron.minutely" - "../backend/jobs/cron.hourly:/etc/cron.hourly" - "../backend/jobs/cron.daily:/etc/cron.daily" - "../backend/jobs/cron.monthly:/etc/cron.monthly" - "../backend:/data/stock" - "../backend/supervisor:/data/supervisor" - "./data/notebooks:/data/notebooks" - "./data/logs:/data/logs" environment: MYSQL_HOST: mysql-stock MYSQL_USER: root MYSQL_PWD: mysql-stock MYSQL_DB: stock_data MYSQL_PORT: 3306 LANG: zh_CN.UTF-8 LC_CTYPE: zh_CN.UTF-8 PYTHONIOENCODING: utf-8 restart: always networks: stock-dev-network: {} # 入口写死,手动启动应用。 #entrypoint: sleep 999999d depends_on: - mysql-stock mysql-stock: # image: hub.atomgit.com/library/mysql:5.7 # https://hub.atomgit.com/repos/amd64/mysql image: docker.m.daocloud.io/library/mysql:8 container_name: mysql-stock # 执行命令:https://juejin.cn/s/mysql%20healthcheck%20docker-compose healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-uroot", "-pmysql-stock"] interval: 10s timeout: 5s retries: 5 ports: - "3306:3306" networks: stock-dev-network: {} volumes: - "./mysql/my.cnf:/etc/mysql/my.cnf" - "./data/mysql-stock/data:/var/lib/mysql" - ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql environment: MYSQL_ROOT_PASSWORD: mysql-stock MYSQL_DATABASE: stock_data TZ: Asia/Shanghai command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_general_ci', '--max_connections=3000' ] restart: always ================================================ FILE: docker-compose/docker/DevBackendDockerfile ================================================ # https://hub.docker.com/_/python?tab=tags&page=1&name=3.11-slim-bullseye # 用这个做为基础镜像,防止每次都进行构建。 #FROM docker.io/python:3.11-slim-bullseye FROM docker.m.daocloud.io/library/python:3.11-slim-bullseye # https://opsx.alibaba.com/mirror # 使用阿里云镜像地址。修改debian apt 更新地址,pip 地址,设置时区。 # 设置debian的镜像源 RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "[global]\n\ trusted-host=mirrors.aliyun.com\n\ index-url=http://mirrors.aliyun.com/pypi/simple" > /etc/pip.conf && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone #增加语言utf-8 ENV LANG=zh_CN.UTF-8 ENV LC_CTYPE=zh_CN.UTF-8 ENV LC_ALL=C ENV PYTHONPATH=/data/stock # 增加 TensorFlow 的支持,使用最新的2.0 编写代码。目前还是使用 1.x 吧,还没有学明白。 # RUN pip3 install tensorflow==2.0.0-rc1 keras # RUN pip3 install tensorflow keras sklearn # 设置 vim 的语言配置 RUN mkdir -p /etc/vim/ && \ echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc && \ echo "set termencoding=utf-8" >> /etc/vim/vimrc && \ echo "set encoding=utf-8" >> /etc/vim/vimrc # 安装 mysqlclient akshare (pandas ,numpy) tornado bokeh # 安装 nodejs 库 # apt-get autoremove -y 删除没有用的依赖lib。减少镜像大小。1MB 也要节省。 # apt-get --purge remove 软件包名称 , 删除已安装包(不保留配置文件)。 RUN apt-get update && apt-get install -y git pkg-config net-tools procps gcc make python3-dev default-libmysqlclient-dev libxml2-dev cron && \ pip3 install mysqlclient sqlalchemy && \ pip3 install supervisor && \ pip3 install numpy pandas && \ pip3 install akshare --upgrade && \ apt-get install -y nodejs && \ pip3 install tornado torndb && \ pip3 install bokeh stockstats scikit-learn && \ apt-get --purge remove -y gcc make python3-dev default-libmysqlclient-dev libxml2-dev && \ rm -rf /root/.cache/* && apt-get clean && apt-get autoremove -y && \ cd /usr/local/lib/python3.11/site-packages && find ./ -name *.pyc | xargs rm -f # /usr/local/lib/python3.11/site-packages/pandas/ # 1.解决 pandas 数据插入问题。直接修改数据库驱动 sqlalchemy # 修改:statement.replace("INSERT INTO","INSERT IGNORE INTO") # /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py # 增加了一个 IGNORE 参数。 # 2.解决torndb在python3下面的问题: # http://blog.csdn.net/littlethunder/article/details/8917378 # 3. 解决 type 问题,使用sed 进行替换。 # File "/usr/local/lib/python3.11/site-packages/torndb.py", line 260, in # CONVERSIONS[field_type] = [(FLAG.BINARY, str)] + CONVERSIONS[field_type] # TypeError: can only concatenate list (not "type") to list RUN echo `date +%Y-%m-%d:%H:%M:%S` >> /etc/docker.build && \ sed -i -e 's/executemany(statement/executemany(statement.replace\("INSERT INTO","INSERT IGNORE INTO")/g' \ /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py && \ rm -f /etc/cron.daily/apt-compat /etc/cron.daily/dpkg /etc/cron.daily/passwd && \ sed -i -e 's/itertools\.izip/zip/g' \ /usr/local/lib/python3.11/site-packages/torndb.py && \ sed -i -e 's/\+ CONVERSIONS\[field_type\]/\+ \[CONVERSIONS\[field_type\],bytes\]/g' \ /usr/local/lib/python3.11/site-packages/torndb.py #add cron sesrvice. #每分钟,每小时1分钟,每天1点1分,每月1号执行 RUN mkdir -p /etc/cron.minutely && mkdir -p /etc/cron.hourly && mkdir -p /etc/cron.monthly && mkdir -p /var/spool/cron/crontabs && \ echo "SHELL=/bin/sh \n\ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin \n\ # min hour day month weekday command \n\ */1 * * * * /bin/run-parts /etc/cron.minutely \n\ 10 * * * * /bin/run-parts /etc/cron.hourly \n\ 30 16 * * * /bin/run-parts /etc/cron.daily \n\ 30 17 1,10,20 * * /bin/run-parts /etc/cron.monthly \n" > /var/spool/cron/crontabs/root && \ chmod 600 /var/spool/cron/crontabs/root #增加服务端口 EXPOSE 8888 9090 ENTRYPOINT ["supervisord","-n","-c","/data/supervisor/supervisord.conf"] ================================================ FILE: docker-compose/docker/DevFrontendDockerfile ================================================ #使用 node:bullseye-slim 做基础镜像减少大小。 # FROM docker.m.daocloud.io/library/node:bullseye-slim # fixbug 最新node 版本编译不过去, # ERROR Error: Cannot find module './passes/web-incoming' FROM docker.m.daocloud.io/library/node:23.5.0-bullseye-slim # https://opsx.alibaba.com/mirror # 使用阿里云镜像地址。修改debian apt 更新地址,pip 地址,设置时区。 # 设置debian的镜像源 RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "[global]\n\ trusted-host=mirrors.aliyun.com\n\ index-url=http://mirrors.aliyun.com/pypi/simple" > /etc/pip.conf && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone #增加语言utf-8 ENV LANG=zh_CN.UTF-8 ENV LC_CTYPE=zh_CN.UTF-8 ENV LC_ALL=C # 增加 TensorFlow 的支持,使用最新的2.0 编写代码。目前还是使用 1.x 吧,还没有学明白。 # RUN pip3 install tensorflow==2.0.0-rc1 keras # RUN pip3 install tensorflow keras sklearn # 设置 vim 的语言配置 RUN mkdir -p /etc/vim/ && \ echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc && \ echo "set termencoding=utf-8" >> /etc/vim/vimrc && \ echo "set encoding=utf-8" >> /etc/vim/vimrc # 安装 mysqlclient akshare (pandas ,numpy) tornado bokeh # 安装 nodejs 库 # apt-get autoremove -y 删除没有用的依赖lib。减少镜像大小。1MB 也要节省。 # apt-get --purge remove 软件包名称 , 删除已安装包(不保留配置文件)。 RUN apt-get update && apt-get install -y python3 make g++ && apt-get clean ================================================ FILE: docker-compose/docker/Dockerfile ================================================ #使用 python:3.8-slim 做基础镜像减少大小。其中tensorflow再用另外的镜像跑数据。 # 之前使用的是python:3.6-slim # 可以更新 3.8-slim-bullseye slim-bullseye # https://hub.docker.com/_/python?tab=tags&page=1&name=3.8-slim-bullseye # 用这个做为基础镜像,防止每次都进行构建。 #FROM docker.io/python:3.8-slim-bullseye #FROM hub.atomgit.com/amd64/python:3.8-slim-bullseye # https://hub.atomgit.com/repos/amd64/python #FROM hub.atomgit.com/amd64/python:3.11-slim-bullseye FROM docker.m.daocloud.io/library/python:3.11-slim-bullseye # https://opsx.alibaba.com/mirror # 使用阿里云镜像地址。修改debian apt 更新地址,pip 地址,设置时区。 # 设置debian的镜像源 RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "[global]\n\ trusted-host=mirrors.aliyun.com\n\ index-url=http://mirrors.aliyun.com/pypi/simple" > /etc/pip.conf && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone #增加语言utf-8 ENV LANG=zh_CN.UTF-8 ENV LC_CTYPE=zh_CN.UTF-8 ENV LC_ALL=C ENV PYTHONPATH=/data/stock # 增加 TensorFlow 的支持,使用最新的2.0 编写代码。目前还是使用 1.x 吧,还没有学明白。 # RUN pip3 install tensorflow==2.0.0-rc1 keras # RUN pip3 install tensorflow keras sklearn # 设置 vim 的语言配置 RUN mkdir -p /etc/vim/ && \ echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc && \ echo "set termencoding=utf-8" >> /etc/vim/vimrc && \ echo "set encoding=utf-8" >> /etc/vim/vimrc # 安装 mysqlclient akshare (pandas ,numpy) tornado bokeh # 安装 nodejs 库 # apt-get autoremove -y 删除没有用的依赖lib。减少镜像大小。1MB 也要节省。 # apt-get --purge remove 软件包名称 , 删除已安装包(不保留配置文件)。 RUN apt-get update && apt-get install -y git pkg-config net-tools procps gcc make python3-dev default-libmysqlclient-dev libxml2-dev cron && \ pip3 install mysqlclient sqlalchemy && \ pip3 install supervisor && \ pip3 install numpy pandas && \ pip3 install akshare --upgrade && \ apt-get install -y nodejs && \ pip3 install tornado torndb && \ pip3 install bokeh stockstats scikit-learn && \ apt-get --purge remove -y gcc make python3-dev default-libmysqlclient-dev libxml2-dev && \ rm -rf /root/.cache/* && apt-get clean && apt-get autoremove -y && \ cd /usr/local/lib/python3.11/site-packages && find ./ -name *.pyc | xargs rm -f # /usr/local/lib/python3.11/site-packages/pandas/ # 1.解决 pandas 数据插入问题。直接修改数据库驱动 sqlalchemy # 修改:statement.replace("INSERT INTO","INSERT IGNORE INTO") # /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py # 增加了一个 IGNORE 参数。 # 2.解决torndb在python3下面的问题: # http://blog.csdn.net/littlethunder/article/details/8917378 # 3. 解决 type 问题,使用sed 进行替换。 # File "/usr/local/lib/python3.11/site-packages/torndb.py", line 260, in # CONVERSIONS[field_type] = [(FLAG.BINARY, str)] + CONVERSIONS[field_type] # TypeError: can only concatenate list (not "type") to list RUN echo `date +%Y-%m-%d:%H:%M:%S` >> /etc/docker.build && \ sed -i -e 's/executemany(statement/executemany(statement.replace\("INSERT INTO","INSERT IGNORE INTO")/g' \ /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py && \ rm -f /etc/cron.daily/apt-compat /etc/cron.daily/dpkg /etc/cron.daily/passwd && \ sed -i -e 's/itertools\.izip/zip/g' \ /usr/local/lib/python3.11/site-packages/torndb.py && \ sed -i -e 's/\+ CONVERSIONS\[field_type\]/\+ \[CONVERSIONS\[field_type\],bytes\]/g' \ /usr/local/lib/python3.11/site-packages/torndb.py #add cron sesrvice. #每分钟,每小时1分钟,每天1点1分,每月1号执行 RUN mkdir -p /etc/cron.minutely && mkdir -p /etc/cron.hourly && mkdir -p /etc/cron.monthly && mkdir -p /var/spool/cron/crontabs && \ echo "SHELL=/bin/sh \n\ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin \n\ # min hour day month weekday command \n\ */1 * * * * /bin/run-parts /etc/cron.minutely \n\ 10 * * * * /bin/run-parts /etc/cron.hourly \n\ 30 16 * * * /bin/run-parts /etc/cron.daily \n\ 30 17 1,10,20 * * /bin/run-parts /etc/cron.monthly \n" > /var/spool/cron/crontabs/root && \ chmod 600 /var/spool/cron/crontabs/root #增加服务端口就两个 6006 8500 9001 EXPOSE 8888 9999 ENTRYPOINT ["supervisord","-n","-c","/data/supervisor/supervisord.conf"] ================================================ FILE: docker-compose/docker/ProdBackendDockerfile ================================================ #使用 python:3.8-slim 做基础镜像减少大小。其中tensorflow再用另外的镜像跑数据。 # 之前使用的是python:3.6-slim # 可以更新 3.8-slim-bullseye slim-bullseye # https://hub.docker.com/_/python?tab=tags&page=1&name=3.8-slim-bullseye # 用这个做为基础镜像,防止每次都进行构建。 #FROM docker.io/python:3.8-slim-bullseye #FROM hub.atomgit.com/amd64/python:3.8-slim-bullseye # https://hub.atomgit.com/repos/amd64/python #FROM hub.atomgit.com/amd64/python:3.11-slim-bullseye FROM docker.m.daocloud.io/library/python:3.11-slim-bullseye # https://opsx.alibaba.com/mirror # 使用阿里云镜像地址。修改debian apt 更新地址,pip 地址,设置时区。 # 设置debian的镜像源 RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "[global]\n\ trusted-host=mirrors.aliyun.com\n\ index-url=http://mirrors.aliyun.com/pypi/simple" > /etc/pip.conf && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone #增加语言utf-8 ENV LANG=zh_CN.UTF-8 ENV LC_CTYPE=zh_CN.UTF-8 ENV LC_ALL=C ENV PYTHONPATH=/data/stock # 增加 TensorFlow 的支持,使用最新的2.0 编写代码。目前还是使用 1.x 吧,还没有学明白。 # RUN pip3 install tensorflow==2.0.0-rc1 keras # RUN pip3 install tensorflow keras sklearn # 设置 vim 的语言配置 RUN mkdir -p /etc/vim/ && \ echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc && \ echo "set termencoding=utf-8" >> /etc/vim/vimrc && \ echo "set encoding=utf-8" >> /etc/vim/vimrc # 安装 mysqlclient akshare (pandas ,numpy) tornado bokeh # 安装 nodejs 库 # apt-get autoremove -y 删除没有用的依赖lib。减少镜像大小。1MB 也要节省。 # apt-get --purge remove 软件包名称 , 删除已安装包(不保留配置文件)。 RUN apt-get update && apt-get install -y git pkg-config net-tools procps gcc make python3-dev default-libmysqlclient-dev libxml2-dev cron && \ pip3 install mysqlclient sqlalchemy && \ pip3 install supervisor && \ pip3 install numpy pandas && \ pip3 install akshare --upgrade && \ apt-get install -y nodejs && \ pip3 install tornado torndb && \ pip3 install bokeh stockstats scikit-learn && \ apt-get --purge remove -y gcc make python3-dev default-libmysqlclient-dev libxml2-dev && \ rm -rf /root/.cache/* && apt-get clean && apt-get autoremove -y && \ cd /usr/local/lib/python3.11/site-packages && find ./ -name *.pyc | xargs rm -f # /usr/local/lib/python3.11/site-packages/pandas/ # 1.解决 pandas 数据插入问题。直接修改数据库驱动 sqlalchemy # 修改:statement.replace("INSERT INTO","INSERT IGNORE INTO") # /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py # 增加了一个 IGNORE 参数。 # 2.解决torndb在python3下面的问题: # http://blog.csdn.net/littlethunder/article/details/8917378 # 3. 解决 type 问题,使用sed 进行替换。 # File "/usr/local/lib/python3.11/site-packages/torndb.py", line 260, in # CONVERSIONS[field_type] = [(FLAG.BINARY, str)] + CONVERSIONS[field_type] # TypeError: can only concatenate list (not "type") to list RUN echo `date +%Y-%m-%d:%H:%M:%S` >> /etc/docker.build && \ sed -i -e 's/executemany(statement/executemany(statement.replace\("INSERT INTO","INSERT IGNORE INTO")/g' \ /usr/local/lib/python3.11/site-packages/sqlalchemy/dialects/mysql/mysqldb.py && \ rm -f /etc/cron.daily/apt-compat /etc/cron.daily/dpkg /etc/cron.daily/passwd && \ sed -i -e 's/itertools\.izip/zip/g' \ /usr/local/lib/python3.11/site-packages/torndb.py && \ sed -i -e 's/\+ CONVERSIONS\[field_type\]/\+ \[CONVERSIONS\[field_type\],bytes\]/g' \ /usr/local/lib/python3.11/site-packages/torndb.py #add cron sesrvice. #每分钟,每小时1分钟,每天1点1分,每月1号执行 RUN mkdir -p /etc/cron.minutely && mkdir -p /etc/cron.hourly && mkdir -p /etc/cron.monthly && mkdir -p /var/spool/cron/crontabs && \ echo "SHELL=/bin/sh \n\ PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin \n\ # min hour day month weekday command \n\ */1 * * * * /bin/run-parts /etc/cron.minutely \n\ 10 * * * * /bin/run-parts /etc/cron.hourly \n\ 30 16 * * * /bin/run-parts /etc/cron.daily \n\ 30 17 1,10,20 * * /bin/run-parts /etc/cron.monthly \n" > /var/spool/cron/crontabs/root && \ chmod 600 /var/spool/cron/crontabs/root #增加服务端口就两个 6006 8500 9001 EXPOSE 8888 9999 ENTRYPOINT ["supervisord","-n","-c","/data/supervisor/supervisord.conf"] ================================================ FILE: docker-compose/docker/ProdFrontendDockerfile ================================================ #使用 node:bullseye-slim 做基础镜像减少大小。 FROM docker.m.daocloud.io/library/node:bullseye-slim # https://opsx.alibaba.com/mirror # 使用阿里云镜像地址。修改debian apt 更新地址,pip 地址,设置时区。 # 设置debian的镜像源 RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \ echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \ echo "[global]\n\ trusted-host=mirrors.aliyun.com\n\ index-url=http://mirrors.aliyun.com/pypi/simple" > /etc/pip.conf && \ ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ echo "Asia/Shanghai" > /etc/timezone #增加语言utf-8 ENV LANG=zh_CN.UTF-8 ENV LC_CTYPE=zh_CN.UTF-8 ENV LC_ALL=C # 增加 TensorFlow 的支持,使用最新的2.0 编写代码。目前还是使用 1.x 吧,还没有学明白。 # RUN pip3 install tensorflow==2.0.0-rc1 keras # RUN pip3 install tensorflow keras sklearn # 设置 vim 的语言配置 RUN mkdir -p /etc/vim/ && \ echo "set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936" >> /etc/vim/vimrc && \ echo "set termencoding=utf-8" >> /etc/vim/vimrc && \ echo "set encoding=utf-8" >> /etc/vim/vimrc # 安装 mysqlclient akshare (pandas ,numpy) tornado bokeh # 安装 nodejs 库 # apt-get autoremove -y 删除没有用的依赖lib。减少镜像大小。1MB 也要节省。 # apt-get --purge remove 软件包名称 , 删除已安装包(不保留配置文件)。 RUN apt-get update && apt-get install -y python3 make g++ && apt-get clean ================================================ FILE: docker-compose/docker/README.md ================================================ # python 基础镜像 基础镜像升级到 2020年7月的版本 保证运行的最少基础环境,基础环境使用python3.6的版本。安装了超级多的lib库。非常的好用。 mysqlclient sqlalchemy requests numpy tushare tornado torndb bokeh stockstats ta-lib jupyter sklearn # 2021年6月版本,使用 akshare 替换掉 tushare 库 akshare 地址: https://www.akshare.xyz/zh_CN/latest/introduction.html AKShare 是基于 Python 的财经数据接口库, 目的是实现对股票、期货、期权、基金、外汇、债券、指数、 加密货币等金融产品的基本面数据、实时和历史行情数据、衍生数据从数据采集、数据清洗到数据落地的一套工具, 主要用于学术研究目的。 # 2021年 9 月版本,镜像裁剪,supervisor 使用python3 supervisor 使用 python3 后好像减少了不少大小。 同时删除掉一直没有用的 ta-lib 和 jupyter 。升级python基础镜像。 ================================================ FILE: docker-compose/docker/build.sh ================================================ #!/bin/sh NOW_MONTH=$(date "+%Y-%m") DOCKER_TAG=pythonstock/pythonstock:base-${NOW_MONTH} echo " docker build -f Dockerfile -t ${DOCKER_TAG} ." docker build -f Dockerfile -t ${DOCKER_TAG} . echo "#################################################################" echo " docker push ${DOCKER_TAG} " ================================================ FILE: docker-compose/docker-compose.yml ================================================ networks: stock-dev-network: driver: bridge version: "3" services: #### 使用node 镜像进行编译,构建成 js 文件给nginx 使用。 frontend-build: image: pythonstock/frontend-dev:latest build: context: . dockerfile: docker/DevFrontendDockerfile container_name: frontend-build volumes: # 设置开发目录,方便开发调试,生产环境拷贝到 /data/html - "../frontend:/usr/src/app" - "./data/html:/data/html" environment: LANG: zh_CN.UTF-8 LC_CTYPE: zh_CN.UTF-8 networks: stock-dev-network: {} # 入口写死,手动启动应用。每次构建时候使用,构建完成结束 entrypoint: /usr/src/app/docker-build.sh nginx: image: docker.m.daocloud.io/library/nginx:stable-alpine container_name: nginx ports: - "8080:8080" volumes: # 设置开发目录,方便开发调试 - "./data/html:/usr/share/nginx/html" - "./nginx/nginx.conf:/etc/nginx/conf.d/default.conf" environment: LANG: zh_CN.UTF-8 LC_CTYPE: zh_CN.UTF-8 restart: always networks: stock-dev-network: {} depends_on: - backend backend: image: pythonstock/backend-dev:latest build: context: . dockerfile: docker/DevBackendDockerfile container_name: backend ports: - "8888:8888" - "9090:9090" volumes: # 设置开发目录,方便开发调试 - "../backend/jobs/crontab:/var/spool/cron/crontabs/root" - "../backend/jobs/cron.minutely:/etc/cron.minutely" - "../backend/jobs/cron.hourly:/etc/cron.hourly" - "../backend/jobs/cron.daily:/etc/cron.daily" - "../backend/jobs/cron.monthly:/etc/cron.monthly" - "../backend:/data/stock" - "../backend/supervisor:/data/supervisor" - "./data/notebooks:/data/notebooks" - "./data/logs:/data/logs" environment: MYSQL_HOST: mysql-stock MYSQL_USER: root MYSQL_PWD: mysql-stock MYSQL_DB: stock_data MYSQL_PORT: 3306 LANG: zh_CN.UTF-8 LC_CTYPE: zh_CN.UTF-8 PYTHONIOENCODING: utf-8 restart: always networks: stock-dev-network: {} # 入口写死,手动启动应用。 #entrypoint: sleep 999999d depends_on: - mysql-stock mysql-stock: # image: hub.atomgit.com/library/mysql:5.7 # https://hub.atomgit.com/repos/amd64/mysql image: docker.m.daocloud.io/library/mysql:8 container_name: mysql-stock # 执行命令:https://juejin.cn/s/mysql%20healthcheck%20docker-compose healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-uroot", "-pmysql-stock"] interval: 10s timeout: 5s retries: 5 ports: - "3306:3306" networks: stock-dev-network: {} volumes: - "./mysql/my.cnf:/etc/mysql/my.cnf" - "./data/mysql-stock/data:/var/lib/mysql" - ./mysql/init.sql:/docker-entrypoint-initdb.d/init.sql environment: MYSQL_ROOT_PASSWORD: mysql-stock MYSQL_DATABASE: stock_data TZ: Asia/Shanghai command: [ '--character-set-server=utf8mb4', '--collation-server=utf8mb4_general_ci', '--max_connections=3000' ] restart: always ================================================ FILE: docker-compose/mysql/init.sql ================================================ SET character_set_client = utf8; -- 切库: use stock_data; -- 建表 -- 表里面都是使用 code 和 date 两个字段做联合主键的。 CREATE TABLE IF NOT EXISTS `stock_zh_a_spot_em` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `last_price` double(20,2) DEFAULT NULL, `change_percent` double(20,2) DEFAULT NULL, `change_amount` double(20,2) DEFAULT NULL, `volume` double(20,2) DEFAULT NULL, `turnover` double(20,2) DEFAULT NULL, `amplitude` double(20,2) DEFAULT NULL, `high` double(20,2) DEFAULT NULL, `low` double(20,2) DEFAULT NULL, `open` double(20,2) DEFAULT NULL, `closed` double(20,2) DEFAULT NULL, `volume_ratio` double(20,2) DEFAULT NULL, `turnover_rate` double(20,2) DEFAULT NULL, `pe_ratio` double(20,2) DEFAULT NULL, `pb_ratio` double(20,2) DEFAULT NULL, `market_cap` double(20,2) DEFAULT NULL, `circulating_market_cap` double(20,2) DEFAULT NULL, `rise_speed` double(20,2) DEFAULT NULL, `change_5min` double(20,2) DEFAULT NULL, `change_ercent_60day` double(20,2) DEFAULT NULL, `ytd_change_percent` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE IF NOT EXISTS `stock_lhb_ggtj_sina` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `ranking_times` double(20,2) DEFAULT NULL, `sum_buy` double(20,2) DEFAULT NULL, `sum_sell` double(20,2) DEFAULT NULL, `net_amount` double(20,2) DEFAULT NULL, `buy_seat` double(20,2) DEFAULT NULL, `sell_seat` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `stock_dzjy_mrtj` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `quote_change` double(20,2) DEFAULT NULL, `close_price` double(20,2) DEFAULT NULL, `average_price` double(20,2) DEFAULT NULL, `overflow_rate` double(20,2) DEFAULT NULL, `trade_number` double(20,2) DEFAULT NULL, `sum_volume` double(20,2) DEFAULT NULL, `sum_turnover` double(20,2) DEFAULT NULL, `turnover_market_rate` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `guess_indicators_daily` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `last_price` double(20,2) DEFAULT NULL, `change_percent` double(20,2) DEFAULT NULL, `change_amount` double(20,2) DEFAULT NULL, `volume` double(20,2) DEFAULT NULL, `turnover` double(20,2) DEFAULT NULL, `amplitude` double(20,2) DEFAULT NULL, `high` double(20,2) DEFAULT NULL, `low` double(20,2) DEFAULT NULL, `open` double(20,2) DEFAULT NULL, `closed` double(20,2) DEFAULT NULL, `volume_ratio` double(20,2) DEFAULT NULL, `turnover_rate` double(20,2) DEFAULT NULL, `pe_ratio` double(20,2) DEFAULT NULL, `pb_ratio` double(20,2) DEFAULT NULL, `market_cap` double(20,2) DEFAULT NULL, `circulating_market_cap` double(20,2) DEFAULT NULL, `rise_speed` double(20,2) DEFAULT NULL, `change_5min` double(20,2) DEFAULT NULL, `change_ercent_60day` double(20,2) DEFAULT NULL, `ytd_change_percent` double(20,2) DEFAULT NULL, `boll` double(20,2) DEFAULT NULL, `boll_lb` double(20,2) DEFAULT NULL, `boll_ub` double(20,2) DEFAULT NULL, `kdjd` double(20,2) DEFAULT NULL, `kdjj` double(20,2) DEFAULT NULL, `kdjk` double(20,2) DEFAULT NULL, `macd` double(20,2) DEFAULT NULL, `macdh` double(20,2) DEFAULT NULL, `macds` double(20,2) DEFAULT NULL, `pdi` double(20,2) DEFAULT NULL, `trix` double(20,2) DEFAULT NULL, `trix_9_sma` double(20,2) DEFAULT NULL, `vr` double(20,2) DEFAULT NULL, `vr_6_sma` double(20,2) DEFAULT NULL, `wr_10` double(20,2) DEFAULT NULL, `wr_6` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `guess_indicators_lite_buy_daily` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `last_price` double(20,2) DEFAULT NULL, `change_percent` double(20,2) DEFAULT NULL, `change_amount` double(20,2) DEFAULT NULL, `volume` double(20,2) DEFAULT NULL, `turnover` double(20,2) DEFAULT NULL, `amplitude` double(20,2) DEFAULT NULL, `high` double(20,2) DEFAULT NULL, `low` double(20,2) DEFAULT NULL, `open` double(20,2) DEFAULT NULL, `closed` double(20,2) DEFAULT NULL, `volume_ratio` double(20,2) DEFAULT NULL, `turnover_rate` double(20,2) DEFAULT NULL, `pe_ratio` double(20,2) DEFAULT NULL, `pb_ratio` double(20,2) DEFAULT NULL, `market_cap` double(20,2) DEFAULT NULL, `circulating_market_cap` double(20,2) DEFAULT NULL, `rise_speed` double(20,2) DEFAULT NULL, `change_5min` double(20,2) DEFAULT NULL, `change_ercent_60day` double(20,2) DEFAULT NULL, `ytd_change_percent` double(20,2) DEFAULT NULL, `boll` double(20,2) DEFAULT NULL, `boll_lb` double(20,2) DEFAULT NULL, `boll_ub` double(20,2) DEFAULT NULL, `kdjd` double(20,2) DEFAULT NULL, `kdjj` double(20,2) DEFAULT NULL, `kdjk` double(20,2) DEFAULT NULL, `macd` double(20,2) DEFAULT NULL, `macdh` double(20,2) DEFAULT NULL, `macds` double(20,2) DEFAULT NULL, `pdi` double(20,2) DEFAULT NULL, `trix` double(20,2) DEFAULT NULL, `trix_9_sma` double(20,2) DEFAULT NULL, `vr` double(20,2) DEFAULT NULL, `vr_6_sma` double(20,2) DEFAULT NULL, `wr_10` double(20,2) DEFAULT NULL, `wr_6` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; CREATE TABLE `guess_indicators_lite_sell_daily` ( `date` varchar(255) NOT NULL, `code` varchar(255) NOT NULL, `name` varchar(255) DEFAULT NULL, `last_price` double(20,2) DEFAULT NULL, `change_percent` double(20,2) DEFAULT NULL, `change_amount` double(20,2) DEFAULT NULL, `volume` double(20,2) DEFAULT NULL, `turnover` double(20,2) DEFAULT NULL, `amplitude` double(20,2) DEFAULT NULL, `high` double(20,2) DEFAULT NULL, `low` double(20,2) DEFAULT NULL, `open` double(20,2) DEFAULT NULL, `closed` double(20,2) DEFAULT NULL, `volume_ratio` double(20,2) DEFAULT NULL, `turnover_rate` double(20,2) DEFAULT NULL, `pe_ratio` double(20,2) DEFAULT NULL, `pb_ratio` double(20,2) DEFAULT NULL, `market_cap` double(20,2) DEFAULT NULL, `circulating_market_cap` double(20,2) DEFAULT NULL, `rise_speed` double(20,2) DEFAULT NULL, `change_5min` double(20,2) DEFAULT NULL, `change_ercent_60day` double(20,2) DEFAULT NULL, `ytd_change_percent` double(20,2) DEFAULT NULL, `boll` double(20,2) DEFAULT NULL, `boll_lb` double(20,2) DEFAULT NULL, `boll_ub` double(20,2) DEFAULT NULL, `kdjd` double(20,2) DEFAULT NULL, `kdjj` double(20,2) DEFAULT NULL, `kdjk` double(20,2) DEFAULT NULL, `macd` double(20,2) DEFAULT NULL, `macdh` double(20,2) DEFAULT NULL, `macds` double(20,2) DEFAULT NULL, `pdi` double(20,2) DEFAULT NULL, `trix` double(20,2) DEFAULT NULL, `trix_9_sma` double(20,2) DEFAULT NULL, `vr` double(20,2) DEFAULT NULL, `vr_6_sma` double(20,2) DEFAULT NULL, `wr_10` double(20,2) DEFAULT NULL, `wr_6` double(20,2) DEFAULT NULL, PRIMARY KEY (`code`,`date`), KEY `idx_code` (`code`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; ================================================ FILE: docker-compose/mysql/my.cnf ================================================ # https://blog.csdn.net/aichogn/article/details/117788275 # mysql 推荐配置 [client] socket=/var/lib/mysql/mysql.sock port=3306 [mysqld] basedir=/var/lib/mysql/ datadir=/var/lib/mysql/data socket=/var/lib/mysql/mysql.sock default-storage-engine=INNODB character_set_server=utf8mb4 collation_server=utf8mb4_general_ci port=3306 # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 server_id=1 ## 最大连接数,MySQL服务器允许的最大连接数16384,连接数越多消耗内存越多 max_connections = 1000 ## 日志过期时间,包括二进制日志(过期自动删除) # expire_logs_days = 15 ## Enable Per Table Data for InnoDB to shrink ibdata1(innoDB表优化) innodb_file_per_table = 1 #默认128M,用于存储页面缓存数据外,另外正常情况下还有大约8%的开销,主要用在每个缓存页帧的描述、adaptive hash等数据结构,适当的增加这个参数的大小,可以有效的减少 InnoDB 类型的表的磁盘 I/O innodb_buffer_pool_size = 2048M innodb_log_file_size = 512M #默认是8MB,InnoDB在写事务日志的时候,为了提高性能,也是先将信息写入Innofb Log Buffer中,当满足innodb_flush_log_trx_commit参数所设置的相应条件(或者日志缓冲区写满)之后,才会将日志写到文件 (或者同步到磁盘)中 innodb_log_buffer_size = 8M innodb_flush_log_at_trx_commit = 2 #表大小写不敏感 lower_case_table_names=1 #跳过密码 安装完后屏蔽该选项 #skip-grant-tables #关闭 binlog skip-log-bin ================================================ FILE: docker-compose/nginx/nginx.conf ================================================ server { listen 8080; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /api/v1 { proxy_pass http://backend:9090; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } ================================================ FILE: docker-compose/nginx.conf ================================================ # 设置nginx启动 # systemctl enable nginx server { listen 8080; server_name www.pythonstock.com; root /usr/share/nginx/html; error_page 404 /404.html; error_page 500 502 503 504 /50x.html; # 开启gzip gzip on; # 启用gzip压缩的最小文件;小于设置值的文件将不会被压缩 gzip_min_length 1k; # gzip 压缩级别 1-10 gzip_comp_level 2; # 进行压缩的文件类型。 gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 是否在http header中添加Vary: Accept-Encoding,建议开启 gzip_vary on; access_log /var/log/nginx/stock.access.log main; error_log /var/log/nginx/stock.error.log warn; # https://medium.com/aviabird/413-414-request-url-entity-too-large-error-nginx-b6dcece6f5dd # 解决GET 参数过长问题。 client_max_body_size 10M; large_client_header_buffers 4 20k; location ^~ /static/ { access_log off; # 需要把源代码,再下载一遍。 root /data/pythonstock/stock/web; expires 30d; # 设置30天超时 # 参考 https://www.cnblogs.com/kevingrace/p/10459429.html # add_header Cache-Control max-age=360000; } location / { proxy_next_upstream http_500 http_502 http_503 http_504 error timeout invalid_header; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:9090; expires 0; } } ================================================ FILE: frontend/.eslintignore ================================================ build/*.js src/assets public dist eslint-disable ================================================ FILE: frontend/.gitignore ================================================ .DS_Store node_modules/ dist/ npm-debug.log* yarn-debug.log* yarn-error.log* package-lock.json tests/**/coverage/ # Editor directories and files .idea .vscode *.suo *.ntvs* *.njsproj *.sln ================================================ FILE: frontend/LICENSE ================================================ MIT License Copyright (c) 2017-present PanJiaChen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: frontend/README.md ================================================ ### 说明,项目迁移到了Gitee 啦,最后一次修改,2023-06-02 执行存档 项目迁移到这里了:此项目后续更新访问这里: https://gitee.com/pythonstock/stock-ui github项目后续就Archives存档了,不再更新了! csdn的pythonstock专栏地址,相关资料都在这里有说明: https://blog.csdn.net/freewebsys/category_9285317.html ## 1,股票系统前端项目 elementUI https://element.eleme.cn/#/zh-CN 使用vue-element-admin的模板进行项目开发: https://panjiachen.github.io/vue-element-admin-site/zh/guide/ 在线预览地址: https://panjiachen.github.io/vue-element-admin/#/dashboard 模板源自: https://gitee.com/panjiachen/vue-admin-template 【相关python stock资料分类】: http://blog.csdn.net/freewebsys/article/category/7076584 ## 2,动态展示表格 http://localhost:9528/#/example/table 在.eslintrc.js 文件中配置 eslint-disable 指令来关闭ESlint语法检测。 ================================================ FILE: frontend/babel.config.js ================================================ module.exports = { presets: [ // https://github.com/vuejs/vue-cli/tree/master/packages/@vue/babel-preset-app '@vue/cli-plugin-babel/preset' ], 'env': { 'development': { // babel-plugin-dynamic-import-node plugin only does one thing by converting all import() to require(). // This plugin can significantly increase the speed of hot updates, when you have a large number of pages. // https://panjiachen.github.io/vue-element-admin-site/guide/advanced/lazy-loading.html 'plugins': ['dynamic-import-node'] } } } ================================================ FILE: frontend/docker-build.sh ================================================ #!/bin/sh sleep 1 # 只依赖启动。 cd /usr/src/app #!/bin/bash # 定义要检查的文件夹路径 modules_path="/usr/src/app/node_modules" # 使用[ ]检查文件夹是否存在 if [ -d "$modules_path" ]; then echo "文件夹 $modules_path 存在" else echo "文件夹 $modules_path 不存在,执行 install 安装" npm install --registry=https://registry.npmmirror.com fi npm run build # 编译完成之后拷贝 html 资源到 影射目录,等待即可。每次编译前都清空内容。 rm -rf /data/html/* cp -r ./dist/* /data/html/ echo "######### build finish and cp all html #########" ================================================ FILE: frontend/docker-entrypoint.sh ================================================ #!/bin/sh sleep 1 # 只依赖启动。 cd /usr/src/app #!/bin/bash # 定义要检查的文件夹路径 modules_path="/usr/src/app/node_modules" # 使用[ ]检查文件夹是否存在 if [ -d "$modules_path" ]; then echo "文件夹 $modules_path 存在" else echo "文件夹 $modules_path 不存在,执行 install 安装" npm install --registry=https://registry.npmmirror.com fi npm run dev sleep 999999d ================================================ FILE: frontend/jest.config.js ================================================ module.exports = { moduleFileExtensions: ['js', 'jsx', 'json', 'vue'], transform: { '^.+\\.vue$': 'vue-jest', '.+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$': 'jest-transform-stub', '^.+\\.jsx?$': 'babel-jest' }, moduleNameMapper: { '^@/(.*)$': '/src/$1' }, snapshotSerializers: ['jest-serializer-vue'], testMatch: [ '**/tests/unit/**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)' ], collectCoverageFrom: ['src/utils/**/*.{js,vue}', '!src/utils/auth.js', '!src/utils/request.js', 'src/components/**/*.{js,vue}'], coverageDirectory: '/tests/unit/coverage', // 'collectCoverage': true, 'coverageReporters': [ 'lcov', 'text-summary' ], testURL: 'http://localhost/' } ================================================ FILE: frontend/jsconfig.json ================================================ { "compilerOptions": { "baseUrl": "./", "paths": { "@/*": ["src/*"] } }, "exclude": ["node_modules", "dist"] } ================================================ FILE: frontend/mock/index.js ================================================ const Mock = require('mockjs') const { param2Obj } = require('./utils') const user = require('./user') const table = require('./table') const mocks = [ ...user, ...table ] // for front mock // please use it cautiously, it will redefine XMLHttpRequest, // which will cause many of your third-party libraries to be invalidated(like progress event). function mockXHR() { // mock patch // https://github.com/nuysoft/Mock/issues/300 Mock.XHR.prototype.proxy_send = Mock.XHR.prototype.send Mock.XHR.prototype.send = function() { if (this.custom.xhr) { this.custom.xhr.withCredentials = this.withCredentials || false if (this.responseType) { this.custom.xhr.responseType = this.responseType } } this.proxy_send(...arguments) } function XHR2ExpressReqWrap(respond) { return function(options) { let result = null if (respond instanceof Function) { const { body, type, url } = options // https://expressjs.com/en/4x/api.html#req result = respond({ method: type, body: JSON.parse(body), query: param2Obj(url) }) } else { result = respond } return Mock.mock(result) } } for (const i of mocks) { Mock.mock(new RegExp(i.url), i.type || 'get', XHR2ExpressReqWrap(i.response)) } } module.exports = { mocks, mockXHR } ================================================ FILE: frontend/mock/mock-server.js ================================================ const chokidar = require('chokidar') const bodyParser = require('body-parser') const chalk = require('chalk') const path = require('path') const Mock = require('mockjs') const mockDir = path.join(process.cwd(), 'mock') function registerRoutes(app) { let mockLastIndex const { mocks } = require('./index.js') const mocksForServer = mocks.map(route => { return responseFake(route.url, route.type, route.response) }) for (const mock of mocksForServer) { app[mock.type](mock.url, mock.response) mockLastIndex = app._router.stack.length } const mockRoutesLength = Object.keys(mocksForServer).length return { mockRoutesLength: mockRoutesLength, mockStartIndex: mockLastIndex - mockRoutesLength } } function unregisterRoutes() { Object.keys(require.cache).forEach(i => { if (i.includes(mockDir)) { delete require.cache[require.resolve(i)] } }) } // for mock server const responseFake = (url, type, respond) => { return { url: new RegExp(`${process.env.VUE_APP_BASE_API}${url}`), type: type || 'get', response(req, res) { console.log('request invoke:' + req.path) res.json(Mock.mock(respond instanceof Function ? respond(req, res) : respond)) } } } module.exports = app => { // parse app.body // https://expressjs.com/en/4x/api.html#req.body app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: true })) const mockRoutes = registerRoutes(app) var mockRoutesLength = mockRoutes.mockRoutesLength var mockStartIndex = mockRoutes.mockStartIndex // watch files, hot reload mock server chokidar.watch(mockDir, { ignored: /mock-server/, ignoreInitial: true }).on('all', (event, path) => { if (event === 'change' || event === 'add') { try { // remove mock routes stack app._router.stack.splice(mockStartIndex, mockRoutesLength) // clear routes cache unregisterRoutes() const mockRoutes = registerRoutes(app) mockRoutesLength = mockRoutes.mockRoutesLength mockStartIndex = mockRoutes.mockStartIndex console.log(chalk.magentaBright(`\n > Mock Server hot reload success! changed ${path}`)) } catch (error) { console.log(chalk.redBright(error)) } } }) } ================================================ FILE: frontend/mock/table.js ================================================ const Mock = require('mockjs') const data = Mock.mock({ 'items|30': [{ id: '@id', title: '@sentence(10, 20)', 'status|1': ['published', 'draft', 'deleted'], author: 'name', display_time: '@datetime', pageviews: '@integer(300, 5000)' }] }) module.exports = [ { url: '/vue-admin-template/table/list', type: 'get', response: config => { const items = data.items return { code: 20000, data: { total: items.length, items: items } } } } ] ================================================ FILE: frontend/mock/user.js ================================================ const tokens = { admin: { token: 'admin-token' }, editor: { token: 'editor-token' } } const users = { 'admin-token': { roles: ['admin'], introduction: 'I am a super administrator', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', name: 'Super Admin' }, 'editor-token': { roles: ['editor'], introduction: 'I am an editor', avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif', name: 'Normal Editor' } } module.exports = [ // user login { url: '/vue-admin-template/user/login', type: 'post', response: config => { const { username } = config.body const token = tokens[username] // mock error if (!token) { return { code: 60204, message: 'Account and password are incorrect.' } } return { code: 20000, data: token } } }, // get user info { url: '/vue-admin-template/user/info\.*', type: 'get', response: config => { const { token } = config.query const info = users[token] // mock error if (!info) { return { code: 50008, message: 'Login failed, unable to get user details.' } } return { code: 20000, data: info } } }, // user logout { url: '/vue-admin-template/user/logout', type: 'post', response: _ => { return { code: 20000, data: 'success' } } } ] ================================================ FILE: frontend/mock/utils.js ================================================ /** * @param {string} url * @returns {Object} */ function param2Obj(url) { const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') if (!search) { return {} } const obj = {} const searchArr = search.split('&') searchArr.forEach(v => { const index = v.indexOf('=') if (index !== -1) { const name = v.substring(0, index) const val = v.substring(index + 1, v.length) obj[name] = val } }) return obj } module.exports = { param2Obj } ================================================ FILE: frontend/package.json ================================================ { "name": "vue-admin-template", "version": "4.4.0", "description": "A vue admin template with Element UI & axios & iconfont & permission control & lint", "author": "Pan ", "scripts": { "dev": "./node_modules/.bin/vue-cli-service serve ", "build": "./node_modules/.bin/vue-cli-service build", "build:stage": "./node_modules/.bin/vue-cli-service build --mode staging", "preview": "node build/index.js --preview", "svgo": "./node_modules/.bin/svgo -f src/icons/svg --config=src/icons/svgo.yml", "lint": "./node_modules/.bin/eslint --ext .js,.vue src", "test:unit": "./node_modules/.bin/jest --clearCache && vue-cli-service test:unit", "test:ci": "npm run lint && npm run test:unit" }, "dependencies": { "axios": "0.28.0", "core-js": "^3.26.1", "element-ui": "2.15.14", "file-saver": "^2.0.5", "js-cookie": "2.2.0", "normalize.css": "7.0.0", "nprogress": "0.2.0", "path-to-regexp": "2.4.0", "vue": "2.6.10", "vue-router": "3.0.6", "vuex": "3.1.0", "xlsx": "^0.18.5" }, "devDependencies": { "@vue/cli-plugin-babel": "4.4.4", "@vue/cli-plugin-eslint": "4.4.4", "@vue/cli-plugin-unit-jest": "4.4.4", "@vue/cli-service": "4.4.4", "@vue/test-utils": "1.0.0-beta.29", "autoprefixer": "9.5.1", "babel-eslint": "10.1.0", "babel-jest": "23.6.0", "babel-plugin-dynamic-import-node": "2.3.3", "chalk": "2.4.2", "connect": "3.6.6", "eslint": "6.7.2", "eslint-plugin-vue": "6.2.2", "html-webpack-plugin": "3.2.0", "mockjs": "1.0.1-beta3", "runjs": "4.3.2", "sass": "1.26.8", "sass-loader": "8.0.2", "script-ext-html-webpack-plugin": "2.1.3", "serve-static": "1.16.0", "svg-sprite-loader": "4.1.3", "svgo": "1.2.2", "vue-template-compiler": "2.6.10" }, "browserslist": [ "> 1%", "last 2 versions" ], "engines": { "node": ">=8.9", "npm": ">= 3.0.0" }, "license": "MIT" } ================================================ FILE: frontend/postcss.config.js ================================================ // https://github.com/michael-ciniawsky/postcss-load-config module.exports = { 'plugins': { // to edit target browsers: use "browserslist" field in package.json 'autoprefixer': {} } } ================================================ FILE: frontend/public/40x.html ================================================ 404 Not Found

404 Not Found


nginx/1.26.3
================================================ FILE: frontend/public/50x.html ================================================ Error

An error occurred.

Sorry, the page you are looking for is currently unavailable.
Please try again later.

If you are the system administrator of this resource then you should check the error log for details.

Faithfully yours, nginx.

================================================ FILE: frontend/public/index.html ================================================ <%= webpackConfig.name %>
================================================ FILE: frontend/src/App.vue ================================================ ================================================ FILE: frontend/src/api/article.js ================================================ import request from '@/utils/request' export function fetchList(query) { return request({ url: '/api/v1/api_data', method: 'get', params: query }) } export function fetchArticle(id) { return request({ url: '/vue-element-admin/article/detail', method: 'get', params: { id } }) } export function fetchPv(pv) { return request({ url: '/vue-element-admin/article/pv', method: 'get', params: { pv } }) } export function createArticle(data) { return request({ url: '/vue-element-admin/article/create', method: 'post', data }) } export function updateArticle(data) { return request({ url: '/vue-element-admin/article/update', method: 'post', data }) } ================================================ FILE: frontend/src/api/menu.js ================================================ import request from '@/utils/request' // 同步获得菜单相关数据。 export function fetchMenuList(query) { return request({ url: '/api/v1/menu_list', method: 'get', params: query }) } ================================================ FILE: frontend/src/api/package.js ================================================ import request from '@/utils/request' export function fetchPackageVersion(query) { return request({ url: '/api/v1/package_verison', method: 'get', params: query }) } ================================================ FILE: frontend/src/api/table.js ================================================ import request from '@/utils/request' export function getList(params) { return request({ url: '/vue-admin-template/table/list', method: 'get', params }) } ================================================ FILE: frontend/src/api/user.js ================================================ import request from '@/utils/request' export function login(data) { return request({ url: '/vue-admin-template/user/login', method: 'post', data }) } export function getInfo(token) { return request({ url: '/vue-admin-template/user/info', method: 'get', params: { token } }) } export function logout() { return request({ url: '/vue-admin-template/user/logout', method: 'post' }) } ================================================ FILE: frontend/src/components/Breadcrumb/index.vue ================================================ ================================================ FILE: frontend/src/components/Hamburger/index.vue ================================================ ================================================ FILE: frontend/src/components/Pagination/index.vue ================================================ ================================================ FILE: frontend/src/components/SvgIcon/index.vue ================================================ ================================================ FILE: frontend/src/directive/el-table/adaptive.js ================================================ import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event' /** * How to use * ... * el-table height is must be set * bottomOffset: 30(default) // The height of the table from the bottom of the page. */ const doResize = (el, binding, vnode) => { const { componentInstance: $table } = vnode const { value } = binding if (!$table.height) { throw new Error(`el-$table must set the height. Such as height='100px'`) } const bottomOffset = (value && value.bottomOffset) || 30 if (!$table) return const height = window.innerHeight - el.getBoundingClientRect().top - bottomOffset $table.layout.setHeight(height) $table.doLayout() } export default { bind(el, binding, vnode) { el.resizeListener = () => { doResize(el, binding, vnode) } // parameter 1 is must be "Element" type addResizeListener(window.document.body, el.resizeListener) }, inserted(el, binding, vnode) { doResize(el, binding, vnode) }, unbind(el) { removeResizeListener(window.document.body, el.resizeListener) } } ================================================ FILE: frontend/src/directive/el-table/index.js ================================================ import adaptive from './adaptive' const install = function(Vue) { Vue.directive('el-height-adaptive-table', adaptive) } if (window.Vue) { window['el-height-adaptive-table'] = adaptive Vue.use(install); // eslint-disable-line } adaptive.install = install export default adaptive ================================================ FILE: frontend/src/directive/waves/index.js ================================================ import waves from './waves' const install = function(Vue) { Vue.directive('waves', waves) } if (window.Vue) { window.waves = waves Vue.use(install); // eslint-disable-line } waves.install = install export default waves ================================================ FILE: frontend/src/directive/waves/waves.css ================================================ .waves-ripple { position: absolute; border-radius: 100%; background-color: rgba(0, 0, 0, 0.15); background-clip: padding-box; pointer-events: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-transform: scale(0); -ms-transform: scale(0); transform: scale(0); opacity: 1; } .waves-ripple.z-active { opacity: 0; -webkit-transform: scale(2); -ms-transform: scale(2); transform: scale(2); -webkit-transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, -webkit-transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out; transition: opacity 1.2s ease-out, transform 0.6s ease-out, -webkit-transform 0.6s ease-out; } ================================================ FILE: frontend/src/directive/waves/waves.js ================================================ import './waves.css' const context = '@@wavesContext' function handleClick(el, binding) { function handle(e) { const customOpts = Object.assign({}, binding.value) const opts = Object.assign({ ele: el, // 波纹作用元素 type: 'hit', // hit 点击位置扩散 center中心点扩展 color: 'rgba(0, 0, 0, 0.15)' // 波纹颜色 }, customOpts ) const target = opts.ele if (target) { target.style.position = 'relative' target.style.overflow = 'hidden' const rect = target.getBoundingClientRect() let ripple = target.querySelector('.waves-ripple') if (!ripple) { ripple = document.createElement('span') ripple.className = 'waves-ripple' ripple.style.height = ripple.style.width = Math.max(rect.width, rect.height) + 'px' target.appendChild(ripple) } else { ripple.className = 'waves-ripple' } switch (opts.type) { case 'center': ripple.style.top = rect.height / 2 - ripple.offsetHeight / 2 + 'px' ripple.style.left = rect.width / 2 - ripple.offsetWidth / 2 + 'px' break default: ripple.style.top = (e.pageY - rect.top - ripple.offsetHeight / 2 - document.documentElement.scrollTop || document.body.scrollTop) + 'px' ripple.style.left = (e.pageX - rect.left - ripple.offsetWidth / 2 - document.documentElement.scrollLeft || document.body.scrollLeft) + 'px' } ripple.style.backgroundColor = opts.color ripple.className = 'waves-ripple z-active' return false } } if (!el[context]) { el[context] = { removeHandle: handle } } else { el[context].removeHandle = handle } return handle } export default { bind(el, binding) { el.addEventListener('click', handleClick(el, binding), false) }, update(el, binding) { el.removeEventListener('click', el[context].removeHandle, false) el.addEventListener('click', handleClick(el, binding), false) }, unbind(el) { el.removeEventListener('click', el[context].removeHandle, false) el[context] = null delete el[context] } } ================================================ FILE: frontend/src/icons/index.js ================================================ import Vue from 'vue' import SvgIcon from '@/components/SvgIcon'// svg component // register globally Vue.component('svg-icon', SvgIcon) const req = require.context('./svg', false, /\.svg$/) const requireAll = requireContext => requireContext.keys().map(requireContext) requireAll(req) ================================================ FILE: frontend/src/icons/svgo.yml ================================================ # replace default config # multipass: true # full: true plugins: # - name # # or: # - name: false # - name: true # # or: # - name: # param1: 1 # param2: 2 - removeAttrs: attrs: - 'fill' - 'fill-rule' ================================================ FILE: frontend/src/layout/components/AppMain.vue ================================================ ================================================ FILE: frontend/src/layout/components/Navbar.vue ================================================ ================================================ FILE: frontend/src/layout/components/Sidebar/FixiOSBug.js ================================================ export default { computed: { device() { return this.$store.state.app.device } }, mounted() { // In order to fix the click on menu on the ios device will trigger the mouseleave bug // https://github.com/PanJiaChen/vue-element-admin/issues/1135 this.fixBugIniOS() }, methods: { fixBugIniOS() { const $subMenu = this.$refs.subMenu if ($subMenu) { const handleMouseleave = $subMenu.handleMouseleave $subMenu.handleMouseleave = (e) => { if (this.device === 'mobile') { return } handleMouseleave(e) } } } } } ================================================ FILE: frontend/src/layout/components/Sidebar/Item.vue ================================================ ================================================ FILE: frontend/src/layout/components/Sidebar/Link.vue ================================================ ================================================ FILE: frontend/src/layout/components/Sidebar/Logo.vue ================================================ ================================================ FILE: frontend/src/layout/components/Sidebar/SidebarItem.vue ================================================ ================================================ FILE: frontend/src/layout/components/Sidebar/index.vue ================================================ ================================================ FILE: frontend/src/layout/components/index.js ================================================ export { default as Navbar } from './Navbar' export { default as Sidebar } from './Sidebar' export { default as AppMain } from './AppMain' ================================================ FILE: frontend/src/layout/index.vue ================================================ ================================================ FILE: frontend/src/layout/mixin/ResizeHandler.js ================================================ import store from '@/store' const { body } = document const WIDTH = 992 // refer to Bootstrap's responsive design export default { watch: { $route(route) { if (this.device === 'mobile' && this.sidebar.opened) { store.dispatch('app/closeSideBar', { withoutAnimation: false }) } } }, beforeMount() { window.addEventListener('resize', this.$_resizeHandler) }, beforeDestroy() { window.removeEventListener('resize', this.$_resizeHandler) }, mounted() { const isMobile = this.$_isMobile() if (isMobile) { store.dispatch('app/toggleDevice', 'mobile') store.dispatch('app/closeSideBar', { withoutAnimation: true }) } }, methods: { // use $_ for mixins properties // https://vuejs.org/v2/style-guide/index.html#Private-property-names-essential $_isMobile() { const rect = body.getBoundingClientRect() return rect.width - 1 < WIDTH }, $_resizeHandler() { if (!document.hidden) { const isMobile = this.$_isMobile() store.dispatch('app/toggleDevice', isMobile ? 'mobile' : 'desktop') if (isMobile) { store.dispatch('app/closeSideBar', { withoutAnimation: true }) } } } } } ================================================ FILE: frontend/src/main.js ================================================ import Vue from 'vue' import 'normalize.css/normalize.css' // A modern alternative to CSS resets import ElementUI from 'element-ui' import 'element-ui/lib/theme-chalk/index.css' import locale from 'element-ui/lib/locale/lang/en' // lang i18n import '@/styles/index.scss' // global css import App from './App' import store from './store' import router from './router' import '@/icons' // icon // import '@/permission' // permission control // 不进行登录校验。 /** * If you don't want to use mock-server * you want to use MockJs for mock api * you can execute: mockXHR() * * Currently MockJs will be used in the production environment, * please remove it before going online ! ! ! */ if (process.env.NODE_ENV === 'production') { const { mockXHR } = require('../mock') mockXHR() } // set ElementUI lang to EN Vue.use(ElementUI, { locale }) // 如果想要中文版 element-ui,按如下方式声明 // Vue.use(ElementUI) Vue.config.productionTip = false new Vue({ el: '#app', router, store, render: h => h(App) }) ================================================ FILE: frontend/src/permission.js ================================================ import router from './router' import store from './store' import { Message } from 'element-ui' import NProgress from 'nprogress' // progress bar import 'nprogress/nprogress.css' // progress bar style import { getToken } from '@/utils/auth' // get token from cookie import getPageTitle from '@/utils/get-page-title' NProgress.configure({ showSpinner: false }) // NProgress Configuration const whiteList = ['/login'] // no redirect whitelist router.beforeEach(async(to, from, next) => { // start progress bar NProgress.start() // set page title document.title = getPageTitle(to.meta.title) // determine whether the user has logged in const hasToken = getToken() if (hasToken) { if (to.path === '/login') { // if is logged in, redirect to the home page next({ path: '/' }) NProgress.done() } else { const hasGetUserInfo = store.getters.name if (hasGetUserInfo) { next() } else { try { // get user info await store.dispatch('user/getInfo') next() } catch (error) { // remove token and go to login page to re-login await store.dispatch('user/resetToken') Message.error(error || 'Has Error') next(`/login?redirect=${to.path}`) NProgress.done() } } } } else { /* has no token*/ if (whiteList.indexOf(to.path) !== -1) { // in the free login whitelist, go directly next() } else { // other pages that do not have permission to access are redirected to the login page. next(`/login?redirect=${to.path}`) NProgress.done() } } }) router.afterEach(() => { // finish progress bar NProgress.done() }) ================================================ FILE: frontend/src/router/index.js ================================================ import Vue from 'vue' import Router from 'vue-router' //Vue全局使用Router Vue.use(Router) /* Layout */ import Layout from '@/layout' import { fetchMenuList } from '@/api/menu' /** * Note: sub-menu only appear when route children.length >= 1 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html * * hidden: true if set true, item will not show in the sidebar(default is false) * alwaysShow: true if set true, will always show the root menu * if not set alwaysShow, when item has more than one children route, * it will becomes nested mode, otherwise not show the root menu * redirect: noRedirect if set noRedirect will no redirect in the breadcrumb * name:'router-name' the name is used by (must set!!!) * meta : { roles: ['admin','editor'] control the page roles (you can set multiple roles) title: 'title' the name show in sidebar and breadcrumb (recommend set) icon: 'svg-name'/'el-icon-x' the icon show in the sidebar breadcrumb: false if set false, the item will hidden in breadcrumb(default is true) activeMenu: '/example/list' if set path, the sidebar will highlight the path you set } */ /** * constantRoutes * a base page that does not have permission requirements * all roles can be accessed */ let constantRoutes = [ { path: '/login', component: () => import('@/views/login/index'), hidden: true }, { path: '/404', component: () => import('@/views/404'), hidden: true }, { path: '/', component: Layout, redirect: '/dashboard', children: [{ path: 'dashboard', name: 'Dashboard', component: () => import('@/views/dashboard/index'), meta: { title: 'Dashboard', icon: 'dashboard' } }] }, { path: '/stock', component: Layout, redirect: '#', name: 'stock-data-table', meta: { title: 'Example', icon: 'el-icon-s-help' }, children: [ { path: 'table/:tableName', name: 'Table', component: () => import('@/views/table/index'), meta: { title: 'Table', icon: 'table' } } ] }, ] fetchMenuList().then(response => { let menu_data = response.data for (const menu of menu_data) { console.info(menu) var childrenList = [] for (const childrenMenu of menu.children) { var tmpChildren = { path: childrenMenu.path, name: childrenMenu.name, component: () => import('@/views/table/index'), meta: { title: childrenMenu.name , icon: 'table' } } childrenList.push(tmpChildren) } var tmp_menu = { path: '/stock'+menu.name, alwaysShow: true, component: Layout, name: menu.name, redirect: '/#'+menu.name , meta: { title: menu.name , icon: 'el-icon-s-help' }, children: childrenList } constantRoutes.push(tmp_menu) constantRoutes.push( { path: '*', redirect: '/404', hidden: true }) } }) // // 404 page must be placed at the end !!! // constantRoutes.push( { path: '*', redirect: '/404', hidden: true }) const createRouter = () => new Router({ // mode: 'history', // require service support scrollBehavior: () => ({ y: 0 }), routes: constantRoutes }) const router = createRouter() // Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465 export function resetRouter() { const newRouter = createRouter() router.matcher = newRouter.matcher // reset router } export default router ================================================ FILE: frontend/src/settings.js ================================================ module.exports = { title: 'Vue Admin Template', /** * @type {boolean} true | false * @description Whether fix the header */ fixedHeader: false, /** * @type {boolean} true | false * @description Whether show the logo in sidebar */ sidebarLogo: false } ================================================ FILE: frontend/src/store/getters.js ================================================ const getters = { sidebar: state => state.app.sidebar, device: state => state.app.device, token: state => state.user.token, avatar: state => state.user.avatar, name: state => state.user.name } export default getters ================================================ FILE: frontend/src/store/index.js ================================================ import Vue from 'vue' import Vuex from 'vuex' import getters from './getters' import app from './modules/app' import settings from './modules/settings' import user from './modules/user' Vue.use(Vuex) const store = new Vuex.Store({ modules: { app, settings, user }, getters }) export default store ================================================ FILE: frontend/src/store/modules/app.js ================================================ import Cookies from 'js-cookie' const state = { sidebar: { opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, withoutAnimation: false }, device: 'desktop' } const mutations = { TOGGLE_SIDEBAR: state => { state.sidebar.opened = !state.sidebar.opened state.sidebar.withoutAnimation = false if (state.sidebar.opened) { Cookies.set('sidebarStatus', 1) } else { Cookies.set('sidebarStatus', 0) } }, CLOSE_SIDEBAR: (state, withoutAnimation) => { Cookies.set('sidebarStatus', 0) state.sidebar.opened = false state.sidebar.withoutAnimation = withoutAnimation }, TOGGLE_DEVICE: (state, device) => { state.device = device } } const actions = { toggleSideBar({ commit }) { commit('TOGGLE_SIDEBAR') }, closeSideBar({ commit }, { withoutAnimation }) { commit('CLOSE_SIDEBAR', withoutAnimation) }, toggleDevice({ commit }, device) { commit('TOGGLE_DEVICE', device) } } export default { namespaced: true, state, mutations, actions } ================================================ FILE: frontend/src/store/modules/settings.js ================================================ import defaultSettings from '@/settings' const { showSettings, fixedHeader, sidebarLogo } = defaultSettings const state = { showSettings: showSettings, fixedHeader: fixedHeader, sidebarLogo: sidebarLogo } const mutations = { CHANGE_SETTING: (state, { key, value }) => { // eslint-disable-next-line no-prototype-builtins if (state.hasOwnProperty(key)) { state[key] = value } } } const actions = { changeSetting({ commit }, data) { commit('CHANGE_SETTING', data) } } export default { namespaced: true, state, mutations, actions } ================================================ FILE: frontend/src/store/modules/user.js ================================================ import { login, logout, getInfo } from '@/api/user' import { getToken, setToken, removeToken } from '@/utils/auth' import { resetRouter } from '@/router' const getDefaultState = () => { return { token: getToken(), name: '', avatar: '' } } const state = getDefaultState() const mutations = { RESET_STATE: (state) => { Object.assign(state, getDefaultState()) }, SET_TOKEN: (state, token) => { state.token = token }, SET_NAME: (state, name) => { state.name = name }, SET_AVATAR: (state, avatar) => { state.avatar = avatar } } const actions = { // user login login({ commit }, userInfo) { const { username, password } = userInfo return new Promise((resolve, reject) => { login({ username: username.trim(), password: password }).then(response => { const { data } = response commit('SET_TOKEN', data.token) setToken(data.token) resolve() }).catch(error => { reject(error) }) }) }, // get user info getInfo({ commit, state }) { return new Promise((resolve, reject) => { getInfo(state.token).then(response => { const { data } = response if (!data) { return reject('Verification failed, please Login again.') } const { name, avatar } = data commit('SET_NAME', name) commit('SET_AVATAR', avatar) resolve(data) }).catch(error => { reject(error) }) }) }, // user logout logout({ commit, state }) { return new Promise((resolve, reject) => { logout(state.token).then(() => { removeToken() // must remove token first resetRouter() commit('RESET_STATE') resolve() }).catch(error => { reject(error) }) }) }, // remove token resetToken({ commit }) { return new Promise(resolve => { removeToken() // must remove token first commit('RESET_STATE') resolve() }) } } export default { namespaced: true, state, mutations, actions } ================================================ FILE: frontend/src/styles/element-ui.scss ================================================ // cover some element-ui styles .el-breadcrumb__inner, .el-breadcrumb__inner a { font-weight: 400 !important; } .el-upload { input[type="file"] { display: none !important; } } .el-upload__input { display: none; } // to fixed https://github.com/ElemeFE/element/issues/2461 .el-dialog { transform: none; left: 0; position: relative; margin: 0 auto; } // refine element ui upload .upload-container { .el-upload { width: 100%; .el-upload-dragger { width: 100%; height: 200px; } } } // dropdown .el-dropdown-menu { a { display: block } } // to fix el-date-picker css style .el-range-separator { box-sizing: content-box; } ================================================ FILE: frontend/src/styles/index.scss ================================================ @import './variables.scss'; @import './mixin.scss'; @import './transition.scss'; @import './element-ui.scss'; @import './sidebar.scss'; body { height: 100%; -moz-osx-font-smoothing: grayscale; -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility; font-family: Helvetica Neue, Helvetica, PingFang SC, Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif; } label { font-weight: 700; } html { height: 100%; box-sizing: border-box; } #app { height: 100%; } *, *:before, *:after { box-sizing: inherit; } a:focus, a:active { outline: none; } a, a:focus, a:hover { cursor: pointer; color: inherit; text-decoration: none; } div:focus { outline: none; } .clearfix { &:after { visibility: hidden; display: block; font-size: 0; content: " "; clear: both; height: 0; } } // main-container global css .app-container { padding: 20px; } ================================================ FILE: frontend/src/styles/mixin.scss ================================================ @mixin clearfix { &:after { content: ""; display: table; clear: both; } } @mixin scrollBar { &::-webkit-scrollbar-track-piece { background: #d3dce6; } &::-webkit-scrollbar { width: 6px; } &::-webkit-scrollbar-thumb { background: #99a9bf; border-radius: 20px; } } @mixin relative { position: relative; width: 100%; height: 100%; } ================================================ FILE: frontend/src/styles/sidebar.scss ================================================ #app { .main-container { min-height: 100%; transition: margin-left .28s; margin-left: $sideBarWidth; position: relative; } .sidebar-container { transition: width 0.28s; width: $sideBarWidth !important; background-color: $menuBg; height: 100%; position: fixed; font-size: 0px; top: 0; bottom: 0; left: 0; z-index: 1001; overflow: hidden; // reset element-ui css .horizontal-collapse-transition { transition: 0s width ease-in-out, 0s padding-left ease-in-out, 0s padding-right ease-in-out; } .scrollbar-wrapper { overflow-x: hidden !important; } .el-scrollbar__bar.is-vertical { right: 0px; } .el-scrollbar { height: 100%; } &.has-logo { .el-scrollbar { height: calc(100% - 50px); } } .is-horizontal { display: none; } a { display: inline-block; width: 100%; overflow: hidden; } .svg-icon { margin-right: 16px; } .sub-el-icon { margin-right: 12px; margin-left: -2px; } .el-menu { border: none; height: 100%; width: 100% !important; } // menu hover .submenu-title-noDropdown, .el-submenu__title { &:hover { background-color: $menuHover !important; } } .is-active>.el-submenu__title { color: $subMenuActiveText !important; } & .nest-menu .el-submenu>.el-submenu__title, & .el-submenu .el-menu-item { min-width: $sideBarWidth !important; background-color: $subMenuBg !important; &:hover { background-color: $subMenuHover !important; } } } .hideSidebar { .sidebar-container { width: 54px !important; } .main-container { margin-left: 54px; } .submenu-title-noDropdown { padding: 0 !important; position: relative; .el-tooltip { padding: 0 !important; .svg-icon { margin-left: 20px; } .sub-el-icon { margin-left: 19px; } } } .el-submenu { overflow: hidden; &>.el-submenu__title { padding: 0 !important; .svg-icon { margin-left: 20px; } .sub-el-icon { margin-left: 19px; } .el-submenu__icon-arrow { display: none; } } } .el-menu--collapse { .el-submenu { &>.el-submenu__title { &>span { height: 0; width: 0; overflow: hidden; visibility: hidden; display: inline-block; } } } } } .el-menu--collapse .el-menu .el-submenu { min-width: $sideBarWidth !important; } // mobile responsive .mobile { .main-container { margin-left: 0px; } .sidebar-container { transition: transform .28s; width: $sideBarWidth !important; } &.hideSidebar { .sidebar-container { pointer-events: none; transition-duration: 0.3s; transform: translate3d(-$sideBarWidth, 0, 0); } } } .withoutAnimation { .main-container, .sidebar-container { transition: none; } } } // when menu collapsed .el-menu--vertical { &>.el-menu { .svg-icon { margin-right: 16px; } .sub-el-icon { margin-right: 12px; margin-left: -2px; } } .nest-menu .el-submenu>.el-submenu__title, .el-menu-item { &:hover { // you can use $subMenuHover background-color: $menuHover !important; } } // the scroll bar appears when the subMenu is too long >.el-menu--popup { max-height: 100vh; overflow-y: auto; &::-webkit-scrollbar-track-piece { background: #d3dce6; } &::-webkit-scrollbar { width: 6px; } &::-webkit-scrollbar-thumb { background: #99a9bf; border-radius: 20px; } } } ================================================ FILE: frontend/src/styles/transition.scss ================================================ // global transition css /* fade */ .fade-enter-active, .fade-leave-active { transition: opacity 0.28s; } .fade-enter, .fade-leave-active { opacity: 0; } /* fade-transform */ .fade-transform-leave-active, .fade-transform-enter-active { transition: all .5s; } .fade-transform-enter { opacity: 0; transform: translateX(-30px); } .fade-transform-leave-to { opacity: 0; transform: translateX(30px); } /* breadcrumb transition */ .breadcrumb-enter-active, .breadcrumb-leave-active { transition: all .5s; } .breadcrumb-enter, .breadcrumb-leave-active { opacity: 0; transform: translateX(20px); } .breadcrumb-move { transition: all .5s; } .breadcrumb-leave-active { position: absolute; } ================================================ FILE: frontend/src/styles/variables.scss ================================================ // sidebar $menuText:#bfcbd9; $menuActiveText:#409EFF; $subMenuActiveText:#f4f4f5; //https://github.com/ElemeFE/element/issues/12951 $menuBg:#304156; $menuHover:#263445; $subMenuBg:#1f2d3d; $subMenuHover:#001528; $sideBarWidth: 210px; // the :export directive is the magic sauce for webpack // https://www.bluematador.com/blog/how-to-share-variables-between-js-and-sass :export { menuText: $menuText; menuActiveText: $menuActiveText; subMenuActiveText: $subMenuActiveText; menuBg: $menuBg; menuHover: $menuHover; subMenuBg: $subMenuBg; subMenuHover: $subMenuHover; sideBarWidth: $sideBarWidth; } ================================================ FILE: frontend/src/utils/auth.js ================================================ import Cookies from 'js-cookie' const TokenKey = 'vue_admin_template_token' export function getToken() { return Cookies.get(TokenKey) } export function setToken(token) { return Cookies.set(TokenKey, token) } export function removeToken() { return Cookies.remove(TokenKey) } ================================================ FILE: frontend/src/utils/get-page-title.js ================================================ import defaultSettings from '@/settings' const title = defaultSettings.title || 'Vue Admin Template' export default function getPageTitle(pageTitle) { if (pageTitle) { return `${pageTitle} - ${title}` } return `${title}` } ================================================ FILE: frontend/src/utils/index.js ================================================ /** * Created by PanJiaChen on 16/11/18. */ /** * Parse the time to string * @param {(Object|string|number)} time * @param {string} cFormat * @returns {string | null} */ export function parseTime(time, cFormat) { if (arguments.length === 0 || !time) { return null } const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string')) { if ((/^[0-9]+$/.test(time))) { // support "1548221490638" time = parseInt(time) } else { // support safari // https://stackoverflow.com/questions/4310953/invalid-date-in-safari time = time.replace(new RegExp(/-/gm), '/') } } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => { const value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value ] } return value.toString().padStart(2, '0') }) return time_str } /** * @param {number} time * @param {string} option * @returns {string} */ export function formatTime(time, option) { if (('' + time).length === 10) { time = parseInt(time) * 1000 } else { time = +time } const d = new Date(time) const now = Date.now() const diff = (now - d) / 1000 if (diff < 30) { return '刚刚' } else if (diff < 3600) { // less 1 hour return Math.ceil(diff / 60) + '分钟前' } else if (diff < 3600 * 24) { return Math.ceil(diff / 3600) + '小时前' } else if (diff < 3600 * 24 * 2) { return '1天前' } if (option) { return parseTime(time, option) } else { return ( d.getMonth() + 1 + '月' + d.getDate() + '日' + d.getHours() + '时' + d.getMinutes() + '分' ) } } /** * @param {string} url * @returns {Object} */ export function param2Obj(url) { const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ') if (!search) { return {} } const obj = {} const searchArr = search.split('&') searchArr.forEach(v => { const index = v.indexOf('=') if (index !== -1) { const name = v.substring(0, index) const val = v.substring(index + 1, v.length) obj[name] = val } }) return obj } ================================================ FILE: frontend/src/utils/request.js ================================================ import axios from 'axios' import { MessageBox, Message } from 'element-ui' import store from '@/store' import { getToken } from '@/utils/auth' // create an axios instance const service = axios.create({ baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url withCredentials: true, // send cookies when cross-domain requests timeout: 5000 // request timeout }) // request interceptor service.interceptors.request.use( config => { // do something before request is sent if (store.getters.token) { // let each request carry token // ['X-Token'] is a custom headers key // please modify it according to the actual situation config.headers['X-Token'] = getToken() } return config }, error => { // do something with request error console.log(error) // for debug return Promise.reject(error) } ) // response interceptor service.interceptors.response.use( /** * If you want to get http information such as headers or status * Please return response => response */ /** * Determine the request status by custom code * Here is just an example * You can also judge the status by HTTP Status Code */ response => { const res = response.data console.log('res.code: ' + res.code) // for debug // if the custom code is not 20000, it is judged as an error. if (res.code !== 20000) { Message({ message: res.message || 'Error', type: 'error', duration: 5 * 1000 }) // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired; if (res.code === 50008 || res.code === 50012 || res.code === 50014) { // to re-login MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', { confirmButtonText: 'Re-Login', cancelButtonText: 'Cancel', type: 'warning' }).then(() => { store.dispatch('user/resetToken').then(() => { location.reload() }) }) } return Promise.reject(new Error(res.message || 'Error')) } else { return res } }, error => { console.log('err' + error) // for debug Message({ message: error.message, type: 'error', duration: 5 * 1000 }) return Promise.reject(error) } ) export default service ================================================ FILE: frontend/src/utils/scroll-to.js ================================================ Math.easeInOutQuad = function(t, b, c, d) { t /= d / 2 if (t < 1) { return c / 2 * t * t + b } t-- return -c / 2 * (t * (t - 2) - 1) + b } // requestAnimationFrame for Smart Animating http://goo.gl/sx5sts var requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60) } })() /** * Because it's so fucking difficult to detect the scrolling element, just move them all * @param {number} amount */ function move(amount) { document.documentElement.scrollTop = amount document.body.parentNode.scrollTop = amount document.body.scrollTop = amount } function position() { return document.documentElement.scrollTop || document.body.parentNode.scrollTop || document.body.scrollTop } /** * @param {number} to * @param {number} duration * @param {Function} callback */ export function scrollTo(to, duration, callback) { const start = position() const change = to - start const increment = 20 let currentTime = 0 duration = (typeof (duration) === 'undefined') ? 500 : duration var animateScroll = function() { // increment the time currentTime += increment // find the value with the quadratic in-out easing function var val = Math.easeInOutQuad(currentTime, start, change, duration) // move the document.body move(val) // do the animation unless its over if (currentTime < duration) { requestAnimFrame(animateScroll) } else { if (callback && typeof (callback) === 'function') { // the animation is done so lets callback callback() } } } animateScroll() } ================================================ FILE: frontend/src/utils/validate.js ================================================ /** * Created by PanJiaChen on 16/11/18. */ /** * @param {string} path * @returns {Boolean} */ export function isExternal(path) { return /^(https?:|mailto:|tel:)/.test(path) } /** * @param {string} str * @returns {Boolean} */ export function validUsername(str) { const valid_map = ['admin', 'editor'] return valid_map.indexOf(str.trim()) >= 0 } ================================================ FILE: frontend/src/vendor/Export2Excel.js ================================================ /* eslint-disable */ import { saveAs } from 'file-saver' import XLSX from 'xlsx' function generateArray(table) { var out = []; var rows = table.querySelectorAll('tr'); var ranges = []; for (var R = 0; R < rows.length; ++R) { var outRow = []; var row = rows[R]; var columns = row.querySelectorAll('td'); for (var C = 0; C < columns.length; ++C) { var cell = columns[C]; var colspan = cell.getAttribute('colspan'); var rowspan = cell.getAttribute('rowspan'); var cellValue = cell.innerText; if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue; //Skip ranges ranges.forEach(function (range) { if (R >= range.s.r && R <= range.e.r && outRow.length >= range.s.c && outRow.length <= range.e.c) { for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null); } }); //Handle Row Span if (rowspan || colspan) { rowspan = rowspan || 1; colspan = colspan || 1; ranges.push({ s: { r: R, c: outRow.length }, e: { r: R + rowspan - 1, c: outRow.length + colspan - 1 } }); }; //Handle Value outRow.push(cellValue !== "" ? cellValue : null); //Handle Colspan if (colspan) for (var k = 0; k < colspan - 1; ++k) outRow.push(null); } out.push(outRow); } return [out, ranges]; }; function datenum(v, date1904) { if (date1904) v += 1462; var epoch = Date.parse(v); return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000); } function sheet_from_array_of_arrays(data, opts) { var ws = {}; var range = { s: { c: 10000000, r: 10000000 }, e: { c: 0, r: 0 } }; for (var R = 0; R != data.length; ++R) { for (var C = 0; C != data[R].length; ++C) { if (range.s.r > R) range.s.r = R; if (range.s.c > C) range.s.c = C; if (range.e.r < R) range.e.r = R; if (range.e.c < C) range.e.c = C; var cell = { v: data[R][C] }; if (cell.v == null) continue; var cell_ref = XLSX.utils.encode_cell({ c: C, r: R }); if (typeof cell.v === 'number') cell.t = 'n'; else if (typeof cell.v === 'boolean') cell.t = 'b'; else if (cell.v instanceof Date) { cell.t = 'n'; cell.z = XLSX.SSF._table[14]; cell.v = datenum(cell.v); } else cell.t = 's'; ws[cell_ref] = cell; } } if (range.s.c < 10000000) ws['!ref'] = XLSX.utils.encode_range(range); return ws; } function Workbook() { if (!(this instanceof Workbook)) return new Workbook(); this.SheetNames = []; this.Sheets = {}; } function s2ab(s) { var buf = new ArrayBuffer(s.length); var view = new Uint8Array(buf); for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF; return buf; } export function export_table_to_excel(id) { var theTable = document.getElementById(id); var oo = generateArray(theTable); var ranges = oo[1]; /* original data */ var data = oo[0]; var ws_name = "SheetJS"; var wb = new Workbook(), ws = sheet_from_array_of_arrays(data); /* add ranges to worksheet */ // ws['!cols'] = ['apple', 'banan']; ws['!merges'] = ranges; /* add worksheet to workbook */ wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' }); saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), "test.xlsx") } export function export_json_to_excel({ multiHeader = [], header, data, filename, merges = [], autoWidth = true, bookType = 'xlsx' } = {}) { /* original data */ filename = filename || 'excel-list' data = [...data] data.unshift(header); for (let i = multiHeader.length - 1; i > -1; i--) { data.unshift(multiHeader[i]) } var ws_name = "SheetJS"; var wb = new Workbook(), ws = sheet_from_array_of_arrays(data); if (merges.length > 0) { if (!ws['!merges']) ws['!merges'] = []; merges.forEach(item => { ws['!merges'].push(XLSX.utils.decode_range(item)) }) } if (autoWidth) { /*设置worksheet每列的最大宽度*/ const colWidth = data.map(row => row.map(val => { /*先判断是否为null/undefined*/ if (val == null) { return { 'wch': 10 }; } /*再判断是否为中文*/ else if (val.toString().charCodeAt(0) > 255) { return { 'wch': val.toString().length * 2 }; } else { return { 'wch': val.toString().length }; } })) /*以第一行为初始值*/ let result = colWidth[0]; for (let i = 1; i < colWidth.length; i++) { for (let j = 0; j < colWidth[i].length; j++) { if (result[j]['wch'] < colWidth[i][j]['wch']) { result[j]['wch'] = colWidth[i][j]['wch']; } } } ws['!cols'] = result; } /* add worksheet to workbook */ wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; var wbout = XLSX.write(wb, { bookType: bookType, bookSST: false, type: 'binary' }); saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), `${filename}.${bookType}`); } ================================================ FILE: frontend/src/views/404.vue ================================================ ================================================ FILE: frontend/src/views/dashboard/index.vue ================================================ ================================================ FILE: frontend/src/views/form/index.vue ================================================ ================================================ FILE: frontend/src/views/login/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/menu1-1/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/menu1-2/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/menu1-2/menu1-2-1/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/menu1-2/menu1-2-2/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu1/menu1-3/index.vue ================================================ ================================================ FILE: frontend/src/views/nested/menu2/index.vue ================================================ ================================================ FILE: frontend/src/views/table/complex-table.vue ================================================ ================================================ FILE: frontend/src/views/table/index.vue ================================================ ================================================ FILE: frontend/src/views/tree/index.vue ================================================ ================================================ FILE: frontend/tests/unit/.eslintrc.js ================================================ module.exports = { env: { jest: true } } ================================================ FILE: frontend/tests/unit/components/Breadcrumb.spec.js ================================================ import { mount, createLocalVue } from '@vue/test-utils' import VueRouter from 'vue-router' import ElementUI from 'element-ui' import Breadcrumb from '@/components/Breadcrumb/index.vue' const localVue = createLocalVue() localVue.use(VueRouter) localVue.use(ElementUI) const routes = [ { path: '/', name: 'home', children: [{ path: 'dashboard', name: 'dashboard' }] }, { path: '/menu', name: 'menu', children: [{ path: 'menu1', name: 'menu1', meta: { title: 'menu1' }, children: [{ path: 'menu1-1', name: 'menu1-1', meta: { title: 'menu1-1' } }, { path: 'menu1-2', name: 'menu1-2', redirect: 'noredirect', meta: { title: 'menu1-2' }, children: [{ path: 'menu1-2-1', name: 'menu1-2-1', meta: { title: 'menu1-2-1' } }, { path: 'menu1-2-2', name: 'menu1-2-2' }] }] }] }] const router = new VueRouter({ routes }) describe('Breadcrumb.vue', () => { const wrapper = mount(Breadcrumb, { localVue, router }) it('dashboard', () => { router.push('/dashboard') const len = wrapper.findAll('.el-breadcrumb__inner').length expect(len).toBe(1) }) it('normal route', () => { router.push('/menu/menu1') const len = wrapper.findAll('.el-breadcrumb__inner').length expect(len).toBe(2) }) it('nested route', () => { router.push('/menu/menu1/menu1-2/menu1-2-1') const len = wrapper.findAll('.el-breadcrumb__inner').length expect(len).toBe(4) }) it('no meta.title', () => { router.push('/menu/menu1/menu1-2/menu1-2-2') const len = wrapper.findAll('.el-breadcrumb__inner').length expect(len).toBe(3) }) // it('click link', () => { // router.push('/menu/menu1/menu1-2/menu1-2-2') // const breadcrumbArray = wrapper.findAll('.el-breadcrumb__inner') // const second = breadcrumbArray.at(1) // console.log(breadcrumbArray) // const href = second.find('a').attributes().href // expect(href).toBe('#/menu/menu1') // }) // it('noRedirect', () => { // router.push('/menu/menu1/menu1-2/menu1-2-1') // const breadcrumbArray = wrapper.findAll('.el-breadcrumb__inner') // const redirectBreadcrumb = breadcrumbArray.at(2) // expect(redirectBreadcrumb.contains('a')).toBe(false) // }) it('last breadcrumb', () => { router.push('/menu/menu1/menu1-2/menu1-2-1') const breadcrumbArray = wrapper.findAll('.el-breadcrumb__inner') const redirectBreadcrumb = breadcrumbArray.at(3) expect(redirectBreadcrumb.contains('a')).toBe(false) }) }) ================================================ FILE: frontend/tests/unit/components/Hamburger.spec.js ================================================ import { shallowMount } from '@vue/test-utils' import Hamburger from '@/components/Hamburger/index.vue' describe('Hamburger.vue', () => { it('toggle click', () => { const wrapper = shallowMount(Hamburger) const mockFn = jest.fn() wrapper.vm.$on('toggleClick', mockFn) wrapper.find('.hamburger').trigger('click') expect(mockFn).toBeCalled() }) it('prop isActive', () => { const wrapper = shallowMount(Hamburger) wrapper.setProps({ isActive: true }) expect(wrapper.contains('.is-active')).toBe(true) wrapper.setProps({ isActive: false }) expect(wrapper.contains('.is-active')).toBe(false) }) }) ================================================ FILE: frontend/tests/unit/components/SvgIcon.spec.js ================================================ import { shallowMount } from '@vue/test-utils' import SvgIcon from '@/components/SvgIcon/index.vue' describe('SvgIcon.vue', () => { it('iconClass', () => { const wrapper = shallowMount(SvgIcon, { propsData: { iconClass: 'test' } }) expect(wrapper.find('use').attributes().href).toBe('#icon-test') }) it('className', () => { const wrapper = shallowMount(SvgIcon, { propsData: { iconClass: 'test' } }) expect(wrapper.classes().length).toBe(1) wrapper.setProps({ className: 'test' }) expect(wrapper.classes().includes('test')).toBe(true) }) }) ================================================ FILE: frontend/tests/unit/utils/formatTime.spec.js ================================================ import { formatTime } from '@/utils/index.js' describe('Utils:formatTime', () => { const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01" const retrofit = 5 * 1000 it('ten digits timestamp', () => { expect(formatTime((d / 1000).toFixed(0))).toBe('7月13日17时54分') }) it('test now', () => { expect(formatTime(+new Date() - 1)).toBe('刚刚') }) it('less two minute', () => { expect(formatTime(+new Date() - 60 * 2 * 1000 + retrofit)).toBe('2分钟前') }) it('less two hour', () => { expect(formatTime(+new Date() - 60 * 60 * 2 * 1000 + retrofit)).toBe('2小时前') }) it('less one day', () => { expect(formatTime(+new Date() - 60 * 60 * 24 * 1 * 1000)).toBe('1天前') }) it('more than one day', () => { expect(formatTime(d)).toBe('7月13日17时54分') }) it('format', () => { expect(formatTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54') expect(formatTime(d, '{y}-{m}-{d}')).toBe('2018-07-13') expect(formatTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54') }) }) ================================================ FILE: frontend/tests/unit/utils/param2Obj.spec.js ================================================ import { param2Obj } from '@/utils/index.js' describe('Utils:param2Obj', () => { const url = 'https://github.com/PanJiaChen/vue-element-admin?name=bill&age=29&sex=1&field=dGVzdA==&key=%E6%B5%8B%E8%AF%95' it('param2Obj test', () => { expect(param2Obj(url)).toEqual({ name: 'bill', age: '29', sex: '1', field: window.btoa('test'), key: '测试' }) }) }) ================================================ FILE: frontend/tests/unit/utils/parseTime.spec.js ================================================ import { parseTime } from '@/utils/index.js' describe('Utils:parseTime', () => { const d = new Date('2018-07-13 17:54:01') // "2018-07-13 17:54:01" it('timestamp', () => { expect(parseTime(d)).toBe('2018-07-13 17:54:01') }) it('timestamp string', () => { expect(parseTime((d + ''))).toBe('2018-07-13 17:54:01') }) it('ten digits timestamp', () => { expect(parseTime((d / 1000).toFixed(0))).toBe('2018-07-13 17:54:01') }) it('new Date', () => { expect(parseTime(new Date(d))).toBe('2018-07-13 17:54:01') }) it('format', () => { expect(parseTime(d, '{y}-{m}-{d} {h}:{i}')).toBe('2018-07-13 17:54') expect(parseTime(d, '{y}-{m}-{d}')).toBe('2018-07-13') expect(parseTime(d, '{y}/{m}/{d} {h}-{i}')).toBe('2018/07/13 17-54') }) it('get the day of the week', () => { expect(parseTime(d, '{a}')).toBe('五') // 星期五 }) it('get the day of the week', () => { expect(parseTime(+d + 1000 * 60 * 60 * 24 * 2, '{a}')).toBe('日') // 星期日 }) it('empty argument', () => { expect(parseTime()).toBeNull() }) it('null', () => { expect(parseTime(null)).toBeNull() }) }) ================================================ FILE: frontend/tests/unit/utils/validate.spec.js ================================================ import { validUsername, isExternal } from '@/utils/validate.js' describe('Utils:validate', () => { it('validUsername', () => { expect(validUsername('admin')).toBe(true) expect(validUsername('editor')).toBe(true) expect(validUsername('xxxx')).toBe(false) }) it('isExternal', () => { expect(isExternal('https://github.com/PanJiaChen/vue-element-admin')).toBe(true) expect(isExternal('http://github.com/PanJiaChen/vue-element-admin')).toBe(true) expect(isExternal('github.com/PanJiaChen/vue-element-admin')).toBe(false) expect(isExternal('/dashboard')).toBe(false) expect(isExternal('./dashboard')).toBe(false) expect(isExternal('dashboard')).toBe(false) }) }) ================================================ FILE: frontend/vue.config.js ================================================ 'use strict' const path = require('path') const defaultSettings = require('./src/settings.js') function resolve(dir) { return path.join(__dirname, dir) } const name = defaultSettings.title || 'vue Admin Template' // page title // If your port is set to 80, // use administrator privileges to execute the command line. // For example, Mac: sudo npm run // You can change the port by the following methods: // port = 9528 npm run dev OR npm run dev --port = 9528 const port = process.env.port || process.env.npm_config_port || 9528 // dev port // All configuration item explanations can be find in https://cli.vuejs.org/config/ module.exports = { /** * You will need to set publicPath if you plan to deploy your site under a sub path, * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/, * then publicPath should be set to "/bar/". * In most cases please use '/' !!! * Detail: https://cli.vuejs.org/config/#publicpath */ publicPath: '/', outputDir: 'dist', assetsDir: 'static', lintOnSave: process.env.NODE_ENV === 'development', productionSourceMap: false, devServer: { host: '0.0.0.0', port: 8080, proxy: { '/api/v1': { target: 'http://backend:9090/api/v1', changeOrigin: true, pathRewrite: { '^/api/v1': '' } } } }, configureWebpack: { // provide the app's title in webpack's name field, so that // it can be accessed in index.html to inject the correct title. name: name, resolve: { alias: { '@': resolve('src') } } }, chainWebpack(config) { // it can improve the speed of the first screen, it is recommended to turn on preload config.plugin('preload').tap(() => [ { rel: 'preload', // to ignore runtime.js // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171 fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/], include: 'initial' } ]) // when there are many pages, it will cause too many meaningless requests config.plugins.delete('prefetch') // set svg-sprite-loader config.module .rule('svg') .exclude.add(resolve('src/icons')) .end() config.module .rule('icons') .test(/\.svg$/) .include.add(resolve('src/icons')) .end() .use('svg-sprite-loader') .loader('svg-sprite-loader') .options({ symbolId: 'icon-[name]' }) .end() config .when(process.env.NODE_ENV !== 'development', config => { config .plugin('ScriptExtHtmlWebpackPlugin') .after('html') .use('script-ext-html-webpack-plugin', [{ // `runtime` must same as runtimeChunk name. default is `runtime` inline: /runtime\..*\.js$/ }]) .end() config .optimization.splitChunks({ chunks: 'all', cacheGroups: { libs: { name: 'chunk-libs', test: /[\\/]node_modules[\\/]/, priority: 10, chunks: 'initial' // only package third parties that are initially dependent }, elementUI: { name: 'chunk-elementUI', // split elementUI into a single package priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm }, commons: { name: 'chunk-commons', test: resolve('src/components'), // can customize your rules minChunks: 3, // minimum common number priority: 5, reuseExistingChunk: true } } }) // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk config.optimization.runtimeChunk('single') } ) } }