"""A collection of functions for collecting, analyzing and plottingfinancial data. User contributions welcome!This module is deprecated in 1.4 and will be moved to `mpl_toolkits`or it's own project in the future."""from __future__ import (absolute_import, division, print_function,unicode_literals)import sixfrom six.moves import xrange, zipimport contextlibimport osimport sysimport warningsif six.PY3:from urllib.request import urlopenelse:from urllib2 import urlopenif six.PY3:import hashlibmd5 = lambda x: hashlib.md5(x.encode())else:from hashlib import md5import datetimeimport numpy as npfrom matplotlib import verbose, get_cachedirfrom matplotlib.dates import date2numfrom matplotlib.cbook import iterable, mkdirsfrom matplotlib.collections import LineCollection, PolyCollectionfrom matplotlib.colors import colorConverterfrom matplotlib.lines import Line2D, TICKLEFT, TICKRIGHTfrom matplotlib.patches import Rectanglefrom matplotlib.transforms import Affine2Dfrom matplotlib.cbook import mplDeprecationcachedir = get_cachedir()# cachedir will be None if there is no writable directory.if cachedir is not None:cachedir = os.path.join(cachedir, 'finance.cache')else:# Should only happen in a restricted environment (such as Google App# Engine). Deal with this gracefully by not caching finance data.cachedir = Nonestock_dt_ohlc = np.dtype([(str('date'), object),(str('year'), np.int16),(str('month'), np.int8),(str('day'), np.int8),(str('d'), np.float), # mpl datenum(str('open'), np.float),(str('high'), np.float),(str('low'), np.float),(str('close'), np.float),(str('volume'), np.float),(str('aclose'), np.float)])stock_dt_ochl = np.dtype([(str('date'), object),(str('year'), np.int16),(str('month'), np.int8),(str('day'), np.int8),(str('d'), np.float), # mpl datenum(str('open'), np.float),(str('close'), np.float),(str('high'), np.float),(str('low'), np.float),(str('volume'), np.float),(str('aclose'), np.float)])_warn_str = ("This function has been deprecated in 1.4 in favor ""of `{fun}_ochl`, ""which maintains the original argument order, ""or `{fun}_ohlc`, ""which uses the open-high-low-close order. ""This function will be removed in 1.5")def parse_yahoo_historical_ochl(fh, adjusted=True, asobject=False):"""Parse the historical data in file handle fh from yahoo finance.Parameters----------adjusted : boolIf True (default) replace open, close, high, low prices withtheir adjusted values. The adjustment is by a scale factor, S =adjusted_close/close. Adjusted prices are actual pricesmultiplied by S.Volume is not adjusted as it is already backward split adjustedby Yahoo. If you want to compute dollars traded, multiply volumeby the adjusted close, regardless of whether you choose adjusted= True|False.asobject : bool or NoneIf False (default for compatibility with earlier versions)return a list of tuples containingd, open, close, high, low, volumeIf None (preferred alternative to False), returna 2-D ndarray corresponding to the list of tuples.Otherwise return a numpy recarray withdate, year, month, day, d, open, close, high, low,volume, adjusted_closewhere d is a floating poing representation of date,as returned by date2num, and date is a python standardlibrary datetime.date instance.The name of this kwarg is a historical artifact. Formerly,True returned a cbook Bunchholding 1-D ndarrays. The behavior of a numpy recarray isvery similar to the Bunch."""return _parse_yahoo_historical(fh, adjusted=adjusted, asobject=asobject,ochl=True)def parse_yahoo_historical_ohlc(fh, adjusted=True, asobject=False):"""Parse the historical data in file handle fh from yahoo finance.Parameters----------adjusted : boolIf True (default) replace open, high, low, close prices withtheir adjusted values. The adjustment is by a scale factor, S =adjusted_close/close. Adjusted prices are actual pricesmultiplied by S.Volume is not adjusted as it is already backward split adjustedby Yahoo. If you want to compute dollars traded, multiply volumeby the adjusted close, regardless of whether you choose adjusted= True|False.asobject : bool or NoneIf False (default for compatibility with earlier versions)return a list of tuples containingd, open, high, low, close, volumeIf None (preferred alternative to False), returna 2-D ndarray corresponding to the list of tuples.Otherwise return a numpy recarray withdate, year, month, day, d, open, high, low, close,volume, adjusted_closewhere d is a floating poing representation of date,as returned by date2num, and date is a python standardlibrary datetime.date instance.The name of this kwarg is a historical artifact. Formerly,True returned a cbook Bunchholding 1-D ndarrays. The behavior of a numpy recarray isvery similar to the Bunch."""return _parse_yahoo_historical(fh, adjusted=adjusted, asobject=asobject,ochl=False)def parse_yahoo_historical(fh, adjusted=True, asobject=False):"""Parse the historical data in file handle fh from yahoo finance.This function has been deprecated in 1.4 in favor of`parse_yahoo_historical_ochl`, which maintains the original argumentorder, or `parse_yahoo_historical_ohlc`, which uses theopen-high-low-close order. This function will be removed in 1.5Parameters----------adjusted : boolIf True (default) replace open, close, high, low prices withtheir adjusted values. The adjustment is by a scale factor, S =adjusted_close/close. Adjusted prices are actual pricesmultiplied by S.Volume is not adjusted as it is already backward split adjustedby Yahoo. If you want to compute dollars traded, multiply volumeby the adjusted close, regardless of whether you choose adjusted= True|False.asobject : bool or NoneIf False (default for compatibility with earlier versions)return a list of tuples containingd, open, close, high, low, volumeIf None (preferred alternative to False), returna 2-D ndarray corresponding to the list of tuples.Otherwise return a numpy recarray withdate, year, month, day, d, open, close, high, low,volume, adjusted_closewhere d is a floating poing representation of date,as returned by date2num, and date is a python standardlibrary datetime.date instance.The name of this kwarg is a historical artifact. Formerly,True returned a cbook Bunchholding 1-D ndarrays. The behavior of a numpy recarray isvery similar to the Bunch.ochl : boolTemporary argument to select between ochl and ohlc ordering.Defaults to True to preserve original functionality."""warnings.warn(_warn_str.format(fun='parse_yahoo_historical'),mplDeprecation)return _parse_yahoo_historical(fh, adjusted=adjusted, asobject=asobject,ochl=True)def _parse_yahoo_historical(fh, adjusted=True, asobject=False,ochl=True):"""Parse the historical data in file handle fh from yahoo finance.Parameters----------adjusted : boolIf True (default) replace open, high, low, close prices withtheir adjusted values. The adjustment is by a scale factor, S =adjusted_close/close. Adjusted prices are actual pricesmultiplied by S.Volume is not adjusted as it is already backward split adjustedby Yahoo. If you want to compute dollars traded, multiply volumeby the adjusted close, regardless of whether you choose adjusted= True|False.asobject : bool or NoneIf False (default for compatibility with earlier versions)return a list of tuples containingd, open, high, low, close, volumeord, open, close, high, low, volumedepending on `ochl`If None (preferred alternative to False), returna 2-D ndarray corresponding to the list of tuples.Otherwise return a numpy recarray withdate, year, month, day, d, open, high, low, close,volume, adjusted_closewhere d is a floating poing representation of date,as returned by date2num, and date is a python standardlibrary datetime.date instance.The name of this kwarg is a historical artifact. Formerly,True returned a cbook Bunchholding 1-D ndarrays. The behavior of a numpy recarray isvery similar to the Bunch.ochl : boolSelects between ochl and ohlc ordering.Defaults to True to preserve original functionality."""if ochl:stock_dt = stock_dt_ochlelse:stock_dt = stock_dt_ohlcresults = []# datefmt = '%Y-%m-%d'fh.readline() # discard headingfor line in fh:vals = line.split(',')if len(vals) != 7:continue # add warning?datestr = vals[0]#dt = datetime.date(*time.strptime(datestr, datefmt)[:3])# Using strptime doubles the runtime. With the present# format, we don't need it.dt = datetime.date(*[int(val) for val in datestr.split('-')])dnum = date2num(dt)open, high, low, close = [float(val) for val in vals[1:5]]volume = float(vals[5])aclose = float(vals[6])if ochl:results.append((dt, dt.year, dt.month, dt.day,dnum, open, close, high, low, volume, aclose))else:results.append((dt, dt.year, dt.month, dt.day,dnum, open, high, low, close, volume, aclose))results.reverse()d = np.array(results, dtype=stock_dt)if adjusted:scale = d['aclose'] / d['close']scale[np.isinf(scale)] = np.nand['open'] *= scaled['high'] *= scaled['low'] *= scaled['close'] *= scaleif not asobject:# 2-D sequence; formerly list of tuples, now ndarrayret = np.zeros((len(d), 6), dtype=np.float)ret[:, 0] = d['d']if ochl:ret[:, 1] = d['open']ret[:, 2] = d['close']ret[:, 3] = d['high']ret[:, 4] = d['low']else:ret[:, 1] = d['open']ret[:, 2] = d['high']ret[:, 3] = d['low']ret[:, 4] = d['close']ret[:, 5] = d['volume']if asobject is None:return retreturn [tuple(row) for row in ret]return d.view(np.recarray) # Close enough to former Bunch returndef fetch_historical_yahoo(ticker, date1, date2, cachename=None,dividends=False):"""Fetch historical data for ticker between date1 and date2. date1 anddate2 are date or datetime instances, or (year, month, day) sequences.Parameters----------ticker : strtickerdate1 : sequence of form (year, month, day), `datetime`, or `date`start datedate2 : sequence of form (year, month, day), `datetime`, or `date`end datecachename : strcachename is the name of the local file cache. If None, willdefault to the md5 hash or the url (which incorporates the tickerand date range)dividends : boolset dividends=True to return dividends instead of price data. Withthis option set, parse functions will not workReturns-------file_handle : file handlea file handle is returnedExamples-------->>> fh = fetch_historical_yahoo('^GSPC', (2000, 1, 1), (2001, 12, 31))"""ticker = ticker.upper()if iterable(date1):d1 = (date1[1] - 1, date1[2], date1[0])else:d1 = (date1.month - 1, date1.day, date1.year)if iterable(date2):d2 = (date2[1] - 1, date2[2], date2[0])else:d2 = (date2.month - 1, date2.day, date2.year)if dividends:g = 'v'verbose.report('Retrieving dividends instead of prices')else:g = 'd'urlFmt = ('http://ichart.cn.finance.yahoo.com/table.csv?a=%d&b=%d&' +'c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=%s&ignore=.csv')url = urlFmt % (d1[0], d1[1], d1[2],d2[0], d2[1], d2[2], ticker, g)# Cache the finance data if cachename is supplied, or there is a writable# cache directory.if cachename is None and cachedir is not None:cachename = os.path.join(cachedir, md5(url).hexdigest())if cachename is not None:if os.path.exists(cachename):fh = open(cachename)verbose.report('Using cachefile %s for ''%s' % (cachename, ticker))else:mkdirs(os.path.abspath(os.path.dirname(cachename)))with contextlib.closing(urlopen(url)) as urlfh:with open(cachename, 'wb') as fh:fh.write(urlfh.read())verbose.report('Saved %s data to cache file ''%s' % (ticker, cachename))fh = open(cachename, 'r')return fhelse:return urlopen(url)def quotes_historical_yahoo(ticker, date1, date2, asobject=False,adjusted=True, cachename=None):""" Get historical data for ticker between date1 and date2.This function has been deprecated in 1.4 in favor of`quotes_yahoo_historical_ochl`, which maintains the original argumentorder, or `quotes_yahoo_historical_ohlc`, which uses theopen-high-low-close order. This function will be removed in 1.5See :func:`parse_yahoo_historical` for explanation of output formatsand the *asobject* and *adjusted* kwargs.Parameters----------ticker : strstock tickerdate1 : sequence of form (year, month, day), `datetime`, or `date`start datedate2 : sequence of form (year, month, day), `datetime`, or `date`end datecachename : str or `None`is the name of the local file cache. If None, willdefault to the md5 hash or the url (which incorporates the tickerand date range)Examples-------->>> sp = f.quotes_historical_yahoo('^GSPC', d1, d2,asobject=True, adjusted=True)>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]>>> [n,bins,patches] = hist(returns, 100)>>> mu = mean(returns)>>> sigma = std(returns)>>> x = normpdf(bins, mu, sigma)>>> plot(bins, x, color='red', lw=2)"""warnings.warn(_warn_str.format(fun='quotes_historical_yahoo'),mplDeprecation)return _quotes_historical_yahoo(ticker, date1, date2, asobject=asobject,adjusted=adjusted, cachename=cachename,ochl=True)def quotes_historical_yahoo_ochl(ticker, date1, date2, asobject=False,adjusted=True, cachename=None):""" Get historical data for ticker between date1 and date2.See :func:`parse_yahoo_historical` for explanation of output formatsand the *asobject* and *adjusted* kwargs.Parameters----------ticker : strstock tickerdate1 : sequence of form (year, month, day), `datetime`, or `date`start datedate2 : sequence of form (year, month, day), `datetime`, or `date`end datecachename : str or `None`is the name of the local file cache. If None, willdefault to the md5 hash or the url (which incorporates the tickerand date range)Examples-------->>> sp = f.quotes_historical_yahoo_ochl('^GSPC', d1, d2,asobject=True, adjusted=True)>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]>>> [n,bins,patches] = hist(returns, 100)>>> mu = mean(returns)>>> sigma = std(returns)>>> x = normpdf(bins, mu, sigma)>>> plot(bins, x, color='red', lw=2)"""return _quotes_historical_yahoo(ticker, date1, date2, asobject=asobject,adjusted=adjusted, cachename=cachename,ochl=True)def quotes_historical_yahoo_ohlc(ticker, date1, date2, asobject=False,adjusted=True, cachename=None):""" Get historical data for ticker between date1 and date2.See :func:`parse_yahoo_historical` for explanation of output formatsand the *asobject* and *adjusted* kwargs.Parameters----------ticker : strstock tickerdate1 : sequence of form (year, month, day), `datetime`, or `date`start datedate2 : sequence of form (year, month, day), `datetime`, or `date`end datecachename : str or `None`is the name of the local file cache. If None, willdefault to the md5 hash or the url (which incorporates the tickerand date range)Examples-------->>> sp = f.quotes_historical_yahoo_ohlc('^GSPC', d1, d2,asobject=True, adjusted=True)>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]>>> [n,bins,patches] = hist(returns, 100)>>> mu = mean(returns)>>> sigma = std(returns)>>> x = normpdf(bins, mu, sigma)>>> plot(bins, x, color='red', lw=2)"""return _quotes_historical_yahoo(ticker, date1, date2, asobject=asobject,adjusted=adjusted, cachename=cachename,ochl=False)def _quotes_historical_yahoo(ticker, date1, date2, asobject=False,adjusted=True, cachename=None,ochl=True):""" Get historical data for ticker between date1 and date2.See :func:`parse_yahoo_historical` for explanation of output formatsand the *asobject* and *adjusted* kwargs.Parameters----------ticker : strstock tickerdate1 : sequence of form (year, month, day), `datetime`, or `date`start datedate2 : sequence of form (year, month, day), `datetime`, or `date`end datecachename : str or `None`is the name of the local file cache. If None, willdefault to the md5 hash or the url (which incorporates the tickerand date range)ochl: booltemporary argument to select between ochl and ohlc orderingExamples-------->>> sp = f.quotes_historical_yahoo('^GSPC', d1, d2,asobject=True, adjusted=True)>>> returns = (sp.open[1:] - sp.open[:-1])/sp.open[1:]>>> [n,bins,patches] = hist(returns, 100)>>> mu = mean(returns)>>> sigma = std(returns)>>> x = normpdf(bins, mu, sigma)>>> plot(bins, x, color='red', lw=2)"""# Maybe enable a warning later as part of a slow transition# to using None instead of False.#if asobject is False:# warnings.warn("Recommend changing to asobject=None")fh = fetch_historical_yahoo(ticker, date1, date2, cachename)try:ret = _parse_yahoo_historical(fh, asobject=asobject,adjusted=adjusted, ochl=ochl)if len(ret) == 0:return Noneexcept IOError as exc:warnings.warn('fh failure\n%s' % (exc.strerror[1]))return Nonereturn retdef plot_day_summary(ax, quotes, ticksize=3,colorup='k', colordown='r',):"""Plots day summaryRepresent the time, open, close, high, low as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.This function has been deprecated in 1.4 in favor of`plot_day_summary_ochl`, which maintains the original argumentorder, or `plot_day_summary_ohlc`, which uses theopen-high-low-close order. This function will be removed in 1.5Parameters----------ax : `Axes`an `Axes` instance to plot toquotes : sequence of (time, open, close, high, low, ...) sequencesdata to plot. time must be in float date format - see date2numticksize : intopen/close tick marker in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------lines : listlist of tuples of the lines added (one tuple per quote)"""warnings.warn(_warn_str.format(fun='plot_day_summary'),mplDeprecation)return _plot_day_summary(ax, quotes, ticksize=ticksize,colorup=colorup, colordown=colordown,ochl=True)def plot_day_summary_oclh(ax, quotes, ticksize=3,colorup='k', colordown='r',):"""Plots day summaryRepresent the time, open, close, high, low as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.Parameters----------ax : `Axes`an `Axes` instance to plot toquotes : sequence of (time, open, close, high, low, ...) sequencesdata to plot. time must be in float date format - see date2numticksize : intopen/close tick marker in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------lines : listlist 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 summaryRepresent the time, open, high, low, close as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.Parameters----------ax : `Axes`an `Axes` instance to plot toquotes : sequence of (time, open, high, low, close, ...) sequencesdata to plot. time must be in float date format - see date2numticksize : intopen/close tick marker in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------lines : listlist 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 summaryRepresent the time, open, high, low, close as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.Parameters----------ax : `Axes`an `Axes` instance to plot toquotes : sequence of quote sequencesdata 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 : intopen/close tick marker in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openochl: boolargument to select between ochl and ohlc ordering of quotesReturns-------lines : listlist of tuples of the lines added (one tuple per quote)"""# unfortunately this has a different return type than plot_day_summary2_*lines = []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 = colorupelse:color = colordownvline = 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()return linesdef candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',alpha=1.0):"""Plot the time, open, close, high, low as a vertical line rangingfrom low to high. Use a rectangular bar to represent theopen-close span. If close >= open, use colorup to color the bar,otherwise use colordownThis function has been deprecated in 1.4 in favor of`candlestick_ochl`, which maintains the original argumentorder, or `candlestick_ohlc`, which uses theopen-high-low-close order. This function will be removed in 1.5Parameters----------ax : `Axes`an Axes instance to plot toquotes : sequence of (time, open, close, high, low, ...) sequencesAs long as the first 5 elements are these values,the record can be as long as you want (eg it may store volume).time must be in float days format - see date2numwidth : floatfraction of a day for the rectangle widthcolorup : colorthe color of the rectangle where close >= opencolordown : colorthe color of the rectangle where close < openalpha : floatthe rectangle alpha levelReturns-------ret : tuplereturns (lines, patches) where lines is a list of linesadded and patches is a list of the rectangle patches added"""warnings.warn(_warn_str.format(fun='candlestick'),mplDeprecation)return _candlestick(ax, quotes, width=width, colorup=colorup,colordown=colordown,alpha=alpha, ochl=True)def 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 rangingfrom low to high. Use a rectangular bar to represent theopen-close span. If close >= open, use colorup to color the bar,otherwise use colordownParameters----------ax : `Axes`an Axes instance to plot toquotes : sequence of (time, open, close, high, low, ...) sequencesAs long as the first 5 elements are these values,the record can be as long as you want (eg it may store volume).time must be in float days format - see date2numwidth : floatfraction of a day for the rectangle widthcolorup : colorthe color of the rectangle where close >= opencolordown : colorthe color of the rectangle where close < openalpha : floatthe rectangle alpha levelReturns-------ret : tuplereturns (lines, patches) where lines is a list of linesadded and patches is a list of the rectangle patches added"""return _candlestick(ax, quotes, width=width, colorup=colorup,colordown=colordown,alpha=alpha, ochl=True)def candlestick_ohlc(ax, quotes, width=0.2, colorup='k', colordown='r',alpha=1.0):"""Plot the time, open, high, low, close as a vertical line rangingfrom low to high. Use a rectangular bar to represent theopen-close span. If close >= open, use colorup to color the bar,otherwise use colordownParameters----------ax : `Axes`an Axes instance to plot toquotes : sequence of (time, open, high, low, close, ...) sequencesAs long as the first 5 elements are these values,the record can be as long as you want (eg it may store volume).time must be in float days format - see date2numwidth : floatfraction of a day for the rectangle widthcolorup : colorthe color of the rectangle where close >= opencolordown : colorthe color of the rectangle where close < openalpha : floatthe rectangle alpha levelReturns-------ret : tuplereturns (lines, patches) where lines is a list of linesadded and patches is a list of the rectangle patches added"""return _candlestick(ax, quotes, width=width, colorup=colorup,colordown=colordown,alpha=alpha, ochl=False)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 rangingfrom low to high. Use a rectangular bar to represent theopen-close span. If close >= open, use colorup to color the bar,otherwise use colordownParameters----------ax : `Axes`an Axes instance to plot toquotes : sequence of quote sequencesdata 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 : floatfraction of a day for the rectangle widthcolorup : colorthe color of the rectangle where close >= opencolordown : colorthe color of the rectangle where close < openalpha : floatthe rectangle alpha levelochl: boolargument to select between ochl and ohlc ordering of quotesReturns-------ret : tuplereturns (lines, patches) where lines is a list of linesadded and patches is a list of the rectangle patches added"""OFFSET = width / 2.0lines = []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 = coloruplower = openheight = close - openelse:color = colordownlower = closeheight = open - closevline = 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, patchesdef plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4,colorup='k', colordown='r',):"""Represent the time, open, close, high, low, as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.This function has been deprecated in 1.4 in favor of`plot_day_summary2_ochl`, which maintains the original argumentorder, or `plot_day_summary2_ohlc`, which uses theopen-high-low-close order. This function will be removed in 1.5Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valuescloses : sequencesequence of closing valueshighs : sequencesequence of high valueslows : sequencesequence of low valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------ret : lista list of lines added to the axes"""warnings.warn(_warn_str.format(fun='plot_day_summary2'), mplDeprecation)return plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize,colorup, colordown)def plot_day_summary2_ochl(ax, opens, closes, highs, lows, ticksize=4,colorup='k', colordown='r',):"""Represent the time, open, close, high, low, as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valuescloses : sequencesequence of closing valueshighs : sequencesequence of high valueslows : sequencesequence of low valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------ret : lista list of lines added to the axes"""return plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize,colorup, colordown)def plot_day_summary2_ohlc(ax, opens, highs, lows, closes, ticksize=4,colorup='k', colordown='r',):"""Represent the time, open, high, low, close as a vertical lineranging from low to high. The left tick is the open and the righttick is the close.Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valueshighs : sequencesequence of high valueslows : sequencesequence of low valuescloses : sequencesequence of closing valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openReturns-------ret : lista list of lines added to the axes"""# note this code assumes if any value open, high, low, close is# missing they all are missingrangeSegments = [((i, low), (i, high)) for i, low, high inzip(xrange(len(lows)), lows, highs) if low != -1]# the ticks will be from ticksize to 0 in points at the origin and# we'll translate these to the i, close locationopenSegments = [((-ticksize, 0), (0, 0))]# the ticks will be from 0 to ticksize in points at the origin and# we'll translate these to the i, close locationcloseSegments = [((0, 0), (ticksize, 0))]offsetsOpen = [(i, open) for i, open inzip(xrange(len(opens)), opens) if open != -1]offsetsClose = [(i, close) for i, close inzip(xrange(len(closes)), closes) if close != -1]scale = ax.figure.dpi * (1.0 / 72.0)tickTransform = Affine2D().scale(scale, 0.0)r, g, b = colorConverter.to_rgb(colorup)colorup = r, g, b, 1r, g, b = colorConverter.to_rgb(colordown)colordown = r, g, b, 1colord = {True: colorup,False: colordown,}colors = [colord[open < close] for open, close inzip(opens, closes) if open != -1 and close != -1]assert(len(rangeSegments) == len(offsetsOpen))assert(len(offsetsOpen) == len(offsetsClose))assert(len(offsetsClose) == len(colors))useAA = 0, # use tuple herelw = 1, # and hererangeCollection = LineCollection(rangeSegments,colors=colors,linewidths=lw,antialiaseds=useAA,)openCollection = LineCollection(openSegments,colors=colors,antialiaseds=useAA,linewidths=lw,offsets=offsetsOpen,transOffset=ax.transData,)openCollection.set_transform(tickTransform)closeCollection = LineCollection(closeSegments,colors=colors,antialiaseds=useAA,linewidths=lw,offsets=offsetsClose,transOffset=ax.transData,)closeCollection.set_transform(tickTransform)minpy, maxx = (0, len(rangeSegments))miny = min([low for low in lows if low != -1])maxy = max([high for high in highs if high != -1])corners = (minpy, miny), (maxx, maxy)ax.update_datalim(corners)ax.autoscale_view()# add these lastax.add_collection(rangeCollection)ax.add_collection(openCollection)ax.add_collection(closeCollection)return rangeCollection, openCollection, closeCollectiondef candlestick2_ochl(ax, opens, closes, highs, lows, width=4,colorup='k', colordown='r',alpha=0.75,):"""Represent the open, close as a bar line and high low range as avertical line.Preserves the original argument order.Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valuescloses : sequencesequence of closing valueshighs : sequencesequence of high valueslows : sequencesequence of low valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openalpha : floatbar transparencyReturns-------ret : tuple(lineCollection, barCollection)"""candlestick2_ohlc(ax, opens, highs, closes, lows, width=width,colorup=colorup, colordown=colordown,alpha=alpha)def candlestick2(ax, opens, closes, highs, lows, width=4,colorup='k', colordown='r',alpha=0.75,):"""Represent the open, close as a bar line and high low range as avertical line.This function has been deprecated in 1.4 in favor of`candlestick2_ochl`, which maintains the original argument order,or `candlestick2_ohlc`, which uses the open-high-low-close order.This function will be removed in 1.5Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valuescloses : sequencesequence of closing valueshighs : sequencesequence of high valueslows : sequencesequence of low valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openalpha : floatbar transparencyReturns-------ret : tuple(lineCollection, barCollection)"""warnings.warn(_warn_str.format(fun='candlestick2'),mplDeprecation)candlestick2_ohlc(ax, opens, highs, lows, closes, width=width,colorup=colorup, colordown=colordown,alpha=alpha)def candlestick2_ohlc(ax, opens, highs, lows, closes, width=4,colorup='k', colordown='r',alpha=0.75,):"""Represent the open, close as a bar line and high low range as avertical line.Parameters----------ax : `Axes`an Axes instance to plot toopens : sequencesequence of opening valueshighs : sequencesequence of high valueslows : sequencesequence of low valuescloses : sequencesequence of closing valuesticksize : intsize of open and close ticks in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openalpha : floatbar transparencyReturns-------ret : tuple(lineCollection, barCollection)"""# note this code assumes if any value open, low, high, close is# missing they all are missingdelta = width / 2.barVerts = [((i - delta, open),(i - delta, close),(i + delta, close),(i + delta, open))for i, open, close in zip(xrange(len(opens)), opens, closes)if open != -1 and close != -1]rangeSegments = [((i, low), (i, high))for i, low, high in zip(xrange(len(lows)), lows, highs)if low != -1]r, g, b = colorConverter.to_rgb(colorup)colorup = r, g, b, alphar, g, b = colorConverter.to_rgb(colordown)colordown = r, g, b, alphacolord = {True: colorup,False: colordown,}colors = [colord[open < close]for open, close in zip(opens, closes)if open != -1 and close != -1]assert(len(barVerts) == len(rangeSegments))useAA = 0, # use tuple herelw = 0.5, # and hererangeCollection = LineCollection(rangeSegments,colors=((0, 0, 0, 1), ),linewidths=lw,antialiaseds = useAA,)barCollection = PolyCollection(barVerts,facecolors=colors,edgecolors=((0, 0, 0, 1), ),antialiaseds=useAA,linewidths=lw,)minx, maxx = 0, len(rangeSegments)miny = min([low for low in lows if low != -1])maxy = max([high for high in highs if high != -1])corners = (minx, miny), (maxx, maxy)ax.update_datalim(corners)ax.autoscale_view()# add these lastax.add_collection(barCollection)ax.add_collection(rangeCollection)return rangeCollection, barCollectiondef volume_overlay(ax, opens, closes, volumes,colorup='k', colordown='r',width=4, alpha=1.0):"""Add a volume overlay to the current axes. The opens and closesare used to determine the color of the bar. -1 is missing. If avalue is missing on one it must be missing on allParameters----------ax : `Axes`an Axes instance to plot toopens : sequencea sequence of openscloses : sequencea sequence of closesvolumes : sequencea sequence of volumeswidth : intthe bar width in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openalpha : floatbar transparencyReturns-------ret : `barCollection`The `barrCollection` added to the axes"""r, g, b = colorConverter.to_rgb(colorup)colorup = r, g, b, alphar, g, b = colorConverter.to_rgb(colordown)colordown = r, g, b, alphacolord = {True: colorup,False: colordown,}colors = [colord[open < close]for open, close in zip(opens, closes)if open != -1 and close != -1]delta = width / 2.bars = [((i - delta, 0), (i - delta, v), (i + delta, v), (i + delta, 0))for i, v in enumerate(volumes)if v != -1]barCollection = PolyCollection(bars,facecolors=colors,edgecolors=((0, 0, 0, 1), ),antialiaseds=(0,),linewidths=(0.5,),)ax.add_collection(barCollection)corners = (0, 0), (len(bars), max(volumes))ax.update_datalim(corners)ax.autoscale_view()# add these lastreturn barCollectiondef volume_overlay2(ax, closes, volumes,colorup='k', colordown='r',width=4, alpha=1.0):"""Add a volume overlay to the current axes. The closes are used todetermine the color of the bar. -1 is missing. If a value ismissing on one it must be missing on allnb: first point is not displayed - it is used only for choosing theright colorParameters----------ax : `Axes`an Axes instance to plot tocloses : sequencea sequence of closesvolumes : sequencea sequence of volumeswidth : intthe bar width in pointscolorup : colorthe color of the lines where close >= opencolordown : colorthe color of the lines where close < openalpha : floatbar transparencyReturns-------ret : `barCollection`The `barrCollection` added to the axes"""return volume_overlay(ax, closes[:-1], closes[1:], volumes[1:],colorup, colordown, width, alpha)def volume_overlay3(ax, quotes,colorup='k', colordown='r',width=4, alpha=1.0):"""Add a volume overlay to the current axes. quotes is a list of (d,open, high, low, close, volume) and close-open is used todetermine the color of the barParameters----------ax : `Axes`an Axes instance to plot toquotes : sequence of (time, open, high, low, close, ...) sequencesdata to plot. time must be in float date format - see date2numwidth : intthe bar width in pointscolorup : colorthe color of the lines where close1 >= close0colordown : colorthe color of the lines where close1 < close0alpha : floatbar transparencyReturns-------ret : `barCollection`The `barrCollection` added to the axes"""r, g, b = colorConverter.to_rgb(colorup)colorup = r, g, b, alphar, g, b = colorConverter.to_rgb(colordown)colordown = r, g, b, alphacolord = {True: colorup,False: colordown,}dates, opens, highs, lows, closes, volumes = list(zip(*quotes))colors = [colord[close1 >= close0]for close0, close1 in zip(closes[:-1], closes[1:])if close0 != -1 and close1 != -1]colors.insert(0, colord[closes[0] >= opens[0]])right = width / 2.0left = -width / 2.0bars = [((left, 0), (left, volume), (right, volume), (right, 0))for d, open, high, low, close, volume in quotes]sx = ax.figure.dpi * (1.0 / 72.0) # scale for pointssy = ax.bbox.height / ax.viewLim.heightbarTransform = Affine2D().scale(sx, sy)dates = [d for d, open, high, low, close, volume in quotes]offsetsBars = [(d, 0) for d in dates]useAA = 0, # use tuple herelw = 0.5, # and herebarCollection = PolyCollection(bars,facecolors=colors,edgecolors=((0, 0, 0, 1),),antialiaseds=useAA,linewidths=lw,offsets=offsetsBars,transOffset=ax.transData,)barCollection.set_transform(barTransform)minpy, maxx = (min(dates), max(dates))miny = 0maxy = max([volume for d, open, high, low, close, volume in quotes])corners = (minpy, miny), (maxx, maxy)ax.update_datalim(corners)#print 'datalim', ax.dataLim.bounds#print 'viewlim', ax.viewLim.boundsax.add_collection(barCollection)ax.autoscale_view()return barCollectiondef index_bar(ax, vals,facecolor='b', edgecolor='l',width=4, alpha=1.0, ):"""Add a bar collection graph with height vals (-1 is missing).Parameters----------ax : `Axes`an Axes instance to plot tovals : sequencea sequence of valuesfacecolor : colorthe color of the bar faceedgecolor : colorthe color of the bar edgeswidth : intthe bar width in pointsalpha : floatbar transparencyReturns-------ret : `barCollection`The `barrCollection` added to the axes"""facecolors = (colorConverter.to_rgba(facecolor, alpha),)edgecolors = (colorConverter.to_rgba(edgecolor, alpha),)right = width / 2.0left = -width / 2.0bars = [((left, 0), (left, v), (right, v), (right, 0))for v in vals if v != -1]sx = ax.figure.dpi * (1.0 / 72.0) # scale for pointssy = ax.bbox.height / ax.viewLim.heightbarTransform = Affine2D().scale(sx, sy)offsetsBars = [(i, 0) for i, v in enumerate(vals) if v != -1]barCollection = PolyCollection(bars,facecolors=facecolors,edgecolors=edgecolors,antialiaseds=(0,),linewidths=(0.5,),offsets=offsetsBars,transOffset=ax.transData,)barCollection.set_transform(barTransform)minpy, maxx = (0, len(offsetsBars))miny = 0maxy = max([v for v in vals if v != -1])corners = (minpy, miny), (maxx, maxy)ax.update_datalim(corners)ax.autoscale_view()# add these lastax.add_collection(barCollection)return barCollection
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。