"""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