PythonStock(10):使用notebook + tushare + pandas 简单的股票分析,蜡烛图

来源:互联网 发布:苹果macbook下载软件 编辑:程序博客网 时间:2024/05/14 11:04

前言


使用Python开发一个股票项目。
项目地址:
https://github.com/pythonstock/stock
相关资料:
http://blog.csdn.net/freewebsys/article/details/78294566
主要使用开发语言是python。
使用的lib库是pandas,tushare,TensorFlow,tornado等。

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/78272356
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,研究股票的蜡烛图


利用 TensorFlow 的docker 镜像,安装 tushare,安装完成直接就可以直接使用了。
上面同时还安装了其他的超级多的框架,非常方便。
开始使用 notebook 画 蜡烛图。
主要使用 matplotlib 画图。
matplotlib.finance.candlestick2_ochl

2,candlestick2_ochl画图


import pandas as pdimport tushare as tsimport datetimeimport matplotlibfrom matplotlib.dates import DateFormatter, WeekdayLocator,\    DayLocator, MONDAYimport matplotlib.pyplot as pltfrom matplotlib.finance import candlestick2_ochlimport matplotlib.ticker as tickerbegin_time = '2017-07-01'end_time = '2017-10-01'code = "000001"df = ts.get_hist_data(code, start=begin_time, end=end_time)df = df.sort_index(0)df_idx = df.index.values#    df.plot()fig, ax = plt.subplots(figsize=(20, 10)) # 设置图片大小。# https://matplotlib.org/api/finance_api.html#module-matplotlib.finance# matplotlib.finance.candlestick2_ochl(ax, opens, closes, highs, lows, width=4, colorup='r', colordown='g', alpha=0.75)candlestick2_ochl(ax = ax,                  opens=df["open"].values, closes=df["close"].values,                 highs=df["high"].values, lows=df["low"].values,                  width=0.75, colorup='r', colordown='g', alpha=0.75)ax.xaxis.set_major_locator(ticker.MaxNLocator(20))# 设置自动格式化时间。def mydate_formatter(x,pos):    try:        return df_idx[int(x)]    except IndexError:        return ''ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate_formatter))plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')ax.grid(True)plt.title(code)plt.show()

展示的蜡烛图:

数据是平安银行的数据。
使用的是tushare 拉去数据。

然后使用matplotlib 的finace 库画蜡烛图。

# https://matplotlib.org/api/finance_api.html#module-matplotlib.finance# matplotlib.finance.candlestick2_ochl(ax, opens, closes, highs, lows, width=4, colorup='r', colordown='g', alpha=0.75)

使用蜡烛图,参数需要输入 opens, closes, highs, lows,也就是【今开盘】,【最高价】,【最低价】,【今收盘】。
同时设置 colorup=’r’, colordown=’g’ 涨是红色,跌是绿色。中国和国外的颜色不一样。

使用 ax.xaxis.set_major_formatter(ticker.FuncFormatter(mydate_formatter))
方法设置了,x坐标当中的日期。
将日期展示。

3,关于镜像信息


最近发现使用docker的好处了,TensorFlow 发布最新的1.3了,
我使用的是docker环境做开发。然后我把基础镜像升级了。
再重新打包项目,就升级了。啥问题都没有。超级轻松。
同时只要是dockerfile 没有问题,在哪里运行都是一样的。
这个是超级爽的,现在已经很少在实体机器上安装东西了。
找个镜像,然后启动就行了。超级方便。同时也少了很多运维的问题,编译安装的问题。
安装编译使用参考:(前提要安装好docker)
https://github.com/pythonstock/stock

import pandas as pdimport tushare as tsimport numpy as npimport tensorflow as tfimport datetimeimport matplotlib as mplimport matplotlib.pyplot as pltimport sysimport platformprint("sys: ",sys.version)print("pandas: ",pd.__version__)print("tushare: ",ts.__version__)print("numpy: ",np.__version__)print("matplotlib: ",mpl.__version__)print("tensorflow: ",tf.__version__)print(platform.platform())print(platform.release())print(platform.machine())

展示信息

sys:  3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609]pandas:  0.20.3tushare:  0.9.8numpy:  1.13.3matplotlib:  2.0.2tensorflow:  1.3.0Linux-4.9.49-moby-x86_64-with-Ubuntu-16.04-xenial4.9.49-mobyx86_64

4,总结


环境搭建好了之后就是仔细的研究代码了。
python 入门很快,学习深入了也不难,关键在坚持。将统计学的东西,机器学习的东西都引用到股票系统当中。
继续加油。

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/78294566
未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

阅读全文
0 0
原创粉丝点击