├── .gitignore ├── LICENSE ├── README.md ├── documents ├── 低风险及套利类.md ├── 公用类.md ├── 前言.md ├── 基础类.md ├── 宏观经济数据类.md ├── 市场类.md ├── 投资参考类.md ├── 股票交易类.md ├── 股票信息类.md ├── 股票所属类.md └── 龙虎榜类.md ├── gugu ├── __init__.py ├── base.py ├── billboard.py ├── classify.py ├── config.py ├── lowriskintarb.py ├── macro.py ├── marketdata.py ├── reference.py ├── stockdata.py ├── stockinfo.py └── utility.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | 3 | # Byte-compiled / optimized / DLL files 4 | *.log 5 | *.swp 6 | *.pdb 7 | .project 8 | .pydevproject 9 | .settings 10 | __pycache__/ 11 | *.py[cod] 12 | 13 | # C extensions 14 | *.so 15 | 16 | # Distribution / packaging 17 | .Python 18 | env/ 19 | build/ 20 | develop-eggs/ 21 | dist/ 22 | downloads/ 23 | eggs/ 24 | source/ 25 | baks/ 26 | lib/ 27 | lib64/ 28 | parts/ 29 | sdist/ 30 | var/ 31 | docs/ 32 | *.egg-info/ 33 | .installed.cfg 34 | *.egg 35 | 36 | # PyInstaller 37 | # Usually these files are written by a python script from a template 38 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 39 | *.manifest 40 | *.spec 41 | 42 | # Installer logs 43 | pip-log.txt 44 | pip-delete-this-directory.txt 45 | 46 | # Unit test / coverage reports 47 | htmlcov/ 48 | .tox/ 49 | .coverage 50 | .cache 51 | nosetests.xml 52 | coverage.xml 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | 61 | # Sphinx documentation 62 | docs/_build/ 63 | docs/*.md 64 | docs/bin 65 | .idea/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | #Visual Studio Environment 71 | *.pyproj 72 | *.sln 73 | .vs/ 74 | 75 | #Vim Environment 76 | tags 77 | *~ 78 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gugu(股估) 2 | 3 | gugu(股估)是适用于金融/投资领域数据分析及量化工程的开源项目,通过对互联网上的公开数据进行采集、清洗和存储,完成了对股票/债券/基金等金融数据的统一调用和分析。其优点是速度快、可定制及高度的可复用性。您不仅可以将其作为单独的数据接口使用,还可以将其集成在您的项目中作为数据获取api进行调用。 4 | 5 | 6 | 使用前提: 7 | 8 | python 2.x/3.x 9 | 10 | 安装方式: 11 | 12 | 方式1: pip install gugu or pip3 install gugu 13 | 14 | 方式2: python setup.py install 15 | 16 | 方式3:访问 https://pypi.python.org/pypi/gugu/ 下载安装 17 | 18 | 版本升级: 19 | 20 | pip install gugu --upgrade or pip3 install gugu --upgrade 21 | 22 | 查看当前版本的方法: 23 | 24 | import gugu 25 | 26 | print(gugu.__version__) 27 | 28 | Quick Start 29 | 30 | 31 | Example 1: 获取大盘指数行情 32 | 33 | import gugu as gg 34 | 35 | obj = gg.MarketData() # 创建市场数据类对象 36 | data = obj.index() # 获取大盘指数行情 37 | 38 | 获取结果: 39 | obj.output() # 输出结果 40 | 41 | 42 | code name change open preclose close high \ 43 | 0 sh000001 上证指数 1.30 2597.7777 2584.5724 2618.2323 2618.4765 44 | 1 sh000002 A股指数 1.30 2720.4606 2706.5853 2741.8442 2742.1007 45 | 2 sh000003 B股指数 1.18 271.9034 272.1140 275.3162 275.3162 46 | 3 sh000008 综合指数 0.61 2557.2054 2543.6348 2559.2362 2561.5066 47 | 4 sh000009 上证380 2.47 3892.8231 3880.0032 3975.8523 3976.0933 48 | 5 sh000010 上证180 1.07 7293.4790 7241.9589 7319.4839 7320.2873 49 | 6 sh000011 基金指数 0.90 5642.7602 5629.3801 5679.8086 5679.8339 50 | 7 sh000012 国债指数 0.01 170.9805 170.9642 170.9819 170.9892 51 | 8 sh000016 上证50 0.65 2502.1044 2483.4709 2499.6442 2503.5526 52 | 9 sh000017 新综指 1.30 2193.5148 2182.3286 2210.7524 2210.9592 53 | 10 sh000300 沪深300 1.43 3225.7335 3201.6331 3247.3971 3247.4956 54 | 11 sh000905 中证500 2.82 4189.1098 4176.4739 4294.2696 4295.0379 55 | 12 sz399001 深证成指 2.74 7524.6650 7479.2150 7683.9970 7684.1120 56 | 13 sz399002 深成指R 2.74 9097.7900 9042.8390 9290.4330 9290.5710 57 | 14 sz399003 成份B指 1.99 4442.0740 4445.4680 4534.0600 4534.0600 58 | 15 sz399004 深证100R 2.49 4063.9900 4029.3260 4129.5800 4130.6630 59 | 16 sz399005 中小板指 3.47 4866.8680 4836.6130 5004.4890 5004.4890 60 | 17 sz399006 创业板指 3.53 1233.3840 1227.9880 1271.2750 1272.1930 61 | 18 sz399008 中小300 3.27 947.0020 942.1740 973.0090 973.0090 62 | 19 sz399100 新 指 数 2.69 5850.7930 5819.9190 5976.5880 5976.5880 63 | 20 sz399101 中小板综 3.03 7393.8140 7358.2710 7581.1830 7581.1830 64 | 21 sz399106 深证综指 2.77 1281.1620 1274.7440 1309.9910 1309.9910 65 | 22 sz399107 深证A指 2.77 1339.4990 1332.7690 1369.6510 1369.6510 66 | 23 sz399108 深证B指 2.09 857.4780 856.8890 874.7950 874.7950 67 | 24 sz399333 中小板R 3.47 5412.0040 5378.3600 5565.0400 5565.0400 68 | 25 sz399606 创业板R 3.53 1292.0270 1286.3740 1331.7190 1332.6810 69 | 70 | low volume amount 71 | 0 2590.5543 131986850 1115.2263 72 | 1 2712.8352 131757820 1114.1248 73 | 2 271.9034 229031 1.1015 74 | 3 2538.8951 35179591 275.8689 75 | 4 3892.8231 33132080 260.5541 76 | 5 7247.4721 45958066 529.0995 77 | 6 5637.2069 41987625 243.7168 78 | 7 170.9733 135921 1.3286 79 | 8 2479.3375 21950054 293.6402 80 | 9 2187.3669 130134579 1096.8641 81 | 10 3207.7168 82019641 947.6641 82 | 11 4189.1098 66099677 476.9347 83 | 12 7517.8340 18099026538 1502.2290 84 | 13 9089.5310 8948052337 883.8526 85 | 14 4442.0740 6063605 0.4918 86 | 15 4045.8820 3531953166 453.0613 87 | 16 4866.4270 2440239019 270.5996 88 | 17 1233.3840 1566379186 196.9856 89 | 18 947.0020 4974249025 426.8148 90 | 19 5850.7930 17446832212 1472.6754 91 | 20 7393.8140 8257066751 651.9033 92 | 21 1281.1620 18099026538 1502.2290 93 | 22 1339.4990 18082520936 1501.3883 94 | 23 857.4780 16505602 0.8408 95 | 24 5411.5130 2440239019 270.5996 96 | 25 1292.0270 1566379186 196.9856 97 | 98 | Example 2: 获取指数ETF及其相关数据 99 | obj = gg.MarketData() # 创建市场数据类对象 100 | data = obj.indexETF() # 获取指数ETF及其相关数据 101 | 102 | 获取结果: 103 | obj.output() # 输出结果 104 | 105 | 106 | fund_id fund_nm index_id creation_unit amount unit_total \ 107 | 0 159901 深100ETF 399330 20.0 101937.0 36.95 108 | 1 159902 中 小 板 399005 50.0 74693.0 18.28 109 | 2 159903 深成ETF 399001 300.0 44815.0 3.74 110 | 3 159905 深红利 399324 50.0 52744.0 7.64 111 | 4 159906 深成长 399346 100.0 14364.0 0.95 112 | 5 159907 中小300 399008 150.0 17660.0 1.79 113 | 6 159908 博时创业 399006 150.0 3623.0 0.40 114 | 7 159909 深TMT 399610 25.0 1552.0 0.52 115 | 8 159910 深F120 399702 100.0 48091.0 7.10 116 | 9 159911 民营ETF 399337 50.0 1307.0 0.39 117 | .. ... ... ... ... ... ... 118 | 110 512660 军工ETF 399967 100.0 111508.0 7.25 119 | 111 512680 军工基金 399967 60.0 35932.0 2.29 120 | 112 512700 银行基金 399986 20.0 11058.0 1.11 121 | 113 512800 银行ETF 399986 30.0 57254.0 5.55 122 | 114 512810 军工行业 399967 100.0 8353.0 0.51 123 | 115 512880 证券ETF 399975 100.0 389729.0 30.94 124 | 116 512900 证券基金 399975 30.0 60671.0 4.64 125 | 117 512980 传媒ETF 399971 100.0 110272.0 7.29 126 | 118 513600 恒指ETF NaN 100.0 2883.0 0.72 127 | 119 513660 恒生通 NaN 100.0 33500.0 8.40 128 | 129 | unit_incr price volume increase_rt estimate_value discount_rt \ 130 | 0 0.33 3.625 18697.36 2.57 3.6215 0.10 131 | 1 -0.05 2.448 6731.85 3.51 2.4471 0.04 132 | 2 0.00 0.835 77.66 1.83 0.8382 -0.37 133 | 3 0.09 1.448 594.53 1.69 1.4477 0.03 134 | 4 0.00 0.663 9.30 2.31 0.6582 0.30 135 | 5 0.00 1.012 55.09 2.43 1.0128 -0.05 136 | 6 -0.01 1.097 28.92 3.49 1.0976 0.02 137 | 7 0.00 3.375 3.50 1.75 3.4384 -1.79 138 | 8 0.19 1.476 128.26 1.72 1.4767 -0.04 139 | 9 0.00 2.970 4.67 2.73 2.9834 -0.42 140 | .. ... ... ... ... ... ... 141 | 110 0.08 0.650 2920.85 2.20 0.6495 0.08 142 | 111 0.01 0.637 20.94 2.25 0.6388 -0.25 143 | 112 0.00 1.000 69.16 0.10 1.0029 -0.29 144 | 113 -0.10 0.969 2861.82 0.21 0.9688 0.02 145 | 114 0.00 0.614 34.02 2.16 0.6154 -0.18 146 | 115 0.54 0.794 21511.10 2.72 0.7940 0.00 147 | 116 0.00 0.765 391.30 2.82 0.7654 -0.07 148 | 117 0.06 0.661 1043.38 2.64 0.6591 0.20 149 | 118 0.00 2.507 49.85 -0.36 2.5237 -0.81 150 | 119 0.00 2.507 99.09 0.68 2.4765 1.23 151 | 152 | fund_nav nav_dt index_nm index_increase_rt pe pb 153 | 0 3.6212 2019-02-01 深证100 0.0 16.665 2.368 154 | 1 2.4470 2019-02-01 中小板指 0.0 20.140 2.814 155 | 2 0.8381 2019-02-01 深证成指 0.0 17.235 2.121 156 | 3 1.4475 2019-02-01 深证红利 0.0 13.370 2.079 157 | 4 0.6610 2019-02-01 深证成长 0.0 18.516 2.893 158 | 5 1.0125 2019-02-01 中小300 0.0 21.089 2.451 159 | 6 1.0968 2019-02-01 创业板指 0.0 28.853 3.598 160 | 7 3.4364 2019-02-01 TMT50 0.0 27.880 2.940 161 | 8 1.4766 2019-02-01 深证F120 0.0 13.895 1.844 162 | 9 2.9825 2019-02-01 深证民营 0.0 21.518 2.945 163 | .. ... ... ... ... ... ... 164 | 110 0.6495 2019-02-01 中证军工 0.0 47.329 2.166 165 | 111 0.6386 2019-02-01 中证军工 0.0 47.329 2.166 166 | 112 1.0029 2019-02-01 中证银行 0.0 6.588 0.873 167 | 113 0.9688 2019-02-01 中证银行 0.0 6.588 0.873 168 | 114 0.6151 2019-02-01 中证军工 0.0 47.329 2.166 169 | 115 0.7940 2019-02-01 证券公司 0.0 25.216 1.341 170 | 116 0.7655 2019-02-01 证券公司 0.0 25.216 1.341 171 | 117 0.6597 2019-02-01 中证传媒 0.0 19.697 2.362 172 | 118 2.5274 2019-02-01 恒生指数 0.0 NaN NaN 173 | 119 2.4907 2019-01-30 恒生指数 0.0 NaN NaN 174 | 175 | Example 3: 获取股票交易历史数据 176 | obj = gg.StockData('600000') # 创建股票交易类对象 177 | data = obj.history(start='2019-01-02', end='2019-01-31') # 获取浦发银行2019年1月份的历史数据 178 | 179 | 获取结果: 180 | obj.output() # 输出结果 181 | 182 | 183 | date open close high low volume code 184 | 0 2019-01-02 9.74 9.70 9.79 9.58 237628.0 600000 185 | 1 2019-01-03 9.70 9.81 9.82 9.66 186542.0 600000 186 | 2 2019-01-04 9.73 9.96 10.00 9.70 271728.0 600000 187 | 3 2019-01-07 10.09 9.98 10.09 9.92 235973.0 600000 188 | 4 2019-01-08 10.03 9.96 10.03 9.91 151049.0 600000 189 | 5 2019-01-09 10.06 9.99 10.16 9.98 231637.0 600000 190 | 6 2019-01-10 9.94 9.96 10.02 9.92 159235.0 600000 191 | 7 2019-01-11 10.03 10.05 10.15 9.96 190690.0 600000 192 | 8 2019-01-14 10.03 10.06 10.10 10.01 203528.0 600000 193 | 9 2019-01-15 10.10 10.11 10.15 10.05 156321.0 600000 194 | 10 2019-01-16 10.10 10.13 10.15 10.07 129883.0 600000 195 | 11 2019-01-17 10.17 10.17 10.28 10.07 227660.0 600000 196 | 12 2019-01-18 10.29 10.43 10.44 10.22 290871.0 600000 197 | 13 2019-01-21 10.36 10.35 10.46 10.30 189477.0 600000 198 | 14 2019-01-22 10.38 10.25 10.38 10.23 162505.0 600000 199 | 15 2019-01-23 10.29 10.27 10.34 10.25 165203.0 600000 200 | 16 2019-01-24 10.34 10.38 10.39 10.27 150066.0 600000 201 | 17 2019-01-25 10.45 10.50 10.56 10.38 196965.0 600000 202 | 18 2019-01-28 10.53 10.42 10.66 10.39 217690.0 600000 203 | 19 2019-01-29 10.39 10.58 10.63 10.38 237680.0 600000 204 | 20 2019-01-30 10.59 10.50 10.64 10.47 160542.0 600000 205 | 21 2019-01-31 10.59 10.73 10.73 10.51 251310.0 600000 206 | 207 | 208 | Example 4: 获取三大需求对GDP贡献数据 209 | obj = gg.Macro() # 创建宏观经济数据类对象 210 | data = obj.demandsToGdp() # 获取获取三大需求对GDP贡献数据 211 | 212 | 获取结果: 213 | obj.output() # 输出结果 214 | 215 | 216 | year cons_to cons_rate asset_to asset_rate goods_to goods_rate 217 | 0 2017 58.8 4.1 32.1 2.2 9.1 0.6 218 | 1 2016 66.5 4.5 43.1 2.9 -9.6 -0.7 219 | 2 2015 59.7 4.1 41.6 2.9 -1.3 -0.1 220 | 3 2014 48.8 3.6 46.9 3.4 4.3 0.3 221 | 4 2013 48.2 3.6 54.2 4.3 -2.3 -0.1 222 | 5 2012 56.7 4.3 42.0 3.4 1.7 0.2 223 | 6 2011 61.9 5.9 46.2 4.4 -8.1 -0.8 224 | 7 2010 46.9 4.8 66.0 7.1 -11.2 -1.3 225 | 8 2009 56.1 5.3 86.5 8.1 -42.6 -4.0 226 | 9 2008 44.2 4.3 53.2 5.1 2.6 0.3 227 | 10 2007 45.3 6.4 44.1 6.3 10.6 1.5 228 | 11 2006 42.0 5.3 42.9 5.5 15.1 1.9 229 | 12 2005 54.4 6.2 33.1 3.8 12.5 1.4 230 | 13 2004 42.6 4.3 61.6 6.2 -4.2 -0.4 231 | 14 2003 35.4 3.6 70.0 7.0 -5.4 -0.6 232 | 15 2002 43.9 5.1 48.5 3.6 4.6 0.4 233 | 16 2001 49.0 4.1 64.0 5.3 -13.0 -1.1 234 | 17 2000 78.1 6.6 22.4 1.9 -0.5 NaN 235 | 18 1999 74.7 5.7 23.7 1.8 1.6 0.1 236 | 19 1998 57.1 4.4 26.4 2.1 16.5 1.3 237 | 20 1997 37.0 3.4 18.6 1.7 44.4 4.2 238 | 21 1996 60.1 6.0 34.3 3.4 5.6 0.6 239 | 22 1995 44.7 4.9 55.0 6.0 0.3 NaN 240 | 23 1994 30.2 4.0 43.8 5.7 26.0 3.4 241 | 24 1993 59.5 8.3 78.6 11.0 -38.1 -5.3 242 | 25 1992 72.5 10.3 34.2 4.9 -6.8 -1.0 243 | 26 1991 65.1 6.0 24.3 2.2 10.6 1.0 244 | 27 1990 47.8 1.8 1.8 0.1 50.4 1.9 245 | 28 1989 39.6 1.6 16.4 0.7 44.0 1.8 246 | 29 1988 49.6 5.6 39.4 4.5 11.0 1.2 247 | 30 1987 50.3 5.8 23.5 2.7 26.2 3.1 248 | 31 1986 45.0 4.0 23.2 2.0 31.8 2.8 249 | 32 1985 85.5 11.5 80.9 10.9 -66.4 -8.9 250 | 33 1984 69.3 10.5 40.5 6.2 -9.8 -1.5 251 | 34 1983 74.1 8.1 40.4 4.4 -14.5 -1.6 252 | 35 1982 64.7 5.9 23.8 2.2 11.5 1.0 253 | 36 1981 93.4 4.9 -4.3 -0.2 10.9 0.5 254 | 37 1980 71.8 5.6 26.4 2.1 1.8 0.1 255 | 38 1979 87.3 6.6 15.4 1.2 -2.7 -0.2 256 | 39 1978 39.4 4.6 66.0 7.7 -5.4 -0.6 257 | 258 | Example 5: 获取可转债及其相关数据 259 | obj = gg.LowRiskIntArb() # 创建低风险及套利类对象 260 | data = obj.conBonds() # 获取可转债及其相关数据 261 | 262 | 获取结果: 263 | obj.output() # 输出结果 264 | 265 | 266 | bond_id bond_nm stock_id stock_nm market convert_price convert_dt \ 267 | 0 127008 特发转债 sz000070 特发信息 sz 6.78 2019-05-22 268 | 1 113506 鼎信转债 sh603421 鼎信通讯 sh 21.65 2018-10-20 269 | 2 113507 天马转债 sh603668 天马科技 sh 7.37 2018-10-23 270 | 3 113520 百合转债 sh603313 梦百合 sh 19.03 2019-05-14 271 | 4 128041 盛路转债 sz002446 盛路通信 sz 6.88 2019-01-23 272 | 5 110032 三一转债 sh600031 三一重工 sh 7.25 2016-07-04 273 | 6 127009 冰轮转债 sz000811 冰轮环境 sz 5.52 2019-07-18 274 | 7 128052 凯龙转债 sz002783 凯龙股份 sz 6.97 2019-06-27 275 | 8 123018 溢利转债 sz300381 溢多利 sz 8.41 2019-06-26 276 | 9 123012 万顺转债 sz300057 万顺股份 sz 6.47 2019-01-26 277 | .. ... ... ... ... ... ... ... 278 | 118 128018 时达转债 sz002527 新时达 sz 11.83 2018-05-10 279 | 119 128028 赣锋转债 sz002460 赣锋锂业 sz 42.58 2018-06-27 280 | 120 128023 亚太转债 sz002284 亚太股份 sz 10.34 2018-06-08 281 | 121 132008 17山高EB sh600350 山东高速 sh 9.62 2018-04-26 282 | 122 127004 模塑转债 sz000700 模塑科技 sz 7.72 2017-12-08 283 | 123 128010 顺昌转债 sz002245 澳洋顺昌 sz 9.28 2016-07-29 284 | 124 123003 蓝思转债 sz300433 蓝思科技 sz 16.08 2018-06-14 285 | 125 132011 17浙报EB sh600633 浙数文化 sh 24.39 2018-08-17 286 | 126 128012 辉丰转债 sz002496 ST辉丰 sz 7.71 2016-10-28 287 | 127 128013 洪涛转债 sz002325 洪涛股份 sz 9.98 2017-02-06 288 | 289 | Example 6: 股票LOF基金及基相关数据 290 | obj = gg.LowRiskIntArb() # 创建低风险及套利类对象 291 | data = obj.stockLof() # 获取股票LOF基金及基相关数据 292 | 293 | 获取结果: 294 | obj.output() # 输出结果 295 | 296 | 297 | fund_id fund_nm price increase_rt volume amount fund_nav nav_dt \ 298 | 0 160105 南方积配 0.812 1.37 6.73 2175.0 0.8155 2019-02-01 299 | 1 160106 南方高增 0.847 2.05 13.22 6851.0 0.8666 2019-02-01 300 | 2 160133 南方天元 1.956 0.98 28.82 22162.0 1.9830 2019-02-01 301 | 3 160211 国泰小盘 1.647 1.92 41.34 5115.0 1.6620 2019-02-01 302 | 4 160212 国泰估值 1.658 2.35 64.51 8375.0 1.6750 2019-02-01 303 | 5 160215 国泰价值 1.192 1.27 10.85 2205.0 1.1990 2019-02-01 304 | 6 160220 国泰民益 1.132 1.71 3.83 3223.0 1.1382 2019-02-01 305 | 7 160311 华夏蓝筹 1.297 0.93 27.65 10515.0 1.3040 2019-02-01 306 | 8 160314 华夏行业 0.842 1.45 12.76 13094.0 0.8490 2019-02-01 307 | 9 160323 华夏磐泰 1.009 0.00 0.00 145.0 0.9721 2019-02-01 308 | .. ... ... ... ... ... ... ... ... 309 | 68 168401 红土定增 0.966 0.00 1.04 1010.0 1.0139 2019-02-01 310 | 69 169101 东证睿丰 1.252 1.54 356.30 39830.0 1.2600 2019-02-01 311 | 70 169201 浙商鼎盈 0.971 0.00 0.00 771.0 0.9783 2019-02-01 312 | 71 501000 国金鑫新 0.961 4.34 2.09 82.0 0.9460 2019-02-01 313 | 72 501001 财通精选 0.953 -2.46 10.37 5423.0 0.9590 2019-02-01 314 | 73 501015 财通升级 0.757 0.40 12.29 36579.0 0.7650 2019-02-01 315 | 74 501017 国泰融丰 0.783 0.00 0.00 5178.0 0.7768 2019-02-01 316 | 75 501022 银华鑫盛 0.779 0.39 3.33 12266.0 0.7980 2019-02-01 317 | 76 501027 国泰融信 0.989 0.00 0.00 311.0 1.0055 2019-02-01 318 | 77 501035 创金睿选 0.938 0.32 2.14 877.0 0.9627 2019-02-01 319 | 320 | 更多文档: 321 | 322 | 请访问: http://www.infodata.cc/ 323 | 324 | Change Log: 325 | 326 | 0.1.8 2020/04/20 327 | (1)Classify类中方法byIndustry去掉按申万行业(sw)获取行业分类数据,保留只使用按新浪行业(sina)获取行业分类数据 328 | (2)LowRiskIntArb类中去掉获取A/H比价数据方法AHRatio 329 | (3)Macro类中将montySupply方法更名为moneySupply;去掉获取贷款基础利率均值数据方法lprMa 330 | (4)StockInfo类中方法stockProfiles经过重新修订可以高效获取上市公司基于基本面的汇总数据信息 331 | (5)修复了N多因网页数据格式更改造成的某些api失效或错误的问题 332 | (6)修复部分bugs 333 | 334 | 0.1.7 2019/04/09 335 | (1)修改pandas.DataFrame的某些用法以适应pandas更高版本 336 | (2)修复部分bug 337 | 338 | 0.1.6 2019/04/07 339 | (1)公用类中添加fceil、nextTradeDate,修改getTodayHistory为getDateInterval,修复lastTradeDate中的bug 340 | (2)低风险及套利类中添加新转债方法newConBonds 341 | (3)修复股票交易类中history方法的一个bug 342 | (4)将文档文件夹修改为documents并将所有文档格式修改为markdown格式 343 | 344 | 0.1.5 2019/03/20 345 | 完善文档,修复bugs 346 | 347 | 0.1.0 2019/02/03 348 | 创建第一个版本 349 | 350 | 351 | -------------------------------------------------------------------------------- /documents/公用类.md: -------------------------------------------------------------------------------- 1 | 公用类Utility中封装了很多针对本项目的很实用的静态方法,用户也可以在您的项目中直接调用,省去了重复编写的麻烦。 2 | 3 | [b](1) str2Dict(string)[/b] 4 | 该方法用于将以字符串类型表示的字典形式直接转化为字典类型 5 | 6 | 参数: 7 | string: str, 以字符串类型表示的字典形式 8 | 9 | 返回值: 10 | dict 11 | 12 | 示例: 13 | [i]import gugu as gg[/i] 14 | [i]result = gg.Utility.str2Dict("{'a':1,'b':'ok'}")[/i] 15 | 16 | [b](2) fceil(number, ndigits=2)[/b] 17 | 该方法用于浮点数尾数向上取整,由于python标准库math只提供了ceil方法用于浮点数向上取整为整数而未提供用于浮点数尾数向上取整的方法,针对可转债等会使用到浮点数尾数向上取整的情况本项目特提供了fceil方法 18 | 19 | 参数: 20 | number: float, 用于尾数向上取整的浮点数 21 | ndigits: int, 向上取整的尾数个数,默认为2 22 | 23 | 返回值: 24 | float 25 | 26 | 示例: 27 | [i]import gugu as gg[/i] 28 | [i]result = gg.Utility.fceil(3.141)[/i] # 结果为3.15(如果参数为-3.141结果为-3.14) 29 | 30 | [b](3) checkQuarter(year, quarter)[/b] 31 | 该方法用于检测季度的合法性 32 | 33 | 参数: 34 | year: int,年份,> 1989 35 | quarter, int, 季度 36 | 37 | 返回值: 38 | True or Exception 39 | 40 | 示例: 41 | [i]import gugu as gg[/i] 42 | [i]result = gg.Utility.checkQuarter(2019, 1)[/i] 43 | 44 | [b](4) symbol(code)[/b] 45 | 该方法用于将缩略形式的股票及指数代码转化为全称形式的代码 46 | 47 | 参数: 48 | code: str,缩略形式的股票及指数代码其中指数代码支持'sh', 'sz', 'hs300', 'sz50', 'cyb', 'zxb', 'zx300', 'zh500' 49 | 50 | 返回值: 51 | str: 全称形式的股票代码及指数代码,如:sz399300,sh600000 52 | 53 | 示例: 54 | [i]import gugu as gg[/i] 55 | [i]result = gg.Utility.symbol('600000')[/i] 56 | 57 | [b](5) random(n=13)[/b] 58 | 该方法用于生成字符串类型的随机数,默认生成13位随机数 59 | 60 | 参数: 61 | n: int,随机数的位数 62 | 63 | 返回值: 64 | str:n位字符串随机数 65 | 66 | 示例: 67 | [i]import gugu as gg[/i] 68 | [i]result = gg.Utility.random()[/i] 69 | 70 | [b](6) getToday()[/b] 71 | 该方法用于获取字符串类型的当前日期 72 | 73 | 返回值: 74 | str:字符串类型的当前日期,如'2019-03-17' 75 | 76 | 示例: 77 | [i]import gugu as gg[/i] 78 | [i]result = gg.Utility.getToday()[/i] 79 | 80 | [b](7) getHour()[/b] 81 | 该方法用于获取int类型的当前小时 82 | 83 | 返回值: 84 | int:int类型的当前小时,如11 85 | 86 | 示例: 87 | [i]import gugu as gg[/i] 88 | [i]result = gg.Utility.getHour()[/i] 89 | 90 | [b](8) getMonth()[/b] 91 | 该方法用于获取int类型的当前月份 92 | 93 | 返回值: 94 | int:int类型的当前月份,如3 95 | 96 | 示例: 97 | [i]import gugu as gg[/i] 98 | [i]result = gg.Utility.getMonth()[/i] 99 | 100 | [b](9) getYear()[/b] 101 | 该方法用于获取int类型的当前年份 102 | 103 | 返回值: 104 | int:int类型的当前年份,如2019 105 | 106 | 示例: 107 | [i]import gugu as gg[/i] 108 | [i]result = gg.Utility.getYear()[/i] 109 | 110 | [b](10) getTodayLastYear()[/b] 111 | 该方法用于获取字符串类型的去年今日 112 | 113 | 返回值: 114 | str:字符串类型的去年今日,如'2018-03-17' 115 | 116 | 示例: 117 | [i]import gugu as gg[/i] 118 | [i]result = gg.Utility.getTodayLastYear()[/i] 119 | 120 | [b](11) getTodayLastMonth()[/b] 121 | 该方法用于获取字符串类型的上月今日,由于该方法通过调用Utility.getDateInterval(-30)获取,因此是30天后的今日,既近似上月今日 122 | 123 | 返回值: 124 | str:字符串类型的上月今日,如'2019-02-15' 125 | 126 | 示例: 127 | [i]import gugu as gg[/i] 128 | [i]result = gg.Utility.getTodayLastMonth()[/i] 129 | 130 | [b](12) getTodayLastWeek()[/b] 131 | 该方法用于获取字符串类型的上周今日 132 | 133 | 返回值: 134 | str:字符串类型的上周今日,如'2019-03-10' 135 | 136 | 示例: 137 | [i]import gugu as gg[/i] 138 | [i]result = gg.Utility.getTodayLastWeek()[/i] 139 | 140 | [b](13) getDateInterval(date, days=-1)[/b] 141 | 该方法用于获取datetime.date类型的距参数date为days天的日期 142 | 143 | 参数: 144 | date: str,日期,format:YYYY-MM-DD,默认为当前日期 145 | days:int,天数,可正可负 146 | 147 | 返回值: 148 | datetime.date类型的距date为days天的日期 149 | 150 | 示例: 151 | [i]import gugu as gg[/i] 152 | [i]result = gg.Utility.getDateInterval(days=-100)[/i] 153 | 154 | [b](14) diffDays(start=None, end=None)[/b] 155 | 该方法用于获取两个日期间的间隔天数 156 | 157 | 参数: 158 | start:str,开始日期,形式'YYYY-MM-DD' 159 | end:str,结束日期,形式'YYYY-MM-DD' 160 | 161 | 返回值: 162 | int类型的间隔天数 163 | 164 | 示例: 165 | [i]import gugu as gg[/i] 166 | [i]result = gg.Utility.diffDays('2019-06-25', '2019-03-17')[/i] 167 | 168 | [b](15) lastTradeDate(date)[/b] 169 | 该方法用于获取上一个str类型的交易日期 170 | 171 | 参数: 172 | date: str,format:YYYY-MM-DD 为空时取当前日期 173 | 174 | 返回值: 175 | str类型的上一个交易日期 176 | 177 | 示例: 178 | [i]import gugu as gg[/i] 179 | [i]result = gg.Utility.lastTradeDate()[/i] 180 | 181 | [b](16) nextTradeDate(date)[/b] 182 | 该方法用于获取下一个str类型的交易日期 183 | 184 | 参数: 185 | date: str,format:YYYY-MM-DD 为空时取当前日期 186 | 187 | 返回值: 188 | str类型的下一个交易日期 189 | 190 | [b](17) isHoliday(date)[/b] 191 | 该方法用于节假日判断 192 | 193 | 参数: 194 | date: str,查询日期 format:YYYY-MM-DD 为空时取当前日期 195 | 196 | 返回值: 197 | True or False 198 | 199 | 示例: 200 | [i]import gugu as gg[/i] 201 | [i]result = gg.Utility.isHoliday()[/i] 202 | 203 | [b](18) isTradeDay(date)[/b] 204 | 该方法用于交易日判断 205 | 206 | 参数: 207 | date: str,查询日期 format:YYYY-MM-DD 为空时取当前日期 208 | 209 | 返回值: 210 | True or False 211 | 212 | 示例: 213 | [i]import gugu as gg[/i] 214 | [i]result = gg.Utility.isTradeDay()[/i] -------------------------------------------------------------------------------- /documents/前言.md: -------------------------------------------------------------------------------- 1 | gugu(股估)是适用于金融/投资领域数据分析及量化工程的开源项目,通过对互联网上的公开数据进行采集、清洗和存储,完成了对股票/债券/基金等金融数据的统一调用和分析。其优点是速度快、可定制及高度的可复用性。您不仅可以将其作为单独的数据接口使用,还可以将其集成在您的项目中作为数据获取api进行调用。 2 | 3 | [b]使用前提:[/b] 4 | python 2.x/3.x 5 | 6 | [b]安装方式:[/b] 7 | 方式1: [i]pip install gugu[/i] or [i]pip3 install gugu[/i] 8 | 方式2: [i]python setup.py install[/i] 9 | 方式3:访问 [url]https://pypi.python.org/pypi/gugu/[/url] 下载安装 10 | 11 | [b]版本升级:[/b] 12 | [i]pip install gugu --upgrade[/i] or [i]pip3 install gugu --upgrade[/i] 13 | 查看当前版本的方法: 14 | [i]import gugu[/i] 15 | [i]print(gugu.__version__)[/i] 16 | 17 | [b]版本信息:[/b] 18 | 0.1.8 2020/04/20 19 | (1)Classify类中方法byIndustry去掉按申万行业(sw)获取行业分类数据,保留只使用按新浪行业(sina)获取行业分类数据 20 | (2)LowRiskIntArb类中去掉获取A/H比价数据方法AHRatio 21 | (3)Macro类中将montySupply方法更名为moneySupply;去掉获取贷款基础利率均值数据方法lprMa 22 | (4)StockInfo类中方法stockProfiles经过重新修订可以高效获取上市公司基于基本面的汇总数据信息 23 | (5)修复了N多因网页数据格式更改造成的某些api失效或错误的问题 24 | (6)修复部分bugs 25 | 26 | 0.1.7 2019/04/09 27 | (1)修改pandas.DataFrame的某些用法以适应pandas更高版本 28 | (2)修复部分bugs 29 | 30 | 0.1.6 2019/04/07 31 | (1)公用类中添加fceil、nextTradeDate,修改getTodayHistory为getDateInterval,修复lastTradeDate中的bugs 32 | (2)低风险及套利类中添加新转债方法newConBonds 33 | (3)修复股票交易类中history方法的一个bug 34 | (4)将文档文件夹修改为documents并将所有文档格式修改为markdown格式 35 | 36 | 0.1.5 2019/03/20 37 | 完善文档,修复bugs 38 | 39 | 0.1.0 2019/02/03 40 | 创建第一个版本 -------------------------------------------------------------------------------- /documents/基础类.md: -------------------------------------------------------------------------------- 1 | Base是gugu的基础类,gugu中的其他类都继承自Base,Base对gugu中其他类的通用操作进行了统一封装。 2 | 3 | [b](1) __init__(pandas=True, inter=True)[/b] 4 | 初始化方法中有两个重要变量: 5 | [list=1] 6 | [*]pandas:控制获取的数据是否使用pandas.DataFrame格式,默认使用,如果您需要对获取的数据进行迭代处理,将pandas设为False返回list格式会更方便处理一些,或者您对pandas还不太熟悉也可以将pandas设为False返回list格式 7 | [*]inter:控制交互模式,当inter为True时表示系统使用交互模式,在数据的获取过程中系统将会在控制台进行交互信息提示,如果您是将gugu集成在您的项目中作为api调用,将inter设为False将会获取更高的性能。 8 | [/list] 9 | 另外在使用过程中您也可以通过setPandas及setInter改变pandas及inter的值,还可以通过getPandas及getInter获取当前的pandas及inter的值 10 | 示例: 11 | [i]import gugu as gg[/i] 12 | [i]obj = gg.MarketData(pandas=False, inter=False)[/i] # 创建市场数据对象(使用list数据格式;不使用交互模式) 13 | 14 | [b](2) output(full=False)[/b] 15 | output方法将对获取的数据格式化(pandas.DataFrame)输出,full为True时将对所有的数据进行输出,为False时将只对部分数据进行简化输出。 16 | 示例: 17 | [i]import gugu as gg[/i] 18 | [i]obj = gg.MarketData()[/i] # 创建市场数据对象 19 | [i]data = obj.index()[/i] # 获取大盘指数行情 20 | [i]obj.output()[/i] # 输出数据 21 | 22 | [b](3) setPandas(pandas=False)[/b] 23 | setPandas方法设置pandas的值,参数pandas为True时获取的数据将使用pandas.DataFrame格式,为False时将使用list格式 24 | 示例: 25 | [i]import gugu as gg[/i] 26 | [i]obj = gg.MarketData()[/i] # 创建市场数据对象 27 | [i]obj.setPandas(pandas=False)[/i] # 设置pandas的值为False,使用list格式返回数据 28 | 29 | [b](4) getPandas()[/b] 30 | getPandas方法获取当前pandas的值 31 | 示例: 32 | [i]import gugu as gg[/i] 33 | [i]obj = gg.MarketData()[/i] # 创建市场数据对象 34 | [i]pandas = obj.getPandas()[/i] # 获取当前pandas值 35 | 36 | [b](5) setInter(inter=False)[/b] 37 | setInter方法设置inter的值,参数inter为True时将使用交互模式,为False时将不使用交互模式 38 | 39 | [b](6) getInter()[/b] 40 | getInter方法获取当前inter的值 -------------------------------------------------------------------------------- /documents/市场类.md: -------------------------------------------------------------------------------- 1 | 市场类MarketData共有三个方法: 2 | 3 | [b](1) index()[/b] 4 | index方法用于获取指数行情 5 | 6 | 返回值: 7 | DataFrame or list: [{'code':, 'name':, ...}, ...] 8 | code:指数代码 9 | name:指数名称 10 | change:涨跌幅 11 | open:开盘价 12 | preclose:昨日收盘价 13 | close:收盘价 14 | high:最高价 15 | low:最低价 16 | volume:成交量(手) 17 | amount:成交金额(亿元) 18 | 19 | 示例: 20 | [i]import gugu as gg[/i] 21 | [i]obj = gg.MarketData()[/i] # 创建市场类对象 22 | [i]data = obj.index()[/i] # 获取指数行情数据 23 | [i]obj.output()[/i] # 输出数据 24 | 25 | 输出: 26 | code name change open preclose close high low volume amount 27 | 0 sh000001 上证指数 0.11 3019.8797 3027.5755 3031.0048 3042.4871 3009.4476 243734939 2227.8733 28 | 1 sh000002 A股指数 0.11 3162.8278 3170.8995 3174.4900 3186.5248 3151.8888 243365127 2225.9919 29 | 2 sh000003 B股指数 0.13 304.5219 304.8948 305.2768 306.2762 303.6536 369812 1.8815 30 | 3 sh000008 综合指数 -0.24 2949.1338 2960.3582 2953.1629 2970.4784 2934.6272 62076177 588.5905 31 | 4 sh000009 上证380 0.67 4688.5118 4697.3185 4728.6132 4754.9673 4671.2098 55280753 508.1371 32 | 5 sh000010 上证180 -0.28 8403.2207 8427.6242 8404.3915 8438.8410 8358.8803 84596609 928.1424 33 | 6 sh000011 基金指数 0.12 6146.6060 6151.6294 6159.0273 6172.9559 6137.3518 45091261 174.6164 34 | 7 sh000012 国债指数 0.00 171.4527 171.4363 171.4412 171.4534 171.4278 135727 1.3276 35 | 8 sh000016 上证50 -0.31 2829.4997 2832.5878 2823.6957 2835.1060 2807.1226 35590064 444.8847 36 | 9 sh000017 新综指 0.11 2550.1951 2556.7031 2559.5979 2569.3006 2541.3731 243333265 2225.5334 37 | 10 sh000300 沪深300 -0.02 3785.3762 3794.1040 3793.3331 3811.4943 3770.7858 137578330 1538.0269 38 | 11 sh000905 中证500 0.85 5146.6204 5158.2467 5202.0871 5233.5263 5125.1540 120740091 967.0635 39 | 12 sz399001 深证成指 0.81 9366.7670 9384.4150 9460.8470 9524.7790 9328.0030 32209955394 2914.1930 40 | 13 sz399002 深成指R 0.81 11330.4120 11351.7610 11444.2150 11521.5510 11283.5220 15342782531 1551.2523 41 | 14 sz399003 成份B指 -0.03 5288.2500 5293.6520 5291.8950 5313.1850 5272.8290 8832150 0.3655 42 | 15 sz399004 深证100R 0.78 5049.9780 5055.1630 5094.8000 5131.5770 5027.1750 5453354045 701.3329 43 | 16 sz399005 中小板指 0.71 6061.0040 6074.0170 6117.0050 6157.5040 6035.4790 2740936133 373.7514 44 | 17 sz399006 创业板指 1.23 1615.5920 1619.7100 1639.6660 1654.6610 1613.4980 2419312755 340.4623 45 | 18 sz399008 中小300 0.83 1180.4590 1182.9540 1192.7500 1200.0490 1175.0020 6953896371 691.5542 46 | 19 sz399100 新 指 数 0.80 7259.6160 7274.9890 7332.9620 7375.0700 7230.3520 31846356288 2897.9186 47 | 20 sz399101 中小板综 0.81 9169.9060 9188.8650 9263.5180 9312.0010 9131.4780 12865391710 1168.3904 48 | 21 sz399106 深证综指 0.75 1595.6830 1599.4820 1611.4410 1620.9770 1589.4830 32209955394 2914.1930 49 | 22 sz399107 深证A指 0.75 1668.6470 1672.6300 1685.1740 1695.1650 1662.1620 32188298739 2913.4094 50 | 23 sz399108 深证B指 0.01 1008.3040 1008.4600 1008.5910 1012.0100 1003.8220 21656655 0.7836 51 | 24 sz399333 中小板R 0.71 6739.8950 6754.3640 6802.1680 6847.2030 6711.5090 2740936133 373.7514 52 | 25 sz399606 创业板R 1.23 1692.4080 1696.7210 1717.6260 1733.3340 1690.2130 2419312755 340.4623 53 | 54 | [b](2) latest()[/b] 55 | latest方法用于一次性获取最近一个日交易日所有股票的交易数据 56 | 57 | 返回值: 58 | DataFrame or list: [{'code':, 'name':, ...}, ...] 59 | code:代码 60 | name:名称 61 | changepercent:涨跌幅 62 | trade:现价 63 | open:开盘价 64 | high:最高价 65 | low:最低价 66 | settlement:昨日收盘价 67 | volume:成交量 68 | turnoverratio:换手率 69 | amount:成交量 70 | per:市盈率 71 | pb:市净率 72 | mktcap:总市值 73 | nmc:流通市值 74 | 75 | 示例: 76 | [i]import gugu as gg[/i] 77 | [i]obj = gg.MarketData()[/i] # 创建市场类对象 78 | [i]data = obj.latest()[/i] # 获取最近一个日交易日所有股票的交易数据 79 | [i]obj.output()[/i] # 输出数据 80 | 81 | 输出: 82 | code name changepercent ... pb mktcap nmc 83 | 0 603999 读者传媒 1.176 ... 2.042 3.467520e+05 3.467520e+05 84 | 1 603998 方盛制药 0.664 ... 2.494 2.645934e+05 2.576692e+05 85 | 2 603997 继峰股份 0.116 ... 3.026 5.498954e+05 5.418000e+05 86 | 3 603996 中新科技 9.961 ... 2.173 3.412705e+05 3.412705e+05 87 | 4 603993 洛阳钼业 -0.414 ... 2.517 1.038923e+07 8.497237e+06 88 | 5 603991 至正股份 -0.147 ... 3.194 1.520514e+05 8.380099e+04 89 | 6 603990 麦迪科技 1.543 ... 8.108 3.502777e+05 2.618216e+05 90 | 7 603989 艾华集团 0.902 ... 4.560 8.291436e+05 8.291436e+05 91 | 8 603988 中电电机 2.358 ... 3.852 2.551920e+05 2.551920e+05 92 | 9 603987 康德莱 0.142 ... 2.394 3.104511e+05 1.834434e+05 93 | 10 603986 兆易创新 7.569 ... 15.497 2.961157e+06 2.157553e+06 94 | 11 603985 恒润股份 -0.180 ... 2.689 2.876640e+05 1.841050e+05 95 | 12 603980 吉华集团 0.071 ... 1.691 7.085000e+05 3.480621e+05 96 | 13 603979 金诚信 0.115 ... 1.264 5.063985e+05 5.063985e+05 97 | 14 603978 深圳新星 0.086 ... 2.708 3.705600e+05 1.689190e+05 98 | 15 603977 国泰集团 2.163 ... 2.540 3.141609e+05 1.412895e+05 99 | 16 603976 正川股份 0.165 ... 2.838 2.756376e+05 6.982079e+04 100 | 17 603970 中农立华 -0.418 ... 3.175 2.668801e+05 1.327729e+05 101 | 18 603969 银龙股份 0.577 ... 2.639 4.398430e+05 4.398430e+05 102 | 19 603968 醋化股份 -0.670 ... 2.167 3.030394e+05 3.030394e+05 103 | 20 603966 法兰泰克 1.761 ... 2.497 2.194188e+05 9.670867e+04 104 | 21 603963 大理药业 10.029 ... 4.258 1.953900e+05 1.147916e+05 105 | 22 603960 克来机电 -1.593 ... 8.958 4.174976e+05 1.599844e+05 106 | 23 603959 百利科技 -2.428 ... 6.750 7.056000e+05 3.351600e+05 107 | 24 603958 哈森股份 1.746 ... 1.771 1.921984e+05 5.768312e+04 108 | 25 603956 威派格 9.979 ... 8.112 6.806842e+05 6.806857e+04 109 | 26 603955 大千生态 0.000 ... 1.718 1.875198e+05 1.182489e+05 110 | 27 603939 益丰药房 -2.051 ... 5.741 2.015912e+06 1.940416e+06 111 | 28 603938 三孚股份 -0.100 ... 2.907 3.010840e+05 1.018271e+05 112 | 29 603937 丽岛新材 0.234 ... 2.080 2.686197e+05 9.755373e+04 113 | ... ... ... ... ... ... ... ... 114 | 3090 002037 久联发展 3.750 ... 1.525 4.856748e+05 2.282411e+05 115 | 3091 002036 联创电子 4.017 ... 3.372 6.846286e+05 6.836994e+05 116 | 3092 002035 华帝股份 2.584 ... 4.246 1.050013e+06 9.321280e+05 117 | 3093 002034 旺能环境 -2.426 ... 1.980 7.035784e+05 3.682709e+05 118 | 3094 002033 丽江旅游 -0.460 ... 1.458 3.566195e+05 3.566126e+05 119 | 3095 002032 苏 泊 尔 -1.014 ... 10.084 5.370935e+06 3.563812e+06 120 | 3096 002031 巨轮智能 2.439 ... 1.817 5.542477e+05 4.764352e+05 121 | 3097 002030 达安基因 1.920 ... 5.718 1.015904e+06 9.730952e+05 122 | 3098 002029 七 匹 狼 0.000 ... 0.965 5.403041e+05 5.401984e+05 123 | 3099 002028 思源电气 -1.244 ... 2.004 9.054093e+05 6.910151e+05 124 | 3100 002027 分众传媒 -0.870 ... 7.421 1.003967e+07 1.003967e+07 125 | 3101 002026 山东威达 2.366 ... 1.106 2.726439e+05 2.398177e+05 126 | 3102 002025 航天电器 1.408 ... 4.633 1.204632e+06 1.204348e+06 127 | 3103 002024 苏宁易购 0.158 ... 1.443 1.179582e+07 7.487019e+06 128 | 3104 002023 海特高新 2.981 ... 2.623 9.149603e+05 7.970701e+05 129 | 3105 002022 科华生物 0.995 ... 2.634 5.749902e+05 5.516217e+05 130 | 3106 002021 中捷资源 2.174 ... 2.356 2.262911e+05 2.261776e+05 131 | 3107 002020 京新药业 -0.299 ... 1.963 7.241471e+05 5.879053e+05 132 | 3108 002019 亿帆医药 4.915 ... 2.266 1.494235e+06 1.006796e+06 133 | 3109 002018 *ST华信 -0.800 ... 1.163 2.824506e+05 2.824506e+05 134 | 3110 002017 东信和平 10.006 ... 6.133 7.608123e+05 7.587921e+05 135 | 3111 002016 世荣兆业 0.276 ... 3.563 8.802960e+05 7.472880e+05 136 | 3112 002015 霞客环保 0.380 ... 3.985 2.115716e+05 2.115716e+05 137 | 3113 002014 永新股份 -0.545 ... 2.000 3.676402e+05 3.549148e+05 138 | 3114 002013 中航机电 0.522 ... 3.205 2.778648e+06 2.419778e+06 139 | 3115 002012 凯恩股份 3.505 ... 1.911 2.347480e+05 2.344216e+05 140 | 3116 002011 盾安环境 1.073 ... 1.172 5.182249e+05 4.752187e+05 141 | 3117 002010 传化智联 -2.061 ... 2.244 2.632314e+06 2.571583e+06 142 | 3118 002009 天奇股份 0.647 ... 1.923 4.035283e+05 3.224370e+05 143 | 3119 002008 大族激光 -1.905 ... 5.367 4.396317e+06 4.092784e+06 144 | 145 | [b](3) indexETF()[/b] 146 | indexETF用于获取指数ETF及其相关数据 147 | 148 | 返回值: 149 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 150 | fund_id: 基金代码 151 | fund_nm: 基金名称 152 | index_id: 跟踪指数代码 153 | creation_unit: 最小申赎单位(万份) 154 | amount: 份额(万份) 155 | unit_total: 规模(亿元) 156 | unit_incr: 规模变化(亿元) 157 | price: 现价 158 | volume: 成交额(万元) 159 | increase_rt: 涨幅(%) 160 | estimate_value: 估值 161 | discount_rt: 溢价率(%) 162 | fund_nav: 净值 163 | nav_dt: 净值日期 164 | index_nm: 指数名称 165 | index_increase_rt: 指数涨幅(%) 166 | pe: 市盈率 167 | 168 | 示例: 169 | [i]import gugu as gg[/i] 170 | [i]obj = gg.MarketData()[/i] # 创建市场类对象 171 | [i]data = obj.indexETF()[/i] # 获取指数ETF及其相关数据 172 | [i]obj.output()[/i] # 输出数据 173 | 174 | 输出: 175 | fund_id fund_nm index_id creation_unit amount unit_total unit_incr price volume increase_rt estimate_value discount_rt fund_nav nav_dt index_nm index_increase_rt pe pb 176 | 0 159901 深100ETF 399330 20.0 115497.0 51.41 0.55 4.451 16526.17 0.68 4.4535 -0.06 4.4190 2019-03-04 深证100 0.78 20.378 2.899 177 | 1 159902 中 小 板 399005 50.0 76993.0 22.96 -0.21 2.982 11313.47 0.78 2.9810 0.03 2.9600 2019-03-04 中小板指 0.71 24.534 3.422 178 | 2 159903 深成ETF 399001 300.0 44215.0 4.55 0.00 1.029 290.67 1.68 1.0321 -0.30 1.0238 2019-03-04 深证成指 0.81 21.127 2.593 179 | 3 159905 深红利 399324 50.0 56044.0 9.71 -0.03 1.733 1185.88 1.35 1.7281 0.28 1.7132 2019-03-04 深证红利 0.87 15.943 2.490 180 | 4 159906 深成长 399346 100.0 14664.0 1.24 0.00 0.847 77.33 0.71 0.8536 -0.77 0.8460 2019-03-04 深证成长 0.90 22.932 3.588 181 | 5 159907 中小300 399008 150.0 18560.0 2.30 0.00 1.237 72.69 1.14 1.2371 -0.01 1.2269 2019-03-04 中小300 0.83 25.739 2.992 182 | 6 159908 博时创业 399006 150.0 4623.0 0.65 0.02 1.410 135.68 1.29 1.4091 0.06 1.3920 2019-03-04 创业板指 1.23 36.482 4.553 183 | 7 159909 深TMT 399610 25.0 3127.0 1.43 0.02 4.564 363.79 0.37 4.5622 0.04 4.4904 2019-03-04 TMT50 1.60 36.469 3.846 184 | 8 159910 深F120 399702 100.0 51791.0 9.20 0.00 1.776 495.14 0.91 1.7767 -0.04 1.7598 2019-03-04 深证F120 0.96 16.759 2.227 185 | 9 159911 民营ETF 399337 50.0 1277.0 0.48 0.00 3.751 9.77 1.68 3.7266 0.66 3.6926 2019-03-04 深证民营 0.92 26.726 3.648 186 | 10 159912 深300ETF 399007 160.0 7675.0 0.95 0.00 1.232 55.39 0.24 1.2432 -0.90 1.2335 2019-03-04 深证300 0.79 20.478 2.699 187 | 11 159913 深价值 399348 100.0 4333.0 0.76 0.00 1.759 20.85 0.11 1.7874 -1.59 1.7690 2019-03-04 深证价值 1.04 13.343 1.971 188 | 12 159915 创业板 399006 200.0 1264145.0 198.60 -4.08 1.571 108061.70 0.96 1.5693 0.11 1.5502 2019-03-04 创业板指 1.23 36.482 4.553 189 | 13 159916 深F60 399701 50.0 10707.0 4.12 0.00 3.852 58.74 0.31 3.8909 -1.00 3.8520 2019-03-04 深证F60 1.01 16.254 2.264 190 | 14 159918 中创400 399624 100.0 9756.0 1.40 0.00 1.435 21.43 -0.28 1.4348 0.01 1.4196 2019-03-04 中创400 1.07 30.212 2.731 191 | 15 159919 300ETF 399300 90.0 578182.0 218.49 -0.92 3.779 28375.05 0.08 3.7842 -0.14 3.7850 2019-03-04 沪深300 -0.02 12.505 1.542 192 | 16 159920 恒生ETF NaN 200.0 316156.0 47.42 0.00 1.500 53100.03 -0.27 1.5046 -0.30 1.5063 2019-03-04 恒生指数 -0.03 NaN NaN 193 | 17 159922 500ETF 000905 45.0 31633.0 17.12 0.00 5.413 1700.32 1.05 5.4316 -0.34 5.3858 2019-03-04 中证500 0.85 20.269 1.925 194 | 18 159923 100ETF 000903 100.0 2529.0 0.42 0.00 1.650 77.01 0.12 1.6377 0.75 1.6410 2019-03-04 中证100 -0.20 11.600 1.462 195 | 19 159925 南方300 000300 200.0 92120.0 14.86 0.00 1.613 48.21 0.37 1.6212 -0.50 1.6215 2019-03-04 沪深300 -0.02 12.505 1.542 196 | 20 159928 消费ETF 399932 50.0 87666.0 20.53 0.05 2.342 943.37 0.00 2.3480 -0.25 2.3496 2019-03-04 中证消费 -0.07 29.880 5.496 197 | 21 159929 医药ETF 399933 50.0 14470.0 2.04 0.07 1.407 722.66 -0.64 1.4034 0.26 1.4028 2019-03-04 中证医药 0.04 28.686 3.692 198 | 22 159930 能源ETF 399928 50.0 3040.0 0.23 -0.07 0.746 33.13 -0.67 0.7509 -0.65 0.7549 2019-03-04 中证能源 -0.53 14.736 1.163 199 | 23 159931 金融ETF 399934 50.0 3183.0 0.54 -0.11 1.707 226.38 -1.10 1.7198 -0.75 1.7250 2019-03-04 中证金融 -0.30 9.274 1.187 200 | 24 159932 500深ETF 399802 100.0 2051.0 0.30 0.00 1.440 16.47 3.23 1.4295 0.74 1.4180 2019-03-04 500深市 0.81 22.140 2.068 201 | 25 159933 金地ETF 000914 50.0 19338.0 3.98 0.00 2.058 4.44 0.05 2.0495 0.42 2.0579 2019-03-04 300金融 -0.41 9.086 1.176 202 | 26 159935 景顺500 000905 200.0 19857.0 2.83 0.00 1.425 18.31 1.06 1.4344 -0.65 1.4223 2019-03-04 中证500 0.85 20.269 1.925 203 | 27 159936 可选消费 000989 200.0 21416.0 2.81 0.00 1.310 17.70 0.00 1.3235 -1.02 1.3116 2019-03-04 全指可选 0.91 18.603 2.043 204 | 28 159938 广发医药 000991 100.0 178728.0 21.72 0.00 1.215 2217.67 -0.49 1.2189 -0.32 1.2180 2019-03-04 全指医药 0.07 29.572 3.452 205 | 29 159939 信息技术 000993 200.0 69883.0 7.02 0.10 1.005 2187.49 0.30 1.0040 0.10 0.9860 2019-03-04 全指信息 1.83 39.042 3.516 206 | .. ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... 207 | 90 512000 券商ETF 399975 30.0 201302.0 19.85 -0.07 0.986 21031.33 -0.90 0.9862 -0.02 0.9948 2019-03-04 证券公司 -0.86 33.975 1.803 208 | 91 512010 医药ETF 000913 100.0 49817.0 7.90 0.17 1.586 3092.09 -0.88 1.5856 0.03 1.5894 2019-03-04 300医药 -0.24 31.681 4.563 209 | 92 512070 非银ETF NaN 100.0 78016.0 16.76 0.41 2.148 1334.05 -1.29 2.1477 0.02 2.1663 2019-03-04 300非银 -0.86 NaN NaN 210 | 93 512100 1000ETF 000852 300.0 23989.0 1.60 -0.10 0.665 606.86 1.22 0.6656 -0.09 0.6576 2019-03-04 中证1000 1.22 26.565 2.225 211 | 94 512120 医药50 000814 50.0 7225.0 1.03 -0.01 1.423 261.39 0.35 1.4183 0.33 1.4190 2019-03-04 细分医药 -0.05 28.239 3.827 212 | 95 512200 房地产 NaN 100.0 8738.0 0.77 0.04 0.881 954.61 0.34 0.8849 -0.44 0.8720 2019-03-04 房地产 1.48 NaN NaN 213 | 96 512220 景顺TMT NaN 150.0 25947.0 3.35 0.00 1.290 14.22 1.10 1.2783 0.92 1.2574 2019-03-04 TMT150 1.66 NaN NaN 214 | 97 512230 景顺医药 000933 50.0 NaN 0.00 0.00 0.000 0.00 0.00 1.3945 2.90 1.4231 2018-03-06 中证医药 0.04 28.686 3.692 215 | 98 512300 500医药 000857 50.0 4712.0 0.48 -0.02 1.027 88.03 0.39 1.0295 -0.25 1.0234 2019-03-04 500医药 0.60 23.430 2.541 216 | 99 512310 500工业 000856 50.0 11189.0 0.57 0.00 0.507 125.01 0.40 0.5079 -0.17 0.5057 2019-03-04 500工业 0.43 24.771 1.891 217 | 100 512330 500信息 000858 50.0 22769.0 1.71 -0.04 0.750 356.49 2.60 0.7505 -0.07 0.7319 2019-03-04 500信息 2.54 34.911 2.974 218 | 101 512340 500原料 000854 50.0 5323.0 0.41 0.00 0.778 7.21 0.65 0.7787 -0.10 0.7796 2019-03-04 500原料 -0.11 10.652 1.640 219 | 102 512400 有色金属 000819 50.0 23354.0 1.60 0.00 0.684 916.06 -0.73 0.6851 -0.16 0.6871 2019-03-04 有色金属 -0.29 25.710 2.180 220 | 103 512500 中证500 000905 120.0 118641.0 30.63 -0.81 2.582 20656.45 1.10 2.5862 -0.16 2.5644 2019-03-04 中证500 0.85 20.269 1.925 221 | 104 512510 ETF500 000905 200.0 38781.0 4.28 0.15 1.104 719.42 0.55 1.1051 -0.10 1.0958 2019-03-04 中证500 0.85 20.269 1.925 222 | 105 512560 中证军工 NaN 100.0 2756.0 0.23 0.00 0.839 52.72 -0.12 0.8459 -0.82 0.8459 2019-03-04 中证军工 0.00 NaN NaN 223 | 106 512580 环保ETF 000827 60.0 226947.0 17.88 0.12 0.788 764.14 -0.13 0.7903 -0.29 0.7829 2019-03-04 中证环保 0.94 22.021 2.296 224 | 107 512600 主要消费 000932 50.0 295.0 0.07 0.00 2.323 10.66 -1.15 2.3373 -0.61 2.3389 2019-03-04 中证消费 -0.07 29.880 5.496 225 | 108 512610 医药卫生 000933 50.0 1164.0 0.16 0.00 1.373 18.25 -1.22 1.3736 -0.05 1.3731 2019-03-04 中证医药 0.04 28.686 3.692 226 | 109 512640 金融地产 000934 50.0 3124.0 0.62 0.00 1.981 0.43 0.00 1.9733 0.39 1.9792 2019-03-04 中证金融 -0.30 9.274 1.187 227 | 110 512660 军工ETF 399967 100.0 149108.0 11.54 0.26 0.774 6214.18 0.00 0.7750 -0.13 0.7735 2019-03-04 中证军工 0.19 56.945 2.606 228 | 111 512680 军工基金 399967 60.0 36333.0 2.77 -0.01 0.763 383.28 0.26 0.7642 -0.16 0.7628 2019-03-04 中证军工 0.19 56.945 2.606 229 | 112 512700 银行基金 399986 20.0 10858.0 1.20 0.02 1.107 253.49 -0.63 1.1116 -0.41 1.1157 2019-03-04 中证银行 -0.37 7.037 0.932 230 | 113 512800 银行ETF 399986 30.0 84314.0 9.03 0.64 1.071 7204.46 -0.46 1.0732 -0.21 1.0772 2019-03-04 中证银行 -0.37 7.037 0.932 231 | 114 512810 军工行业 399967 100.0 9279.0 0.68 0.01 0.734 306.89 0.27 0.7337 0.04 0.7323 2019-03-04 中证军工 0.19 56.945 2.606 232 | 115 512880 证券ETF 399975 100.0 438729.0 45.36 -1.23 1.034 48779.64 -0.96 1.0351 -0.11 1.0441 2019-03-04 证券公司 -0.86 33.975 1.803 233 | 116 512900 证券基金 399975 30.0 85301.0 8.52 0.14 0.999 664.63 -1.19 0.9988 0.02 1.0075 2019-03-04 证券公司 -0.86 33.975 1.803 234 | 117 512980 传媒ETF 399971 100.0 108112.0 9.19 -0.03 0.850 685.69 0.71 0.8527 -0.32 0.8411 2019-03-04 中证传媒 1.38 25.004 2.996 235 | 118 513600 恒指ETF NaN 100.0 2783.0 0.72 0.00 2.599 28.59 -0.31 2.6151 -0.62 2.6181 2019-03-04 恒生指数 -0.03 NaN NaN 236 | 119 513660 恒生通 NaN 100.0 31900.0 8.25 -0.28 2.585 31.34 -0.50 2.5945 -0.37 2.5975 2019-03-04 恒生指数 -0.03 NaN NaN -------------------------------------------------------------------------------- /documents/股票交易类.md: -------------------------------------------------------------------------------- 1 | 股票交易类StockData共包含以下几个方法: 2 | 3 | [b](1) __init__(code=None, pandas=True, inter=True)[/b] 4 | 继承自基础类的初始化方法增加了一个表示股票代码的参数code,创建股票交易类的对象时您需要将欲获取数据的股票代码赋值于code 5 | 6 | 示例: 7 | [i]import gugu as gg[/i] 8 | [i]obj = gg.StockData(code='600000')[/i] # 以'600000'创建股票交易对象,获取'浦发银行'交易数据 9 | 10 | [b](2) history(start='', end='', ktype='D', autype='qfq', index=False, retry=3, pause=0.001)[/b] 11 | 获取股票交易历史数据 12 | 13 | 参数: 14 | start:string 15 | 开始日期 format:YYYY-MM-DD 为空时取上市首日 16 | end:string 17 | 结束日期 format:YYYY-MM-DD 为空时取最近一个交易日 18 | autype:string 19 | 复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq 20 | ktype:string 21 | 数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D 22 | retry: int, 默认 3 23 | 如遇网络等问题重复执行的次数 24 | pause : int, 默认 0 25 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 26 | 27 | 返回值: 28 | DataFrame or list: [{'date':, 'open':, ...}, ...] 29 | date 交易日期 30 | open 开盘价 31 | high 最高价 32 | close 收盘价 33 | low 最低价 34 | volume 成交量 35 | code 股票代码 36 | 37 | 示例: 38 | [i]import gugu as gg[/i] 39 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象 40 | [i]data = obj.history()[/i] # 获取浦发银行(600000)历史交易数据 41 | [i]obj.output()[/i] # 输出数据 42 | 43 | 输出: 44 | date open close high low volume code 45 | 0 2016-07-18 11.823 11.853 11.958 11.770 277669.0 600000 46 | 1 2016-07-19 11.853 11.823 11.875 11.763 171798.0 600000 47 | 2 2016-07-20 11.800 11.838 11.845 11.748 148665.0 600000 48 | 3 2016-07-21 11.830 11.898 11.928 11.800 220204.0 600000 49 | 4 2016-07-22 11.883 11.748 11.890 11.710 167117.0 600000 50 | 5 2016-07-25 11.748 11.793 11.845 11.748 114419.0 600000 51 | 6 2016-07-26 11.793 11.830 11.868 11.763 204623.0 600000 52 | 7 2016-07-27 11.823 11.928 11.935 11.612 406973.0 600000 53 | 8 2016-07-28 11.853 11.785 11.868 11.763 174513.0 600000 54 | 9 2016-07-29 11.763 11.800 11.808 11.733 143483.0 600000 55 | 10 2016-08-01 11.785 11.875 11.913 11.763 229099.0 600000 56 | 11 2016-08-02 11.868 11.838 11.913 11.785 108778.0 600000 57 | 12 2016-08-03 11.755 11.778 11.815 11.740 105839.0 600000 58 | 13 2016-08-04 11.793 11.718 11.793 11.642 156104.0 600000 59 | 14 2016-08-05 11.725 11.823 11.875 11.718 176485.0 600000 60 | 15 2016-08-08 11.845 11.830 11.845 11.770 131293.0 600000 61 | 16 2016-08-09 11.838 11.868 11.875 11.808 154808.0 600000 62 | 17 2016-08-10 11.875 11.898 11.913 11.823 184995.0 600000 63 | 18 2016-08-11 11.838 12.078 12.326 11.830 526894.0 600000 64 | 19 2016-08-12 12.116 12.296 12.319 12.026 469916.0 600000 65 | 20 2016-08-15 12.401 12.732 12.792 12.364 773338.0 600000 66 | 21 2016-08-16 12.732 12.552 12.770 12.529 493840.0 600000 67 | 22 2016-08-17 12.567 12.507 12.665 12.417 364465.0 600000 68 | 23 2016-08-18 12.439 12.356 12.604 12.289 371101.0 600000 69 | 24 2016-08-19 12.349 12.341 12.401 12.296 199273.0 600000 70 | 25 2016-08-22 12.341 12.334 12.379 12.259 197686.0 600000 71 | 26 2016-08-23 12.334 12.326 12.447 12.266 317543.0 600000 72 | 27 2016-08-24 12.334 12.319 12.409 12.281 170854.0 600000 73 | 28 2016-08-25 12.296 12.281 12.334 12.176 247499.0 600000 74 | 29 2016-08-26 12.296 12.281 12.326 12.244 146880.0 600000 75 | .. ... ... ... ... ... ... ... 76 | 611 2019-01-17 10.170 10.170 10.280 10.070 227660.0 600000 77 | 612 2019-01-18 10.290 10.430 10.440 10.220 290871.0 600000 78 | 613 2019-01-21 10.360 10.350 10.460 10.300 189477.0 600000 79 | 614 2019-01-22 10.380 10.250 10.380 10.230 162505.0 600000 80 | 615 2019-01-23 10.290 10.270 10.340 10.250 165203.0 600000 81 | 616 2019-01-24 10.340 10.380 10.390 10.270 150066.0 600000 82 | 617 2019-01-25 10.450 10.500 10.560 10.380 196965.0 600000 83 | 618 2019-01-28 10.530 10.420 10.660 10.390 217690.0 600000 84 | 619 2019-01-29 10.390 10.580 10.630 10.380 237680.0 600000 85 | 620 2019-01-30 10.590 10.500 10.640 10.470 160542.0 600000 86 | 621 2019-01-31 10.590 10.730 10.730 10.510 251310.0 600000 87 | 622 2019-02-01 10.820 10.770 10.820 10.620 191193.0 600000 88 | 623 2019-02-11 10.620 10.700 10.730 10.620 202165.0 600000 89 | 624 2019-02-12 10.710 10.720 10.730 10.630 167696.0 600000 90 | 625 2019-02-13 10.740 10.860 10.930 10.680 302486.0 600000 91 | 626 2019-02-14 10.850 10.820 10.910 10.800 198152.0 600000 92 | 627 2019-02-15 10.780 10.660 10.850 10.660 177587.0 600000 93 | 628 2019-02-18 10.750 10.840 10.870 10.700 201022.0 600000 94 | 629 2019-02-19 10.850 10.800 10.960 10.780 259527.0 600000 95 | 630 2019-02-20 10.920 10.830 10.920 10.810 222863.0 600000 96 | 631 2019-02-21 10.830 10.790 10.930 10.750 254023.0 600000 97 | 632 2019-02-22 10.760 11.010 11.010 10.760 259074.0 600000 98 | 633 2019-02-25 11.030 11.920 11.950 11.030 937122.0 600000 99 | 634 2019-02-26 11.910 11.650 12.150 11.600 836432.0 600000 100 | 635 2019-02-27 11.710 11.830 12.070 11.670 712016.0 600000 101 | 636 2019-02-28 11.830 11.740 11.920 11.680 393934.0 600000 102 | 637 2019-03-01 11.810 12.020 12.060 11.600 632546.0 600000 103 | 638 2019-03-04 12.120 12.020 12.380 11.950 850247.0 600000 104 | 639 2019-03-05 12.010 11.980 12.040 11.870 545642.0 600000 105 | 640 2019-03-06 11.990 12.050 12.150 11.940 272160.0 600000 106 | 107 | [b](3) xrxd(date='', retry=3, pause=0.001)[/b] 108 | 获取股票除权除息信息,注意该方法返回值既不是pandas.DataFrame类型也不是list类型而是dict类型,因此输出数据时不能使用基类的output方法而是直接使用print()函数输出 109 | 110 | 参数: 111 | date: string 112 | format:YYYY-MM-DD 113 | retry: int, 默认 3 114 | 如遇网络等问题重复执行的次数 115 | pause : int, 默认 0 116 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 117 | 118 | 返回值: 119 | Dict or None 120 | nd, 对应年份 121 | fh_sh, 分红数额 122 | djr, 股权登记日 123 | cqr, 除权日 124 | FHcontent, 除权除息信息 125 | 126 | 示例: 127 | [i]import gugu as gg[/i] 128 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象 129 | [i]data = obj.xrxd(date='2018-07-13')[/i] # 获取浦发银行(600000)2018-07-13号的除权除息信息 130 | [i]print(data)[/i] # 使用print()函数输出结果 131 | 132 | 输出: 133 | {'nd': '2017', 'fh_sh': '1', 'djr': '2018-07-12', 'cqr': '2018-07-13', 'FHcontent': '10派1元'} 134 | 135 | [b](4) realtime()[/b] 136 | 获取实时分笔数据,实时获取个股当前价格及成交信息,您可以通过该方法实现实时交易监控。注意,当您调用该方法时,在创建股票交易对象时code参数可以是6位数字股票代码,或者指数代码(sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板) 可输入的类型:str、list、set或者pandas的Series对象 137 | 138 | 返回值: 139 | DataFrame or list: [{'name':, 'open':, ...}, ...] 140 | name,股票名字 141 | open,今日开盘价 142 | pre_close,昨日收盘价 143 | price,当前价格 144 | high,今日最高价 145 | low,今日最低价 146 | bid,竞买价,即“买一”报价 147 | ask,竞卖价,即“卖一”报价 148 | volumn,成交量 maybe you need do volumn/100 149 | amount,成交金额(元 CNY) 150 | b1_v,委买一(笔数 bid volume) 151 | b1_p,委买一(价格 bid price) 152 | b2_v,“买二” 153 | b2_p,“买二” 154 | b3_v,“买三” 155 | b3_p,“买三” 156 | b4_v,“买四” 157 | b4_p,“买四” 158 | b5_v,“买五” 159 | b5_p,“买五” 160 | a1_v,委卖一(笔数 ask volume) 161 | a1_p,委卖一(价格 ask price) 162 | ... 163 | date,日期; 164 | time,时间; 165 | 166 | 示例: 167 | [i]import gugu as gg[/i] 168 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象(个股) 169 | [i]obj = gg.StockData(code = ['000001', '600000', '300001'])[/i] # 创建股票交易对象(多股) 170 | [i]obj = gg.StockData(code=series['code'].tail(10))[/i] # 创建股票交易对象(通过Series一次获取10个股票的实时分笔数据) 171 | [i]obj = gg.StockData(code=['sh','sz','hs300','sz50','zxb','cyb'])[/i] # 创建股票交易对象(获取上证指数、深圳成指、沪深300指数、上证50、中小板、创业板实时指数) 172 | [i]obj = gg.StockData(code=['sh','600000'])[/i] # 创建股票交易对象(获取上证指数及个股实时数据) 173 | [i]data = obj.realtime()[/i] # 获取实时交易数据 174 | [i]obj.output()[/i] # 输出数据 175 | 176 | 输出: 177 | name open pre_close price ... a5_p date time code 178 | 0 浦发银行 11.440 11.500 11.490 ... 11.530 2019-03-11 11:30:00 600000 179 | 选择性输出: 180 | [i]data[['code','name','price','bid','ask','volume','amount','time']][/i] 181 | code name price bid ask volume amount time 182 | 0 600000 浦发银行 11.490 11.480 11.490 23045971 263743509.000 11:30:00 183 | 184 | [b](5) historyTicks(date=None, retry=3, pause=0.001)[/b] 185 | 获取历史分笔明细数据 186 | 187 | 参数: 188 | date : string 189 | 日期 format:YYYY-MM-DD, 默认为前一个交易日 190 | retry : int, 默认 3 191 | 如遇网络等问题重复执行的次数 192 | pause : int, 默认 0 193 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 194 | 195 | 返回值: 196 | DataFrame or list: [{'time':, 'price':, ...}, ...] 197 | time:时间 198 | price:成交价格 199 | change:价格变动 200 | volume:成交手 201 | amount:成交金额(元) 202 | type:买卖类型【买盘、卖盘、中性盘】 203 | 204 | 示例: 205 | [i]import gugu as gg[/i] 206 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象 207 | [i]data = obj.historyTicks()[/i] # 获取历史分笔明细数据 208 | [i]obj.output()[/i] # 输出数据 209 | 210 | 输出: 211 | time price change volume amount type 212 | 0 15:00:00 11.50 0.00 0 0 买盘 213 | 1 15:00:00 11.50 0.04 7742 8903300 卖盘 214 | 2 14:58:35 11.46 0.00 0 0 卖盘 215 | 3 14:58:19 11.46 0.00 0 0 卖盘 216 | 4 14:58:02 11.46 0.00 0 0 卖盘 217 | 5 14:57:47 11.46 0.00 0 0 卖盘 218 | 6 14:57:44 11.46 0.00 0 0 卖盘 219 | 7 14:57:08 11.46 0.00 0 0 卖盘 220 | 8 14:57:01 11.46 0.00 0 0 卖盘 221 | 9 14:56:56 11.46 -0.01 138 158148 卖盘 222 | 10 14:56:53 11.47 0.00 95 108965 买盘 223 | 11 14:56:47 11.47 0.00 96 110112 买盘 224 | 12 14:56:44 11.47 11.47 60 68820 买盘 225 | 13 14:56:44 11.47 0.00 60 68820 买盘 226 | 14 14:56:41 11.47 0.00 138 158286 买盘 227 | 15 14:56:38 11.47 -0.01 15 17205 中性盘 228 | 16 14:56:35 11.48 0.01 47 53956 买盘 229 | 17 14:56:32 11.47 0.00 41 47038 卖盘 230 | 18 14:56:29 11.47 0.00 25 28675 卖盘 231 | 19 14:56:26 11.47 -0.01 127 145669 卖盘 232 | 20 14:56:23 11.48 0.01 119 136612 买盘 233 | 21 14:56:20 11.47 0.00 137 157139 卖盘 234 | 22 14:56:17 11.47 0.00 11 13753 买盘 235 | 23 14:56:14 11.47 0.01 154 176638 买盘 236 | 24 14:56:11 11.46 -0.01 65 74490 卖盘 237 | 25 14:56:05 11.47 -0.01 13 14911 买盘 238 | 26 14:56:02 11.48 0.00 11 12628 买盘 239 | 27 14:55:59 11.48 0.01 29 33292 买盘 240 | 28 14:55:56 11.47 0.01 1 1812 中性盘 241 | 29 14:55:53 11.46 -0.03 8 9168 卖盘 242 | ... ... ... ... ... ... ... 243 | 4423 09:31:25 11.68 0.00 18 21024 卖盘 244 | 4424 09:31:22 11.68 -0.02 141 164688 卖盘 245 | 4425 09:31:19 11.70 0.00 466 545220 买盘 246 | 4426 09:31:16 11.70 0.01 34 39780 买盘 247 | 4427 09:31:13 11.69 0.01 510 596190 买盘 248 | 4428 09:31:10 11.68 -0.01 53 61904 卖盘 249 | 4429 09:31:07 11.69 0.01 86 100534 买盘 250 | 4430 09:31:04 11.68 -0.01 295 344560 卖盘 251 | 4431 09:31:01 11.69 0.01 74 86506 买盘 252 | 4432 09:30:58 11.68 -0.01 127 148336 卖盘 253 | 4433 09:30:55 11.69 0.02 250 292250 买盘 254 | 4434 09:30:52 11.67 -0.01 447 521649 卖盘 255 | 4435 09:30:49 11.68 0.00 366 427488 买盘 256 | 4436 09:30:46 11.68 0.01 210 245280 买盘 257 | 4437 09:30:43 11.67 -0.01 586 683862 卖盘 258 | 4438 09:30:40 11.68 0.02 266 310688 买盘 259 | 4439 09:30:37 11.66 -0.01 165 192390 卖盘 260 | 4440 09:30:34 11.67 0.00 221 257907 中性盘 261 | 4441 09:30:31 11.67 -0.01 581 678027 中性盘 262 | 4442 09:30:28 11.68 0.03 464 541952 买盘 263 | 4443 09:30:25 11.65 -0.02 115 133975 卖盘 264 | 4444 09:30:22 11.67 0.02 546 637182 买盘 265 | 4445 09:30:19 11.65 -0.01 1008 1174320 卖盘 266 | 4446 09:30:16 11.66 0.00 769 896654 买盘 267 | 4447 09:30:13 11.66 -0.02 1407 1640562 卖盘 268 | 4448 09:30:10 11.68 0.00 1147 1339696 买盘 269 | 4449 09:30:07 11.68 -0.04 1408 1644544 卖盘 270 | 4450 09:30:04 11.72 0.00 944 1106368 买盘 271 | 4451 09:30:01 11.72 11.72 717 840324 买盘 272 | 4452 09:25:00 11.72 11.72 7134 8361048 买盘 273 | 274 | [b](6) todayTicks(retry=3, pause=0.001)[/b] 275 | 获取当日分笔明细数据 276 | 277 | 参数: 278 | retry : int, 默认 3 279 | 如遇网络等问题重复执行的次数 280 | pause : int, 默认 0 281 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 282 | 283 | 返回值: 284 | DataFrame or list: [{'time':, 'price':, ...}, ...] 285 | time:时间 286 | price:当前价格 287 | pchange:涨跌幅 288 | change:价格变动 289 | volume:成交手 290 | amount:成交金额(元) 291 | type:买卖类型【买盘、卖盘、中性盘】 292 | 293 | 示例: 294 | [i]import gugu as gg[/i] 295 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象 296 | [i]data = obj.todayTicks()[/i] # 获取当日分笔明细数据 297 | [i]obj.output()[/i] # 输出数据 298 | 299 | 输出: 300 | time price pchange change volume amount type 301 | 0 14:05:06 11.44 -0.52 0.01 4 4576 买盘 302 | 1 14:05:03 11.43 -0.61 0.00 11 12573 卖盘 303 | 2 14:05:00 11.43 -0.61 -0.01 51 58293 卖盘 304 | 3 14:04:54 11.44 -0.52 0.00 3 3432 买盘 305 | 4 14:04:51 11.44 -0.52 0.01 5 5720 买盘 306 | 5 14:04:48 11.43 -0.61 0.00 57 65151 卖盘 307 | 6 14:04:45 11.43 -0.61 0.00 184 210312 卖盘 308 | 7 14:04:39 11.43 -0.61 -0.01 4 4572 卖盘 309 | 8 14:04:36 11.44 -0.52 0.00 9 10296 买盘 310 | 9 14:04:33 11.44 -0.52 0.01 67 76648 买盘 311 | 10 14:04:30 11.43 -0.61 -0.01 66 75438 卖盘 312 | 11 14:04:27 11.44 -0.52 0.00 19 21736 买盘 313 | 12 14:04:21 11.44 -0.52 0.00 29 33176 买盘 314 | 13 14:04:18 11.44 -0.52 0.01 55 62920 买盘 315 | 14 14:04:09 11.43 -0.61 0.00 33 37719 卖盘 316 | 15 14:04:06 11.43 -0.61 0.00 4 4572 卖盘 317 | 16 14:04:03 11.43 -0.61 0.00 12 13716 卖盘 318 | 17 14:04:00 11.43 -0.61 -0.01 38 43434 卖盘 319 | 18 14:03:54 11.44 -0.52 0.00 8 9152 买盘 320 | 19 14:03:48 11.44 -0.52 0.01 42 48048 买盘 321 | 20 14:03:45 11.43 -0.61 0.00 23 26289 卖盘 322 | 21 14:03:42 11.43 -0.61 0.00 11 12573 卖盘 323 | 22 14:03:39 11.43 -0.61 0.00 20 22860 卖盘 324 | 23 14:03:33 11.43 -0.61 0.00 28 32004 卖盘 325 | 24 14:03:30 11.43 -0.61 -0.01 33 37719 卖盘 326 | 25 14:03:27 11.44 -0.52 0.01 4 4576 买盘 327 | 26 14:03:21 11.43 -0.61 0.00 10 11430 卖盘 328 | 27 14:03:18 11.43 -0.61 0.00 98 112014 卖盘 329 | 28 14:03:15 11.43 -0.61 0.00 10 11430 卖盘 330 | 29 14:03:09 11.43 -0.61 0.00 1 1143 卖盘 331 | ... ... ... ... ... ... ... ... 332 | 3123 09:31:22 11.54 +0.35 0.00 159 183486 卖盘 333 | 3124 09:31:19 11.54 +0.35 -0.01 83 95782 卖盘 334 | 3125 09:31:16 11.55 +0.43 0.01 291 336105 买盘 335 | 3126 09:31:13 11.54 +0.35 0.00 12 13848 买盘 336 | 3127 09:31:10 11.54 +0.35 0.01 508 586232 买盘 337 | 3128 09:31:07 11.53 +0.26 0.00 66 76098 卖盘 338 | 3129 09:31:04 11.53 +0.26 0.00 347 400091 买盘 339 | 3130 09:31:01 11.53 +0.26 0.02 347 400091 买盘 340 | 3131 09:30:58 11.51 +0.09 -0.02 302 347602 卖盘 341 | 3132 09:30:55 11.53 +0.26 -0.01 45 51885 卖盘 342 | 3133 09:30:52 11.54 +0.35 -0.01 26 30004 卖盘 343 | 3134 09:30:49 11.55 +0.43 0.01 1 1155 买盘 344 | 3135 09:30:46 11.54 +0.35 0.00 6 6924 卖盘 345 | 3136 09:30:43 11.54 +0.35 0.00 24 27696 卖盘 346 | 3137 09:30:40 11.54 +0.35 0.02 40 46160 买盘 347 | 3138 09:30:37 11.52 +0.17 0.02 341 392832 买盘 348 | 3139 09:30:34 11.50 0.00 -0.03 507 583050 卖盘 349 | 3140 09:30:31 11.53 +0.26 -0.05 271 312463 中性盘 350 | 3141 09:30:28 11.58 +0.70 0.07 491 568578 买盘 351 | 3142 09:30:25 11.51 +0.09 -0.01 84 96684 卖盘 352 | 3143 09:30:22 11.52 +0.17 0.01 31 35712 买盘 353 | 3144 09:30:19 11.51 +0.09 0.02 1012 1164812 卖盘 354 | 3145 09:30:16 11.49 -0.09 0.00 239 275254 卖盘 355 | 3146 09:30:13 11.49 -0.09 0.00 584 671016 卖盘 356 | 3147 09:30:10 11.49 -0.09 0.04 1129 1297221 买盘 357 | 3148 09:30:07 11.45 -0.43 -0.04 273 312917 卖盘 358 | 3149 09:30:04 11.49 -0.09 0.04 815 936918 买盘 359 | 3150 09:30:01 11.45 -0.43 0.01 221 253045 买盘 360 | 3151 09:25:00 11.44 -0.52 0.00 2931 3354105 中性盘 361 | 3152 09:25:00 11.44 -0.52 0.00 2931 3354105 0 362 | 363 | [b](7) bigDeal(date=None, vol=400, retry=3, pause=0.001)[/b] 364 | 获取大单数据,默认为大于等于400手,数据来源于新浪财经。 365 | 366 | 参数: 367 | date:string 368 | 日期 format:YYYY-MM-DD 369 | retry : int, 默认 3 370 | 如遇网络等问题重复执行的次数 371 | pause : int, 默认 0 372 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 373 | 374 | 返回值: 375 | DataFrame or list: [{'code':, 'name', ...}, ...] 376 | code:代码 377 | name:名称 378 | time:时间 379 | price:当前价格 380 | volume:成交手 381 | preprice :上一笔价格 382 | type:买卖类型【买盘、卖盘、中性盘】 383 | 384 | 示例: 385 | [i]import gugu as gg[/i] 386 | [i]obj = gg.StockData(code='600000')[/i] # 创建股票交易对象 387 | [i]data = obj.bigDeal(date='2019-03-11')[/i] # 获取2019-03-11号大单数据 388 | [i]obj.output()[/i] # 输出数据 389 | 390 | 输出: 391 | code name time price volume preprice type 392 | 0 600000 浦发银行 14:10:06 11.41 60500 11.41 卖盘 393 | 1 600000 浦发银行 14:09:24 11.41 53600 11.40 买盘 394 | 2 600000 浦发银行 14:08:03 11.40 45100 11.41 卖盘 395 | 3 600000 浦发银行 14:07:42 11.40 89000 11.40 卖盘 396 | 4 600000 浦发银行 14:07:33 11.41 61900 11.41 卖盘 397 | 5 600000 浦发银行 14:06:57 11.42 93400 11.43 卖盘 398 | 6 600000 浦发银行 14:06:54 11.43 130500 11.42 买盘 399 | 7 600000 浦发银行 14:05:09 11.43 50000 11.44 卖盘 400 | 8 600000 浦发银行 14:02:24 11.44 63800 11.44 卖盘 401 | 9 600000 浦发银行 13:56:33 11.46 41100 11.45 买盘 402 | 10 600000 浦发银行 13:54:45 11.46 89357 11.46 卖盘 403 | 11 600000 浦发银行 13:48:24 11.45 100900 11.44 买盘 404 | 12 600000 浦发银行 13:47:27 11.43 58559 11.43 卖盘 405 | 13 600000 浦发银行 13:40:18 11.42 54300 11.41 中性盘 406 | 14 600000 浦发银行 13:40:06 11.41 46200 11.42 卖盘 407 | 15 600000 浦发银行 13:39:48 11.42 202000 11.42 卖盘 408 | 16 600000 浦发银行 13:30:15 11.43 62700 11.43 卖盘 409 | 17 600000 浦发银行 13:28:33 11.44 42300 11.44 卖盘 410 | 18 600000 浦发银行 13:28:06 11.45 44400 11.46 卖盘 411 | 19 600000 浦发银行 13:28:03 11.46 43600 11.45 买盘 412 | 20 600000 浦发银行 13:28:00 11.45 49600 11.46 卖盘 413 | 21 600000 浦发银行 13:27:51 11.45 65200 11.45 卖盘 414 | 22 600000 浦发银行 13:27:45 11.46 144220 11.46 卖盘 415 | 23 600000 浦发银行 13:27:36 11.46 43800 11.47 卖盘 416 | 24 600000 浦发银行 13:23:48 11.49 51200 11.48 买盘 417 | 25 600000 浦发银行 13:22:21 11.47 58670 11.47 卖盘 418 | 26 600000 浦发银行 13:15:57 11.48 48900 11.48 卖盘 419 | 27 600000 浦发银行 13:13:27 11.49 43300 11.48 买盘 420 | 28 600000 浦发银行 13:11:30 11.48 45100 11.49 卖盘 421 | 29 600000 浦发银行 13:07:36 11.49 69600 11.51 卖盘 422 | .. ... ... ... ... ... ... ... 423 | 134 600000 浦发银行 09:36:10 11.48 40300 11.46 买盘 424 | 135 600000 浦发银行 09:36:07 11.46 70400 11.47 卖盘 425 | 136 600000 浦发银行 09:36:04 11.47 80100 11.48 卖盘 426 | 137 600000 浦发银行 09:35:58 11.49 62500 11.50 卖盘 427 | 138 600000 浦发银行 09:35:52 11.51 68200 11.50 买盘 428 | 139 600000 浦发银行 09:35:34 11.50 43700 11.51 卖盘 429 | 140 600000 浦发银行 09:35:22 11.51 111900 11.52 卖盘 430 | 141 600000 浦发银行 09:34:58 11.54 50000 11.52 买盘 431 | 142 600000 浦发银行 09:34:34 11.54 69100 11.54 卖盘 432 | 143 600000 浦发银行 09:34:13 11.49 95616 11.49 买盘 433 | 144 600000 浦发银行 09:34:10 11.49 45300 11.49 买盘 434 | 145 600000 浦发银行 09:34:07 11.49 55000 11.48 买盘 435 | 146 600000 浦发银行 09:34:01 11.48 40500 11.48 买盘 436 | 147 600000 浦发银行 09:33:07 11.49 101200 11.47 买盘 437 | 148 600000 浦发银行 09:32:46 11.48 56600 11.46 买盘 438 | 149 600000 浦发银行 09:32:34 11.45 65300 11.47 卖盘 439 | 150 600000 浦发银行 09:32:31 11.47 113500 11.47 买盘 440 | 151 600000 浦发银行 09:32:28 11.47 56800 11.48 卖盘 441 | 152 600000 浦发银行 09:32:10 11.47 48500 11.49 卖盘 442 | 153 600000 浦发银行 09:31:49 11.49 58700 11.49 买盘 443 | 154 600000 浦发银行 09:31:34 11.53 44200 11.54 卖盘 444 | 155 600000 浦发银行 09:31:31 11.54 46000 11.54 卖盘 445 | 156 600000 浦发银行 09:31:10 11.54 50800 11.53 买盘 446 | 157 600000 浦发银行 09:30:34 11.50 50700 11.53 卖盘 447 | 158 600000 浦发银行 09:30:28 11.58 49100 11.51 买盘 448 | 159 600000 浦发银行 09:30:19 11.51 101200 11.49 卖盘 449 | 160 600000 浦发银行 09:30:13 11.49 58400 11.49 卖盘 450 | 161 600000 浦发银行 09:30:10 11.49 112900 11.45 买盘 451 | 162 600000 浦发银行 09:30:04 11.49 81542 11.45 买盘 452 | 163 600000 浦发银行 09:25:00 11.44 293191 0.00 买盘 -------------------------------------------------------------------------------- /documents/股票所属类.md: -------------------------------------------------------------------------------- 1 | 股票所属类Classify主要用来获取上市公司股票所属行业分类数据及概念分类数据 2 | 3 | [b](1) byIndustry(retry=3, pause=0.001)[/b] 4 | 该方法用于获取股票所属行业分类数据 5 | 6 | 参数: 7 | retry : int, 默认 10 8 | 如遇网络等问题重复执行的次数 9 | pause : int, 默认 0.001 10 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 11 | 12 | 返回值: 13 | DataFrame or List: [{'code':, 'name':, ...}, ...] 14 | code :股票代码 15 | name :股票名称 16 | c_name :行业名称 17 | 18 | 示例: 19 | [i]import gugu as gg[/i] 20 | [i]obj = gg.Classify()[/i] # 创建股票所属类对象 21 | [i]data = obj.byIndustry()[/i] # 获取行业分类数据 22 | [i]obj.output()[/i] # 输出数据 23 | 24 | 输出: 25 | code name c_name 26 | 0 600051 宁波联合 综合行业 27 | 1 600209 *ST罗顿 综合行业 28 | 2 600212 江泉实业 综合行业 29 | 3 600256 广汇能源 综合行业 30 | 4 600576 祥源文化 综合行业 31 | 5 600603 广汇物流 综合行业 32 | 6 600614 鹏起科技 综合行业 33 | 7 600620 天宸股份 综合行业 34 | 8 600622 光大嘉宝 综合行业 35 | 9 600624 复旦复华 综合行业 36 | 10 600647 同达创业 综合行业 37 | 11 600656 退市博元 综合行业 38 | 12 600687 刚泰控股 综合行业 39 | 13 600701 *ST工新 综合行业 40 | 14 600730 中国高科 综合行业 41 | 15 600770 综艺股份 综合行业 42 | 16 600777 新潮能源 综合行业 43 | 17 600800 天津磁卡 综合行业 44 | 18 600805 悦达投资 综合行业 45 | 19 600811 东方集团 综合行业 46 | 20 600832 东方明珠 综合行业 47 | 21 600846 同济科技 综合行业 48 | 22 600892 大晟文化 综合行业 49 | 23 000008 神州高铁 综合行业 50 | 24 000301 东方盛虹 综合行业 51 | 25 000576 广东甘化 综合行业 52 | 26 000632 三木集团 综合行业 53 | 27 000633 合金投资 综合行业 54 | 28 000652 泰达股份 综合行业 55 | 29 000701 厦门信达 综合行业 56 | ... ... ... ... 57 | 2323 600983 惠而浦 家电行业 58 | 2324 603366 日出东方 家电行业 59 | 2325 603519 立霸股份 家电行业 60 | 2326 000016 深康佳A 家电行业 61 | 2327 000100 TCL 集团 家电行业 62 | 2328 000333 美的集团 家电行业 63 | 2329 000418 小天鹅A 家电行业 64 | 2330 000521 长虹美菱 家电行业 65 | 2331 000527 美的电器 家电行业 66 | 2332 000533 顺钠股份 家电行业 67 | 2333 000541 佛山照明 家电行业 68 | 2334 000651 格力电器 家电行业 69 | 2335 000921 海信家电 家电行业 70 | 2336 002005 德豪润达 家电行业 71 | 2337 002032 苏 泊 尔 家电行业 72 | 2338 002035 华帝股份 家电行业 73 | 2339 002076 雪 莱 特 家电行业 74 | 2340 002242 九阳股份 家电行业 75 | 2341 002260 *ST德奥 家电行业 76 | 2342 002290 中科新材 家电行业 77 | 2343 002403 爱仕达 家电行业 78 | 2344 002429 兆驰股份 家电行业 79 | 2345 002473 *ST圣莱 家电行业 80 | 2346 002508 老板电器 家电行业 81 | 2347 002543 万和电气 家电行业 82 | 2348 002668 奥马电器 家电行业 83 | 2349 002677 浙江美大 家电行业 84 | 2350 002681 奋达科技 家电行业 85 | 2351 002705 新宝股份 家电行业 86 | 2352 002723 金莱特 家电行业 87 | 88 | [b](2) byConcept(retry=3, pause=0.001)[/b] 89 | 该方法用于获取概念分类数据 90 | 91 | 参数: 92 | retry : int, 默认 3 93 | 如遇网络等问题重复执行的次数 94 | pause : int, 默认 0.001 95 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 96 | 97 | 返回值: 98 | DataFrame or List: [{'code':, 'name':, ...}, ...] 99 | code :股票代码 100 | name :股票名称 101 | c_name :行业名称 102 | 103 | 示例: 104 | [i]import gugu as gg[/i] 105 | [i]obj = gg.Classify()[/i] # 创建股票所属类对象 106 | [i]data = obj.byConcept()[/i] # 获取概念分类数据 107 | [i]obj.output()[/i] # 输出数据 108 | 109 | 输出: 110 | code name c_name 111 | 0 600007 中国国贸 外资背景 112 | 1 600114 东睦股份 外资背景 113 | 2 600132 重庆啤酒 外资背景 114 | 3 600182 S佳通 外资背景 115 | 4 600595 中孚实业 外资背景 116 | 5 600641 万业企业 外资背景 117 | 6 600779 水井坊 外资背景 118 | 7 600801 华新水泥 外资背景 119 | 8 600819 耀皮玻璃 外资背景 120 | 9 000001 平安银行 外资背景 121 | 10 000005 世纪星源 外资背景 122 | 11 000541 佛山照明 外资背景 123 | 12 000608 阳光股份 外资背景 124 | 13 000659 珠海中富 外资背景 125 | 14 000869 张 裕A 外资背景 126 | 15 000895 双汇发展 外资背景 127 | 16 000932 华菱钢铁 外资背景 128 | 17 000935 四川双马 外资背景 129 | 18 002032 苏 泊 尔 外资背景 130 | 19 002047 宝鹰股份 外资背景 131 | 0 600023 浙能电力 核电核能 132 | 1 600089 特变电工 核电核能 133 | 2 600202 *ST哈空 核电核能 134 | 3 600226 瀚叶股份 核电核能 135 | 4 600269 赣粤高速 核电核能 136 | 5 600292 远达环保 核电核能 137 | 6 600307 酒钢宏兴 核电核能 138 | 7 600328 兰太实业 核电核能 139 | 8 600416 湘电股份 核电核能 140 | 9 600483 福能股份 核电核能 141 | .. ... ... ... 142 | 0 600069 银鸽投资 摘帽概念 143 | 1 600163 中闽能源 摘帽概念 144 | 2 600179 安通控股 摘帽概念 145 | 3 600242 中昌数据 摘帽概念 146 | 4 600311 荣华实业 摘帽概念 147 | 5 600346 恒力股份 摘帽概念 148 | 6 600381 青海春天 摘帽概念 149 | 7 600401 *ST海润 摘帽概念 150 | 8 600408 *ST安泰 摘帽概念 151 | 9 600444 国机通用 摘帽概念 152 | 10 600608 ST沪科 摘帽概念 153 | 11 600691 阳煤化工 摘帽概念 154 | 12 600779 水井坊 摘帽概念 155 | 13 600793 宜宾纸业 摘帽概念 156 | 14 600870 *ST厦华 摘帽概念 157 | 15 600962 国投中鲁 摘帽概念 158 | 16 600984 建设机械 摘帽概念 159 | 17 000059 华锦股份 摘帽概念 160 | 18 000068 华控赛格 摘帽概念 161 | 19 000510 金路集团 摘帽概念 162 | 20 000557 西部创业 摘帽概念 163 | 21 000590 启迪古汉 摘帽概念 164 | 22 000659 珠海中富 摘帽概念 165 | 23 000677 恒天海龙 摘帽概念 166 | 24 000799 酒鬼酒 摘帽概念 167 | 25 000815 美利云 摘帽概念 168 | 26 000927 一汽夏利 摘帽概念 169 | 27 002306 *ST云网 摘帽概念 170 | 28 002417 深南股份 摘帽概念 171 | 29 002633 申科股份 摘帽概念 -------------------------------------------------------------------------------- /documents/龙虎榜类.md: -------------------------------------------------------------------------------- 1 | 龙虎榜类BillBoard主要用于获取龙虎榜列表、个股上榜统计数据、营业部上榜统计数据、机构席位追踪统计数据及最近一个交易日机构席位成交明细统计等数据 2 | 3 | [b](1) topList(date = None, retry=3, pause=0.001)[/b] 4 | 该方法用于获取每日龙虎榜列表 5 | 6 | 参数: 7 | date:string 8 | 明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据 9 | retry : int, 默认 3 10 | 如遇网络等问题重复执行的次数 11 | pause : int, 默认 0 12 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 13 | 14 | 返回值: 15 | DataFrame or List: [{'code':, 'name':, ...}, ...] 16 | code:代码 17 | name :名称 18 | pchange:涨跌幅 19 | amount:龙虎榜成交额(万) 20 | buy:买入额(万) 21 | bratio:占总成交比例 22 | sell:卖出额(万) 23 | sratio :占总成交比例 24 | reason:上榜原因 25 | unscramble: 解读 26 | date :日期 27 | 28 | 示例: 29 | [i]import gugu as gg[/i] 30 | [i]obj = gg.BillBoard()[/i] # 创建龙虎榜类对象 31 | [i]data = obj.topList(date='2019-03-14')[/i] # 获取2019-03-14号虎榜列表数据 32 | [i]obj.output()[/i] # 输出数据 33 | 34 | 输出: 35 | code name pchange ... bratio sratio date 36 | 0 000068 华控赛格 -10.0264 ... 0.06 0.17 2019-03-14 37 | 1 000159 国际实业 10.0733 ... 0.31 0.12 2019-03-14 38 | 2 000682 东方电子 6.6335 ... 0.07 0.08 2019-03-14 39 | 3 000682 东方电子 6.6335 ... 0.07 0.08 2019-03-14 40 | 4 000715 中兴商业 9.9773 ... 0.30 0.15 2019-03-14 41 | 5 000788 北大医药 5.2632 ... 0.12 0.12 2019-03-14 42 | 6 000789 万年青 10 ... 0.21 0.12 2019-03-14 43 | 7 000793 华闻传媒 -9.8925 ... 0.05 0.13 2019-03-14 44 | 8 000806 银河生物 -10.0149 ... 0.06 0.08 2019-03-14 45 | 9 000839 中信国安 -10.0443 ... 0.14 0.20 2019-03-14 46 | 10 000931 中关村 -10.0204 ... 0.05 0.17 2019-03-14 47 | 11 000936 华西股份 -9.4183 ... 0.07 0.17 2019-03-14 48 | 12 000981 银亿股份 9.9783 ... 0.15 0.18 2019-03-14 49 | 13 000990 诚志股份 10.0121 ... 0.12 0.12 2019-03-14 50 | 14 002063 远光软件 -9.9828 ... 0.10 0.22 2019-03-14 51 | 15 002098 浔兴股份 10.0105 ... 0.18 0.06 2019-03-14 52 | 16 002122 *ST天马 4.9242 ... 0.70 0.46 2019-03-14 53 | 17 002157 正邦科技 -9.9765 ... 0.15 0.14 2019-03-14 54 | 18 002297 博云新材 -5.7803 ... 0.06 0.19 2019-03-14 55 | 19 002299 圣农发展 -10 ... 0.20 0.25 2019-03-14 56 | 20 002356 赫美集团 10.0145 ... 0.30 0.57 2019-03-14 57 | 21 002458 益生股份 -10.0041 ... 0.15 0.18 2019-03-14 58 | 22 002495 佳隆股份 10 ... 0.10 0.14 2019-03-14 59 | 23 002495 佳隆股份 10 ... 0.07 0.14 2019-03-14 60 | 24 002535 林州重机 9.9138 ... 0.11 0.08 2019-03-14 61 | 25 002535 林州重机 9.9138 ... 0.09 0.07 2019-03-14 62 | 26 002565 顺灏股份 -9.9783 ... 0.06 0.16 2019-03-14 63 | 27 002617 露笑科技 6.8273 ... 0.07 0.07 2019-03-14 64 | 28 002684 猛狮科技 9.9863 ... 0.06 0.05 2019-03-14 65 | 29 002733 雄韬股份 -9.7463 ... 0.05 0.26 2019-03-14 66 | .. ... ... ... ... ... ... ... 67 | 42 300313 天山生物 9.9863 ... 0.14 0.10 2019-03-14 68 | 43 300531 优博讯 10.0086 ... 0.13 0.15 2019-03-14 69 | 44 300531 优博讯 10.0086 ... 0.10 0.14 2019-03-14 70 | 45 300579 数字认证 0.2642 ... 0.14 0.12 2019-03-14 71 | 46 300647 超频三 10 ... 0.08 0.05 2019-03-14 72 | 47 300647 超频三 10 ... 0.06 0.05 2019-03-14 73 | 48 300647 超频三 10 ... 0.08 0.05 2019-03-14 74 | 49 300648 星云股份 9.986 ... 0.24 0.26 2019-03-14 75 | 50 300658 延江股份 8.2759 ... 0.28 0.17 2019-03-14 76 | 51 300713 英可瑞 10 ... 0.12 0.08 2019-03-14 77 | 52 300741 华宝股份 9.4552 ... 0.12 0.12 2019-03-14 78 | 53 300741 华宝股份 9.4552 ... 0.09 0.14 2019-03-14 79 | 54 300746 汉嘉设计 -1.8275 ... 0.04 0.07 2019-03-14 80 | 55 300762 上海瀚讯 43.9803 ... 0.99 0.17 2019-03-14 81 | 56 600064 南京高科 4.6434 ... 0.05 0.04 2019-03-14 82 | 57 600067 冠城大通 10.1099 ... 0.09 0.14 2019-03-14 83 | 58 600290 华仪电气 10.0575 ... 0.06 0.04 2019-03-14 84 | 59 600375 华菱星马 -10.0424 ... 0.12 0.13 2019-03-14 85 | 60 600391 航发科技 2.3588 ... 0.06 0.05 2019-03-14 86 | 61 600421 ST仰帆 5.0476 ... 0.35 0.36 2019-03-14 87 | 62 600698 湖南天雁 10.0939 ... 0.10 0.09 2019-03-14 88 | 63 600825 新华传媒 -10.0522 ... 0.11 0.10 2019-03-14 89 | 64 600836 界龙实业 -10.0639 ... 0.05 0.08 2019-03-14 90 | 65 600856 中天能源 10.0642 ... 0.22 0.12 2019-03-14 91 | 66 600895 张江高科 -5.6013 ... 0.06 0.14 2019-03-14 92 | 67 601066 中信建投 2.4505 ... 0.05 0.06 2019-03-14 93 | 68 601865 福莱特 4.3593 ... 0.08 0.05 2019-03-14 94 | 69 603956 威派格 3.6736 ... 0.09 0.10 2019-03-14 95 | 70 603956 威派格 3.6736 ... 0.06 0.13 2019-03-14 96 | 71 603997 继峰股份 8.506 ... 0.14 0.26 2019-03-14 97 | 98 | [b](2) countTops(days=5, retry=3, pause=0.001)[/b] 99 | 该方法用于获取个股上榜统计数据 100 | 101 | 参数: 102 | days:int 103 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 104 | retry : int, 默认 3 105 | 如遇网络等问题重复执行的次数 106 | pause : int, 默认 0 107 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 108 | 109 | 返回值: 110 | DataFrame or List: [{'code':, 'name':, ...}, ...] 111 | code:代码 112 | name:名称 113 | count:上榜次数 114 | bamount:累积购买额(万) 115 | samount:累积卖出额(万) 116 | net:净额(万) 117 | bcount:买入席位数 118 | scount:卖出席位数 119 | 120 | 示例: 121 | [i]import gugu as gg[/i] 122 | [i]obj = gg.BillBoard()[/i] # 创建龙虎榜类对象 123 | [i]data = obj.countTops()[/i] # 获取5天以来个股上榜统计数据 124 | [i]obj.output()[/i] # 输出数据 125 | 126 | 输出: 127 | code name count bamount samount net bcount scount 128 | 0 000839 中信国安 2 185278.34 88044.47 97233.87 11 12 129 | 1 300033 同花顺 1 56049.88 17746.81 38303.07 5 5 130 | 2 600460 士兰微 1 57934.75 33395.57 24539.18 5 5 131 | 3 600226 瀚叶股份 1 40676.28 23854.35 16821.93 7 8 132 | 4 002437 誉衡药业 4 55466.03 42309.98 13156.05 17 15 133 | 5 600776 东方通信 1 32290.53 20463.57 11826.96 5 5 134 | 6 300759 康龙化成 1 14702.77 3101.34 11601.43 4 5 135 | 7 300539 横河模具 1 21578.25 10277.09 11301.16 6 7 136 | 8 603421 鼎信通讯 1 28292.73 17053.28 11239.45 5 5 137 | 9 600928 西安银行 2 34205.87 23062.17 11143.70 7 10 138 | 10 002157 正邦科技 3 142652.44 132041.46 10610.98 10 11 139 | 11 002477 雏鹰农牧 1 17391.66 6814.80 10576.85 5 5 140 | 12 000789 万年青 1 24090.93 13751.37 10339.56 5 5 141 | 13 000723 美锦能源 1 37948.81 27882.26 10066.55 5 5 142 | 14 002371 北方华创 2 58386.53 48543.80 9842.73 4 8 143 | 15 002234 民和股份 2 40395.94 30733.16 9662.78 6 7 144 | 16 600624 复旦复华 2 27700.92 18382.01 9318.91 12 13 145 | 17 002011 盾安环境 1 14046.48 5563.80 8482.68 5 5 146 | 18 601865 福莱特 4 42797.31 34331.59 8465.72 19 20 147 | 19 300170 汉得信息 2 72541.46 64107.22 8434.25 9 9 148 | 20 300085 银之杰 1 18366.90 10016.27 8350.64 5 5 149 | 21 000811 冰轮环境 1 11498.87 3198.64 8300.23 3 5 150 | 22 002175 东方网络 3 57069.06 49214.26 7854.80 13 13 151 | 23 000911 南宁糖业 1 16792.65 9334.93 7457.72 4 5 152 | 24 600136 当代明诚 1 18979.34 11654.13 7325.21 4 4 153 | 25 600351 亚宝药业 1 17117.69 10080.38 7037.31 5 5 154 | 26 300139 晓程科技 3 34917.97 28003.32 6914.65 14 15 155 | 27 000993 闽东电力 1 9096.54 2360.70 6735.84 5 5 156 | 28 600290 华仪电气 4 45020.57 38313.65 6706.92 17 13 157 | 29 000955 欣龙控股 1 9323.05 2627.40 6695.65 5 5 158 | .. ... ... ... ... ... ... ... ... 159 | 181 002285 世联行 1 36121.36 45139.87 -9018.51 5 5 160 | 182 002063 远光软件 2 31970.32 41556.75 -9586.43 10 10 161 | 183 601615 明阳智能 4 54864.13 65211.15 -10347.02 16 16 162 | 184 000936 华西股份 1 9419.73 20157.73 -10738.00 5 5 163 | 185 300423 鲁亿通 1 7542.14 18353.11 -10810.98 5 5 164 | 186 002750 龙津药业 3 42876.44 53852.29 -10975.85 13 15 165 | 187 002565 顺灏股份 2 22960.74 33947.59 -10986.85 10 10 166 | 188 600406 国电南瑞 1 35716.15 48103.03 -12386.88 2 4 167 | 189 000670 盈方微 4 79982.54 92803.81 -12821.28 21 18 168 | 190 600604 市北高新 3 88560.02 102294.33 -13734.31 11 11 169 | 191 600131 岷江水电 3 85588.14 101030.16 -15442.02 8 3 170 | 192 002495 佳隆股份 2 21184.12 37609.32 -16425.20 12 12 171 | 193 603000 人民网 1 95506.23 112655.55 -17149.32 7 8 172 | 194 002765 蓝黛传动 1 4521.48 23037.72 -18516.23 5 5 173 | 195 002916 深南电路 1 17082.55 36509.67 -19427.12 4 5 174 | 196 000971 高升控股 4 30247.64 50389.63 -20141.99 16 15 175 | 197 000876 新希望 2 49836.70 71504.40 -21667.71 7 4 176 | 198 600536 中国软件 2 59752.89 82138.78 -22385.89 8 8 177 | 199 000981 银亿股份 4 59877.03 82582.80 -22705.78 18 16 178 | 200 002124 天邦股份 2 25492.14 49795.70 -24303.55 8 10 179 | 201 002600 领益智造 1 14520.75 41767.35 -27246.61 5 4 180 | 202 000818 航锦科技 1 28452.20 58672.02 -30219.82 5 5 181 | 203 002733 雄韬股份 1 7681.49 38178.68 -30497.19 5 5 182 | 204 002356 赫美集团 3 39324.12 71314.55 -31990.43 14 14 183 | 205 300498 温氏股份 1 76078.84 110188.18 -34109.34 4 5 184 | 206 000050 深天马A 1 25847.90 62577.52 -36729.62 5 5 185 | 207 600756 浪潮软件 2 23801.21 60848.62 -37047.41 10 10 186 | 208 601519 大智慧 1 14927.01 53884.71 -38957.70 5 5 187 | 209 300280 紫天科技 1 27459.50 113345.08 -85885.58 8 9 188 | 210 300017 网宿科技 4 287853.96 443788.57 -155934.62 14 15 189 | 190 | [b](3) brokerTops(days=5, retry=3, pause=0.001)[/b] 191 | 该方法用于获取营业部上榜统计数据 192 | 193 | 参数: 194 | days:int 195 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 196 | retry : int, 默认 3 197 | 如遇网络等问题重复执行的次数 198 | pause : int, 默认 0 199 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 200 | 201 | 返回值: 202 | DataFrame or List: [{'broker':, 'count':, ...}, ...] 203 | broker:营业部名称 204 | count:上榜次数 205 | bamount:累积购买额(万) 206 | bcount:买入席位数 207 | samount:累积卖出额(万) 208 | scount:卖出席位数 209 | top3:买入前三股票 210 | 211 | 示例: 212 | [i]import gugu as gg[/i] 213 | [i]obj = gg.BillBoard()[/i] # 创建龙虎榜类对象 214 | [i]data = obj.brokerTops()[/i] # 获取5天以来营业部上榜统计数据 215 | [i]obj.output()[/i] # 输出数据 216 | 217 | 输出: 218 | broker count ... scount top3 219 | 0 中国银河证券股份有限公司杭州新塘路证券营业部 3 ... 1 深天马A,新联电子,领益智造 220 | 1 中信证券股份有限公司长兴明珠路证券营业部 5 ... 3 盈方微,朗源股份 221 | 2 西南证券股份有限公司杭州庆春东路证券营业部 4 ... 2 深天马A,远光软件,领益智造 222 | 3 长城证券股份有限公司仙桃钱沟路证券营业部 8 ... 4 盈方微,东信和平,东方网络, 223 | 4 光大证券股份有限公司杭州庆春路证券营业部 2 ... 1 深天马A,领益智造 224 | 5 中信证券股份有限公司深圳总部证券营业部 12 ... 7 盈方微,民和股份,仙坛股份, 225 | 6 西藏东方财富证券股份有限公司拉萨团结路第二证券营业部 192 ... 71 士兰微,中信建投,东方通信, 226 | 7 中信证券股份有限公司杭州延安路证券营业部 10 ... 2 岷江水电,名家汇,新联电子, 227 | 8 兴业证券股份有限公司陕西分公司 5 ... 1 中信国安,士兰微,深天马A 228 | 9 中国中投证券有限责任公司深圳分公司 2 ... 1 盈方微 229 | 10 中信证券股份有限公司上海牡丹江路证券营业部 18 ... 6 安控科技,汉得信息,同花顺, 230 | 11 中信证券(山东)有限责任公司淄博分公司 12 ... 7 盈方微,弘高创意,林州重机, 231 | 12 华泰证券股份有限公司吴江盛泽镇市场路证券营业部 6 ... 4 网宿科技,深天马A 232 | 13 中国中投证券有限责任公司杭州环球中心证券营业部 7 ... 4 盈方微,安控科技,高升控股 233 | 14 招商证券股份有限公司哈尔滨长江路证券营业部 3 ... 2 市北高新,深天马A 234 | 15 安信证券股份有限公司南昌胜利路证券营业部 17 ... 8 盈方微,利欧股份,银亿股份, 235 | 16 深股通专用 71 ... 42 正邦科技,同花顺,网宿科技, 236 | 17 东兴证券股份有限公司重庆邹容路证券营业部 3 ... 3 盈方微,蓝黛传动 237 | 18 光大证券股份有限公司北京光华路证券营业部 2 ... 2 东信和平,深天马A 238 | 19 第一创业证券股份有限公司上海世纪大道证券营业部 2 ... 2 盈方微 239 | 20 国信证券股份有限公司杭州分公司 1 ... 0 华东科技 240 | 21 金元证券股份有限公司三亚解放路证券营业部 1 ... 0 华东科技 241 | 22 平安证券股份有限公司福州长乐北路证券营业部 30 ... 13 紫天科技,上海莱士,华东科技, 242 | 23 中国中投证券有限责任公司北京北三环东路证券营业部 3 ... 3 东方电子,华东科技,东信和平 243 | 24 中泰证券股份有限公司湖南分公司 2 ... 2 华东科技,科创信息 244 | 25 华泰证券股份有限公司常州和平北路证券营业部 3 ... 2 航锦科技,亚宝药业 245 | 26 万联证券股份有限公司南昌丰和中大道证券营业部 1 ... 0 创维数字 246 | 27 海通证券股份有限公司嘉兴中山西路证券营业部 1 ... 0 国风塑业 247 | 28 西南证券股份有限公司上海普陀区平利路证券营业部 1 ... 0 创维数字 248 | 29 中泰证券股份有限公司深圳分公司 15 ... 8 华控赛格,航锦科技,电广传媒, 249 | ... ... ... ... ... ... 250 | 1192 中国银河证券股份有限公司上海宜川路证券营业部 1 ... 0 金通灵 251 | 1193 华泰证券股份有限公司南通人民中路证券营业部 1 ... 0 金通灵 252 | 1194 广发证券股份有限公司南京水佑岗证券营业部 1 ... 1 上海钢联 253 | 1195 中信证券股份有限公司北京北三环中路证券营业部 1 ... 1 金通灵 254 | 1196 申万宏源证券有限公司上海杨浦区平凉路证券营业部 1 ... 1 上海钢联 255 | 1197 招商证券股份有限公司上海浦东新区浦东南路证券营业部 2 ... 1 上海钢联 256 | 1198 广发证券股份有限公司上海东方路证券营业部 2 ... 2 上海钢联,光一科技 257 | 1199 国泰君安证券股份有限公司成都双庆路证券营业部 1 ... 0 珈伟新能 258 | 1200 华福证券有限责任公司福清清昌大道证券营业部 1 ... 0 珈伟新能 259 | 1201 国海证券股份有限公司郑州商务外环路证券营业部 1 ... 0 银邦股份 260 | 1202 开源证券股份有限公司江苏分公司 1 ... 0 光一科技 261 | 1203 申万宏源证券有限公司宁波分公司 1 ... 1 光一科技 262 | 1204 华泰证券股份有限公司启东人民中路证券营业部 1 ... 1 光一科技 263 | 1205 广发证券股份有限公司杭州学院路证券营业部 1 ... 0 厚普股份 264 | 1206 兴业证券股份有限公司厦门吕岭路证券营业部 1 ... 0 安控科技 265 | 1207 安信证券股份有限公司杭州莫干山路证券营业部 1 ... 0 安控科技 266 | 1208 国信证券股份有限公司深圳南山科苑北路证券营业部 1 ... 1 厚普股份 267 | 1209 中泰证券股份有限公司上海虹梅路证券营业部 1 ... 1 名家汇 268 | 1210 财通证券股份有限公司温州人民东路证券营业部 1 ... 1 名家汇 269 | 1211 开源证券股份有限公司绍兴胜利东路证券营业部 1 ... 0 名家汇 270 | 1212 广发证券股份有限公司南昌北京西路证券营业部 1 ... 0 友讯达 271 | 1213 南京证券股份有限公司盐城解放南路证券营业部 1 ... 1 友讯达 272 | 1214 国泰君安证券股份有限公司上海威海路证券营业部 1 ... 1 友讯达 273 | 1215 长江证券股份有限公司成都光华村街证券营业部 1 ... 1 天宇股份 274 | 1216 财通证券股份有限公司新昌环城南路证券营业部 1 ... 1 天宇股份 275 | 1217 中国银河证券股份有限公司北京安贞门证券营业部 1 ... 1 天宇股份 276 | 1218 浙商证券股份有限公司台州环城东路证券营业部 1 ... 0 天宇股份 277 | 1219 华林证券股份有限公司温州车站大道证券营业部 1 ... 1 康龙化成 278 | 1220 光大证券股份有限公司哈尔滨经纬二道街证券营业部 1 ... 1 康龙化成 279 | 1221 招商证券股份有限公司昆明北京路证券营业部 1 ... 1 康龙化成 280 | 281 | [b](4) instTops(days=5, retry=3, pause=0.001)[/b] 282 | 该方法用于获取机构席位追踪统计数据 283 | 284 | 参数: 285 | days:int 286 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 287 | retry : int, 默认 3 288 | 如遇网络等问题重复执行的次数 289 | pause : int, 默认 0 290 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 291 | 292 | 返回值: 293 | DataFrame or List: [{'code':, 'name':, ...}, ...] 294 | code:代码 295 | name:名称 296 | bamount:累积买入额(万) 297 | bcount:买入次数 298 | samount:累积卖出额(万) 299 | scount:卖出次数 300 | net:净额(万) 301 | 302 | 示例: 303 | [i]import gugu as gg[/i] 304 | [i]obj = gg.BillBoard()[/i] # 创建龙虎榜类对象 305 | [i]data = obj.instTops()[/i] # 获取5天以来机构席位追踪统计数据 306 | [i]obj.output()[/i] # 输出数据 307 | 308 | 输出: 309 | code name bamount bcount samount scount net 310 | 0 002371 北方华创 32126.16 7 5931.07 2 26195.09 311 | 1 300498 温氏股份 21335.70 2 0.00 0 21335.70 312 | 2 300579 数字认证 18957.89 9 2271.66 2 16686.23 313 | 3 002234 民和股份 23293.14 5 8699.70 4 14593.44 314 | 4 000811 冰轮环境 9610.98 3 772.70 1 8838.28 315 | 5 600136 当代明诚 10725.61 2 3563.43 2 7162.18 316 | 6 600895 张江高科 5999.70 1 0.00 0 5999.70 317 | 7 000917 电广传媒 5873.86 2 0.00 0 5873.86 318 | 8 002157 正邦科技 23843.19 3 18168.60 3 5674.59 319 | 9 600406 国电南瑞 28758.53 4 23127.01 2 5631.52 320 | 10 300759 康龙化成 5550.81 2 0.00 0 5550.81 321 | 11 603707 健友股份 10321.90 1 4815.93 2 5505.97 322 | 12 002100 天康生物 5038.92 3 0.00 0 5038.92 323 | 13 000911 南宁糖业 4935.66 2 0.00 0 4935.66 324 | 14 300226 上海钢联 4665.78 2 0.00 0 4665.78 325 | 15 300033 同花顺 4527.03 1 0.00 0 4527.03 326 | 16 600351 亚宝药业 4153.03 1 0.00 0 4153.03 327 | 17 300017 网宿科技 4113.60 1 0.00 0 4113.60 328 | 18 002124 天邦股份 7702.17 3 5339.92 1 2362.25 329 | 19 002063 远光软件 2235.37 1 0.00 0 2235.37 330 | 20 000670 盈方微 1999.91 1 0.00 0 1999.91 331 | 21 300556 丝路视觉 1502.01 2 0.00 0 1502.01 332 | 22 300702 天宇股份 3060.30 4 1581.97 2 1478.33 333 | 23 603556 海兴电力 3764.91 1 2347.41 1 1417.50 334 | 24 002881 美格智能 1301.93 1 0.00 0 1301.93 335 | 25 300730 科创信息 1048.88 1 0.00 0 1048.88 336 | 26 300531 优博讯 4399.45 2 3678.28 1 721.17 337 | 27 002902 铭普光磁 3383.84 2 2665.19 1 718.65 338 | 28 300356 光一科技 636.86 1 0.00 0 636.86 339 | 29 002169 智光电气 2092.84 2 1472.77 1 620.07 340 | .. ... ... ... ... ... ... ... 341 | 58 600064 南京高科 0.00 0 2049.40 1 -2049.40 342 | 59 002437 誉衡药业 0.00 0 2067.38 2 -2067.38 343 | 60 300467 迅游科技 1655.93 2 3765.22 3 -2109.29 344 | 61 601018 宁波港 0.00 0 2162.12 1 -2162.12 345 | 62 000839 中信国安 0.00 0 2374.96 1 -2374.96 346 | 63 002297 博云新材 1493.13 1 4084.00 1 -2590.87 347 | 64 002916 深南电路 5551.01 2 8202.50 1 -2651.49 348 | 65 601929 吉视传媒 0.00 0 3078.90 1 -3078.90 349 | 66 300648 星云股份 0.00 0 3879.39 4 -3879.39 350 | 67 600536 中国软件 9720.14 2 13965.62 2 -4245.48 351 | 68 300170 汉得信息 0.00 0 4354.23 1 -4354.23 352 | 69 601311 骆驼股份 0.00 0 4500.47 1 -4500.47 353 | 70 601179 中国西电 426.53 1 5078.01 1 -4651.48 354 | 71 002458 益生股份 6437.52 2 12203.17 5 -5765.65 355 | 72 002746 仙坛股份 1489.78 1 7511.47 4 -6021.69 356 | 73 000971 高升控股 0.00 0 6097.40 2 -6097.40 357 | 74 300365 恒华科技 9275.05 3 15536.22 4 -6261.17 358 | 75 002321 华英农业 0.00 0 7284.27 3 -7284.27 359 | 76 600975 新五丰 5128.61 2 12790.76 2 -7662.15 360 | 77 600460 士兰微 0.00 0 8157.19 1 -8157.19 361 | 78 002546 新联电子 0.00 0 8882.73 2 -8882.73 362 | 79 603859 能科股份 0.00 0 10280.58 3 -10280.58 363 | 80 600667 太极实业 0.00 0 10798.50 1 -10798.50 364 | 81 000876 新希望 14797.46 2 28174.69 5 -13377.23 365 | 82 000977 浪潮信息 0.00 0 13525.94 2 -13525.94 366 | 83 000981 银亿股份 0.00 0 19470.15 3 -19470.15 367 | 84 002600 领益智造 0.00 0 19976.04 2 -19976.04 368 | 85 000860 顺鑫农业 0.00 0 21980.88 3 -21980.88 369 | 86 002299 圣农发展 7466.80 2 29453.64 7 -21986.84 370 | 87 600131 岷江水电 42442.44 6 96539.94 13 -54097.50 371 | 372 | [b](5) instDetail(retry=3, pause=0.001)[/b] 373 | 该方法用于获取最近一个交易日机构席位成交明细统计数据 374 | 375 | 参数: 376 | retry : int, 默认 3 377 | 如遇网络等问题重复执行的次数 378 | pause : int, 默认 0 379 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 380 | 381 | 返回值: 382 | DataFrame or List: [{'code':, 'name':, ...}, ...] 383 | code:股票代码 384 | name:股票名称 385 | date:交易日期 386 | bamount:机构席位买入额(万) 387 | samount:机构席位卖出额(万) 388 | type:类型 389 | 390 | 示例: 391 | [i]import gugu as gg[/i] 392 | [i]obj = gg.BillBoard()[/i] # 创建龙虎榜类对象 393 | [i]data = obj.instDetail()[/i] # 获取最近一个交易日机构席位成交明细统计数据 394 | [i]obj.output()[/i] # 输出数据 395 | 396 | 输出: 397 | code name ... samount type 398 | 0 000981 银亿股份 ... 1444.62 振幅值达15%的证券 399 | 1 000981 银亿股份 ... 1444.62 跌幅偏离值达7%的证券 400 | 2 000860 顺鑫农业 ... 21980.88 涨幅偏离值达7%的证券 401 | 3 300759 康龙化成 ... 0.00 换手率达20%的证券 402 | 4 300759 康龙化成 ... 0.00 涨幅偏离值达7%的证券 403 | 5 000917 电广传媒 ... 0.00 涨幅偏离值达7%的证券 404 | 6 300702 天宇股份 ... 1581.97 跌幅偏离值达7%的证券 405 | 7 000723 美锦能源 ... 1983.83 连续三个交易日内,涨幅偏离值累计达20%的证券 406 | 8 000811 冰轮环境 ... 772.70 涨幅偏离值达7%的证券 407 | 9 000670 盈方微 ... 0.00 换手率达20%的证券 408 | 10 000670 盈方微 ... 0.00 振幅值达15%的证券 409 | 11 600131 岷江水电 ... 40098.22 连续三个交易日内,跌幅偏离值累计达20%的证券 410 | 12 300471 厚普股份 ... 572.62 连续三个交易日内,涨幅偏离值累计达20%的证券 411 | 13 300356 光一科技 ... 0.00 跌幅偏离值达7%的证券 412 | 14 300337 银邦股份 ... 1074.71 振幅值达15%的证券 413 | 15 300226 上海钢联 ... 0.00 涨幅偏离值达7%的证券 414 | 16 300018 中元股份 ... 298.00 涨幅偏离值达7%的证券 415 | 17 600536 中国软件 ... 6878.07 跌幅偏离值达7%的证券 416 | 18 300017 网宿科技 ... 0.00 跌幅偏离值达7%的证券 417 | 19 002950 奥美医疗 ... 11.76 连续三个交易日内,涨幅偏离值累计达20%的证券 418 | 20 002437 誉衡药业 ... 1092.65 跌幅偏离值达7%的证券 419 | 21 002077 大港股份 ... 1106.43 振幅值达15%的证券 420 | 22 002057 中钢天源 ... 631.06 涨幅偏离值达7%的证券 421 | 23 002299 圣农发展 ... 14878.42 跌幅偏离值达7%的证券 422 | 24 300648 星云股份 ... 3879.39 涨幅偏离值达7%的证券 423 | 25 002297 博云新材 ... 4084.00 振幅值达15%的证券 424 | 26 002098 浔兴股份 ... 391.55 涨幅偏离值达7%的证券 425 | 27 002157 正邦科技 ... 4530.75 跌幅偏离值达7%的证券 426 | 28 300579 数字认证 ... 1481.96 连续三个交易日内,涨幅偏离值累计达20%的证券 427 | 29 002063 远光软件 ... 0.00 跌幅偏离值达7%的证券 428 | .. ... ... ... ... ... 429 | 90 300761 立华股份 ... 0.00 换手率达20%的证券 430 | 91 000971 高升控股 ... 2826.00 换手率达20%的证券 431 | 92 000971 高升控股 ... 2826.00 涨幅偏离值达7%的证券 432 | 93 300498 温氏股份 ... 0.00 涨幅偏离值达7%的证券 433 | 94 000981 银亿股份 ... 1184.43 涨幅偏离值达7%的证券 434 | 95 300556 丝路视觉 ... 0.00 振幅值达15%的证券 435 | 96 300423 鲁亿通 ... 2039.72 跌幅偏离值达7%的证券 436 | 97 002437 誉衡药业 ... 974.73 振幅值达15%的证券 437 | 98 002600 领益智造 ... 19976.04 跌幅偏离值达7%的证券 438 | 99 600667 太极实业 ... 10798.50 连续三个交易日内,涨幅偏离值累计达20%的证券 439 | 100 002371 北方华创 ... 3463.15 振幅值达15%的证券 440 | 101 600351 亚宝药业 ... 0.00 连续三个交易日内,涨幅偏离值累计达20%的证券 441 | 102 603707 健友股份 ... 4815.93 连续三个交易日内,涨幅偏离值累计达20%的证券 442 | 103 600131 岷江水电 ... 19286.13 连续三个交易日内,涨幅偏离值累计达20%的证券 443 | 104 002175 东方网络 ... 770.89 振幅值达15%的证券 444 | 105 002234 民和股份 ... 2288.13 连续三个交易日内,涨幅偏离值累计达20%的证券 445 | 106 600928 西安银行 ... 1312.96 换手率达20%的证券 446 | 107 601018 宁波港 ... 2162.12 涨幅偏离值达7%的证券 447 | 108 000971 高升控股 ... 3271.40 换手率达20%的证券 448 | 109 601179 中国西电 ... 5078.01 涨幅偏离值达7%的证券 449 | 110 000971 高升控股 ... 3271.40 振幅值达15%的证券 450 | 111 000971 高升控股 ... 3271.40 跌幅偏离值达7%的证券 451 | 112 300730 科创信息 ... 0.00 换手率达20%的证券 452 | 113 300674 宇信科技 ... 1231.53 换手率达20%的证券 453 | 114 300659 中孚信息 ... 2358.72 振幅值达15%的证券 454 | 115 000810 创维数字 ... 911.03 跌幅偏离值达7%的证券 455 | 116 300195 长荣股份 ... 1739.94 连续三个交易日内,涨幅偏离值累计达20%的证券 456 | 117 300249 依米康 ... 1011.68 跌幅偏离值达7%的证券 457 | 118 002949 华阳国际 ... 1394.06 连续三个交易日内,日均换手率与前五个交易日的日均换手率的比值达到30倍,且换手率累计达20%的股票 458 | 119 002949 华阳国际 ... 1394.06 换手率达20%的证券 -------------------------------------------------------------------------------- /gugu/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | __version__ = '0.1.8' 4 | __author__ = 'TabQ' 5 | 6 | 7 | from gugu.base import Base 8 | 9 | from gugu.billboard import BillBoard 10 | 11 | from gugu.classify import Classify 12 | 13 | from gugu.lowriskintarb import LowRiskIntArb 14 | 15 | from gugu.macro import Macro 16 | 17 | from gugu.marketdata import MarketData 18 | 19 | from gugu.reference import Reference 20 | 21 | from gugu.stockdata import StockData 22 | 23 | from gugu.stockinfo import StockInfo 24 | 25 | from gugu.utility import Utility 26 | 27 | -------------------------------------------------------------------------------- /gugu/base.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 基础类 4 | Created on 2018/12/27 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | 10 | import pandas as pd 11 | import sys 12 | import requests 13 | from requests.adapters import HTTPAdapter 14 | from urllib3.util import Retry 15 | import gugu.config as cf 16 | 17 | 18 | class Base(): 19 | def __init__(self, pandas=True, inter=True): 20 | self.__pandas = False if not pandas else True 21 | self._PY3 = (sys.version_info[0] >= 3) 22 | self._inter = False if not inter else True # 交互模式 23 | 24 | self._session = requests.Session() 25 | retry = Retry(connect=5, backoff_factor=1) 26 | adapter = HTTPAdapter(max_retries=retry) 27 | self._session.mount('http://', adapter) 28 | self._session.mount('https://', adapter) 29 | self._session.keep_alive = False 30 | 31 | self._data = pd.DataFrame() 32 | 33 | 34 | def _result(self): 35 | """ 36 | 返回结果:使用pandas时返回DataFrame否则返回list 37 | Parameters 38 | ----- 39 | dataframe:pandas.DataFrame 40 | index:string 41 | 返回dict时选用的键 e.g. code 42 | return 43 | ----- 44 | pandas.DataFrame or dict 45 | """ 46 | if self._data.empty: 47 | return None 48 | elif self.__pandas: 49 | return self._data 50 | else: 51 | return self._data.to_dict('records') 52 | 53 | 54 | def output(self, full=False): 55 | print('') 56 | 57 | if not full: 58 | print(self._data) 59 | else: 60 | pd.set_option('display.max_columns', None) 61 | pd.set_option('display.max_rows', None) 62 | pd.set_option('max_colwidth',100) 63 | 64 | print(self._data) 65 | 66 | 67 | def getPandas(self): 68 | return self.__pandas 69 | 70 | 71 | def setPandas(self, pandas=False): 72 | self.__pandas = pandas 73 | 74 | 75 | def getInter(self): 76 | """ 77 | 获取交互模式状态 78 | return 79 | ------ 80 | True or False 81 | """ 82 | return self._inter 83 | 84 | 85 | def setInter(self, inter=False): 86 | """ 87 | 设置交互模式 88 | Parameters 89 | ------ 90 | inter: bool 91 | """ 92 | self._inter = inter 93 | 94 | 95 | def _writeHead(self): 96 | if self._inter: 97 | sys.stdout.write(cf.GETTING_TIPS) 98 | sys.stdout.flush() 99 | 100 | 101 | def _writeConsole(self): 102 | if self._inter: 103 | sys.stdout.write(cf.GETTING_FLAG) 104 | sys.stdout.flush() 105 | 106 | 107 | -------------------------------------------------------------------------------- /gugu/billboard.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 龙虎榜类 4 | Created on 2019/01/11 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | 10 | import time 11 | import re 12 | import pandas as pd 13 | from pandas.compat import StringIO 14 | import lxml.html 15 | from lxml import etree 16 | from gugu.base import Base, cf 17 | from gugu.utility import Utility 18 | 19 | class BillBoard(Base): 20 | def topList(self, date=None, retry=3, pause=0.001): 21 | """ 22 | 获取每日龙虎榜列表 23 | Parameters 24 | -------- 25 | date:string 26 | 明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据 27 | retry : int, 默认 3 28 | 如遇网络等问题重复执行的次数 29 | pause : int, 默认 0 30 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 31 | 32 | Return 33 | ------ 34 | DataFrame or List: [{'code':, 'name':, ...}, ...] 35 | code:代码 36 | name :名称 37 | pchange:涨跌幅 38 | amount:龙虎榜成交额(万) 39 | buy:买入额(万) 40 | bratio:占总成交比例 41 | sell:卖出额(万) 42 | sratio :占总成交比例 43 | reason:上榜原因 44 | unscramble: 解读 45 | date :日期 46 | """ 47 | self._data = pd.DataFrame() 48 | 49 | if date is None: 50 | if Utility.getHour() < 18: 51 | date = Utility.lastTradeDate() 52 | else: 53 | date = Utility.getToday() 54 | else: 55 | if not Utility.isTradeDay(date): 56 | return None 57 | 58 | for _ in range(retry): 59 | time.sleep(pause) 60 | 61 | try: 62 | # http://data.eastmoney.com/DataCenter_V3/stock2016/TradeDetail/pagesize=200,page=1,sortRule=-1,sortType=,startDate=2019-01-10,endDate=2019-01-10,gpfw=0,js=vardata_tab_1.html 63 | request = self._session.get( cf.LHB_URL % (date, date), timeout=10 ) 64 | request.encoding = 'gbk' 65 | text = request.text.split('_1=')[1] 66 | dataDict = Utility.str2Dict(text) 67 | 68 | self._data = pd.DataFrame(dataDict['data'], columns=cf.LHB_TMP_COLS) 69 | self._data.columns = cf.LHB_COLS 70 | self._data['buy'] = self._data['buy'].astype(float) 71 | self._data['sell'] = self._data['sell'].astype(float) 72 | self._data['amount'] = self._data['amount'].astype(float) 73 | self._data['Turnover'] = self._data['Turnover'].astype(float) 74 | self._data['bratio'] = self._data['buy'] / self._data['Turnover'] 75 | self._data['sratio'] = self._data['sell'] / self._data['Turnover'] 76 | self._data['bratio'] = self._data['bratio'].map(cf.FORMAT) 77 | self._data['sratio'] = self._data['sratio'].map(cf.FORMAT) 78 | self._data['date'] = date 79 | for col in ['amount', 'buy', 'sell']: 80 | self._data[col] = self._data[col].astype(float) 81 | self._data[col] = self._data[col] / 10000 82 | self._data[col] = self._data[col].map(cf.FORMAT) 83 | self._data = self._data.drop('Turnover', axis=1) 84 | except: 85 | pass 86 | else: 87 | return self._result() 88 | 89 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 90 | 91 | 92 | def countTops(self, days=5, retry=3, pause=0.001): 93 | """ 94 | 获取个股上榜统计数据 95 | Parameters 96 | -------- 97 | days:int 98 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 99 | retry : int, 默认 3 100 | 如遇网络等问题重复执行的次数 101 | pause : int, 默认 0 102 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 103 | Return 104 | ------ 105 | DataFrame or List: [{'code':, 'name':, ...}, ...] 106 | code:代码 107 | name:名称 108 | count:上榜次数 109 | bamount:累积购买额(万) 110 | samount:累积卖出额(万) 111 | net:净额(万) 112 | bcount:买入席位数 113 | scount:卖出席位数 114 | """ 115 | self._data = pd.DataFrame() 116 | 117 | if Utility.checkLhbInput(days) is True: 118 | self._writeHead() 119 | 120 | # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/ggtj/index.phtml?last=5&p=1 121 | self._data = self.__parsePage(kind=cf.LHB_KINDS[0], last=days, column=cf.LHB_GGTJ_COLS, dataArr=pd.DataFrame(), pageNo=1, retry=retry, pause=pause) 122 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 123 | if self._data is not None: 124 | self._data = self._data.drop_duplicates('code') 125 | 126 | return self._result() 127 | 128 | 129 | def brokerTops(self, days=5, retry=3, pause=0.001): 130 | """ 131 | 获取营业部上榜统计数据 132 | Parameters 133 | -------- 134 | days:int 135 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 136 | retry : int, 默认 3 137 | 如遇网络等问题重复执行的次数 138 | pause : int, 默认 0 139 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 140 | Return 141 | --------- 142 | DataFrame or List: [{'broker':, 'count':, ...}, ...] 143 | broker:营业部名称 144 | count:上榜次数 145 | bamount:累积购买额(万) 146 | bcount:买入席位数 147 | samount:累积卖出额(万) 148 | scount:卖出席位数 149 | top3:买入前三股票 150 | """ 151 | self._data = pd.DataFrame() 152 | 153 | if Utility.checkLhbInput(days) is True: 154 | self._writeHead() 155 | 156 | # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/yytj/index.phtml?last=5&p=1 157 | self._data = self.__parsePage(kind=cf.LHB_KINDS[1], last=days, column=cf.LHB_YYTJ_COLS, dataArr=pd.DataFrame(), pageNo=1, retry=retry, pause=pause) 158 | 159 | return self._result() 160 | 161 | 162 | def instTops(self, days=5, retry=3, pause=0.001): 163 | """ 164 | 获取机构席位追踪统计数据 165 | Parameters 166 | -------- 167 | days:int 168 | 天数,统计n天以来上榜次数,默认为5天,其余是10、30、60 169 | retry : int, 默认 3 170 | 如遇网络等问题重复执行的次数 171 | pause : int, 默认 0 172 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 173 | 174 | Return 175 | -------- 176 | DataFrame or List: [{'code':, 'name':, ...}, ...] 177 | code:代码 178 | name:名称 179 | bamount:累积买入额(万) 180 | bcount:买入次数 181 | samount:累积卖出额(万) 182 | scount:卖出次数 183 | net:净额(万) 184 | """ 185 | self._data = pd.DataFrame() 186 | 187 | if Utility.checkLhbInput(days) is True: 188 | self._writeHead() 189 | 190 | # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/jgzz/index.phtml?last=5&p=1 191 | self._data = self.__parsePage(kind=cf.LHB_KINDS[2], last=days, column=cf.LHB_JGZZ_COLS, dataArr=pd.DataFrame(), pageNo=1, retry=retry, pause=pause, drop_column=[2,3]) 192 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 193 | 194 | return self._result() 195 | 196 | 197 | def instDetail(self, retry=3, pause=0.001): 198 | """ 199 | 获取最近一个交易日机构席位成交明细统计数据 200 | Parameters 201 | -------- 202 | retry : int, 默认 3 203 | 如遇网络等问题重复执行的次数 204 | pause : int, 默认 0 205 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 206 | 207 | Return 208 | ---------- 209 | DataFrame or List: [{'code':, 'name':, ...}, ...] 210 | code:股票代码 211 | name:股票名称 212 | date:交易日期 213 | bamount:机构席位买入额(万) 214 | samount:机构席位卖出额(万) 215 | type:类型 216 | """ 217 | self._data = pd.DataFrame() 218 | 219 | self._writeHead() 220 | 221 | # http://vip.stock.finance.sina.com.cn/q/go.php/vLHBData/kind/jgmx/index.phtml?last=&p=1 222 | self._data = self.__parsePage(kind=cf.LHB_KINDS[3], last='', column=cf.LHB_JGMX_COLS, dataArr=pd.DataFrame(), pageNo=1, retry=retry, pause=pause) 223 | if len(self._data) > 0: 224 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 225 | 226 | return self._result() 227 | 228 | 229 | def __parsePage(self, kind, last, column, dataArr, pageNo=1, retry=3, pause=0.001, drop_column=None): 230 | self._writeConsole() 231 | 232 | for _ in range(retry): 233 | time.sleep(pause) 234 | 235 | try: 236 | request = self._session.get( cf.LHB_SINA_URL % (kind, last, pageNo), timeout=10 ) 237 | request.encoding = 'gbk' 238 | html = lxml.html.parse(StringIO(request.text)) 239 | res = html.xpath("//table[@id=\"dataTable\"]/tr") 240 | if self._PY3: 241 | sarr = [etree.tostring(node).decode('utf-8') for node in res] 242 | else: 243 | sarr = [etree.tostring(node) for node in res] 244 | sarr = ''.join(sarr) 245 | sarr = '%s
'%sarr 246 | df = pd.read_html(sarr)[0] 247 | if drop_column is not None: 248 | df = df.drop(drop_column, axis=1) 249 | df.columns = column 250 | dataArr = dataArr.append(df, ignore_index=True) 251 | nextPage = html.xpath('//div[@class=\"pages\"]/a[last()]/@onclick') 252 | if len(nextPage) > 0: 253 | pageNo = re.findall(r'\d+', nextPage[0])[0] 254 | return self.__parsePage(kind, last, column, dataArr, pageNo, retry, pause, drop_column) 255 | else: 256 | return dataArr 257 | except Exception as e: 258 | print(e) 259 | 260 | 261 | -------------------------------------------------------------------------------- /gugu/classify.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 股票所属类 4 | Created on 2019/01/10 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | import time 10 | import json 11 | import re 12 | import pandas as pd 13 | from gugu.base import Base, cf 14 | 15 | class Classify(Base): 16 | def byIndustry(self, retry=3, pause=0.001): 17 | """ 18 | 获取行业分类数据 19 | Parameters 20 | ---------- 21 | retry : int, 默认 10 22 | 如遇网络等问题重复执行的次数 23 | pause : int, 默认 0.001 24 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 25 | 26 | Returns 27 | ------- 28 | DataFrame or List: [{'code':, 'name':, ...}, ...] 29 | code :股票代码 30 | name :股票名称 31 | c_name :行业名称 32 | """ 33 | self._data = pd.DataFrame() 34 | 35 | # http://vip.stock.finance.sina.com.cn/q/view/newSinaHy.php 36 | df = self.__getTypeData(cf.SINA_INDUSTRY_INDEX_URL % 'newSinaHy.php') 37 | 38 | self._writeHead() 39 | for row in df.values: 40 | rowDf = self.__getDetail(row[0], retry, pause) 41 | rowDf['c_name'] = row[1] 42 | self._data = self._data.append(rowDf, ignore_index=True) 43 | 44 | return self._result() 45 | 46 | 47 | def byConcept(self, retry=3, pause=0.001): 48 | """ 49 | 获取概念分类数据 50 | Parameters 51 | ---------- 52 | retry : int, 默认 3 53 | 如遇网络等问题重复执行的次数 54 | pause : int, 默认 0.001 55 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 56 | Return 57 | -------- 58 | DataFrame 59 | code :股票代码 60 | name :股票名称 61 | c_name :概念名称 62 | """ 63 | self._data = pd.DataFrame() 64 | 65 | self._writeHead() 66 | # http://money.finance.sina.com.cn/q/view/newFLJK.php?param=class 67 | df = self.__getTypeData( cf.SINA_CONCEPTS_INDEX_URL ) 68 | 69 | for row in df.values: 70 | rowDf = self.__getDetail(row[0], retry, pause) 71 | rowDf['c_name'] = row[1] 72 | self._data = self._data.append(rowDf) 73 | 74 | time.sleep(1) 75 | 76 | return self._result() 77 | 78 | 79 | def __getTypeData(self, url): 80 | try: 81 | request = self._session.get(url, timeout=10) 82 | request.encoding = 'gbk' 83 | text = request.text.split('=')[1] 84 | dataJson = json.loads(text) 85 | df = pd.DataFrame([[row.split(',')[0], row.split(',')[1]] for row in dataJson.values()], columns=['tag', 'name']) 86 | 87 | return df 88 | except Exception as e: 89 | print(str(e)) 90 | 91 | def __getDetail(self, tag, retry=3, pause=0.001): 92 | self._writeConsole() 93 | 94 | for retryCount in range(retry): 95 | time.sleep(pause) 96 | 97 | try: 98 | # http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=1000&sort=symbol&asc=1&node=new_zhhy&symbol=&_s_r_a=page 99 | request = self._session.get( cf.SINA_DATA_DETAIL_URL % tag, timeout=10 ) 100 | reg = re.compile(r'\,(.*?)\:') 101 | text = reg.sub(r',"\1":', request.text) 102 | text = text.replace('"{symbol', '{"symbol') 103 | text = text.replace('{symbol', '{"symbol"') 104 | jstr = json.dumps(text) 105 | js = json.loads(jstr) 106 | 107 | df = pd.DataFrame(pd.read_json(js, dtype={'code':object}), columns=cf.FOR_CLASSIFY_B_COLS) 108 | 109 | return df 110 | except: 111 | time.sleep((retryCount+1)*10) 112 | 113 | raise IOError(cf.NETWORK_ERR_MSG) -------------------------------------------------------------------------------- /gugu/lowriskintarb.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 低风险及套利类 4 | Created on 2019/01/30 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | 10 | import re 11 | import json 12 | import pandas as pd 13 | import numpy as np 14 | import time 15 | from gugu.base import Base 16 | import gugu.config as cf 17 | import sys 18 | 19 | class LowRiskIntArb(Base): 20 | def ratingFundA(self): 21 | """ 22 | 分级基金A及其相关数据 23 | return 24 | ------ 25 | DataFrame or List: [{'funda_id':, 'funda_name':, ...}, ...] 26 | funda_id: 分级A代码 27 | funda_name: 分级A名称 28 | funda_current_price: 现价 29 | funda_increase_rt: 涨幅(%) 30 | funda_volume: 成交额 31 | funda_value: 净值 32 | funda_discount_rt: 折价率(%) 33 | funda_coupon: 本期利率 34 | funda_coupon_next: 下期利率 35 | funda_profit_rt_next: 修正收益率(%) 36 | funda_index_id: 参考指数代码 37 | funda_index_name: 参考指数名称 38 | funda_index_increase_rt: 指数涨幅(%) 39 | funda_lower_recalc_rt: 下折母基需跌(%) 40 | lower_recalc_profit_rt: 理论下折收益(%) 41 | fundb_upper_recalc_rt: 上折母基需涨(%) 42 | funda_base_est_dis_rt_t1: T-1溢价率(%) 43 | funda_base_est_dis_rt_t2: T-2溢价率(%) 44 | funda_amount: A份额(万份) 45 | funda_amount_increase: A新增(万份) 46 | abrate: A:B 47 | next_recalc_dt: 下次定折日期 48 | """ 49 | self._data = pd.DataFrame() 50 | 51 | self._data = self.__parsePage(cf.RATING_FUNDA_URL, cf.RATING_FUNDA_COLS) 52 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 53 | self._data['next_recalc_dt'] = self._data['next_recalc_dt'].map(self.__nextRecalcDt) 54 | for col in ['funda_current_price', 'funda_increase_rt', 'funda_volume', 'funda_value', 55 | 'funda_discount_rt', 'funda_coupon', 'funda_coupon_next', 'funda_profit_rt_next', 56 | 'funda_index_increase_rt', 'funda_lower_recalc_rt', 'lower_recalc_profit_rt', 57 | 'fundb_upper_recalc_rt', 'funda_base_est_dis_rt_t1', 'funda_base_est_dis_rt_t2', 58 | 'funda_amount_increase']: 59 | self._data[col] = self._data[col].astype(float) 60 | 61 | return self._result() 62 | 63 | 64 | def __nextRecalcDt(self, x): 65 | pattern = re.compile(r'(\d{4}-\d{2}-\d{2})') 66 | 67 | return re.sub(pattern, r'\1', x) 68 | 69 | 70 | def ratingFundB(self): 71 | """ 72 | 分级基金B及其相关数据 73 | return 74 | ------ 75 | DataFrame or List: [{'fundb_id':, 'fundb_name':, ...}, ...] 76 | fundb_id: 分级B代码 77 | fundb_name: 分级B名称 78 | fundb_base_fund_id: 对应母基代码 79 | funda_id: 分级A代码 80 | funda_name: 分级A名称 81 | coupon: 优惠 82 | manage_fee: 管理费 83 | funda_current_price: 分级A现价 84 | funda_upper_price: 分级A最高价 85 | funda_lower_price: 分级A最低价 86 | funda_increase_rt: 分级A增长率(%) 87 | fundb_current_price: 分级B现价 88 | fundb_upper_price: 分级B最高价 89 | fundb_lower_price: 分级B最低价 90 | fundb_increase_rt: 分级B增长率(%) 91 | fundb_volume: 分级B成交额(万元) 92 | fundb_value: 分级B净值 93 | fundm_value: 母基净值 94 | fundb_discount_rt: 分级B溢价率(%) 95 | fundb_price_leverage_rt: 分级B价格杠杆 96 | fundb_net_leverage_rt: 分级B净值杠杆 97 | fundb_capital_rasising_rt: 分级B融资成本(%) 98 | fundb_lower_recalc_rt: 下折母基需跌(%) 99 | fundb_upper_recalc_rt: 上折母基需涨(%) 100 | b_est_val: 估值 101 | fundb_index_id: 参考指数代码 102 | fundb_index_name: 参考指数名称 103 | fundb_index_increase_rt: 参考指数涨幅(%) 104 | funda_ratio: 分级A占比 105 | fundb_ratio: 分级B占比 106 | fundb_base_price: 分级B基础价格 107 | fundB_amount: B份额(万份) 108 | fundB_amount_increase: B新增份额(万份) 109 | abrate: A:B 110 | """ 111 | self._data = pd.DataFrame() 112 | 113 | self._data = self.__parsePage(cf.RATING_FUNDB_URL, cf.RATING_FUNDB_COLS) 114 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 115 | for col in ['coupon', 'manage_fee', 'funda_current_price', 'funda_upper_price', 'funda_lower_price', 116 | 'funda_increase_rt', 'fundb_current_price', 'fundb_upper_price', 'fundb_lower_price', 117 | 'fundb_increase_rt', 'fundb_volume', 'fundb_value', 'fundm_value', 'fundb_discount_rt', 118 | 'fundb_price_leverage_rt', 'fundb_net_leverage_rt', 'fundb_capital_rasising_rt', 119 | 'fundb_lower_recalc_rt', 'fundb_upper_recalc_rt', 'b_est_val', 'fundb_index_increase_rt', 120 | 'fundb_base_price', 'fundB_amount', 'fundB_amount_increase']: 121 | self._data[col] = self._data[col].astype(float) 122 | 123 | return self._result() 124 | 125 | 126 | def ratingFundM(self): 127 | """ 128 | 分级基金母基金及其相关数据 129 | return 130 | ------ 131 | DataFrame or List: [{'base_fund_id':, 'base_fund_nm', ...}, ...] 132 | base_fund_id: 母基代码 133 | base_fund_nm: 母基名称 134 | market: 所属市场 135 | issue_dt: 创立日期 136 | manage_fee: 管理费 137 | index_id: 跟踪指数代码 138 | index_nm: 跟踪指数名称 139 | lower_recalc_price: 下折 140 | a_ratio: A占比 141 | b_ratio: B占比 142 | next_recalc_dt: 下次定折 143 | fundA_id: A基代码 144 | fundA_nm: A基名称 145 | coupon: 本期利率 146 | coupon_next: 下期利率 147 | fundB_id: B基代码 148 | fundB_nm: B基名称 149 | price: 母基净值 150 | base_lower_recalc_rt: 下折需跌(%) 151 | abrate: A:B 152 | """ 153 | self._data = pd.DataFrame() 154 | 155 | self._data = self.__parsePage(cf.RATING_FUNDM_URL, cf.RATING_FUNDM_COLS) 156 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 157 | for col in ['manage_fee', 'lower_recalc_price', 'a_ratio', 'b_ratio', 'coupon', 'coupon_next', 158 | 'price', 'base_lower_recalc_rt']: 159 | self._data[col] = self._data[col].astype(float) 160 | 161 | return self._result() 162 | 163 | 164 | def conBonds(self): 165 | """ 166 | 可转债及其相关数据 167 | return 168 | ------ 169 | DataFrame or List: [{'bond_id':, 'bond_nm':, ...}, ...] 170 | bond_id: 代码 171 | bond_nm: 名称 172 | stock_id: 正股全代码 173 | stock_nm: 正股名称 174 | market: 所属市场 175 | convert_price: 转股价 176 | convert_dt: 转股起始日 177 | issue_dt: 创立日期 178 | maturity_dt: 到期日 179 | next_put_dt: 回售起始日 180 | put_price: 回售价 181 | put_count_days: 回售计算天数 182 | put_total_days: 回售总天数 183 | redeem_price: 赎回价 184 | redeem_price_ratio: 赎回率 185 | redeem_count_days: 赎回计算天数 186 | redeem_total_days: 赎回总天数 187 | orig_iss_amt: 发行规模(亿) 188 | curr_iss_amt: 剩余规模(亿) 189 | rating_cd: 债券评级 190 | issuer_rating_cd: 主体评级 191 | guarantor: 担保 192 | active_fl: 活跃标志 193 | ration_rt: 股东配售率(%) 194 | pb: 市净率 195 | sprice: 正股价 196 | sincrease_rt: 正股涨跌(%) 197 | last_time: 最后时间 198 | convert_value: 转股价值 199 | premium_rt: 溢价率(%) 200 | year_left: 剩余年限 201 | ytm_rt: 到期税前收益(%) 202 | ytm_rt_tax: 到期税后收益(%) 203 | price: 现价 204 | increase_rt: 涨跌幅(%) 205 | volume: 成交额(万元) 206 | force_redeem_price: 强赎触发价 207 | put_convert_price: 回售触发价 208 | convert_amt_ratio: 转债占比 209 | stock_cd: 正股简码 210 | pre_bond_id: 转债全码 211 | """ 212 | self._data = pd.DataFrame() 213 | 214 | self._data = self.__parsePage(cf.CON_BONDS_URL, cf.CON_BONDS_COLS) 215 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 216 | 217 | if self._PY3: 218 | self._data['sincrease_rt'] = self._data['sincrease_rt'].map(lambda x: 0 if x == "停牌" else x) 219 | else: 220 | self._data['sincrease_rt'] = self._data['sincrease_rt'].map(lambda x: 0 if x.encode('utf-8') == "停牌" else x) 221 | 222 | for col in ['convert_price', 'put_price', 'redeem_price', 'redeem_price_ratio', 'orig_iss_amt', 223 | 'curr_iss_amt', 'ration_rt', 'pb', 'sprice', 'sincrease_rt', 'convert_value', 'premium_rt', 224 | 'year_left', 'ytm_rt', 'ytm_rt_tax', 'price', 'increase_rt', 'volume', 'force_redeem_price', 225 | 'put_convert_price', 'convert_amt_ratio']: 226 | self._data[col] = self._data[col].astype(float) 227 | 228 | return self._result() 229 | 230 | 231 | def newConBonds(self): 232 | """ 233 | 新转债及其相关数据 234 | return 235 | ------ 236 | DataFrame or List: [{'bond_id':, 'bond_nm':, ...}, ...] 237 | bond_id: 转债代码 238 | bond_nm: 转债名称 239 | apply_cd: 申购代码 240 | apply_date: 申购日期 241 | list_date: 上市日期 242 | amount: 发行规模(亿元) 243 | convert_price: 转股价 244 | pma_rt: 现价比转股价 245 | stock_id: 正股代码 246 | stock_nm: 正股名称 247 | price: 正股现价 248 | increase_rt: 正股涨跌幅 249 | ration_rt: 股东配售率(%) 250 | online_amount: 网上规模(亿元) 251 | lucky_draw_rt: 中签率(%) 252 | individual_limit: 个人申购限额 253 | underwriter_rt: 包销比例(%) 254 | rating_cd: 评级 255 | progress_nm: 方案进展 256 | apply_tips: 申购提示 257 | valid_apply: 申购户数(万户) 258 | """ 259 | self._data = pd.DataFrame() 260 | 261 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 262 | self._data = self.__parsePage(cf.NEW_CON_BONDS_URL, cf.NEW_CON_BONDS_COLS) 263 | for col in ['amount', 'convert_price', 'pma_rt', 'price', 'increase_rt', 'ration_rt', 264 | 'online_amount', 'lucky_draw_rt', 'underwriter_rt', 'valid_apply']: 265 | self._data[col] = self._data[col].astype(float) 266 | 267 | return self._result() 268 | 269 | 270 | def closedStockFund(self): 271 | """ 272 | 封闭股基及其相关数据 273 | return 274 | ------ 275 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 276 | fund_id: 代码 277 | fund_nm: 名称 278 | issue_dt: 创立日期 279 | duration: 持续时间 280 | last_time: 最后时间 281 | price: 现价 282 | increase_rt: 涨幅(%) 283 | volume: 成交金额(万) 284 | net_value: 净值 285 | nav_dt: 净值日期 286 | realtime_estimate_value: 最近估值 287 | discount_rt: 折价率(%) 288 | left_year: 剩余年限 289 | annualize_dscnt_rt: 年化折价率(%) 290 | quote_incr_rt: 周价增(%) 291 | nav_incr_rt: 周净增(%) 292 | spread: 净价差(%) 293 | stock_ratio: 股票占比(%) 294 | report_dt: 报告日期 295 | daily_nav_incr_rt: 当日净增(%) 296 | daily_spread: 日净价差(%) 297 | """ 298 | self._data = pd.DataFrame() 299 | 300 | self._data = self.__parsePage(cf.CLOSED_STOCK_FUND_URL, cf.CLOSED_STOCK_FUND_COLS) 301 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 302 | for col in ['price', 'increase_rt', 'volume', 'net_value', 'realtime_estimate_value', 'discount_rt', 303 | 'left_year', 'annualize_dscnt_rt', 'quote_incr_rt', 'nav_incr_rt', 'spread', 'stock_ratio', 304 | 'daily_nav_incr_rt', 'daily_spread']: 305 | self._data[col] = self._data[col].astype(float) 306 | 307 | return self._result() 308 | 309 | 310 | def closedBondFund(self): 311 | """ 312 | 封闭债基及其相关数据 313 | return 314 | ------ 315 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 316 | fund_id: 代码 317 | fund_nm: 名称 318 | maturity_dt: 到期日期 319 | left_year: 剩余年限 320 | est_val: 最近估值 321 | discount_rt: 折价率(%) 322 | annual_discount_rt: 年化折价率(%) 323 | trade_price: 现价 324 | increase_rt: 涨幅(%) 325 | volume: 成交金额 326 | last_time: 最后时间 327 | fund_nav: 最近净值 328 | last_chg_dt: 净值日期 329 | price_incr_rt: 净值日增(%) 330 | stock_ratio: 股票比例 331 | bond_ratio: 债券比例 332 | report_dt: 报告日期 333 | is_outdate: 是否超期 334 | """ 335 | self._data = pd.DataFrame() 336 | 337 | self._data = self.__pargePageByTimeStamp(cf.CLOSED_BOND_FUND_URL, cf.CLOSED_BOND_FUND_COLS) 338 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 339 | for col in ['left_year', 'est_val', 'discount_rt', 'annual_discount_rt', 'trade_price', 340 | 'increase_rt', 'volume', 'fund_nav', 'price_incr_rt', 'stock_ratio', 'bond_ratio']: 341 | self._data[col] = self._data[col].astype(float) 342 | 343 | return self._result() 344 | 345 | 346 | def dividendRate(self): 347 | """ 348 | A股股息率 349 | return 350 | ------ 351 | DataFrame or List: [{'stock_id':, 'stock_nm':, ...}, ...] 352 | stock_id: 股票代码 353 | stock_nm: 股票名称 354 | dividend_rate: 股息率(TTM) 355 | dividend_rate2: 静态股息率 356 | ipo_date: ipo日期 357 | price: 价格 358 | volume: 成交额(万元) 359 | increase_rt: 涨幅(%) 360 | pe: 市盈率(TTM) 361 | pb: 市净率 362 | total_value: 市值(亿元) 363 | eps_growth_ttm: 净利同比增长(%) 364 | roe: 最新年报ROE(%) 365 | revenue_average: 5年营收复合增长(%) 366 | profit_average: 5年利润复合增长(%) 367 | roe_average: 5年平均ROE(%) 368 | pb_temperature: PB温度(℃) 369 | pe_temperature: PE温度(℃) 370 | int_debt_rate: 有息负债率(%) 371 | cashflow_average: 5年现金流复合增长(%) 372 | dividend_rate_average: 5年分红率复合增长(%) 373 | dividend_rate5: 5年平均股息率(%) 374 | industry_nm: 所属行业 375 | active_flg: 活跃标志 376 | last_time: 最后时间 377 | """ 378 | self._data = pd.DataFrame() 379 | 380 | self._data = self.__parsePage(cf.DIVIDEND_RATE_URL, cf.DIVIDEND_RATE_COLS) 381 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 382 | for col in ['dividend_rate', 'dividend_rate2', 'price', 'volume', 'increase_rt', 'pe', 'pb', 'total_value', 383 | 'eps_growth_ttm', 'roe', 'revenue_average', 'profit_average', 'roe_average', 'pb_temperature', 384 | 'pe_temperature', 'int_debt_rate', 'cashflow_average', 'dividend_rate_average', 'dividend_rate5']: 385 | self._data[col] = self._data[col].astype(float) 386 | 387 | return self._result() 388 | 389 | 390 | def stockLof(self): 391 | """ 392 | 股票LOF基金及基相关数据 393 | return 394 | ------ 395 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 396 | fund_id: 基金代码 397 | fund_nm: 基金名称 398 | price: 现价 399 | increase_rt: 涨幅(%) 400 | volume: 成交(万元) 401 | amount: 场内份额(万份) 402 | fund_nav: 基金净值 403 | nav_dt: 净值日期 404 | estimate_value: 实时估值 405 | discount_rt: 溢价率(%) 406 | stock_ratio: 股票占比(%) 407 | stock_increase_rt: 重仓涨幅(%) 408 | apply_fee: 申购费(%) 409 | redeem_fee: 赎回费(%) 410 | apply_redeem_status: 申赎状态 411 | """ 412 | self._data = pd.DataFrame() 413 | 414 | self._data = self.__parsePage(cf.STOCK_LOF_URL, cf.STOCK_LOF_COLS) 415 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 416 | for col in ['price', 'increase_rt', 'volume', 'amount', 'fund_nav', 417 | 'estimate_value', 'discount_rt', 'stock_ratio', 'stock_increase_rt', 'apply_fee', 'redeem_fee']: 418 | self._data[col] = self._data[col].astype(float) 419 | 420 | return self._result() 421 | 422 | 423 | def indexLof(self): 424 | """ 425 | 指数LOF基金及基相关数据 426 | return 427 | ------ 428 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 429 | fund_id: 基金代码 430 | fund_nm: 基金名称 431 | price: 现价 432 | increase_rt: 涨幅(%) 433 | volume: 成交(万元) 434 | amount: 场内份额(万份) 435 | fund_nav: 基金净值 436 | nav_dt: 净值日期 437 | estimate_value: 实时估值 438 | discount_rt: 溢价率(%) 439 | index_id: 指数代码 440 | index_nm: 指数名称 441 | index_increase_rt: 指数涨幅(%) 442 | apply_fee: 申购费(%) 443 | redeem_fee: 赎回费(%) 444 | apply_redeem_status: 申赎状态 445 | """ 446 | self._data = pd.DataFrame() 447 | 448 | self._data = self.__parsePage(cf.INDEX_LOF_URL, cf.INDEX_LOF_COLS) 449 | self._data = self._data.applymap(lambda x: np.NaN if x == "-" else x) 450 | for col in ['price', 'increase_rt', 'volume', 'amount', 'fund_nav', 'estimate_value', 451 | 'discount_rt', 'index_increase_rt', 'apply_fee', 'redeem_fee']: 452 | self._data[col] = self._data[col].astype(float) 453 | 454 | return self._result() 455 | 456 | 457 | def __parsePage(self, url, column): 458 | page = 1 459 | while(True): 460 | try: 461 | request = self._session.get(url % page) 462 | text = request.text.replace('%', '') 463 | dataDict = json.loads(text) 464 | if dataDict['page'] < page: 465 | break 466 | 467 | dataList = [] 468 | for row in dataDict['rows']: 469 | dataList.append(row['cell']) 470 | 471 | self._data = self._data.append(pd.DataFrame(dataList, columns=column), ignore_index=True) 472 | 473 | page += 1 474 | except Exception as e: 475 | print(str(e)) 476 | 477 | return self._data 478 | 479 | 480 | def __pargePageByTimeStamp(self, url, column): 481 | timestamp = int(time.time()*1000) 482 | try: 483 | request = self._session.get(url % timestamp) 484 | text = request.text.replace('%', '') 485 | dataDict = json.loads(text) 486 | dataList = [] 487 | for row in dataDict['rows']: 488 | dataList.append(row['cell']) 489 | 490 | self._data = self._data.append(pd.DataFrame(dataList, columns = column), ignore_index=True) 491 | except Exception as e: 492 | print(str(e)) 493 | 494 | return self._data -------------------------------------------------------------------------------- /gugu/macro.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 宏观经济数据类 4 | Created on 2019/01/09 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | import pandas as pd 10 | import numpy as np 11 | import re 12 | import json 13 | import time 14 | from gugu.utility import Utility 15 | from gugu.base import Base, cf 16 | import sys 17 | 18 | 19 | class Macro(Base): 20 | def gdpYear(self, retry=3, pause=0.001): 21 | """ 22 | 获取年度国内生产总值数据 23 | Parameters 24 | -------- 25 | retry : int, 默认 3 26 | 如遇网络等问题重复执行的次数 27 | pause : int, 默认 0.001 28 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 29 | 30 | Return 31 | -------- 32 | DataFrame or List: [{'year':, 'gdp':, ...}, ...] 33 | year :统计年度 34 | gdp :国内生产总值(亿元) 35 | pc_gdp :人均国内生产总值(元) 36 | gnp :国民生产总值(亿元) 37 | pi :第一产业(亿元) 38 | si :第二产业(亿元) 39 | industry :工业(亿元) 40 | cons_industry :建筑业(亿元) 41 | ti :第三产业(亿元) 42 | trans_industry :交通运输仓储邮电通信业(亿元) 43 | lbdy :批发零售贸易及餐饮业(亿元) 44 | """ 45 | self._data = pd.DataFrame() 46 | 47 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK4224641560861/MacPage_Service.get_pagedata?cate=nation&event=0&from=0&num=70&condition=&_=4224641560861 48 | datastr = self.__parsePage('nation', 0, 70, retry, pause) 49 | datastr = datastr.replace('"', '').replace('null', '0') 50 | js = json.loads(datastr) 51 | self._data = pd.DataFrame(js, columns=cf.GDP_YEAR_COLS) 52 | self._data[self._data==0] = np.NaN 53 | 54 | return self._result() 55 | 56 | def gdpQuarter(self, retry=3, pause=0.001): 57 | """ 58 | 获取季度国内生产总值数据 59 | Parameters 60 | -------- 61 | retry : int, 默认 3 62 | 如遇网络等问题重复执行的次数 63 | pause : int, 默认 0.001 64 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 65 | 66 | Return 67 | -------- 68 | DataFrame or List: [{'quarter':, 'gdp':, ...}, ...] 69 | quarter :季度 70 | gdp :国内生产总值(亿元) 71 | gdp_yoy :国内生产总值同比增长(%) 72 | pi :第一产业增加值(亿元) 73 | pi_yoy:第一产业增加值同比增长(%) 74 | si :第二产业增加值(亿元) 75 | si_yoy :第二产业增加值同比增长(%) 76 | ti :第三产业增加值(亿元) 77 | ti_yoy :第三产业增加值同比增长(%) 78 | """ 79 | self._data = pd.DataFrame() 80 | 81 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK3935140379887/MacPage_Service.get_pagedata?cate=nation&event=1&from=0&num=250&condition=&_=3935140379887 82 | datastr = self.__parsePage('nation', 1, 250, retry, pause) 83 | datastr = datastr.replace('"', '').replace('null', '0') 84 | js = json.loads(datastr) 85 | self._data = pd.DataFrame(js, columns=cf.GDP_QUARTER_COLS) 86 | self._data['quarter'] = self._data['quarter'].astype(object) 87 | self._data[self._data==0] = np.NaN 88 | 89 | return self._result() 90 | 91 | def demandsToGdp(self, retry=3, pause=0.001): 92 | """ 93 | 获取三大需求对GDP贡献数据 94 | Parameters 95 | -------- 96 | retry : int, 默认 3 97 | 如遇网络等问题重复执行的次数 98 | pause : int, 默认 0.001 99 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 100 | 101 | Return 102 | -------- 103 | DataFrame or List: [{'year':, 'cons_to':, ...}, ...] 104 | year :统计年度 105 | cons_to :最终消费支出贡献率(%) 106 | cons_rate :最终消费支出拉动(百分点) 107 | asset_to :资本形成总额贡献率(%) 108 | asset_rate:资本形成总额拉动(百分点) 109 | goods_to :货物和服务净出口贡献率(%) 110 | goods_rate :货物和服务净出口拉动(百分点) 111 | """ 112 | self._data = pd.DataFrame() 113 | 114 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK3153587567694/MacPage_Service.get_pagedata?cate=nation&event=4&from=0&num=80&condition=&_=3153587567694 115 | datastr = self.__parsePage('nation', 4, 80, retry, pause) 116 | datastr = datastr.replace('"','').replace('null','0') 117 | js = json.loads(datastr) 118 | self._data = pd.DataFrame(js,columns=cf.GDP_FOR_COLS) 119 | self._data[self._data==0] = np.NaN 120 | 121 | return self._result() 122 | 123 | 124 | def idsPullToGdp(self, retry=3, pause=0.001): 125 | """ 126 | 获取三大产业对GDP拉动数据 127 | Parameters 128 | -------- 129 | retry : int, 默认 3 130 | 如遇网络等问题重复执行的次数 131 | pause : int, 默认 0.001 132 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 133 | 134 | Return 135 | -------- 136 | DataFrame or List: [{'year':, 'gdp_yoy':, ...}, ...] 137 | year :统计年度 138 | gdp_yoy :国内生产总值同比增长(%) 139 | pi :第一产业拉动率(%) 140 | si :第二产业拉动率(%) 141 | industry:其中工业拉动(%) 142 | ti :第三产业拉动率(%) 143 | """ 144 | self._data = pd.DataFrame() 145 | 146 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK1083239038283/MacPage_Service.get_pagedata?cate=nation&event=5&from=0&num=60&condition=&_=1083239038283 147 | datastr = self.__parsePage('nation', 5, 60, retry, pause) 148 | datastr = datastr.replace('"', '').replace('null', '0') 149 | js = json.loads(datastr) 150 | self._data = pd.DataFrame(js, columns=cf.GDP_PULL_COLS) 151 | self._data[self._data==0] = np.NaN 152 | 153 | return self._result() 154 | 155 | 156 | def idsCtbToGdp(self, retry=3, pause=0.001): 157 | """ 158 | 获取三大产业贡献率数据 159 | Parameters 160 | -------- 161 | retry : int, 默认 3 162 | 如遇网络等问题重复执行的次数 163 | pause : int, 默认 0.001 164 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 165 | 166 | Return 167 | -------- 168 | DataFrame or List: [{'year':, 'gdp_yoy':, ...}, ...] 169 | year :统计年度 170 | gdp_yoy :国内生产总值 171 | pi :第一产业献率(%) 172 | si :第二产业献率(%) 173 | industry:其中工业献率(%) 174 | ti :第三产业献率(%) 175 | """ 176 | self._data = pd.DataFrame() 177 | 178 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK4658347026358/MacPage_Service.get_pagedata?cate=nation&event=6&from=0&num=60&condition=&_=4658347026358 179 | datastr = self.__parsePage('nation', 6, 60, retry, pause) 180 | datastr = datastr.replace('"', '').replace('null', '0') 181 | js = json.loads(datastr) 182 | self._data = pd.DataFrame(js, columns=cf.GDP_CONTRIB_COLS) 183 | self._data[self._data==0] = np.NaN 184 | 185 | return self._result() 186 | 187 | 188 | def cpi(self, retry=3, pause=0.001): 189 | """ 190 | 获取居民消费价格指数数据 191 | Parameters 192 | -------- 193 | retry : int, 默认 3 194 | 如遇网络等问题重复执行的次数 195 | pause : int, 默认 0.001 196 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 197 | 198 | Return 199 | -------- 200 | DataFrame or List: [{'month':, 'cpi':,}, ...] 201 | month :统计月份 202 | cpi :价格指数 203 | """ 204 | self._data = pd.DataFrame() 205 | 206 | datastr = self.__parsePage('price', 0, 600, retry, pause) 207 | js = json.loads(datastr) 208 | self._data = pd.DataFrame(js, columns=cf.CPI_COLS) 209 | self._data['cpi'] = self._data['cpi'].astype(float) 210 | 211 | return self._result() 212 | 213 | 214 | def ppi(self, retry=3, pause=0.001): 215 | """ 216 | 获取工业品出厂价格指数数据 217 | Parameters 218 | -------- 219 | retry : int, 默认 3 220 | 如遇网络等问题重复执行的次数 221 | pause : int, 默认 0.001 222 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 223 | 224 | Return 225 | -------- 226 | DataFrame or List: [{'month':, 'ppiip':, ...}, ...] 227 | month :统计月份 228 | ppiip :工业品出厂价格指数 229 | ppi :生产资料价格指数 230 | qm:采掘工业价格指数 231 | rmi:原材料工业价格指数 232 | pi:加工工业价格指数 233 | cg:生活资料价格指数 234 | food:食品类价格指数 235 | clothing:衣着类价格指数 236 | roeu:一般日用品价格指数 237 | dcg:耐用消费品价格指数 238 | """ 239 | self._data = pd.DataFrame() 240 | 241 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK6734345383111/MacPage_Service.get_pagedata?cate=price&event=3&from=0&num=600&condition=&_=6734345383111 242 | datastr = self.__parsePage('price', 3, 600, retry, pause) 243 | js = json.loads(datastr) 244 | self._data = pd.DataFrame(js, columns=cf.PPI_COLS) 245 | for i in self._data.columns: 246 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, np.NaN, x)) 247 | if i != 'month': 248 | self._data[i] = self._data[i].astype(float) 249 | 250 | return self._result() 251 | 252 | 253 | def depositRate(self, retry=3, pause=0.001): 254 | """ 255 | 获取存款利率数据 256 | Parameters 257 | -------- 258 | retry : int, 默认 3 259 | 如遇网络等问题重复执行的次数 260 | pause : int, 默认 0.001 261 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 262 | 263 | Return 264 | -------- 265 | DataFrame or List: [{'date':, 'deposit_type':, ...}, ...] 266 | date :变动日期 267 | deposit_type :存款种类 268 | rate:利率(%) 269 | """ 270 | self._data = pd.DataFrame() 271 | 272 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK1250640915421/MacPage_Service.get_pagedata?cate=fininfo&event=2&from=0&num=600&condition=&_=1250640915421 273 | datastr = self.__parsePage('fininfo', 2, 600, retry, pause) 274 | js = json.loads(datastr) 275 | self._data = pd.DataFrame(js, columns=cf.DEPOSIT_COLS) 276 | for i in self._data.columns: 277 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, '--', x)) 278 | 279 | return self._result() 280 | 281 | 282 | def loanRate(self, retry=3, pause=0.001): 283 | """ 284 | 获取贷款利率数据 285 | Parameters 286 | -------- 287 | retry : int, 默认 3 288 | 如遇网络等问题重复执行的次数 289 | pause : int, 默认 0.001 290 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 291 | 292 | Return 293 | -------- 294 | DataFrame or List: [{'date':, 'loan_type':, ...}, ...] 295 | date :执行日期 296 | loan_type :存款种类 297 | rate:利率(%) 298 | """ 299 | self._data = pd.DataFrame() 300 | 301 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK7542659823280/MacPage_Service.get_pagedata?cate=fininfo&event=3&from=0&num=800&condition=&_=7542659823280 302 | datastr = self.__parsePage('fininfo', 3, 800, retry, pause) 303 | js = json.loads(datastr) 304 | self._data = pd.DataFrame(js, columns=cf.LOAN_COLS) 305 | for i in self._data.columns: 306 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, '--', x)) 307 | 308 | return self._result() 309 | 310 | 311 | def rrr(self, retry=3, pause=0.001): 312 | """ 313 | 获取存款准备金率数据 314 | Parameters 315 | -------- 316 | retry : int, 默认 3 317 | 如遇网络等问题重复执行的次数 318 | pause : int, 默认 0.001 319 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 320 | 321 | Return 322 | -------- 323 | DataFrame or List: [{'date':, 'before':, ...}, ...] 324 | date :变动日期 325 | before :调整前存款准备金率(%) 326 | now:调整后存款准备金率(%) 327 | changed:调整幅度(%) 328 | """ 329 | self._data = pd.DataFrame() 330 | 331 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK8028217046046/MacPage_Service.get_pagedata?cate=fininfo&event=4&from=0&num=100&condition=&_=8028217046046 332 | datastr = self.__parsePage('fininfo', 4, 100, retry, pause) 333 | datastr = datastr if self._PY3 else datastr.decode('gbk') 334 | js = json.loads(datastr) 335 | self._data = pd.DataFrame(js, columns=cf.RRR_COLS) 336 | for i in self._data.columns: 337 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, '--', x)) 338 | 339 | return self._result() 340 | 341 | 342 | def moneySupply(self, retry=3, pause=0.001): 343 | """ 344 | 获取货币供应量数据 345 | Parameters 346 | -------- 347 | retry : int, 默认 3 348 | 如遇网络等问题重复执行的次数 349 | pause : int, 默认 0.001 350 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 351 | 352 | Return 353 | -------- 354 | DataFrame or List: [{'month':, 'm2':, ...}, ...] 355 | month :统计时间 356 | m2 :货币和准货币(广义货币M2)(亿元) 357 | m2_yoy:货币和准货币(广义货币M2)同比增长(%) 358 | m1:货币(狭义货币M1)(亿元) 359 | m1_yoy:货币(狭义货币M1)同比增长(%) 360 | m0:流通中现金(M0)(亿元) 361 | m0_yoy:流通中现金(M0)同比增长(%) 362 | cd:活期存款(亿元) 363 | cd_yoy:活期存款同比增长(%) 364 | qm:准货币(亿元) 365 | qm_yoy:准货币同比增长(%) 366 | ftd:定期存款(亿元) 367 | ftd_yoy:定期存款同比增长(%) 368 | sd:储蓄存款(亿元) 369 | sd_yoy:储蓄存款同比增长(%) 370 | rests:其他存款(亿元) 371 | rests_yoy:其他存款同比增长(%) 372 | """ 373 | self._data = pd.DataFrame() 374 | 375 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK9019314616219/MacPage_Service.get_pagedata?cate=fininfo&event=1&from=0&num=600&condition=&_=9019314616219 376 | datastr = self.__parsePage('fininfo', 1, 600, retry, pause) 377 | datastr = datastr if self._PY3 else datastr.decode('gbk') 378 | js = json.loads(datastr) 379 | self._data = pd.DataFrame(js, columns=cf.MONEY_SUPPLY_COLS) 380 | for i in self._data.columns: 381 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, '--', x)) 382 | 383 | return self._result() 384 | 385 | 386 | def moneySupplyBal(self, retry=3, pause=0.001): 387 | """ 388 | 获取货币供应量(年底余额)数据 389 | Parameters 390 | -------- 391 | retry : int, 默认 3 392 | 如遇网络等问题重复执行的次数 393 | pause : int, 默认 0.001 394 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 395 | 396 | Return 397 | -------- 398 | DataFrame or List: [{'year':, 'm2':, ...}, ...] 399 | year :统计年度 400 | m2 :货币和准货币(亿元) 401 | m1:货币(亿元) 402 | m0:流通中现金(亿元) 403 | cd:活期存款(亿元) 404 | qm:准货币(亿元) 405 | ftd:定期存款(亿元) 406 | sd:储蓄存款(亿元) 407 | rests:其他存款(亿元) 408 | """ 409 | self._data = pd.DataFrame() 410 | 411 | # http://money.finance.sina.com.cn/mac/api/jsonp.php/SINAREMOTECALLCALLBACK3430820865181/MacPage_Service.get_pagedata?cate=fininfo&event=0&from=0&num=200&condition=&_=3430820865181 412 | datastr = self.__parsePage('fininfo', 0, 200, retry, pause) 413 | datastr = datastr if self._PY3 else datastr.decode('gbk') 414 | js = json.loads(datastr) 415 | self._data = pd.DataFrame(js, columns=cf.MONEY_SUPPLY_BLA_COLS) 416 | for i in self._data.columns: 417 | self._data[i] = self._data[i].apply(lambda x:np.where(x is None, '--', x)) 418 | 419 | return self._result() 420 | 421 | 422 | def __parsePage(self, cate='', event=0, num=0, retry=3, pause=0.001): 423 | for _ in range(retry): 424 | time.sleep(pause) 425 | 426 | try: 427 | rdInt = Utility.random() 428 | request = self._session.get( cf.MACRO_URL % (rdInt, cate, event, num, rdInt), timeout=10 ) 429 | if self._PY3: 430 | request.encoding = 'gbk' 431 | 432 | regSym = re.compile(r'\,count:(.*?)\}') 433 | datastr = regSym.findall(request.text) 434 | datastr = datastr[0] 435 | datastr = datastr.split('data:')[1] 436 | except Exception as e: 437 | print(e) 438 | else: 439 | return datastr 440 | 441 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 442 | 443 | 444 | def shibor(self, year=None): 445 | """ 446 | 获取上海银行间同业拆放利率 447 | Parameters 448 | ------ 449 | year:年份(int) 450 | 451 | Return 452 | ------ 453 | DataFrame or List: [{'date':, 'ON':, ...}, ...] 454 | date:日期 455 | ON:隔夜拆放利率 456 | 1W:1周拆放利率 457 | 2W:2周拆放利率 458 | 1M:1个月拆放利率 459 | 3M:3个月拆放利率 460 | 6M:6个月拆放利率 461 | 9M:9个月拆放利率 462 | 1Y:1年拆放利率 463 | """ 464 | self._data = pd.DataFrame() 465 | 466 | lab = cf.SHIBOR_TYPE['Shibor'] 467 | # http://www.shibor.org/shibor/web/html/downLoad.html?nameNew=Historical_Shibor_Data_2018.xls&downLoadPath=data&nameOld=Shibor数据2018.xls&shiborSrc=http://www.shibor.org/shibor/ 468 | self._data = self.__parseExcel(year, 'Shibor', lab, cf.SHIBOR_COLS) 469 | 470 | return self._result() 471 | 472 | 473 | def shiborQuote(self, year=None): 474 | """ 475 | 获取Shibor银行报价数据 476 | Parameters 477 | ------ 478 | year:年份(int) 479 | 480 | Return 481 | ------ 482 | DataFrame or List: [{'date':, 'bank':, ...}, ...] 483 | date:日期 484 | bank:报价银行名称 485 | ON:隔夜拆放利率 486 | 1W:1周拆放利率 487 | 2W:2周拆放利率 488 | 1M:1个月拆放利率 489 | 3M:3个月拆放利率 490 | 6M:6个月拆放利率 491 | 9M:9个月拆放利率 492 | 1Y:1年拆放利率 493 | """ 494 | self._data = pd.DataFrame() 495 | 496 | lab = cf.SHIBOR_TYPE['Quote'] 497 | # http://www.shibor.org/shibor/web/html/downLoad.html?nameNew=Historical_Quote_Data_2018.xls&downLoadPath=data&nameOld=报价数据2018.xls&shiborSrc=http://www.shibor.org/shibor/ 498 | self._data = self.__parseExcel(year, 'Quote', lab, cf.QUOTE_COLS) 499 | 500 | return self._result() 501 | 502 | 503 | def shiborMa(self, year=None): 504 | """ 505 | 获取Shibor均值数据 506 | Parameters 507 | ------ 508 | year:年份(int) 509 | 510 | Return 511 | ------ 512 | DataFrame or List: [{'date':, 'ON_5':, ...}, ...] 513 | date:日期 514 | 其它分别为各周期5、10、20均价 515 | """ 516 | self._data = pd.DataFrame() 517 | 518 | lab = cf.SHIBOR_TYPE['Tendency'] 519 | self._data = self.__parseExcel(year, 'Shibor_Tendency', lab, cf.SHIBOR_MA_COLS) 520 | 521 | return self._result() 522 | 523 | 524 | def lpr(self, startDate, endDate=None): 525 | """ 526 | 获取贷款基础利率 527 | Parameters 528 | ------ 529 | startDate:开始日期(格式:YYYY-MM-DD) 530 | endDate:结束日期(格式:YYYY-MM-DD,为None时默认取当前日期) 531 | 532 | Return 533 | ------ 534 | DataFrame or List: [{'date':, '1Y':, ...}, ...] 535 | date:日期 536 | 1Y:1年贷款基础利率 537 | """ 538 | self._data = pd.DataFrame() 539 | 540 | if endDate is None: 541 | endDate = Utility.getToday() 542 | 543 | request = self._session.get(cf.LPR_URL % (startDate, endDate)) 544 | dataDict = json.loads(request.text) 545 | dataList = [] 546 | for row in dataDict['records']: 547 | data = {'date': row['showDateCN'], '1Y': float(row['1Y'])} 548 | dataList.append(data) 549 | self._data = pd.DataFrame(dataList, columns=cf.LPR_COLS) 550 | 551 | return self._result() 552 | 553 | 554 | def __parseExcel(self, year, datatype, lab, column): 555 | year = Utility.getYear() if year is None else year 556 | lab = lab.encode('utf-8') if self._PY3 else lab 557 | 558 | try: 559 | df = pd.read_excel( cf.SHIBOR_DATA_URL % (datatype, year, lab, year), skiprows=[0] ) 560 | df.columns = column 561 | df['date'] = df['date'].map(lambda x: x.date()) 562 | df['date'] = df['date'].astype('datetime64[ns]') 563 | except Exception as e: 564 | print(e) 565 | else: 566 | return df -------------------------------------------------------------------------------- /gugu/marketdata.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 市场类 4 | Created on 2018/12/26 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | 10 | import pandas as pd 11 | from pandas.compat import StringIO 12 | import re 13 | import json 14 | import numpy as np 15 | import time 16 | from gugu.base import Base, cf 17 | 18 | 19 | class MarketData(Base): 20 | def index(self): 21 | """ 22 | 获取大盘指数行情 23 | return 24 | ------- 25 | DataFrame or list: [{'code':, 'name':, ...}, ...] 26 | code:指数代码 27 | name:指数名称 28 | change:涨跌幅 29 | open:开盘价 30 | preclose:昨日收盘价 31 | close:收盘价 32 | high:最高价 33 | low:最低价 34 | volume:成交量(手) 35 | amount:成交金额(亿元) 36 | """ 37 | self._data = pd.DataFrame() 38 | 39 | # http://hq.sinajs.cn/rn=xppzh&list=sh000001,sh000002,sh000003,sh000008,sh000009,sh000010,sh000011,sh000012,sh000016,sh000017,sh000300,sh000905,sz399001,sz399002,sz399003,sz399004,sz399005,sz399006,sz399008,sz399100,sz399101,sz399106,sz399107,sz399108,sz399333,sz399606 40 | request = self._session.get( cf.INDEX_URL, timeout=10 ) 41 | request.encoding = 'gbk' 42 | text = request.text.replace('var hq_str_', '') 43 | text = text.replace('";', '').replace('"', '').replace('=', ',') 44 | text = '%s%s'%(cf.INDEX_HEADER, text) 45 | 46 | self._data = pd.read_csv(StringIO(text), sep=',', thousands=',') 47 | self._data['change'] = (self._data['close'] / self._data['preclose'] - 1 ) * 100 48 | self._data['amount'] = self._data['amount'] / 100000000 49 | self._data['change'] = self._data['change'].map(cf.FORMAT) 50 | self._data['amount'] = self._data['amount'].map(cf.FORMAT4) 51 | self._data = self._data[cf.INDEX_COLS] 52 | self._data['code'] = self._data['code'].map(lambda x:str(x).zfill(6)) 53 | self._data['change'] = self._data['change'].astype(float) 54 | self._data['amount'] = self._data['amount'].astype(float) 55 | 56 | return self._result() 57 | 58 | 59 | def latest(self): 60 | """ 61 | 一次性获取最近一个日交易日所有股票的交易数据 62 | return 63 | ------- 64 | DataFrame or list: [{'code':, 'name':, ...}, ...] 65 | code:代码 66 | name:名称 67 | changepercent:涨跌幅 68 | trade:现价 69 | open:开盘价 70 | high:最高价 71 | low:最低价 72 | settlement:昨日收盘价 73 | volume:成交量 74 | turnoverratio:换手率 75 | amount:成交量 76 | per:市盈率 77 | pb:市净率 78 | mktcap:总市值 79 | nmc:流通市值 80 | """ 81 | self._data = pd.DataFrame() 82 | 83 | self._writeHead() 84 | 85 | self._data = self.__handleLatest(1) 86 | if self._data is not None: 87 | for i in range(2, cf.PAGE_NUM[0]): 88 | newData = self.__handleLatest(i) 89 | self._data = self._data.append(newData, ignore_index=True) 90 | 91 | return self._result() 92 | 93 | 94 | def __handleLatest(self, pageNum=1): 95 | """ 96 | 处理当日行情分页数据,格式为json 97 | Parameters 98 | ------ 99 | pageNum:页码 100 | return 101 | ------- 102 | DataFrame 当日所有股票交易数据(DataFrame) 103 | """ 104 | self._writeConsole() 105 | # http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?num=80&sort=code&asc=0&node=hs_a&symbol=&_s_r_a=page&page=1 106 | request = self._session.get( cf.LATEST_URL % pageNum, timeout=10 ) 107 | if self._PY3: 108 | request.encoding = 'gbk' 109 | text = request.text 110 | if text == 'null': 111 | return None 112 | 113 | reg = re.compile(r'\,(.*?)\:') 114 | text = reg.sub(r',"\1":', text) 115 | text = text.replace('"{"symbol', '{"symbol') 116 | text = text.replace('{symbol', '{"symbol"') 117 | text = text.replace('""', '"') 118 | jstr = json.dumps(text) 119 | js = json.loads(jstr) 120 | df = pd.DataFrame(pd.read_json(js, dtype={'code':object}), columns=cf.DAY_TRADING_COLUMNS) 121 | df = df.drop('symbol', axis=1) 122 | 123 | return df 124 | 125 | 126 | def indexETF(self): 127 | """ 128 | 获取指数ETF及其相关数据 129 | return 130 | ------ 131 | DataFrame or List: [{'fund_id':, 'fund_nm':, ...}, ...] 132 | fund_id: 基金代码 133 | fund_nm: 基金名称 134 | index_id: 跟踪指数代码 135 | creation_unit: 最小申赎单位(万份) 136 | amount: 份额(万份) 137 | unit_total: 规模(亿元) 138 | unit_incr: 规模变化(亿元) 139 | price: 现价 140 | volume: 成交额(万元) 141 | increase_rt: 涨幅(%) 142 | estimate_value: 估值 143 | discount_rt: 溢价率(%) 144 | fund_nav: 净值 145 | nav_dt: 净值日期 146 | index_nm: 指数名称 147 | index_increase_rt: 指数涨幅(%) 148 | pe: 市盈率 149 | pb: 市净率 150 | """ 151 | self._data = pd.DataFrame() 152 | 153 | page = 1 154 | while(True): 155 | try: 156 | timestamp = int(time.time()*1000) 157 | request = self._session.get( cf.INDEX_ETF_URL % (timestamp, page) ) 158 | text = request.text.replace('%', '') 159 | dataDict = json.loads(text) 160 | if dataDict['page'] < page: 161 | break 162 | 163 | dataList = [] 164 | for row in dataDict['rows']: 165 | dataList.append(row['cell']) 166 | self._data = self._data.append(pd.DataFrame(dataList, columns = cf.INDEX_ETF_COLS), ignore_index=True) 167 | 168 | page += 1 169 | except Exception as e: 170 | print(str(e)) 171 | 172 | self._data[self._data.astype(str)=="-"] = np.NaN 173 | for col in ['creation_unit', 'amount', 'unit_total', 'unit_incr', 'price', 'volume', 'increase_rt', 174 | 'estimate_value', 'discount_rt', 'fund_nav', 'index_increase_rt', 'pe', 'pb']: 175 | self._data[col] = self._data[col].astype(float) 176 | 177 | self._data['index_id'] = self._data['index_id'].map(lambda x : x if x != "0" else None) 178 | 179 | return self._result() 180 | 181 | 182 | -------------------------------------------------------------------------------- /gugu/stockdata.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 股票交易类 4 | Created on 2018/12/26 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | from __future__ import division 10 | 11 | import time 12 | import json 13 | import re 14 | import lxml.html 15 | from lxml import etree 16 | import pandas as pd 17 | from pandas.compat import StringIO 18 | from gugu.base import Base, cf 19 | from gugu.utility import Utility 20 | 21 | class StockData(Base): 22 | def __init__(self, code=None, pandas=True, inter=True): 23 | Base.__init__(self, pandas, inter) 24 | self.__code = code 25 | 26 | 27 | def history(self, start='', end='', ktype='D', autype='qfq', index=False, retry=3, pause=0.001): 28 | """ 29 | 获取股票交易历史数据 30 | --------- 31 | Parameters: 32 | start:string 33 | 开始日期 format:YYYY-MM-DD 为空时取上市首日 34 | end:string 35 | 结束日期 format:YYYY-MM-DD 为空时取最近一个交易日 36 | autype:string 37 | 复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq 38 | ktype:string 39 | 数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D 40 | retry: int, 默认 3 41 | 如遇网络等问题重复执行的次数 42 | pause : int, 默认 0 43 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 44 | return 45 | ------- 46 | DataFrame or list: [{'date':, 'open':, ...}, ...] 47 | date 交易日期 (index) 48 | open 开盘价 49 | high 最高价 50 | close 收盘价 51 | low 最低价 52 | volume 成交量 53 | code 股票代码 54 | """ 55 | self._data = pd.DataFrame() 56 | 57 | url = '' 58 | dataflag = '' 59 | symbol = cf.INDEX_SYMBOL[self.__code] if index else Utility.symbol(self.__code) 60 | autype = '' if autype is None else autype 61 | 62 | if (start is not None) & (start != ''): 63 | end = Utility.getToday() if end is None or end == '' else end 64 | 65 | if ktype.upper() in cf.K_LABELS: 66 | fq = autype if autype is not None else '' 67 | if self.__code[:1] in ('1', '5') or index: 68 | fq = '' 69 | 70 | kline = '' if autype is None else 'fq' 71 | if ((start is None or start == '') or (end is None or end == '')) or (start == end): 72 | # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq¶m=sh600000,day,,,640,qfq&r=0.73327461855388564 73 | urls = [cf.HISTORY_URL % (kline, fq, symbol, cf.TT_K_TYPE[ktype.upper()], start, end, fq, Utility.random(17))] 74 | else: 75 | years = Utility.ttDates(start, end) 76 | urls = [] 77 | for year in years: 78 | startdate = str(year) + '-01-01' 79 | enddate = str(year+1) + '-12-31' 80 | # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq2008¶m=sh600000,day,2008-01-01,2009-12-31,640,qfq&r=0.73327461855388564 81 | url = cf.HISTORY_URL % (kline, fq+str(year), symbol, cf.TT_K_TYPE[ktype.upper()], startdate, enddate, fq, Utility.random(17)) 82 | urls.append(url) 83 | dataflag = '%s%s'%(fq, cf.TT_K_TYPE[ktype.upper()]) # qfqday 84 | elif ktype in cf.K_MIN_LABELS: 85 | # http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600000,m30,,640&_var=m30_today&r=0.5537154641907898 86 | urls = [cf.HISTORY_MIN_URL % (symbol, ktype, ktype, Utility.random(16))] 87 | dataflag = 'm%s'%ktype # m30 88 | else: 89 | raise TypeError('ktype input error.') 90 | 91 | for url in urls: 92 | self._data = self._data.append(self.__handleHistory(url, dataflag, symbol, index, ktype, retry, pause), ignore_index=True) 93 | if ktype not in cf.K_MIN_LABELS: 94 | if ((start is not None) & (start != '')) & ((end is not None) & (end != '')): 95 | self._data = self._data[(self._data.date >= start) & (self._data.date <= end)] 96 | 97 | return self._result() 98 | 99 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 100 | 101 | 102 | def __handleHistory(self, url, dataflag='', symbol='', index = False, ktype = '', retry=3, pause=0.001): 103 | for _ in range(retry): 104 | time.sleep(pause) 105 | 106 | try: 107 | request = self._session.get(url, timeout=10) 108 | if self._PY3: 109 | request.encoding = 'gbk' 110 | lines = request.text 111 | if len(lines) < 100: #no data 112 | return None 113 | except Exception as e: 114 | print(e) 115 | else: 116 | lines = lines.split('=')[1] 117 | reg = re.compile(r',{"nd.*?}') 118 | lines = re.subn(reg, '', lines) 119 | js = json.loads(lines[0]) 120 | dataflag = dataflag if dataflag in list(js['data'][symbol].keys()) else cf.TT_K_TYPE[ktype.upper()] 121 | 122 | if ktype in cf.K_MIN_LABELS: 123 | for value in js['data'][symbol][dataflag]: 124 | value.pop() 125 | value.pop() 126 | 127 | df = pd.DataFrame(js['data'][symbol][dataflag], columns=cf.KLINE_TT_COLS) 128 | df['code'] = symbol if index else self.__code 129 | if ktype in cf.K_MIN_LABELS: 130 | df['date'] = df['date'].map(lambda x: '%s-%s-%s %s:%s'%(x[0:4], x[4:6], x[6:8], x[8:10], x[10:12])) 131 | for col in df.columns[1:6]: 132 | df[col] = df[col].astype(float) 133 | 134 | return df 135 | 136 | 137 | def xrxd(self, date='', retry=3, pause=0.001): 138 | """ 139 | 获取股票除权除息信息 140 | Parameters 141 | ------ 142 | date: string 143 | format:YYYY-MM-DD 144 | retry: int, 默认 3 145 | 如遇网络等问题重复执行的次数 146 | pause : int, 默认 0 147 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 148 | 149 | return 150 | ------ 151 | Dict or None 152 | nd, 对应年份 153 | fh_sh, 分红数额 154 | djr, 股权登记日 155 | cqr, 除权日 156 | FHcontent, 除权除息信息 157 | """ 158 | data = None 159 | 160 | if not date: 161 | date = Utility.getToday() 162 | 163 | symbol = Utility.symbol(self.__code) 164 | 165 | for _ in range(retry): 166 | time.sleep(pause) 167 | 168 | url = cf.HISTORY_URL % ('fq', 'qfq', symbol, 'day', date, date, 'qfq', Utility.random(17)) 169 | try: 170 | request = self._session.get(url, timeout=10) 171 | pattern = re.compile(r'({"nd".+?})') 172 | result = re.search(pattern, request.text) 173 | if result: 174 | data = eval(result.group(1)) 175 | except Exception as e: 176 | print(str(e)) 177 | 178 | return data 179 | 180 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 181 | 182 | 183 | def realtime(self): 184 | """ 185 | 获取实时交易数据 getting real time quotes data 186 | 用于跟踪交易情况(本次执行的结果-上一次执行的数据) 187 | Parameters 188 | ------ 189 | stockdata(code) code : string, array-like object (list, tuple, Series). 190 | return 191 | ------- 192 | DataFrame or list: [{'name':, 'open':, ...}, ...] 193 | 0:name,股票名字 194 | 1:open,今日开盘价 195 | 2:pre_close,昨日收盘价 196 | 3:price,当前价格 197 | 4:high,今日最高价 198 | 5:low,今日最低价 199 | 6:bid,竞买价,即“买一”报价 200 | 7:ask,竞卖价,即“卖一”报价 201 | 8:volumn,成交量 maybe you need do volumn/100 202 | 9:amount,成交金额(元 CNY) 203 | 10:b1_v,委买一(笔数 bid volume) 204 | 11:b1_p,委买一(价格 bid price) 205 | 12:b2_v,“买二” 206 | 13:b2_p,“买二” 207 | 14:b3_v,“买三” 208 | 15:b3_p,“买三” 209 | 16:b4_v,“买四” 210 | 17:b4_p,“买四” 211 | 18:b5_v,“买五” 212 | 19:b5_p,“买五” 213 | 20:a1_v,委卖一(笔数 ask volume) 214 | 21:a1_p,委卖一(价格 ask price) 215 | ... 216 | 30:date,日期; 217 | 31:time,时间; 218 | """ 219 | self._data = pd.DataFrame() 220 | 221 | symbols_list = '' 222 | if isinstance(self.__code, list) or isinstance(self.__code, set) or isinstance(self.__code, tuple) or isinstance(self.__code, pd.Series): 223 | for code in self.__code: 224 | symbols_list += Utility.symbol(code) + ',' 225 | else: 226 | symbols_list = Utility.symbol(self.__code) 227 | symbols_list = symbols_list[:-1] if len(symbols_list) > 8 else symbols_list 228 | 229 | # http://hq.sinajs.cn/rn=4879967949085&list=sh600000,sh600004 230 | request = self._session.get( cf.LIVE_DATA_URL % (Utility.random(), symbols_list), timeout=10 ) 231 | request.encoding = 'gbk' 232 | reg = re.compile(r'="(.*?),?";') 233 | data = reg.findall(request.text) 234 | regSym = re.compile(r'(?:sh|sz)(.*?)\=') 235 | syms = regSym.findall(request.text) 236 | data_list = [] 237 | syms_list = [] 238 | for index, row in enumerate(data): 239 | if len(row)>1: 240 | data_list.append([astr for astr in row.split(',')]) 241 | syms_list.append(syms[index]) 242 | if len(syms_list) == 0: 243 | return None 244 | 245 | self._data = pd.DataFrame(data_list, columns=cf.LIVE_DATA_COLS) 246 | self._data = self._data.drop('s', axis=1) 247 | self._data['code'] = syms_list 248 | ls = [cls for cls in self._data.columns if '_v' in cls] 249 | for txt in ls: 250 | self._data[txt] = self._data[txt].map(lambda x : x[:-2]) 251 | 252 | self._data[self._data.astype(str)==""] = 0 253 | # 价格、成交量等转换为浮点类型 254 | for col in ['open', 'pre_close', 'price', 'high', 'low', 'bid', 'ask', 'volume','amount', 255 | 'b1_v', 'b1_p', 'b2_v', 'b2_p', 'b3_v', 'b3_p', 'b4_v', 'b4_p', 'b5_v', 'b5_p', 256 | 'a1_v', 'a1_p', 'a2_v', 'a2_p', 'a3_v', 'a3_p', 'a4_v', 'a4_p', 'a5_v', 'a5_p']: 257 | self._data[col] = self._data[col].astype(float) 258 | 259 | return self._result() 260 | 261 | 262 | def historyTicks(self, date=None, retry=3, pause=0.001): 263 | """ 264 | 获取历史分笔明细数据 265 | Parameters 266 | ------ 267 | date : string 268 | 日期 format:YYYY-MM-DD, 默认为前一个交易日 269 | retry : int, 默认 3 270 | 如遇网络等问题重复执行的次数 271 | pause : int, 默认 0 272 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 273 | return 274 | ------- 275 | DataFrame or list: [{'time':, 'price':, ...}, ...] 276 | time:时间 277 | price:成交价格 278 | change:价格变动 279 | volume:成交手 280 | amount:成交金额(元) 281 | type:买卖类型【买盘、卖盘、中性盘】 282 | """ 283 | self._data = pd.DataFrame() 284 | 285 | symbol = Utility.symbol(self.__code) 286 | date = Utility.lastTradeDate() if date is None else date 287 | 288 | try: 289 | self._writeHead() 290 | 291 | page = 1 292 | while(True): 293 | # http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php?symbol=sh600000&date=2018-12-26&page=1 294 | # http://market.finance.sina.com.cn/transHis.php?date=2019-01-25&symbol=sh600000&page=1 295 | url = cf.HISTORY_TICKS_URL % (date, symbol, page) 296 | tick_data = self.__handleTicks(url, cf.HISTORY_TICK_COLUMNS, retry, pause) 297 | if tick_data is not None: 298 | self._data = self._data.append(tick_data, ignore_index=True) 299 | page = page + 1 300 | else: 301 | break 302 | except Exception as er: 303 | print(str(er)) 304 | else: 305 | return self._result() 306 | 307 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 308 | 309 | 310 | def __handleTicks(self, url, column, retry, pause): 311 | self._writeConsole() 312 | 313 | for _ in range(retry): 314 | time.sleep(pause) 315 | 316 | try: 317 | html = lxml.html.parse(url) 318 | res = html.xpath('//table[@id=\"datatbl\"]/tbody/tr') 319 | if not res: 320 | return None 321 | 322 | if self._PY3: 323 | sarr = [etree.tostring(node).decode('utf-8') for node in res] 324 | else: 325 | sarr = [etree.tostring(node) for node in res] 326 | sarr = ''.join(sarr) 327 | sarr = '%s
'%sarr 328 | sarr = sarr.replace('--', '0') 329 | df = pd.read_html(StringIO(sarr), parse_dates=False) 330 | df = pd.read_html(StringIO(sarr), parse_dates=False)[0] 331 | df.columns = column 332 | if 'pchange' in column: 333 | df['pchange'] = df['pchange'].map(lambda x : x.replace('%', '')) 334 | except Exception as e: 335 | print(e) 336 | else: 337 | return df 338 | 339 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 340 | 341 | def todayTicks(self, retry=3, pause=0.001): 342 | """ 343 | 获取当日分笔明细数据 344 | Parameters 345 | ------ 346 | retry : int, 默认 3 347 | 如遇网络等问题重复执行的次数 348 | pause : int, 默认 0 349 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 350 | return 351 | ------- 352 | DataFrame or list: [{'time':, 'price':, ...}, ...] 353 | time:时间 354 | price:当前价格 355 | pchange:涨跌幅 356 | change:价格变动 357 | volume:成交手 358 | amount:成交金额(元) 359 | type:买卖类型【买盘、卖盘、中性盘】 360 | 361 | """ 362 | self._data = pd.DataFrame() 363 | 364 | if self.__code is None or len(self.__code)!=6 : 365 | return None 366 | 367 | if not Utility.isTradeDay(): 368 | return None 369 | 370 | # 不到交易时间 371 | openTime = time.mktime(time.strptime(Utility.getToday() + ' 09:25:00', '%Y-%m-%d %H:%M:%S')) 372 | now = time.time() 373 | if now < openTime: 374 | return None 375 | 376 | symbol = Utility.symbol(self.__code) 377 | date = Utility.getToday() 378 | 379 | for _ in range(retry): 380 | time.sleep(pause) 381 | 382 | try: 383 | # http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/CN_Transactions.getAllPageTime?date=2018-12-26&symbol=sh600000 384 | request = self._session.get( cf.TODAY_TICKS_PAGE_URL % (date, symbol), timeout=10 ) 385 | request.encoding = 'gbk' 386 | text = request.text 387 | data_dict = Utility.str2Dict(text) 388 | pages = len(data_dict['detailPages']) 389 | 390 | self._writeHead() 391 | for pNo in range(1, pages+1): 392 | # http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradedetail.php?symbol=sh600000&date=2018-12-26&page=1 393 | url = cf.TODAY_TICKS_URL % (symbol, date, pNo) 394 | self._data = self._data.append(self.__handleTicks(url, cf.TODAY_TICK_COLUMNS, retry, pause), ignore_index=True) 395 | except Exception as e: 396 | print(str(e)) 397 | else: 398 | return self._result() 399 | 400 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 401 | 402 | 403 | def bigDeal(self, date=None, vol=400, retry=3, pause=0.001): 404 | """ 405 | 获取大单数据 406 | Parameters 407 | ------ 408 | date:string 409 | 日期 format:YYYY-MM-DD 410 | retry : int, 默认 3 411 | 如遇网络等问题重复执行的次数 412 | pause : int, 默认 0 413 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 414 | return 415 | ------- 416 | DataFrame or list: [{'code':, 'name', ...}, ...] 417 | code:代码 418 | name:名称 419 | time:时间 420 | price:当前价格 421 | volume:成交手 422 | preprice :上一笔价格 423 | type:买卖类型【买盘、卖盘、中性盘】 424 | """ 425 | self._data = pd.DataFrame() 426 | 427 | if self.__code is None or len(self.__code) != 6 or date is None: 428 | return None 429 | 430 | symbol = Utility.symbol(self.__code) 431 | vol = vol*100 432 | 433 | for _ in range(retry): 434 | time.sleep(pause) 435 | 436 | try: 437 | # http://vip.stock.finance.sina.com.cn/quotes_service/view/cn_bill_download.php?symbol=sh600000&num=60&page=1&sort=ticktime&asc=0&volume=40000&amount=0&type=0&day=2018-12-26 438 | request = self._session.get( cf.SINA_DD % (symbol, vol, date), timeout=10 ) 439 | request.encoding = 'gbk' 440 | lines = request.text 441 | if len(lines) < 100: 442 | return None 443 | self._data = pd.read_csv(StringIO(lines), names=cf.SINA_DD_COLS, skiprows=[0]) 444 | if self._data is not None: 445 | self._data['code'] = self._data['code'].map(lambda x: x[2:]) 446 | except Exception as e: 447 | print(e) 448 | else: 449 | return self._result() 450 | 451 | raise IOError(cf.NETWORK_URL_ERROR_MSG) 452 | 453 | 454 | -------------------------------------------------------------------------------- /gugu/stockinfo.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 股票信息类 4 | Created on 2019/01/02 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | from __future__ import division 10 | 11 | import pandas as pd 12 | from pandas.compat import StringIO 13 | import json 14 | import lxml.html 15 | from lxml import etree 16 | import random 17 | import re 18 | import time 19 | from gugu.utility import Utility 20 | from gugu.base import Base, cf 21 | import sys 22 | 23 | ua_list = [ 24 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101', 25 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122', 26 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71', 27 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95', 28 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71', 29 | 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36', 30 | 'Mozilla/5.0 (Windows NT 5.1; U; en; rv:1.8.1) Gecko/20061208 Firefox/2.0.0 Opera 9.50', 31 | 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0', 32 | 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:69.0) Gecko/20100101 Firefox/69.0', 33 | ] 34 | headers = { 35 | 'Accept': '*/*', 36 | 'Accept-Encoding': 'gzip, deflate, br', 37 | 'Accept-Language': 'zh-CN,zh;q=0.9', 38 | 'Connection': 'keep-alive', 39 | 'Upgrade-Insecure-Requests': '1', 40 | 'User-Agent': random.choice(ua_list), 41 | 'Cache-Control': 'max-age=0', 42 | } 43 | 44 | class StockInfo(Base): 45 | def stockProfiles(self): 46 | """ 47 | 获取上市公司基于基本面的汇总数据信息 48 | Return 49 | -------- 50 | DataFrame or List: [{'symbol':, 'net_profit_cagr':, ...}, ...] 51 | symbol: 代码 52 | net_profit_cagr: 净利润复合年均增长率 53 | ps: 市销率 54 | percent: 涨幅 55 | pb_ttm: 滚动市净率 56 | float_shares: 流通股本 57 | current: 当前价格 58 | amplitude: 振幅 59 | pcf: 市现率 60 | current_year_percent: 今年涨幅 61 | float_market_capital: 流通市值 62 | market_capital: 总市值 63 | dividend_yield: 股息率 64 | roe_ttm: 滚动净资产收益率 65 | total_percent: 总涨幅 66 | income_cagr: 收益复合年均增长率 67 | amount: 成交额 68 | chg: 涨跌点数 69 | issue_date_ts: 发行日unix时间戳 70 | main_net_inflows: 主营净收入 71 | volume: 成交量 72 | volume_ratio: 量比 73 | pb: 市净率 74 | followers: 雪球网关注人数 75 | turnover_rate: 换手率 76 | name: 名称 77 | pe_ttm: 滚动市盈率 78 | total_shares: 总股本 79 | """ 80 | self._data = pd.DataFrame() 81 | 82 | self._writeHead() 83 | 84 | self._data = self.__handleStockProfiles() 85 | self._data['issue_date_ts'] = self._data['issue_date_ts'].map(lambda x: int(x/1000)) 86 | 87 | return self._result() 88 | 89 | def __handleStockProfiles(self): 90 | try: 91 | request = self._session.get(cf.XQ_HOME, headers=headers) 92 | cookies = request.cookies 93 | except Exception as e: 94 | print(str(e)) 95 | 96 | page = 1 97 | while True: 98 | self._writeConsole() 99 | 100 | try: 101 | timestamp = int(time.time()*1000) 102 | request = self._session.get(cf.XQ_STOCK_PROFILES_URL % (page, timestamp), headers=headers, cookies=cookies) 103 | 104 | dataDict = json.loads(request.text) 105 | if not dataDict.get('data').get('list'): 106 | break 107 | 108 | dataList = [] 109 | for row in dataDict.get('data').get('list'): 110 | dataList.append(row) 111 | 112 | self._data = self._data.append(pd.DataFrame(dataList, columns=cf.XQ_STOCK_PROFILES_COLS), ignore_index=True) 113 | 114 | page += 1 115 | time.sleep(1) 116 | except Exception as e: 117 | print(str(e)) 118 | 119 | return self._data 120 | 121 | def report(self, year, quarter, retry=3, pause=0.001): 122 | """ 123 | 获取业绩报表数据 124 | Parameters 125 | -------- 126 | year:int 年度 e.g:2014 127 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 128 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 129 | retry : int, 默认 3 130 | 如遇网络等问题重复执行的次数 131 | pause : int, 默认 0.001 132 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 133 | Return 134 | -------- 135 | DataFrame or List: [{'code':, 'name':, ...}, ...] 136 | code,代码 137 | name,名称 138 | eps,每股收益 139 | eps_yoy,每股收益同比(%) 140 | bvps,每股净资产 141 | roe,净资产收益率(%) 142 | epcf,每股现金流量(元) 143 | net_profits,净利润(万元) 144 | profits_yoy,净利润同比(%) 145 | distrib,分配方案 146 | report_date,发布日期 147 | """ 148 | self._data = pd.DataFrame() 149 | 150 | if Utility.checkQuarter(year, quarter) is True: 151 | self._writeHead() 152 | 153 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/mainindex/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 154 | self._data = self.__parsePage(cf.REPORT_URL, year, quarter, 1, cf.REPORT_COLS, pd.DataFrame(), retry, pause, 11) 155 | if self._data is not None: 156 | self._data['code'] = self._data['code'].map(lambda x:str(x).zfill(6)) 157 | 158 | return self._result() 159 | 160 | def profit(self, year, quarter, retry=3, pause=0.001): 161 | """ 162 | 获取盈利能力数据 163 | Parameters 164 | -------- 165 | year:int 年度 e.g:2014 166 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 167 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 168 | retry : int, 默认 3 169 | 如遇网络等问题重复执行的次数 170 | pause : int, 默认 0.001 171 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 172 | Return 173 | -------- 174 | DataFrame or List: [{'code':, 'name':, ...}, ...] 175 | code,代码 176 | name,名称 177 | roe,净资产收益率(%) 178 | net_profit_ratio,净利率(%) 179 | gross_profit_rate,毛利率(%) 180 | net_profits,净利润(万元) 181 | eps,每股收益 182 | business_income,营业收入(百万元) 183 | bips,每股主营业务收入(元) 184 | """ 185 | self._data = pd.DataFrame() 186 | 187 | if Utility.checkQuarter(year, quarter) is True: 188 | self._writeHead() 189 | 190 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/profit/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 191 | self._data = self.__parsePage(cf.PROFIT_URL, year, quarter, 1, cf.PROFIT_COLS, pd.DataFrame(), retry, pause) 192 | if self._data is not None: 193 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 194 | 195 | return self._result() 196 | 197 | def operation(self, year, quarter, retry=3, pause=0.001): 198 | """ 199 | 获取营运能力数据 200 | Parameters 201 | -------- 202 | year:int 年度 e.g:2014 203 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 204 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 205 | retry : int, 默认 3 206 | 如遇网络等问题重复执行的次数 207 | pause : int, 默认 0.001 208 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 209 | Return 210 | -------- 211 | DataFrame or List: [{'code':, 'name':, ...}, ...] 212 | code,代码 213 | name,名称 214 | arturnover,应收账款周转率(次) 215 | arturndays,应收账款周转天数(天) 216 | inventory_turnover,存货周转率(次) 217 | inventory_days,存货周转天数(天) 218 | currentasset_turnover,流动资产周转率(次) 219 | currentasset_days,流动资产周转天数(天) 220 | """ 221 | self._data = pd.DataFrame() 222 | 223 | if Utility.checkQuarter(year, quarter) is True: 224 | self._writeHead() 225 | 226 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/operation/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 227 | self._data = self.__parsePage(cf.OPERATION_URL, year, quarter, 1, cf.OPERATION_COLS, pd.DataFrame(), retry, pause) 228 | if self._data is not None: 229 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 230 | 231 | return self._result() 232 | 233 | def growth(self, year, quarter, retry=3, pause=0.001): 234 | """ 235 | 获取成长能力数据 236 | Parameters 237 | -------- 238 | year:int 年度 e.g:2014 239 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 240 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 241 | retry : int, 默认 3 242 | 如遇网络等问题重复执行的次数 243 | pause : int, 默认 0.001 244 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 245 | Return 246 | -------- 247 | DataFrame or List: [{'code':, 'name':, ...}, ...] 248 | code,代码 249 | name,名称 250 | mbrg,主营业务收入增长率(%) 251 | nprg,净利润增长率(%) 252 | nav,净资产增长率 253 | targ,总资产增长率 254 | epsg,每股收益增长率 255 | seg,股东权益增长率 256 | """ 257 | self._data = pd.DataFrame() 258 | 259 | if Utility.checkQuarter(year, quarter) is True: 260 | self._writeHead() 261 | 262 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/grow/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 263 | self._data = self.__parsePage(cf.GROWTH_URL, year, quarter, 1, cf.GROWTH_COLS, pd.DataFrame(), retry, pause) 264 | if self._data is not None: 265 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 266 | 267 | return self._result() 268 | 269 | def debtPaying(self, year, quarter, retry=3, pause=0.001): 270 | """ 271 | 获取偿债能力数据 272 | Parameters 273 | -------- 274 | year:int 年度 e.g:2014 275 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 276 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 277 | retry : int, 默认 3 278 | 如遇网络等问题重复执行的次数 279 | pause : int, 默认 0.001 280 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 281 | Return 282 | -------- 283 | DataFrame or List: [{'code':, 'name':, ...}, ...] 284 | code,代码 285 | name,名称 286 | currentratio,流动比率 287 | quickratio,速动比率 288 | cashratio,现金比率 289 | icratio,利息支付倍数 290 | sheqratio,股东权益比率 291 | adratio,股东权益增长率 292 | """ 293 | self._data = pd.DataFrame() 294 | 295 | if Utility.checkQuarter(year, quarter) is True: 296 | self._writeHead() 297 | 298 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/debtpaying/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 299 | self._data = self.__parsePage(cf.DEBTPAYING_URL, year, quarter, 1, cf.DEBTPAYING_COLS, pd.DataFrame(), retry, pause) 300 | if self._data is not None: 301 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 302 | 303 | return self._result() 304 | 305 | def cashFlow(self, year, quarter, retry=3, pause=0.001): 306 | """ 307 | 获取现金流量数据 308 | Parameters 309 | -------- 310 | year:int 年度 e.g:2014 311 | quarter:int 季度 :1、2、3、4,只能输入这4个季度 312 | 说明:由于是从网站获取的数据,需要一页页抓取,速度取决于您当前网络速度 313 | retry : int, 默认 3 314 | 如遇网络等问题重复执行的次数 315 | pause : int, 默认 0.001 316 | 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 317 | Return 318 | -------- 319 | DataFrame or List: [{'code':, 'name':, ...}, ...] 320 | code,代码 321 | name,名称 322 | cf_sales,经营现金净流量对销售收入比率 323 | rateofreturn,资产的经营现金流量回报率 324 | cf_nm,经营现金净流量与净利润的比率 325 | cf_liabilities,经营现金净流量对负债比率 326 | cashflowratio,现金流量比率 327 | """ 328 | self._data = pd.DataFrame() 329 | 330 | if Utility.checkQuarter(year, quarter) is True: 331 | self._writeHead() 332 | 333 | # http://vip.stock.finance.sina.com.cn/q/go.php/vFinanceAnalyze/kind/cashflow/index.phtml?s_i=&s_a=&s_c=&reportdate=2018&quarter=3&p=1&num=60 334 | self._data = self.__parsePage(cf.CASHFLOW_URL, year, quarter, 1, cf.CASHFLOW_COLS, pd.DataFrame(), retry, pause) 335 | if self._data is not None: 336 | self._data['code'] = self._data['code'].map(lambda x: str(x).zfill(6)) 337 | 338 | return self._result() 339 | 340 | def __parsePage(self, url, year, quarter, page, column, dataArr, retry, pause, drop_column=None): 341 | self._writeConsole() 342 | 343 | for _ in range(retry): 344 | time.sleep(pause) 345 | 346 | try: 347 | request = self._session.get( url % (year, quarter, page, cf.PAGE_NUM[1]), timeout=10 ) 348 | request.encoding = 'gbk' 349 | text = request.text.replace('--', '') 350 | html = lxml.html.parse(StringIO(text)) 351 | res = html.xpath("//table[@class=\"list_table\"]/tr") 352 | if self._PY3: 353 | sarr = [etree.tostring(node).decode('utf-8') for node in res] 354 | else: 355 | sarr = [etree.tostring(node) for node in res] 356 | sarr = ''.join(sarr) 357 | sarr = '%s
'%sarr 358 | df = pd.read_html(sarr)[0] 359 | if drop_column is not None: 360 | df = df.drop(drop_column, axis=1) 361 | df.columns = column 362 | dataArr = dataArr.append(df, ignore_index=True) 363 | nextPage = html.xpath('//div[@class=\"pages\"]/a[last()]/@onclick') 364 | if len(nextPage) > 0: 365 | page = re.findall(r'\d+', nextPage[0])[0] 366 | return self.__parsePage(url, year, quarter, page, column, dataArr, retry, pause, drop_column) 367 | else: 368 | return dataArr 369 | except Exception as e: 370 | print(e) 371 | 372 | raise IOError(cf.NETWORK_URL_ERROR_MSG) -------------------------------------------------------------------------------- /gugu/utility.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | """ 3 | 公用类 4 | Created on 2019/01/15 5 | @author: TabQ 6 | @group : gugu 7 | @contact: 16621596@qq.com 8 | """ 9 | 10 | from __future__ import division 11 | import datetime 12 | import json 13 | import requests 14 | from requests.adapters import HTTPAdapter 15 | from urllib3.util import Retry 16 | import gugu.config as cf 17 | 18 | class Utility(): 19 | @staticmethod 20 | def str2Dict(string): 21 | string = eval(string, type('Dummy', (dict,), dict(__getitem__ = lambda s, n:n))()) 22 | string = json.dumps(string) 23 | 24 | return json.loads(string) 25 | 26 | 27 | @staticmethod 28 | def fceil(number, ndigits=2): 29 | if number == 0: 30 | return number 31 | 32 | tail_number = 1 / pow(10, ndigits) 33 | ndigits_number = round(number, ndigits) 34 | 35 | result = number - ndigits_number 36 | if result == 0: 37 | return number 38 | elif result > 0: 39 | return ndigits_number + tail_number 40 | else: 41 | return ndigits_number 42 | 43 | 44 | @staticmethod 45 | def checkQuarter(year, quarter): 46 | if isinstance(year, str) or year < 1989 : 47 | raise TypeError(cf.DATE_CHK_MSG) 48 | elif quarter is None or isinstance(quarter, str) or quarter not in [1, 2, 3, 4]: 49 | raise TypeError(cf.DATE_CHK_Q_MSG) 50 | else: 51 | return True 52 | 53 | 54 | @staticmethod 55 | def checkLhbInput(last): 56 | if last not in [5, 10, 30, 60]: 57 | raise TypeError(cf.LHB_MSG) 58 | else: 59 | return True 60 | 61 | 62 | @staticmethod 63 | def symbol(code): 64 | """ 65 | 生成symbol代码标志 66 | """ 67 | if code in cf.INDEX_LABELS: 68 | return cf.INDEX_LIST[code] 69 | elif len(code) != 6 : 70 | return '' 71 | else: 72 | # 可转债代码 73 | cb_code = code[:2] 74 | if cb_code == '11': 75 | return 'sh%s' % code 76 | if cb_code == '12': 77 | return 'sz%s' % code 78 | 79 | return 'sh%s' % code if code[:1] in ['5', '6', '9'] else 'sz%s' % code 80 | 81 | 82 | @staticmethod 83 | def random(n=13): 84 | from random import randint 85 | 86 | start = 10**(n-1) 87 | end = (10**n)-1 88 | 89 | return str(randint(start, end)) 90 | 91 | 92 | @staticmethod 93 | def getToday(): 94 | return str(datetime.datetime.today().date()) 95 | 96 | 97 | @staticmethod 98 | def getHour(): 99 | return datetime.datetime.today().hour 100 | 101 | 102 | @staticmethod 103 | def getMonth(): 104 | return datetime.datetime.today().month 105 | 106 | 107 | @staticmethod 108 | def getYear(): 109 | return datetime.datetime.today().year 110 | 111 | 112 | @staticmethod 113 | def getTodayLastYear(): 114 | return str(Utility.getDateInterval(days=-365)) 115 | 116 | 117 | @staticmethod 118 | def getTodayLastMonth(): 119 | return str(Utility.getDateInterval(days=-30)) 120 | 121 | 122 | @staticmethod 123 | def getTodayLastWeek(): 124 | return str(Utility.getDateInterval(days=-7)) 125 | 126 | 127 | @staticmethod 128 | def getDateInterval(date=str(datetime.datetime.today().date()), days=-1): 129 | return datetime.datetime.strptime(date, '%Y-%m-%d').date() + datetime.timedelta(days) 130 | 131 | 132 | @staticmethod 133 | def diffDays(start=None, end=None): 134 | d1 = datetime.datetime.strptime(end, '%Y-%m-%d') 135 | d2 = datetime.datetime.strptime(start, '%Y-%m-%d') 136 | delta = d1 - d2 137 | 138 | return delta.days 139 | 140 | 141 | @staticmethod 142 | def ttDates(start='', end=''): 143 | startyear = int(start[0:4]) 144 | endyear = int(end[0:4]) 145 | dates = [d for d in range(startyear, endyear+1, 2)] 146 | 147 | return dates 148 | 149 | 150 | @staticmethod 151 | def lastTradeDate(date=str(datetime.datetime.today().date())): 152 | lastdate = str(Utility.getDateInterval(date, -1)) 153 | 154 | if Utility.isTradeDay(lastdate): 155 | return lastdate 156 | else: 157 | return Utility.lastTradeDate(lastdate) 158 | 159 | 160 | @staticmethod 161 | def nextTradeDate(date=str(datetime.datetime.today().date())): 162 | nextdate = str(Utility.getDateInterval(date, 1)) 163 | 164 | if Utility.isTradeDay(nextdate): 165 | return nextdate 166 | else: 167 | return Utility.nextTradeDate(nextdate) 168 | 169 | 170 | @staticmethod 171 | def isHoliday(date=str(datetime.datetime.today().date())): 172 | """ 173 | 节假日判断 174 | Parameters 175 | ------ 176 | date: string 177 | 查询日期 format:YYYY-MM-DD 为空时取当前日期 178 | return 179 | ------ 180 | True or False 181 | """ 182 | session = requests.Session() 183 | retry = Retry(connect=5, backoff_factor=1) 184 | adapter = HTTPAdapter(max_retries=retry) 185 | session.mount('http://', adapter) 186 | session.keep_alive = False 187 | 188 | try: 189 | request = session.get( cf.HOLIDAY_URL % date, timeout=10 ) 190 | dataDict = json.loads(request.text) 191 | if dataDict['code'] != 0: 192 | raise IOError(cf.HOLIDAY_SERVE_ERR) 193 | elif dataDict['holiday'] is None: 194 | return False 195 | else: 196 | return True 197 | except Exception as e: 198 | print(e) 199 | 200 | 201 | @staticmethod 202 | def isTradeDay(date=str(datetime.datetime.today().date())): 203 | """ 204 | 交易日判断 205 | Parameters 206 | ------ 207 | date: string 208 | 查询日期 format:YYYY-MM-DD 为空时取当前日期 209 | return 210 | ------ 211 | True or False 212 | """ 213 | date = datetime.datetime.strptime(date, '%Y-%m-%d') 214 | w = int(date.strftime('%w')) 215 | 216 | if w in [6, 0]: 217 | return False 218 | else: 219 | return not Utility.isHoliday(date) 220 | 221 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [egg_info] 2 | tag_build = 3 | tag_date = 0 4 | tag_svn_revision = 0 5 | 6 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from setuptools import setup, find_packages 4 | 5 | try: 6 | import pypandoc 7 | long_description = pypandoc.convert('README.md', 'rst') 8 | except (IOError, ImportError): 9 | long_description = '' 10 | 11 | 12 | setup( 13 | name='gugu', 14 | version='0.1.8', 15 | description='an api for getting financial data', 16 | long_description=long_description, 17 | author='TabQ', 18 | author_email='16621596@qq.com', 19 | url='http://www.infodata.cc', 20 | license="Apache License, Version 2.0", 21 | packages=find_packages(), 22 | install_requires=[ 23 | 'pandas==0.24.1', 24 | 'requests', 25 | 'lxml', 26 | 'simplejson', 27 | 'xlrd' 28 | ], 29 | keywords='Financial Data Crawler', 30 | classifiers=['Development Status :: 4 - Beta', 31 | 'Programming Language :: Python :: 2.6', 32 | 'Programming Language :: Python :: 2.7', 33 | 'Programming Language :: Python :: 3.2', 34 | 'Programming Language :: Python :: 3.3', 35 | 'Programming Language :: Python :: 3.4', 36 | 'Programming Language :: Python :: 3.5'], 37 | ) 38 | --------------------------------------------------------------------------------