├── .gitignore ├── Pipfile ├── README.md ├── app ├── __init__.py ├── get_data.py ├── get_go_market.py ├── stock_compare_intial.py └── track.py ├── config ├── database_sample.yml └── track_sample.yml ├── doc ├── plan.md └── plan_for_feature.md ├── requirements.txt └── run.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 2 | 3 | *.iml 4 | 5 | ## Directory-based project format: 6 | .idea/ 7 | # if you remove the above rule, at least ignore the following: 8 | 9 | # User-specific stuff: 10 | # .idea/workspace.xml 11 | # .idea/tasks.xml 12 | # .idea/dictionaries 13 | # .idea/shelf 14 | 15 | # Sensitive or high-churn files: 16 | # .idea/dataSources.ids 17 | # .idea/dataSources.xml 18 | # .idea/sqlDataSources.xml 19 | # .idea/dynamic.xml 20 | # .idea/uiDesigner.xml 21 | 22 | # Gradle: 23 | # .idea/gradle.xml 24 | # .idea/libraries 25 | 26 | # Mongo Explorer plugin: 27 | # .idea/mongoSettings.xml 28 | 29 | ## File-based project format: 30 | *.ipr 31 | *.iws 32 | *.xlsx 33 | ## Plugin-specific files: 34 | 35 | # IntelliJ 36 | /out/ 37 | 38 | # mpeltonen/sbt-idea plugin 39 | .idea_modules/ 40 | 41 | # JIRA plugin 42 | atlassian-ide-plugin.xml 43 | 44 | # Crashlytics plugin (for Android Studio and IntelliJ) 45 | com_crashlytics_export_strings.xml 46 | crashlytics.properties 47 | crashlytics-build.properties 48 | fabric.properties 49 | # Byte-compiled / optimized / DLL files 50 | __pycache__/ 51 | *.py[cod] 52 | *$py.class 53 | 54 | # C extensions 55 | *.so 56 | 57 | # Distribution / packaging 58 | .Python 59 | env/ 60 | build/ 61 | develop-eggs/ 62 | dist/ 63 | downloads/ 64 | eggs/ 65 | .eggs/ 66 | #lib/ 67 | lib64/ 68 | parts/ 69 | sdist/ 70 | var/ 71 | *.egg-info/ 72 | .installed.cfg 73 | *.egg 74 | 75 | # PyInstaller 76 | # Usually these files are written by a python script from a template 77 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 78 | *.manifest 79 | *.spec 80 | 81 | # Installer logs 82 | pip-log.txt 83 | pip-delete-this-directory.txt 84 | 85 | # Unit test / coverage reports 86 | htmlcov/ 87 | .tox/ 88 | .coverage 89 | .coverage.* 90 | .cache 91 | nosetests.xml 92 | coverage.xml 93 | *,cover 94 | .hypothesis/ 95 | 96 | # Translations 97 | *.mo 98 | *.pot 99 | 100 | # Django stuff: 101 | *.log 102 | 103 | # Sphinx documentation 104 | docs/_build/ 105 | 106 | # PyBuilder 107 | target/ 108 | [._]*.s[a-w][a-z] 109 | [._]s[a-w][a-z] 110 | *.un~ 111 | Session.vim 112 | .netrwhist 113 | *~ 114 | mail_address* 115 | test* 116 | text_read.py 117 | *test* 118 | py/password_hash/.idea 119 | */.idea 120 | config/database.yml 121 | config/track.yml 122 | venv 123 | tmp 124 | -------------------------------------------------------------------------------- /Pipfile: -------------------------------------------------------------------------------- 1 | [[source]] 2 | url = "https://pypi.python.org/simple" 3 | verify_ssl = true 4 | name = "pypi" 5 | 6 | [packages] 7 | numpy = "==1.12.1" 8 | pandas = "==0.19.2" 9 | "psycopg2" = "==2.7.1" 10 | python-dateutil = "==2.6.0" 11 | pytz = "==2017.2" 12 | pyyaml = "==3.12" 13 | requests = "==2.13.0" 14 | six = "==1.10.0" 15 | sqlalchemy = "==1.1.9" 16 | tushare = "==0.7.4" 17 | 18 | [dev-packages] 19 | 20 | [requires] 21 | python_version = "3.6" 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # istock 2 | istock 是一个抓取沪深股市的行情,并按照个人的喜好进行分析和整理的项目。集成了数据抓取、量化分析、以及制定相关的选股测略等相关的模块。深度分析股票涨跌的相关因素,找到一个合理的风险控制策略,保证稳定的盈利状态。 3 | 4 | ### 配置项目 5 | 6 | - 初始化环境 7 | 8 | ```shell 9 | $ git clone https://github.com/itpubs/istock.git 10 | $ cd istock 11 | $ pyvenv venv 12 | $ source venv/bin/activate 13 | $ pip install -r requirements.txt 14 | ``` 15 | 16 | - 配置项目 17 | 18 | ```shell 19 | $ ./run.py --init 20 | ``` 21 | 22 | -------------------------------------------------------------------------------- /app/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexcn/istock/53512ec6ab013a082e3b5b794ec2c77eaead956d/app/__init__.py -------------------------------------------------------------------------------- /app/get_data.py: -------------------------------------------------------------------------------- 1 | import tushare as ts 2 | import psycopg2 3 | from sqlalchemy.dialects.postgresql import * 4 | 5 | from yaml import load 6 | 7 | try: 8 | from yaml import CLoader as Loader 9 | except: 10 | from yaml import Loader 11 | data_stream = open('../config/database.yml', 'r') 12 | db_config = load(data_stream, Loader=Loader) 13 | data_stream.close() 14 | 15 | USER = db_config['production']['username'] 16 | HOST = db_config['production']['host'] 17 | PASSWORD = db_config['production']['password'] 18 | DATABASE = db_config['production']['database'] 19 | 20 | 21 | 22 | 23 | engine = 'postgres://%s:%s@%s/%s' % (USER, PASSWORD, HOST, DATABASE) 24 | 25 | 26 | conn = psycopg2.connect(host=db_config['production']['host'], database=db_config['production']['database'], user=db_config['production']['username'], password=db_config['production']['password']) 27 | 28 | cur = conn.cursor() 29 | 30 | 31 | # 获取历史数据,存到数据库 32 | hist_data_type = { 33 | 'date': DATE, 34 | 'code': CHAR(6) 35 | } 36 | cur.execute('select distinct code from stock_basics;') 37 | 38 | r = cur.fetchall() 39 | 40 | for row in r: 41 | print('获取 ' + row[0] + ' 的历史行情') 42 | d = ts.get_hist_data(row[0]) 43 | d['code'] = row[0] 44 | d.to_sql('hist_data', engine, if_exists='append', dtype=hist_data_type) 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /app/get_go_market.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import tushare as ts 5 | 6 | all_stock = ts.get_stock_basics() 7 | 8 | print(len(all_stock)) 9 | -------------------------------------------------------------------------------- /app/stock_compare_intial.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import tushare as ts 5 | 6 | stock_basic_info = ts.get_stock_basics().index 7 | 8 | for item in stock_basic_info: 9 | print(item) 10 | 11 | # hist = ts.get_k_data() 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/track.py: -------------------------------------------------------------------------------- 1 | import tushare as ts 2 | from yaml import load 3 | 4 | try: 5 | from yaml import CLoader as Loader 6 | except: 7 | from yaml import Loader 8 | 9 | 10 | # code here 11 | 12 | 13 | config_stream = open('../config/config.yml', 'r') 14 | config = load(data_stream, Loader=Loader) 15 | config_stream.close() 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /config/database_sample.yml: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | 3 | default: &default 4 | port: 5432 5 | host: localhost 6 | 7 | development: &development 8 | <<: *default 9 | database: 10 | username: 11 | password: 12 | 13 | test: &test 14 | <<: *development 15 | 16 | production: &production 17 | <<: *development 18 | -------------------------------------------------------------------------------- /config/track_sample.yml: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | 3 | 4 | # 这里可以填写需要最终股票的代码 5 | stock: 6 | code: 7 | - 000001 8 | - 000002 9 | positions: # 持仓股票 10 | '000001': 1.1 # 股票代码: 成本价格 -------------------------------------------------------------------------------- /doc/plan.md: -------------------------------------------------------------------------------- 1 | 1、实现股票数据的抓取 2 | - 实现分析成交量的功能 3 | - 抓起后整理数据,整理数据后绘图(已经实现的功能) 4 | - 找出股票的异动辩护,分析市场的趋势,找出最好的量化分析模型。 5 | - 获取分时成交量,获取多空双方市场的博弈。 6 | 2、实现绘图功能 7 | - 建立可以持续盈利的量化模型 8 | - 数据展现于综合分析 9 | 3、实现分析功能(分析个股以及群体的特征) 10 | - 实现尽可能多的纬度的分析 11 | - 如果无法客服GIL的问题,则通过多进程的方式进行抓去数据 12 | - 可以考虑提供图形界面 13 | - 使用 PostgreSQL 存储历史数据以及分析结果 14 | 4、对整体股票进行统计 15 | 5、实现量化交易策略 16 | -------------------------------------------------------------------------------- /doc/plan_for_feature.md: -------------------------------------------------------------------------------- 1 | 抓去每一支股票的所有历史数据,并存到数据库 2 | - 实现对象关系映射 3 | - 提供restful接口 4 | - 输出图形 5 | 6 | 分析每一支股票的趋势(较上市 、最高点、最低点、平均值、以及当前的状况 包括当前的市场信心) 7 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.12.1 2 | pandas==0.19.2 3 | psycopg2==2.7.1 4 | PyYAML==3.12 5 | SQLAlchemy==1.1.9 6 | tushare==0.7.4 7 | -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | #!venv/bin/python 2 | # -*- encoding: utf-8 -*- 3 | 4 | 5 | import argparse 6 | 7 | 8 | parser = argparse.ArgumentParser(description='Command line tool to manage the project.') 9 | 10 | 11 | parser.add_argument(dest='filenames', metavar='filename', nargs='*') 12 | 13 | parser.add_argument('-p', '--pat', metavar='pattern', required=True, 14 | dest='patterns', action='append', 15 | help='text pattern to search for') 16 | 17 | parser.add_argument('-v', dest='verbose', action='store_true', 18 | help='verbose mode') 19 | 20 | parser.add_argument('-o', dest='outfile', action='store', 21 | help='output file') 22 | 23 | parser.add_argument('--speed', dest='speed', action='store', 24 | choices={'slow', 'fast'}, default='slow', 25 | help='search speed') 26 | 27 | args = parser.parse_args() 28 | 29 | # Output the collected arguments 30 | print(args.filenames) 31 | print(args.patterns) 32 | print(args.verbose) 33 | print(args.outfile) 34 | print(args.speed) 35 | --------------------------------------------------------------------------------