tushare简介

TuShare是一个免费、开源的python财经数据接口包。主要实现对股票等金融数据从数据采集、清洗加工到数据存储的过程,能够为金融分析人员提供快速、整洁、和多样的便于分析的数据,为他们在数据获取方面极大地减轻工作量,使他们更加专注于策略和模型的研究与实现上。考虑到Python pandas包在金融量化分析中体现出的优势,TuShare返回的绝大部分的数据格式都是pandas DataFrame类型,非常便于用pandas/NumPy/Matplotlib进行数据分析和可视化。当然,如果您习惯了用Excel或者关系型数据库做分析,您也可以通过TuShare的数据存储功能,将数据全部保存到本地后进行分析。应一些用户的请求,从0.2.5版本开始,TuShare同时兼容Python 2.x和Python 3.x,对部分代码进行了重构,并优化了一些算法,确保数据获取的高效和稳定。 http://tushare.org/

股票数据获取

import tushare as ts

ts.get_hist_data('600848')#一次性获取全部日k线数据

结果显示:

open high close low volume p_change ma5 \

date

2012-01-11 6.880 7.380 7.060 6.880 14129.96 2.62 7.060

2012-01-12 7.050 7.100 6.980 6.900 7895.19 -1.13 7.020

2012-01-13 6.950 7.000 6.700 6.690 6611.87 -4.01 6.913

2012-01-16 6.680 6.750 6.510 6.480 2941.63 -2.84 6.813

2012-01-17 6.660 6.880 6.860 6.460 8642.57 5.38 6.822

2012-01-18 7.000 7.300 6.890 6.880 13075.40 0.44 6.788

2012-01-19 6.690 6.950 6.890 6.680 6117.32 0.00 6.770

2012-01-20 6.870 7.080 7.010 6.870 6813.09 1.74 6.832

ma10 ma20 v_ma5 v_ma10 v_ma20 turnover

date

2012-01-11 7.060 7.060 14129.96 14129.96 14129.96 0.48

2012-01-12 7.020 7.020 11012.58 11012.58 11012.58 0.27

2012-01-13 6.913 6.913 9545.67 9545.67 9545.67 0.23

2012-01-16 6.813 6.813 7894.66 7894.66 7894.66 0.10

2012-01-17 6.822 6.822 8044.24 8044.24 8044.24 0.30

2012-01-18 6.833 6.833 7833.33 8882.77 8882.77 0.45

2012-01-19 6.841 6.841 7477.76 8487.71 8487.71 0.21

2012-01-20 6.863 6.863 7518.00 8278.38 8278.38 0.23

绘制K线图

matplotlib.finance 工具包的绘制K线图

def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',

alpha=1.0, ochl=True):

"""

Plot the time, open, high, low, close as a vertical line ranging

from low to high. Use a rectangular bar to represent the

open-close span. If close >= open, use colorup to color the bar,

otherwise use colordown

Parameters

----------

ax : `Axes`

an Axes instance to plot to

quotes : sequence of quote sequences

data to plot. time must be in float date format - see date2num

(time, open, high, low, close, ...) vs

(time, open, close, high, low, ...)

set by `ochl`

width : float

fraction of a day for the rectangle width

colorup : color

the color of the rectangle where close >= open

colordown : color

the color of the rectangle where close < open

alpha : float

the rectangle alpha level

ochl: bool

argument to select between ochl and ohlc ordering of quotes

Returns

-------

ret : tuple

returns (lines, patches) where lines is a list of lines

added and patches is a list of the rectangle patches added

"""

OFFSET = width / 2.0

lines = []

patches = []

for q in quotes:

if ochl:

t, open, close, high, low = q[:5]

else:

t, open, high, low, close = q[:5]

if close >= open:

color = colorup

lower = open

height = close - open

else:

color = colordown

lower = close

height = open - close

vline = Line2D(

xdata=(t, t), ydata=(low, high),

color=color,

linewidth=0.5,

antialiased=True,

)

rect = Rectangle(

xy=(t - OFFSET, lower),

width=width,

height=height,

facecolor=color,

edgecolor=color,

)

rect.set_alpha(alpha)

lines.append(vline)

patches.append(rect)

ax.add_line(vline)

ax.add_patch(rect)

ax.autoscale_view()

return lines, patches

tushare 的 pandas dataframe 生成K线图

def _candlestick(ax, df, width=0.2, colorup='k', colordown='r',

alpha=1.0):

"""

Plot the time, open, high, low, close as a vertical line ranging

from low to high. Use a rectangular bar to represent the

open-close span. If close >= open, use colorup to color the bar,

otherwise use colordown

Parameters

----------

ax : `Axes`

an Axes instance to plot to

df : pandas data from tushare

width : float

fraction of a day for the rectangle width

colorup : color

the color of the rectangle where close >= open

colordown : color

the color of the rectangle where close < open

alpha : float

the rectangle alpha level

ochl: bool

argument to select between ochl and ohlc ordering of quotes

Returns

-------

ret : tuple

returns (lines, patches) where lines is a list of lines

added and patches is a list of the rectangle patches added

"""

OFFSET = width / 2.0

lines = []

patches = []

for date_string,row in df.iterrows():

date_time = datetime.datetime.strptime(date_string,'%Y-%m-%d')

t = date2num(date_time)

open, high, close, low = row[:4]

if close >= open:

color = colorup

lower = open

height = close - open

else:

color = colordown

lower = close

height = open - close

vline = Line2D(

xdata=(t, t), ydata=(low, high),

color=color,

linewidth=0.5,

antialiased=True,

)

rect = Rectangle(

xy=(t - OFFSET, lower),

width=width,

height=height,

facecolor=color,

edgecolor=color,

)

rect.set_alpha(alpha)

lines.append(vline)

patches.append(rect)

ax.add_line(vline)

ax.add_patch(rect)

ax.autoscale_view()

return lines, patches

def drawPic(df, code, name):

mondays = WeekdayLocator(MONDAY) # 主要刻度

alldays = DayLocator() # 次要刻度

#weekFormatter = DateFormatter('%b %d') # 如:Jan 12

mondayFormatter = DateFormatter('%m-%d-%Y') # 如:2-29-2015

dayFormatter = DateFormatter('%d') # 如:12

fig, ax = plt.subplots()

fig.subplots_adjust(bottom=0.2)

ax.xaxis.set_major_locator(mondays)

ax.xaxis.set_minor_locator(alldays)

ax.xaxis.set_major_formatter(mondayFormatter)

_candlestick(ax, df, width=0.6, colorup='r', colordown='g')

ax.xaxis_date()

ax.autoscale_view()

plt.setp(plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

ax.grid(True)

plt.title(name + ' ' + code, fontproperties=zhfont)

plt.show()

def makePicture(code, name):

df = ts.get_hist_data(code, start=begin_time, end=end_time)

df = df.sort_index(0)

# df.plot()

drawPic(df, code, name)

python生成k线图_基于tushare生成k线图相关推荐

  1. vue连线 插件_基于Vue的任务节点图绘制插件(vue-task-node)

    简介: vue-task-node 是一个基于Vue的任务节点图绘制插件(vue-task-node is a Vue based task node mapping plug-in) 此篇博客会随插 ...

  2. 基于python的入侵检测系统毕设_基于深度学习的射频指纹的物联网设备入侵检测...

    摘要:物联网(IoT)和4G/5G无线网络增加了大量设备和新服务,商用现货(COTS)物联网设备得到了广泛部署.为了确保具备无线传输能力的这些系统的安全运作,射频(RF)监视对于监视它们在RF频谱中的 ...

  3. python小车行驶路线图_基于opencv-Python小车循线学习笔记

    基于opencv-Python小车循线学习笔记 加入摄像头模块,让小车实现自动循迹行驶 思路为:摄像头读取图像,进行二值化,将白色的赛道凸显出来 选择下方的一行像素,黑色为0,白色为255 找到白色值 ...

  4. python做圆柱绕流_基于snappyHexMesh生成网格的圆柱绕流算例

    基于snappyHexMesh生成网格的圆柱绕流算例 1.创建几何体STL文件 在了解了原理之后,要生成网格第一步就是要创建一个几何体的STL文件,可以使用3Dbulider,3DCAD等软件生成几何 ...

  5. echart关系树状图_干货 | 25个常用Matplotlib图的Python代码

    50个Matplotlib图的汇编,在数据分析和可视化中最有用.此列表允许您使用Python的Matplotlib和Seaborn库选择要显示的可视化对象. 1.关联 散点图 带边界的气泡图 带线性回 ...

  6. python路径规划算法可视化_基于粒子群算法的牙齿正畸路径规划方法python实现

    这篇是基于粒子群算法的牙齿正畸路径规划研究的python实现,参考的是徐晓强等人的<基于改进粒子群算法的牙齿正畸路径规划方法>,与这篇文章的区别在于: 1.徐等的文章设计了一种改进的粒子群 ...

  7. python django做网页论文_基于PythonDjango框架的多媒体发布系统

    , Models. py 组成,其中 Urls.py 为整个项目的路由表,当使用者访问特 定的 url 时 , Urls.py 将请求指向 Views.py (视图函数)中特定 的函数,在视图函数中与 ...

  8. vue族谱架构_基于 Vue 实现动态家谱图/组织结构图

    Vue-Tree-Chart 最近一个项目里有个前端绘制家谱图的需求,大概是下面这个样子: 点击节点会弹出操作菜单,实现增删改查等操作,查阅网上资料发现,现有案例基本都是基于orgchart这个jQu ...

  9. python图书馆管理系统实验报告_基于python图书馆管理系统设计实例详解

    写完这个项目后,导师说这个你完全可以当作毕业项目使用了,写的很全,很多的都设计考虑周全,但我的脚步绝不止于现在,我想要的是星辰大海!与君共勉! 这个项目不是我的作业, 只是无意中被拉进来了,然后就承担 ...

最新文章

  1. Oracle 金融类型获得前一交易日
  2. PyTorch核心开发者灵魂发问:我们怎么越来越像Julia了?
  3. OpenCV java 图像基本处理-模糊 (8)
  4. ABAP销售合同冻结Bapi
  5. java怎么添加地图_javaweb怎样添加百度地图
  6. swift textView字数限制,textView点击完成隐藏键盘
  7. Use Selenium webdriver in Javascript
  8. 云桌面部署_云桌面时代降临-青椒云工作站
  9. fun(int **p)的使用
  10. 聊聊spring-boot-starter-data-redis的配置变更
  11. oracle创建联机重做日志,oracle联机重做日志文件管理!
  12. 职工信息管理系统—C语言工程实践
  13. c语言程序调试方法有哪些,c语言程序的调试方法有哪些
  14. 基于PHP物流网站信息管理系统
  15. 手机电脑浏览器抓取京东Cookies教程
  16. 计算机无法备份,win7不能备份系统如何解决?win7不能备份系统的解决方法
  17. 2021年高处作业安装拆除维护证考试题库及安装拆除维护试题解析
  18. try语句的基本用法
  19. 车载网络测试 - UDS诊断篇 - 诊断服务$10
  20. HTML5期末大作业:游戏网站——网络游戏官网(悦世界) 6个页面 HTML+CSS+JavaScript ~ ~ 学生HTML个人网页作业作品下载...

热门文章

  1. 墨刀常用功能大盘点,谁是你心中的第一名?!
  2. LabVIEW中的VISA函数串口通信的简单例子
  3. html提交提示非法字符串,教你如何过滤高亮显示非法字符
  4. Linux下软件源码包安装问题解决方法
  5. gitk工具的使用方法
  6. 新手日入200➕,必备的5类短视频自媒体工具分享,抓紧收藏
  7. ACrush 回忆录
  8. Vue 中使用Animate动画
  9. 如何在Apple Watch上设置和使用密码
  10. 偏瘫能恢复吗?成都顾连康复治疗妙招