python kline_关于k Line Chart (k线图)

这段代码提供了用于分析和绘制金融数据的函数,包括plot_day_summary和candlestick等,用于展示开盘价、收盘价、最高价、最低价等信息。通过matplotlib库创建垂直线和矩形条来表示数据,可用于创建K线图。
摘要由CSDN通过智能技术生成

"""A collection of functions for analyzing and plotting

financial data. User contributions welcome!"""

from __future__ import(absolute_import, division, print_function,

unicode_literals)importnumpy as npimportmatplotlib.pyplot as pltfrom matplotlib importcolors as mcolorsfrom matplotlib.collections importLineCollection, PolyCollectionfrom matplotlib.lines importTICKLEFT, TICKRIGHT, Line2Dfrom matplotlib.patches importRectanglefrom matplotlib.transforms importAffine2Dfrom six.moves importxrange, zipdef plot_day_summary_oclh(ax, quotes, ticksize=3,

colorup='k', colordown='r'):"""Plots day summary

Represent the time, open, close, high, low as a vertical line

ranging from low to high. The left tick is the open and the right

tick is the close.

Parameters

----------

ax : `Axes`

an `Axes` instance to plot to

quotes : sequence of (time, open, close, high, low, ...) sequences

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

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns

-------

lines : list

list of tuples of the lines added (one tuple per quote)"""

return _plot_day_summary(ax, quotes, ticksize=ticksize,

colorup=colorup, colordown=colordown,

ochl=True)def plot_day_summary_ohlc(ax, quotes, ticksize=3,

colorup='k', colordown='r'):"""Plots day summary

Represent the time, open, high, low, close as a vertical line

ranging from low to high. The left tick is the open and the right

tick is the close.

Parameters

----------

ax : `Axes`

an `Axes` instance to plot to

quotes : sequence of (time, open, high, low, close, ...) sequences

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

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

Returns

-------

lines : list

list of tuples of the lines added (one tuple per quote)"""

return _plot_day_summary(ax, quotes, ticksize=ticksize,

colorup=colorup, colordown=colordown,

ochl=False)def _plot_day_summary(ax, quotes, ticksize=3,

colorup='k', colordown='r',

ochl=True):"""Plots day summary

Represent the time, open, high, low, close as a vertical line

ranging from low to high. The left tick is the open and the right

tick is the close.

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`

ticksize : int

open/close tick marker in points

colorup : color

the color of the lines where close >= open

colordown : color

the color of the lines where close < open

ochl: bool

argument to select between ochl and ohlc ordering of quotes

Returns

-------

lines : list

list of tuples of the lines added (one tuple per quote)"""

#unfortunately this has a different return type than plot_day_summary2_*

lines =[]for q inquotes:ifochl:

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

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

color=colorupelse:

color=colordown

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

color=color,

antialiased=False, #no need to antialias vert lines

)

oline= Line2D(xdata=(t, t), ydata=(open, open),

color=color,

antialiased=False,

marker=TICKLEFT,

markersize=ticksize,

)

cline= Line2D(xdata=(t, t), ydata=(close, close),

color=color,

antialiased=False,

markersize=ticksize,

marker=TICKRIGHT)

lines.extend((vline, oline, cline))

ax.add_line(vline)

ax.add_line(oline)

ax.add_line(cline)

ax.autoscale_view()returnlinesdef candlestick_ochl(ax, quotes, width=0.2, colorup='k', colordown='r',

alpha=1.0):"""Plot the time, open, close, high, low 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 (time, open, close, high, low, ...) sequences

As long as the first 5 elements are these values,

the record can be as long as you want (e.g., it may store volume).

time must be in float days format - see date2num

width : float

fraction of a day for the rectangle width

colorup : color

the

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值