├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md └── plot ├── 111.md ├── hs300_kline_5min.html ├── k_candle.gif ├── kline_candle.py └── pyecharts_k-candle.png /.gitattributes: -------------------------------------------------------------------------------- 1 | *.js linguist-language=python 2 | *.css linguist-language=python 3 | *.html linguist-language=python -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 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 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | 49 | # Translations 50 | *.mo 51 | *.pot 52 | 53 | # Django stuff: 54 | *.log 55 | local_settings.py 56 | 57 | # Flask stuff: 58 | instance/ 59 | .webassets-cache 60 | 61 | # Scrapy stuff: 62 | .scrapy 63 | 64 | # Sphinx documentation 65 | docs/_build/ 66 | 67 | # PyBuilder 68 | target/ 69 | 70 | # Jupyter Notebook 71 | .ipynb_checkpoints 72 | 73 | # pyenv 74 | .python-version 75 | 76 | # celery beat schedule file 77 | celerybeat-schedule 78 | 79 | # SageMath parsed files 80 | *.sage.py 81 | 82 | # dotenv 83 | .env 84 | 85 | # virtualenv 86 | .venv 87 | venv/ 88 | ENV/ 89 | 90 | # Spyder project settings 91 | .spyderproject 92 | .spyproject 93 | 94 | # Rope project settings 95 | .ropeproject 96 | 97 | # mkdocs documentation 98 | /site 99 | 100 | # mypy 101 | .mypy_cache/ 102 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 willowj 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #画k线蜡烛图 2 | 3 | [ **python_dataEE/plot/kline_candle.py** ](https://github.com/willowj/python_dataEE/blob/master/plot/kline_candle.py) 4 | 5 | > python: jupyter-notbook #*建议使用jupyter-notbook* 6 | 7 | ```python 8 | from kline_candle import Kline_js 9 | import tushare as ts 10 | df = ts.get_hist_data('hs300', ktype='5') 11 | Kline_js('hs300_k-line-5min', 12 | df, 13 | ma=('ma10', 'ma20'), 14 | width=800, height=400, 15 | ) 16 | ``` 17 | 18 | ![image](https://github.com/willowj/python_dataEE/blob/master/plot/k_candle.gif) 19 | 20 | params: 21 | 22 | > name, df, prices_cols=None, ma=('ma10',), width=1600, height=750, kline_xaxis_pos='top', render_path=None 23 | 24 | - name: *str* #图例名称 25 | - df: *pandas.DataFrame* #columns包含 prices_cols、‘volume’ 26 | - prices_cols : *list* #默认 [u'open', u'close', u'low', u'high'] 27 | - ma=('ma10',): *list or tuple* #移动平均周期 28 | - width=1600, height=750 # 默认图片大小 29 | - kline_xaxis_pos='top' #k-line图例默认在上方 30 | - render_path: *str* #html file path to save 31 | 32 | https://github.com/willowj/python_dataEE/blob/master/plot/kline_candle.py 33 | 34 | required modules : 35 | 36 | > pyecharts 37 | 38 | > pandas 39 | 40 | > tushare (for test) 41 | -------------------------------------------------------------------------------- /plot/111.md: -------------------------------------------------------------------------------- 1 | ![image](k_candle.gif) -------------------------------------------------------------------------------- /plot/k_candle.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/willowj/python_dataEE/ce50f9bd0a022a73569465ec1678f0263f11851e/plot/k_candle.gif -------------------------------------------------------------------------------- /plot/kline_candle.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # 2017/09/16 3 | # author : willow_j 4 | # email : willow_j@foxmail.com 5 | 6 | from __future__ import unicode_literals 7 | from __future__ import print_function 8 | 9 | from pyecharts import Kline, Bar, Line, Grid, Overlap 10 | 11 | 12 | def kline_js(name, df, prices_cols=None, ma=('ma10',), width=1600, height=750, kline_xaxis_pos='top', render_path=None): 13 | ''' 14 | @params: 15 | - name: str #图例名称 16 | - df: pandas.DataFrame #columns包含 prices_cols、‘volume’ 17 | - prices_cols : list #默认 [u'open', u'close', u'low', u'high'] 18 | - ma=('ma10',): list or tuple #移动平均周期 19 | - width=1600, height=750 # 默认图片大小 20 | - kline_xaxis_pos='top' #k-line图例默认在上方 21 | - render_path: str #html file path to save 22 | ''' 23 | if not prices_cols: 24 | prices_cols = [u'open', u'close', u'low', u'high'] 25 | 26 | if not set(prices_cols+['volume']).issubset(set(df.columns)): 27 | raise AttributeError("%s or 'volume' not in columns" % 28 | str(prices_cols)) 29 | 30 | 31 | kline = Kline(name, width=width, height=height) 32 | kline.add('k-candle', 33 | df.index.format(), 34 | df[prices_cols].values.tolist(), 35 | is_datazoom_show=True, 36 | datazoom_xaxis_index=[0, 1], 37 | xaxis_pos=kline_xaxis_pos, 38 | is_xaxislabel_align=True, 39 | ) 40 | 41 | # volume 42 | if not 'price_change' in df.columns: 43 | df['price_change'] = df[prices_cols[1]].diff() 44 | ups = df.where(df.price_change > 0, 0)['volume'] 45 | downs = df.where(~(df.price_change > 0), 0)['volume'] 46 | 47 | bar = Bar() 48 | bar.add('up', 49 | x_axis=ups.index.format(), 50 | y_axis=ups.values.tolist(), 51 | is_datazoom_show=True, 52 | legend_top="70%", 53 | is_stack=True, 54 | is_xaxislabel_align=True, 55 | ) 56 | bar.add('down', 57 | x_axis=downs.index.format(), 58 | y_axis=downs.values.tolist(), 59 | is_datazoom_show=True, 60 | is_stack=True, 61 | legend_top="70%", 62 | legend_orient='vertical', 63 | legend_pos='left', 64 | yaxis_pos='right', 65 | is_xaxislabel_align=True, 66 | # mark_line=["average"], 67 | ) 68 | 69 | # merge 70 | grid1 = Grid() 71 | grid1.add(kline, grid_bottom="18%") 72 | grid1.add(bar, grid_top="75%") 73 | 74 | # add ma 75 | Line_draw = False 76 | for ma_ in ma: 77 | if ma_ in df.columns: 78 | if Line_draw is False: 79 | Line_draw = True 80 | line = Line() 81 | line.add(ma_, df.index.format(), df[ma_].values.tolist()) 82 | if Line_draw: 83 | overlap = Overlap() 84 | overlap.add(kline) # overlap kline and ma 85 | overlap.add(line) 86 | 87 | if render_path: 88 | grid1.render(render_path) 89 | return grid1 # .render('k-line.html') 90 | 91 | if __name__ == '__main__': 92 | import tushare as ts 93 | 94 | name = 'hs300' 95 | period = '5' 96 | 97 | df = ts.get_hist_data(name, ktype=period) 98 | df.sort_index(inplace=True) 99 | if period.isdigit(): 100 | period += 'min' 101 | 102 | kline_js('%s_kline_%s' % (name, period), 103 | df, 104 | ma=['ma10', 'ma20'], 105 | width=900, height=600, 106 | render_path='%s_kline_%s.html' % (name, period) 107 | ) 108 | -------------------------------------------------------------------------------- /plot/pyecharts_k-candle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/willowj/python_dataEE/ce50f9bd0a022a73569465ec1678f0263f11851e/plot/pyecharts_k-candle.png --------------------------------------------------------------------------------