Advertisement
Guest User

_candlestick

a guest
Sep 28th, 2015
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.31 KB | None | 0 0
  1. def _candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
  2.                 alpha=1.0, ochl=True, coloredge=None):
  3.  
  4.     """
  5.    Plot the time, open, high, low, close as a vertical line ranging
  6.    from low to high.  Use a rectangular bar to represent the
  7.    open-close span.  If close >= open, use colorup to color the bar,
  8.    otherwise use colordown
  9.  
  10.    Parameters
  11.    ----------
  12.    ax : `Axes`
  13.        an Axes instance to plot to
  14.    quotes : sequence of quote sequences
  15.        data to plot.  time must be in float date format - see date2num
  16.        (time, open, high, low, close, ...) vs
  17.        (time, open, close, high, low, ...)
  18.        set by `ochl`
  19.    width : float
  20.        fraction of a day for the rectangle width
  21.    colorup : color
  22.        the color of the rectangle where close >= open
  23.    colordown : color
  24.         the color of the rectangle where close <  open
  25.    alpha : float
  26.        the rectangle alpha level
  27.    ochl: bool
  28.        argument to select between ochl and ohlc ordering of quotes
  29.    coloregde : color (optional)
  30.        the color of the edge. default is color of the candlebody  
  31.  
  32.    Returns
  33.    -------
  34.    ret : tuple
  35.        returns (lines, patches) where lines is a list of lines
  36.        added and patches is a list of the rectangle patches added
  37.  
  38.    """
  39.     if coloredge is None:
  40.         coloredgeup = colorup
  41.         coloredgedown = colordown
  42.     else:
  43.         coloredgeup=coloredge
  44.         coloredgedown=coloredge
  45.        
  46.     OFFSET = width / 2.0
  47.  
  48.     lines = []
  49.     patches = []
  50.    
  51.     for q in quotes:
  52.         if ochl:
  53.             t, open, close, high, low = q[:5]
  54.         else:
  55.             t, open, high, low, close = q[:5]
  56.            
  57.         if close >= open:
  58.             color = colorup
  59.             lower = open
  60.             height = close-open
  61.             vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgeup, linewidth=0.5, antialiased=True, zorder=0)
  62.         else:
  63.             color = colordown
  64.             lower = close
  65.             height = open-close
  66.             vline = Line2D(xdata=(t, t), ydata=(low, high), color=coloredgedown, linewidth=0.5, antialiased=True, zorder=0)
  67.  
  68.         if coloredge is None:
  69.             edgecolor = color
  70.            
  71.         rect = Rectangle(
  72.             xy=(t - OFFSET, lower),
  73.             width = width,
  74.             height = height,
  75.             facecolor = color,
  76.             edgecolor = coloredge,
  77.             zorder=1,
  78.         )
  79.         rect.set_alpha(alpha)
  80.  
  81.         lines.append(vline)
  82.         patches.append(rect)
  83.         ax.add_line(vline)
  84.         ax.add_patch(rect)
  85.     ax.autoscale_view()
  86.  
  87.     return lines, patches
  88.  
  89.  
  90.  
  91.  
  92. def candlestick(ax, quotes, width=0.2, colorup='k', colordown='r',
  93.                 alpha=1.0, coloredge=None):
  94.  
  95.     """
  96.    Plot the time, open, close, high, low as a vertical line ranging
  97.    from low to high.  Use a rectangular bar to represent the
  98.    open-close span.  If close >= open, use colorup to color the bar,
  99.    otherwise use colordown
  100.  
  101.  
  102.    This function has been deprecated in 1.4 in favor of
  103.    `candlestick_ochl`, which maintains the original argument
  104.    order, or `candlestick_ohlc`, which uses the
  105.    open-high-low-close order.  This function will be removed in 1.5
  106.  
  107.  
  108.    Parameters
  109.    ----------
  110.    ax : `Axes`
  111.        an Axes instance to plot to
  112.    quotes : sequence of (time, open, close, high, low, ...) sequences
  113.        As long as the first 5 elements are these values,
  114.        the record can be as long as you want (e.g., it may store volume).
  115.  
  116.        time must be in float days format - see date2num
  117.  
  118.    width : float
  119.        fraction of a day for the rectangle width
  120.    colorup : color
  121.        the color of the rectangle where close >= open
  122.    colordown : color
  123.         the color of the rectangle where close <  open
  124.    alpha : float
  125.        the rectangle alpha level
  126.    coloregde : color (optional)
  127.        the color of the edge. default is color of the candlebody  
  128.  
  129.    Returns
  130.    -------
  131.    ret : tuple
  132.        returns (lines, patches) where lines is a list of lines
  133.        added and patches is a list of the rectangle patches added
  134.  
  135.    """
  136.     warnings.warn(_warn_str.format(fun='candlestick'),
  137.                   mplDeprecation)
  138.  
  139.     return _candlestick(ax, quotes, width=width, colorup=colorup,
  140.                         colordown=colordown,
  141.                         alpha=alpha, ochl=True, coloredge=coloredge)
  142.  
  143.  
  144. def candlestick_ochl(ax, quotes, width=0.2, colorup='k', colordown='r',
  145.                 alpha=1.0, coloredge=None):
  146.  
  147.     """
  148.    Plot the time, open, close, high, low as a vertical line ranging
  149.    from low to high.  Use a rectangular bar to represent the
  150.    open-close span.  If close >= open, use colorup to color the bar,
  151.    otherwise use colordown
  152.  
  153.    Parameters
  154.    ----------
  155.    ax : `Axes`
  156.        an Axes instance to plot to
  157.    quotes : sequence of (time, open, close, high, low, ...) sequences
  158.        As long as the first 5 elements are these values,
  159.        the record can be as long as you want (e.g., it may store volume).
  160.  
  161.        time must be in float days format - see date2num
  162.  
  163.    width : float
  164.        fraction of a day for the rectangle width
  165.    colorup : color
  166.        the color of the rectangle where close >= open
  167.    colordown : color
  168.         the color of the rectangle where close <  open
  169.    alpha : float
  170.        the rectangle alpha level
  171.    coloregde : color (optional)
  172.        the color of the edge. default is color of the candlebody  
  173.  
  174.    Returns
  175.    -------
  176.    ret : tuple
  177.        returns (lines, patches) where lines is a list of lines
  178.        added and patches is a list of the rectangle patches added
  179.  
  180.    """
  181.     return _candlestick(ax, quotes, width=width, colorup=colorup,
  182.                 colordown=colordown,
  183.                 alpha=alpha, ochl=True, coloredge=coloredge)
  184.  
  185.  
  186.  
  187. def candlestick_ohlc(ax, quotes, width=0.2, colorup='k', colordown='r',
  188.                 alpha=1.0, coloredge=None):
  189.  
  190.     """
  191.    Plot the time, open, high, low, close as a vertical line ranging
  192.    from low to high.  Use a rectangular bar to represent the
  193.    open-close span.  If close >= open, use colorup to color the bar,
  194.    otherwise use colordown
  195.  
  196.    Parameters
  197.    ----------
  198.    ax : `Axes`
  199.        an Axes instance to plot to
  200.    quotes : sequence of (time, open, high, low, close, ...) sequences
  201.        As long as the first 5 elements are these values,
  202.        the record can be as long as you want (e.g., it may store volume).
  203.  
  204.        time must be in float days format - see date2num
  205.  
  206.    width : float
  207.        fraction of a day for the rectangle width
  208.    colorup : color
  209.        the color of the rectangle where close >= open
  210.    colordown : color
  211.         the color of the rectangle where close <  open
  212.    alpha : float
  213.        the rectangle alpha level
  214.    coloregde : color (optional)
  215.        the color of the edge. default is color of the candlebody  
  216.  
  217.    Returns
  218.    -------
  219.    ret : tuple
  220.        returns (lines, patches) where lines is a list of lines
  221.        added and patches is a list of the rectangle patches added
  222.  
  223.    """
  224.     return _candlestick(ax, quotes, width=width, colorup=colorup,
  225.                         colordown=colordown,
  226.                         alpha=alpha, ochl=False, coloredge=coloredge)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement