├── LICENSE ├── codes └── daily_classfier.py ├── .gitignore └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 挖地兔 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /codes/daily_classfier.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | from datetime import datetime, timedelta 4 | import tushare as ts 5 | 6 | # 首次使用,需要设置token 7 | # ts.set_token("******") 8 | 9 | 10 | def daily_classifier(ts_code, trade_date): 11 | """ A 股每日走势的分类 12 | 13 | 使用该方法前,请仔细阅读:http://blog.sina.com.cn/s/blog_486e105c010009uy.html 14 | 15 | 每个交易日产生 8 根 30 分钟 K 线,分别在前三根和最后三根中计算中枢,其结果分以下情况: 16 | 1)没有中枢; 17 | 2)仅有一个中枢,或计算得到的两个中枢区间有重叠; 18 | 3)有两个中枢,且没有重叠。 19 | """ 20 | start_date = datetime.strptime(trade_date, '%Y%m%d') 21 | end_date = start_date + timedelta(days=1) 22 | end_date = end_date.date().__str__().replace("-", "") 23 | 24 | df = ts.pro_bar(ts_code=ts_code, freq='30min', start_date=trade_date, end_date=end_date) 25 | df.sort_values('trade_time', inplace=True) 26 | data = df[['ts_code', 'trade_time', 'high', 'low']].iloc[1:, :] 27 | data = data.reset_index(drop=True) 28 | assert len(data) == 8, "每个交易日,A股有且只有8跟30分钟K线" 29 | 30 | def _central(tri): 31 | c_low = max(tri['low']) 32 | c_high = min(tri['high']) 33 | if c_low >= c_high: 34 | # None means no central found 35 | central = None 36 | else: 37 | central = { 38 | "time_span": "%s - %s" % (tri.iloc[0, 1], tri.iloc[2, 1]), 39 | "price_span": (c_low, c_high) 40 | } 41 | return central 42 | 43 | first_central = _central(data.iloc[:3, :]) 44 | last_central = _central(data.iloc[-3:, :]) 45 | return first_central, last_central 46 | 47 | 48 | if __name__ == '__main__': 49 | fc, lc = daily_classifier('600122.SH', "20190521") 50 | print(fc, lc) 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | .idea/ 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # Environments 86 | .env 87 | .venv 88 | env/ 89 | venv/ 90 | ENV/ 91 | env.bak/ 92 | venv.bak/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | .spyproject 97 | 98 | # Rope project settings 99 | .ropeproject 100 | 101 | # mkdocs documentation 102 | /site 103 | 104 | # mypy 105 | .mypy_cache/ 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tushare_tutorial 2 | 3 | [Tushare金融大数据开放社区](https://tushare.pro/)免费提供各类金融数据和区块链数据 , 助力智能投资与创新型投资。欢迎使用,[点击注册](https://tushare.pro/register?reg=7) 4 | 5 | ## Examples (click to see codes) 6 | 7 | * [使用通用行情接口实现缠论中的每日走势分类](./codes/daily_classfier.py) 8 | 9 | 10 | 11 | ## 《挖地兔》公众号文章列表 12 | 13 | | 文章标题 | 发布时间 | 作者 | 14 | | ------------------------------------------------------------ | ---------- | -------------- | 15 | | [如何正确使用Pandas库提升项目的运行速度?]() | 2019-05-21 | One | 16 | | [被动市场有效性是否真的有作用?]() | 2019-05-17 | Thoudancer | 17 | | [如何聪明的配置Smart Beta?]() | 2019-05-15 | 404 | 18 | | [他才是有史以来最伟大的因子组合投资者]() | 2019-04-22 | 房庄颜 | 19 | | [你真的懂线程吗?史上最全Python线程解析]() | 2019-04-14 | Little monster | 20 | | [利用Tushare数据实现知识图谱效果]() | 2019-04-10 | Libra | 21 | | [利用python进行蒙特卡罗模拟]() | 2019-04-03 | One | 22 | | [量化投资,你需要了解的A股财务数据]() | 2019-03-14 | 刀疤连;方信光 | 23 | | [这些方法解决了数据清洗80%的工作量]() | 2019-05-07 | Little Monster | 24 | | [Python数据分析从小白到高手的几个步骤]() | 2019-01-24 | 米哥 | 25 | | [利用Python实现摘要自动提取,完美瘦身只需一行代码]() | 2018-12-28 | 曾斌 | 26 | | [新闻联播也可以拿来做数据分析?]() | 2018-12-24 | 米哥 | 27 | | [在Jupyter Notebook构建规范和可复用的数据分析过程]() | 2018-11-26 | 米哥 | 28 | | [利用Python搞定期货数据,获得免费API]() | 2018-11-20 | 米哥 | 29 | | [像Python一样方便的用R获取金融数据]() | 2018-11-05 | 楚宵、晓子 | 30 | | [利用Matlab获取免费金融数据]() | 2018-11-01 | Lianrui Fu | 31 | | [通过http调取Tushare数据]() | 2018-10-25 | 米哥 | 32 | | [Tushare金融大数据开放社区发布]() | 2018-08-13 | 米哥 | 33 | | [数据接口之可转债及相关]() | 2017-11-19 | 米哥 | 34 | | [中国股市指数,了解一下]() | 2018-09-07 | 米哥 | 35 | --------------------------------------------------------------------------------