³ò
@wOGc           @   sA  d  Z  d d k Z d d k Z d d k Z d d k Z d d k l Z y d d k Z Wn e j
 o e	 d ƒ ‚ n Xd d k
 Z d d k l Z l Z d d k l Z d d k l Z l Z d d k l Z d d	 k l Z l Z d d
 k l Z d d k l Z l Z l Z d d k l  Z  d d k! l" Z" l# Z# l$ Z$ l% Z% l& Z& l' Z' e ƒ  Z( e i) i* e( d ƒ Z+ e, e- d „ Z. d d „ Z0 e, e- d d „ Z1 d d d d „ Z2 d d d d d „ Z3 d d d d „ Z4 d d d d d „ Z5 d d d d d „ Z6 d d d d d „ Z7 d d d d d „ Z8 d  d! d d d" „ Z9 d S(#   so   
A collection of modules for collecting, analyzing and plotting
financial data.   User contributions welcome!

iÿÿÿÿN(   t   urlopens8   The finance module requires datetime support (python2.3)(   t   verboset   get_configdir(   t   Artist(   t   date2numt   num2date(   t   Bunch(   t   LineCollectiont   PolyCollection(   t   colorConverter(   t   Line2Dt   TICKLEFTt	   TICKRIGHT(   t	   Rectangle(   t   scale_transformt   Valuet   zerot   onet   scale_sep_transformt   blend_xy_sep_transforms   finance.cachec         C   s   g  } |  i  ƒ  } d } xx| d D]l} | i d ƒ } t | ƒ d j o q# n | d } | d j oI y) d } t i t i | | ƒ d  Œ  }	 Wq² t j
 o d } q² Xn t i t i | | ƒ d  Œ  }	 t	 |	 ƒ }
 g  } | d d !D] } | t
 | ƒ qï ~ \ } } } } t | d ƒ } | oB t
 | d	 ƒ } | | } | | 9} | | 9} | | 9} | } n | i |
 | | | | | f ƒ q# W| i ƒ  | ot t | ƒ d j o d Sn+ t t i t | Œ  ƒ \ } } } } } } t d
 | d | d | d | d | d | ƒ Sn | Sd S(   s  
    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
    i   t   ,i   i    s   %Y-%m-%di   s   %d-%b-%yi   i   t   datet   opent   closet   hight   lowt   volumeN(   t	   readlinest   Nonet   splitt   lent   datetimeR   t   timet   strptimet
   ValueErrorR   t   floatt   intt   appendt   reverset   mapt   npyt   asarrayt   zipR   (   t   fht   asobjectt   adjustedt   resultst   linest   datefmtt   linet   valst   datestrt   dtt   dt   _[1]t   valR   R   R   R   R   t   acloset   mR   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   parse_yahoo_historical"   s@     
#:




#
*/c   	      C   sp  |  i  ƒ  }  | i d | i | i f } | i d | i | i f } d } | | d | d | d | d | d | d |  f } | d	 j o( t i i t t	 i	 | ƒ i
 ƒ  ƒ } n t i i | ƒ o' t | ƒ } t i d | |  f ƒ n} t i i t ƒ p t i t ƒ n t | d ƒ } | i t | ƒ i ƒ  ƒ | i ƒ  t i d |  | f ƒ t | d ƒ } | S(
   se  
    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
    i   s_   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=.csvi    i   s   Using cachefile %s for %st   ws   Saved %s data to cache file %st   rN(   t   uppert   montht   dayt   yearR   t   ost   patht   joint   cachedirt   md5t	   hexdigestt   existst   fileR   t   reportt   isdirt   mkdirt   writeR    t   readR   (	   t   tickert   date1t   date2t	   cachenamet   d1t   d2t   urlFmtt   urlR+   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   fetch_historical_yahooV   s$    ($
c   	      C   sk   t  |  | | | ƒ } y t | | | ƒ } Wn9 t j
 o- } t i d t d | i d ƒ d Sn X| S(   sŒ  
    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)
    s   urlopen() failure
s   
i   N(   RV   R:   t   IOErrort   warningst   warnRU   t   strerrorR   (	   RN   RO   RP   R,   R-   RQ   R+   t   rett   exc(    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   quotes_historical_yahooƒ   s     	i   t   kR<   c         C   s7  g  } x | D]} | d  \ } } }	 }
 } |	 | j o
 | } n | } t  d | | f d | |
 f d | d t ƒ } t  d | | f d | | f d | d t d t d | ƒ } t  d | | f d |	 |	 f d | d t d | d t ƒ } | i | | | f ƒ |  i | ƒ |  i | ƒ |  i | ƒ q W|  i ƒ  | S(   s%  
    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
    i   t   xdatat   ydatat   colort   antialiasedt   markert
   markersize(   R
   t   FalseR   R   t   extendt   add_linet   autoscale_view(   t   axt   quotest   ticksizet   colorupt	   colordownR/   t   qt   tR   R   R   R   Ra   t   vlinet   olinet   cline(    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   plot_day_summaryª   s8     			
gš™™™™™É?g      ð?c         C   s6  | d } g  } g  } x	| D]}	 |	 d  \ }
 } } } } | | j o | } | } | | } n | } | } | | } t  d |
 |
 f d | | f d d d d d	 t ƒ } t d
 |
 | | f d | d | d | d | ƒ } | i | ƒ | i | ƒ | i | ƒ |  i | ƒ |  i | ƒ q W|  i ƒ  | | f S(   s‰  

    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
    g       @i   R_   R`   Ra   R^   t	   linewidthg      à?Rb   t   xyt   widtht   heightt	   facecolort	   edgecolor(   R
   t   TrueR   t	   set_alphaR%   Rg   t	   add_patchRh   (   Ri   Rj   Rv   Rl   Rm   t   alphat   OFFSETR/   t   patchesRn   Ro   R   R   R   R   Ra   t   lowerRw   Rp   t   rect(    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   candlestickè   s>    
 
		
i   c   )      C   s
  g  } t  t t | ƒ ƒ | | ƒ D]9 \ }	 }
 } |
 d j o | |	 |
 f |	 | f f q# q# ~ } | d f d
 f g } d | d f f g } g  } t  t t | ƒ ƒ | ƒ D]* \ }	 } | d j o | |	 | f q° q° ~ } g  } t  t t | ƒ ƒ | ƒ D]* \ }	 } | d j o | |	 | f qq~ } |  i i t d d ƒ } t | t ƒ  ƒ } t i	 | ƒ \ } } } | | | d f } t i	 | ƒ \ } } } | | | d f } h  | t
 <| t <} g  } t  | | ƒ D]; \ } } | d j o" | d j o | | | | j  qâqâ~ } t | ƒ t | ƒ j p t ‚ t | ƒ t | ƒ j p t ‚ t | ƒ t | ƒ j p t ‚ d } d } t | d | d | d | ƒ} t | d | d | d | d | d	 |  i ƒ}  |  i | ƒ t | d | d | d | d | d	 |  i ƒ}! |! i | ƒ d t | ƒ }" }# t g  }$ | D] }
 |
 d j o |$ |
 qEqE~$ ƒ }% t g  }& | D] } | d j o |& | q}q}~& ƒ }' |" |% f |# |' f f }( |  i |( ƒ |  i ƒ  |  i | ƒ |  i |  ƒ |  i |! ƒ | |  |! f S(   s³  

    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
    iÿÿÿÿi    i   g      R@t   colorst
   linewidthst   antialiasedst   offsetst   transOffset(   i    i    (   i    i    (   i    (   i   (   R*   t   xrangeR   t   figuret   dpiR   R   R   R	   t   to_rgbRz   Re   t   AssertionErrorR   t	   transDatat   set_transformt   mint   maxt   update_datalimRh   t   add_collection()   Ri   t   openst   closest   highst   lowsRk   Rl   Rm   R6   t   iR   R   t   rangeSegmentst   openSegmentst   closeSegmentst   _[2]R   t   offsetsOpent   _[3]R   t   offsetsCloset   scalet   tickTransformR<   t   gt   bt   colordt   _[4]Rƒ   t   useAAt   lwt   rangeCollectiont   openCollectiont   closeCollectiont   minpyt   maxxt   _[5]t   minyt   _[6]t   maxyt   corners(    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   plot_day_summary2.  s^    eSSX   				88
g      è?c	   *      C   s"  | d }	 | d }
 g  } t  | | ƒ D]] \ } } | d j oD | d j o7 | |
 d f |
 | | f |	 | | f |	 d f f q) q) ~ } g  } t  t t | ƒ ƒ | | ƒ D]9 \ } } } | d j o | | | f | | f f q² q² ~ } g  } t  t t | ƒ ƒ | ƒ D]* \ } } | d j o | | | f qq~ } |  i i t d d ƒ } |  i i ƒ  i ƒ  |  i i	 ƒ  i ƒ  |  i
 i ƒ  i ƒ  |  i
 i	 ƒ  i ƒ  } t | | ƒ } t i | ƒ \ } } } | | | | f } t i | ƒ \ } } } | | | | f } h  | t <| t <} g  } t  | | ƒ D]; \ } } | d j o" | d j o | | | | j  q>q>~ } t | ƒ t | ƒ j p t ‚ t | ƒ t | ƒ j p t ‚ t | ƒ t | ƒ j p t ‚ d } d }  t | d d d |  d	 | ƒ}! t | d
 | d d d	 | d |  d | d |  i ƒ}" |" i | ƒ d t | ƒ }# }$ t g  }% | D] } | d j o |% | qmqm~% ƒ }& t g  }' | D] } | d j o |' | q¥q¥~' ƒ }( |# |& f |$ |( f f }) |  i |) ƒ |  i ƒ  |  i |" ƒ |  i |! ƒ |! |" f S(   s‡  

    Represent the open, close as a bar line and high low range as a
    vertical line.


    ax          : an Axes instance to plot to
    width       : the bar width in points
    colorup     : the color of the lines where close >= open
    colordown   : the color of the lines where close <  open
    alpha       : bar transparency

    return value is lineCollection, barCollection
    g       @iÿÿÿÿi    i   g      R@g      à?Rƒ   R„   R…   t
   facecolorst
   edgecolorsR†   R‡   (   i    (   g      à?(   i    i    i    i   (   (   i    i    i    i   (   i    i    i    i   (   (   i    i    i    i   (   R*   Rˆ   R   R‰   RŠ   R   t   bboxt   urt   yt   llt   viewLimR   R	   R‹   Rz   Re   RŒ   R   R   R   RŽ   R   R   R‘   Rh   R’   (*   Ri   R“   R”   R•   R–   Rv   Rl   Rm   R}   t   rightt   leftR6   R   R   t   barVertsR›   R—   R   R   R˜   R   t   offsetsBarst   sxt   syt   barTransformR<   R¡   R¢   R£   R¤   Rƒ   R¥   R¦   R§   t   barCollectionRª   R«   R¬   R­   R®   R¯   R°   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   candlestick2‹  sR    
zeSNX   			88
c   "      C   s®  t  i | ƒ \ } }	 }
 | |	 |
 | f } t  i | ƒ \ } }	 }
 | |	 |
 | f } h  | t <| t <} g  } t | | ƒ D]; \ } } | d j o" | d j o | | | | j  q€ q€ ~ } | d } | d } g  } | D]B } | d j o/ | | d f | | f | | f | d f f qä qä ~ } |  i i t d d ƒ } |  i i	 ƒ  i
 ƒ  |  i i ƒ  i
 ƒ  |  i i	 ƒ  i
 ƒ  |  i i ƒ  i
 ƒ  } t | | ƒ } g  } t | ƒ D]* \ } } | d j o | | d f q·q·~ } t | d | d d d d d	 d d | d |  i ƒ} | i | ƒ d t | ƒ } } d } t g  } | D] } | d j o | | qQqQ~ ƒ }  | | f | |  f f }! |  i |! ƒ |  i ƒ  | S(   s½  
    Add a volume overlay to the current axes.  The opens and closes
    are used to determine the color of the bar.  -1 is missing.  If a
    value is missing on one it must be missing on all

    ax          : an Axes instance to plot to
    width       : the bar width in points
    colorup     : the color of the lines where close >= open
    colordown   : the color of the lines where close <  open
    alpha       : bar transparency


    iÿÿÿÿg       @i    i   g      R@R²   R³   R…   R„   g      à?R†   R‡   (   i    i    i    i   (   (   i    i    i    i   (   i    (   g      à?(   R	   R‹   Rz   Re   R*   R‰   RŠ   R   R´   Rµ   R¶   R·   R¸   R   t	   enumerateR   R   RŽ   R   R   R‘   Rh   ("   Ri   R“   R”   t   volumesRl   Rm   Rv   R}   R<   R¡   R¢   R£   R6   R   R   Rƒ   R¹   Rº   R›   t   vt   barsR½   R¾   R¿   R   R—   R¼   RÀ   Rª   R«   R­   R¤   R¯   R°   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   volume_overlayã  s:    X
VND	8
c         C   s+   t  |  | d  | d | d | | | | ƒ S(   s  
    Add a volume overlay to the current axes.  The closes are used to
    determine the color of the bar.  -1 is missing.  If a value is
    missing on one it must be missing on all

    ax          : an Axes instance to plot to
    width       : the bar width in points
    colorup     : the color of the lines where close >= 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

    iÿÿÿÿi   (   RÆ   (   Ri   R”   RÃ   Rl   Rm   Rv   R}   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   volume_overlay2$  s    c   -      C   s-  t  i | ƒ \ } } } | | | | f } t  i | ƒ \ } } } | | | | f } h  | t <| t <}	 t | Œ  \ }
 } } } } } g  } t | d  | d ƒ D]; \ } } | d j o" | d j o | |	 | | j q¦ q¦ ~ } | i d |	 | d | d j ƒ | d } | d } g  } | D]C \ } } } } } } | | d f | | f | | f | d f f q,~ } |  i i t d d ƒ } |  i	 i
 ƒ  i ƒ  |  i	 i ƒ  i ƒ  |  i i
 ƒ  i ƒ  |  i i ƒ  i ƒ  } t | | ƒ }  g  }! | D] \ } } } } } } |! | qú~! }
 g  }" |
 D] } |" | d f q-~" }# d }$ d }% t | d | d d d	 |$ d
 |% d |# d |  i ƒ}& |& i |  ƒ t |
 ƒ t |
 ƒ }' }( d }) t g  }* | D] \ } } } } } } |* | qÂ~* ƒ }+ |' |) f |( |+ f f }, |  i |, ƒ |  i |& ƒ |  i ƒ  |& S(   s‡  
    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


    iÿÿÿÿi   i    g       @g      R@g      à?R²   R³   R…   R„   R†   R‡   (   i    (   g      à?(   i    i    i    i   (   (   i    i    i    i   (   R	   R‹   Rz   Re   R*   t   insertR‰   RŠ   R   R´   Rµ   R¶   R·   R¸   R   R   R   RŽ   R   R   R‘   R’   Rh   (-   Ri   Rj   Rl   Rm   Rv   R}   R<   R¡   R¢   R£   t   datesR“   R”   R•   R–   RÃ   R6   t   close0t   close1Rƒ   R¹   Rº   R›   R5   R   R   R   R   R   RÅ   R½   R¾   R¿   R   R¤   R¼   R¥   R¦   RÀ   Rª   R«   R­   R¬   R¯   R°   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt   volume_overlay3:  sF    `"
WN3'	9
R¢   t   lc         C   s!  t  i | | ƒ f } t  i | | ƒ f } | d } | d }	 g  }
 | D]B } | d j o/ |
 |	 d f |	 | f | | f | d f f qJ qJ ~
 } |  i i t d d ƒ } |  i i ƒ  i ƒ  |  i i ƒ  i ƒ  |  i	 i ƒ  i ƒ  |  i	 i ƒ  i ƒ  } t
 | | ƒ } g  } t | ƒ D]* \ } } | d j o | | d f qq~ } t | d | d | d d d	 d d | d |  i ƒ} | i | ƒ d t | ƒ } } d } t g  } | D] } | d j o | | q·q·~ ƒ } | | f | | f f } |  i | ƒ |  i ƒ  |  i | ƒ | S(   sÄ   
    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


    g       @iÿÿÿÿi    i   g      R@R²   R³   R…   R„   g      à?R†   R‡   (   i    (   g      à?(   R	   t   to_rgbaR‰   RŠ   R   R´   Rµ   R¶   R·   R¸   R   RÂ   R   R   RŽ   R   R   R‘   Rh   R’   (   Ri   R2   Rx   Ry   Rv   R}   R²   R³   R¹   Rº   R6   RÄ   RÅ   R½   R¾   R¿   R›   R—   R¼   RÀ   Rª   R«   R­   R   R¯   R°   (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pyt	   index_barƒ  s2    
VND	8
(:   t   __doc__RA   R    RX   RE   t   urllibR    R   t   ImportErrort
   SystemExitt   numpyR(   t
   matplotlibR   R   t   artistR   RÉ   R   R   t   matplotlib.cbookR   t   matplotlib.collectionsR   R   t   matplotlib.colorsR	   R/   R
   R   R   R   R   t   matplotlib.transformsR   R   R   R   R   R   t	   configdirRB   RC   RD   Re   Rz   R:   R   RV   R]   Rs   R‚   R±   RÁ   RÆ   RÇ   RÌ   RÏ   (    (    (    s;   /Users/cmoad/workspace/matplotlib/lib/matplotlib/finance.pys   <module>   sJ   0.	4-'=	E\W@H