/opt/alt/python27/lib64/python2.7/site-packages/matplotlib
NameSizeModeActions
backends/-0755rm
delaunay/-0755rm
mpl-data/-0755rm
numerix/-0755rm
projections/-0755rm
sphinxext/-0755rm
afm.py150890644editdlrm
afm.pyc173880644editdlrm
afm.pyo173880644editdlrm
artist.py356230644editdlrm
artist.pyc414310644editdlrm
artist.pyo414310644editdlrm
axes.py2800890644editdlrm
axes.pyc2525680644editdlrm
axes.pyo2514560644editdlrm
axis.py573540644editdlrm
axis.pyc608580644editdlrm
axis.pyo605510644editdlrm
backend_bases.py723180644editdlrm
backend_bases.pyc799690644editdlrm
backend_bases.pyo798790644editdlrm
bezier.py149170644editdlrm
bezier.pyc138120644editdlrm
bezier.pyo138120644editdlrm
blocking_input.py136900644editdlrm
blocking_input.pyc147620644editdlrm
blocking_input.pyo144440644editdlrm
cbook.py427560644editdlrm
cbook.pyc586920644editdlrm
cbook.pyo584660644editdlrm
cm.py80980644editdlrm
cm.pyc100320644editdlrm
cm.pyo100320644editdlrm
collections.py407840644editdlrm
collections.pyc451110644editdlrm
collections.pyo451110644editdlrm
colorbar.py274560644editdlrm
colorbar.pyc258350644editdlrm
colorbar.pyo258350644editdlrm
colors.py391100644editdlrm
colors.pyc406900644editdlrm
colors.pyo406900644editdlrm
contour.py428950644editdlrm
contour.pyc364630644editdlrm
contour.pyo363010644editdlrm
dates.py360070755editdlrm
dates.pyc411350644editdlrm
dates.pyo410710644editdlrm
dviread.py320330644editdlrm
dviread.pyc354880644editdlrm
dviread.pyo353450644editdlrm
figure.py412640644editdlrm
figure.pyc437060644editdlrm
figure.pyo435750644editdlrm
finance.py205820644editdlrm
finance.pyc186750644editdlrm
finance.pyo185150644editdlrm
fontconfig_pattern.py64290644editdlrm
fontconfig_pattern.pyc67100644editdlrm
fontconfig_pattern.pyo67100644editdlrm
font_manager.py426530644editdlrm
font_manager.pyc412750644editdlrm
font_manager.pyo412750644editdlrm
ft2font.so3061680755editdlrm
hatch.py65640644editdlrm
hatch.pyc94840644editdlrm
hatch.pyo94840644editdlrm
image.py315400644editdlrm
image.pyc337830644editdlrm
image.pyo337370644editdlrm
legend.py323020644editdlrm
legend.pyc238760644editdlrm
legend.pyo237030644editdlrm
lines.py546850644editdlrm
lines.pyc579910644editdlrm
lines.pyo578860644editdlrm
mathtext.py1020090644editdlrm
mathtext.pyc1060700644editdlrm
mathtext.pyo1056820644editdlrm
mlab.py942940644editdlrm
mlab.pyc1019150644editdlrm
mlab.pyo1017350644editdlrm
mpl.py7850644editdlrm
mpl.pyc11890644editdlrm
mpl.pyo11890644editdlrm
nxutils.so97040755editdlrm
offsetbox.py298070644editdlrm
offsetbox.pyc348250644editdlrm
offsetbox.pyo347700644editdlrm
patches.py1284850644editdlrm
patches.pyc1337360644editdlrm
patches.pyo1335980644editdlrm
path.py217650644editdlrm
path.pyc214990644editdlrm
path.pyo212790644editdlrm
pylab.py109810644editdlrm
pylab.pyc124380644editdlrm
pylab.pyo124380644editdlrm
pyparsing.py1439240644editdlrm
pyparsing.pyc1511830644editdlrm
pyparsing.pyo1511830644editdlrm
pyplot.py824680644editdlrm
pyplot.pyc819820644editdlrm
pyplot.pyo819820644editdlrm
quiver.py375400644editdlrm
quiver.pyc341520644editdlrm
quiver.pyo341520644editdlrm
rcsetup.py234340644editdlrm
rcsetup.pyc215220644editdlrm
rcsetup.pyo213520644editdlrm
scale.py142330644editdlrm
scale.pyc202540644editdlrm
scale.pyo202540644editdlrm
spines.py126840644editdlrm
spines.pyc118830644editdlrm
spines.pyo114560644editdlrm
table.py168280644editdlrm
table.pyc173730644editdlrm
table.pyo172220644editdlrm
texmanager.py224640644editdlrm
texmanager.pyc202090644editdlrm
texmanager.pyo202090644editdlrm
text.py598220644editdlrm
text.pyc606960644editdlrm
text.pyo606960644editdlrm
ticker.py404280644editdlrm
ticker.pyc521640644editdlrm
ticker.pyo520280644editdlrm
tight_bbox.py36550644editdlrm
tight_bbox.pyc41570644editdlrm
tight_bbox.pyo41570644editdlrm
transforms.py753870644editdlrm
transforms.pyc924320644editdlrm
transforms.pyo915230644editdlrm
ttconv.so654880755editdlrm
type1font.py115370644editdlrm
type1font.pyc114900644editdlrm
type1font.pyo114900644editdlrm
units.py49760644editdlrm
units.pyc59180644editdlrm
units.pyo59180644editdlrm
widgets.py408330644editdlrm
widgets.pyc436080644editdlrm
widgets.pyo434220644editdlrm
windowing.py4540644editdlrm
windowing.pyc13610644editdlrm
windowing.pyo13610644editdlrm
_cm.py3704140644editdlrm
_cm.pyc1030550644editdlrm
_cm.pyo1030550644editdlrm
_cntr.so209600755editdlrm
_delaunay.so611840755editdlrm
_image.so2828560755editdlrm
_mathtext_data.py579900644editdlrm
_mathtext_data.pyc638000644editdlrm
_mathtext_data.pyo638000644editdlrm
_path.so2803120755editdlrm
_png.so1565040755editdlrm
_pylab_helpers.py27150644editdlrm
_pylab_helpers.pyc38380644editdlrm
_pylab_helpers.pyo38380644editdlrm
__init__.py289720644editdlrm
__init__.pyc294350644editdlrm
__init__.pyo293810644editdlrm
Edit: /opt/alt/python27/lib64/python2.7/site-packages/matplotlib/finance.py (20582B)
""" A collection of modules for collecting, analyzing and plotting financial data. User contributions welcome! """ #from __future__ import division import os, time, warnings from urllib2 import urlopen try: from hashlib import md5 except ImportError: from md5 import md5 #Deprecated in 2.5 try: import datetime except ImportError: raise ImportError('The finance module requires datetime support (python2.3)') import numpy as np from matplotlib import verbose, get_configdir from dates import date2num from matplotlib.cbook import Bunch from matplotlib.collections import LineCollection, PolyCollection from matplotlib.colors import colorConverter from lines import Line2D, TICKLEFT, TICKRIGHT from patches import Rectangle from matplotlib.transforms import Affine2D configdir = get_configdir() cachedir = os.path.join(configdir, 'finance.cache') def parse_yahoo_historical(fh, asobject=False, adjusted=True): """ Parse the historical data in file handle fh from yahoo finance and return results as a list of d, open, close, high, low, volume where d is a floating poing representation of date, as returned by date2num if adjust=True, use adjusted prices """ results = [] lines = fh.readlines() datefmt = None for line in lines[1:]: vals = line.split(',') if len(vals)!=7: continue datestr = vals[0] if datefmt is None: try: datefmt = '%Y-%m-%d' dt = datetime.date(*time.strptime(datestr, datefmt)[:3]) except ValueError: datefmt = '%d-%b-%y' # Old Yahoo--cached file? dt = datetime.date(*time.strptime(datestr, datefmt)[:3]) d = date2num(dt) open, high, low, close = [float(val) for val in vals[1:5]] volume = int(vals[5]) if adjusted: aclose = float(vals[6]) m = aclose/close open *= m high *= m low *= m close = aclose results.append((d, open, close, high, low, volume)) results.reverse() if asobject: if len(results)==0: return None else: date, open, close, high, low, volume = map(np.asarray, zip(*results)) return Bunch(date=date, open=open, close=close, high=high, low=low, volume=volume) else: return results def fetch_historical_yahoo(ticker, date1, date2, cachename=None): """ Fetch historical data for ticker between date1 and date2. date1 and date2 are datetime instances Ex: fh = fetch_historical_yahoo('^GSPC', d1, d2) cachename is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range) a file handle is returned """ ticker = ticker.upper() d1 = (date1.month-1, date1.day, date1.year) d2 = (date2.month-1, date2.day, date2.year) urlFmt = 'http://table.finance.yahoo.com/table.csv?a=%d&b=%d&c=%d&d=%d&e=%d&f=%d&s=%s&y=0&g=d&ignore=.csv' url = urlFmt % (d1[0], d1[1], d1[2], d2[0], d2[1], d2[2], ticker) if cachename is None: cachename = os.path.join(cachedir, md5(url).hexdigest()) if os.path.exists(cachename): fh = file(cachename) verbose.report('Using cachefile %s for %s'%(cachename, ticker)) else: if not os.path.isdir(cachedir): os.mkdir(cachedir) urlfh = urlopen(url) fh = file(cachename, 'w') fh.write(urlfh.read()) fh.close() verbose.report('Saved %s data to cache file %s'%(ticker, cachename)) fh = file(cachename, 'r') return fh def quotes_historical_yahoo(ticker, date1, date2, asobject=False, adjusted=True, cachename=None): """ Get historical data for ticker between date1 and date2. date1 and date2 are datetime instances results are a list of tuples (d, open, close, high, low, volume) where d is a floating poing representation of date, as returned by date2num if asobject is True, the return val is an object with attrs date, open, close, high, low, volume, which are equal length arrays if adjust=True, use adjusted prices Ex: 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) cachename is the name of the local file cache. If None, will default to the md5 hash or the url (which incorporates the ticker and date range) """ fh = fetch_historical_yahoo(ticker, date1, date2, cachename) try: ret = parse_yahoo_historical(fh, asobject, adjusted) except IOError, exc: warnings.warn('urlopen() failure\n' + url + '\n' + exc.strerror[1]) return None return ret def plot_day_summary(ax, quotes, ticksize=3, colorup='k', colordown='r', ): """ quotes is a list of (time, open, close, high, low, ...) tuples 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. time must be in float date format - see date2num ax : an Axes instance to plot to ticksize : open/close tick marker in points colorup : the color of the lines where close >= open colordown : the color of the lines where close < open return value is a list of lines added """ lines = [] for q in quotes: t, open, close, high, low = q[:5] if close>=open : color = colorup else : 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() return lines def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r', alpha=1.0): """ quotes is a list of (time, open, close, high, low, ...) tuples. As long as the first 5 elements of the tuples are these values, the tuple can be as long as you want (eg it may store volume). time must be in float days format - see date2num 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 ax : an Axes instance to plot to width : fraction of a day for the rectangle width colorup : the color of the rectangle where close >= open colordown : the color of the rectangle where close < open alpha : the rectangle alpha level return value is 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: t, open, close, high, low = 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='k', 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 plot_day_summary2(ax, opens, closes, highs, lows, ticksize=4, colorup='k', colordown='r', ): """ 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. ax : an Axes instance to plot to ticksize : size of open and close ticks in points colorup : the color of the lines where close >= open colordown : the color of the lines where close < open return value is a list of lines added """ # note this code assumes if any value open, close, low, high is # missing they all are missing rangeSegments = [ ((i, low), (i, high)) for i, low, high in zip(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 location openSegments = [ ((-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 location closeSegments = [ ((0, 0), (ticksize, 0)) ] offsetsOpen = [ (i, open) for i, open in zip(xrange(len(opens)), opens) if open != -1 ] offsetsClose = [ (i, close) for i, close in zip(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,1 r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,1 colord = { True : colorup, False : colordown, } colors = [colord[open= open colordown : the color of the lines where close < open alpha : bar transparency return value is lineCollection, barCollection """ # note this code assumes if any value open, close, low, high is # missing they all are missing delta = 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,alpha r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,alpha colord = { True : colorup, False : colordown, } colors = [colord[open= open colordown : the color of the lines where close < open alpha : bar transparency """ r,g,b = colorConverter.to_rgb(colorup) colorup = r,g,b,alpha r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,alpha colord = { True : colorup, False : colordown, } colors = [colord[open= open colordown : the color of the lines where close < open alpha : bar transparency nb: first point is not displayed - it is used only for choosing the right color """ 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, close, high, low, volume) and close-open is used to determine the color of the bar kwarg width : the bar width in points colorup : the color of the lines where close1 >= close0 colordown : the color of the lines where close1 < close0 alpha : bar transparency """ r,g,b = colorConverter.to_rgb(colorup) colorup = r,g,b,alpha r,g,b = colorConverter.to_rgb(colordown) colordown = r,g,b,alpha colord = { True : colorup, False : colordown, } dates, opens, closes, highs, lows, volumes = 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.0 left = -width/2.0 bars = [ ( (left, 0), (left, volume), (right, volume), (right, 0)) for d, open, close, high, low, volume in quotes] sx = ax.figure.dpi * (1.0/72.0) # scale for points sy = ax.bbox.height / ax.viewLim.height barTransform = Affine2D().scale(sx,sy) dates = [d for d, open, close, high, low, volume in quotes] offsetsBars = [(d, 0) for d in dates] useAA = 0, # use tuple here lw = 0.5, # and here barCollection = 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 = 0 maxy = max([volume for d, open, close, high, low, volume in quotes]) corners = (minpy, miny), (maxx, maxy) ax.update_datalim(corners) #print 'datalim', ax.dataLim.get_bounds() #print 'viewlim', ax.viewLim.get_bounds() ax.add_collection(barCollection) ax.autoscale_view() return barCollection def index_bar(ax, vals, facecolor='b', edgecolor='l', width=4, alpha=1.0, ): """ Add a bar collection graph with height vals (-1 is missing). ax : an Axes instance to plot to width : the bar width in points alpha : bar transparency """ facecolors = (colorConverter.to_rgba(facecolor, alpha),) edgecolors = (colorConverter.to_rgba(edgecolor, alpha),) right = width/2.0 left = -width/2.0 bars = [ ( (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 points sy = ax.bbox.height / ax.viewLim.height barTransform = 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 = 0 maxy = max([v for v in vals if v!=-1]) corners = (minpy, miny), (maxx, maxy) ax.update_datalim(corners) ax.autoscale_view() # add these last ax.add_collection(barCollection) return barCollection