candlestick in finance.py

Hi all,

I am implementing a program using the candlestick function which plots some finance data.

After plotting some graph, I found that it doesn’t follow the candlestick charting convention according to:

http://stockcharts.com/school/doku.php?id=chart_school:chart_analysis:introduction_to_candlesticks

I have made some changes to candlestick in finance.py but I have no idea how to modify candlestick2… because I am a python newbie.

I am posting the modified code to the list because I don’t know how to get it patched into the source control. Please let me know If I post it to the wrong list.

----------------------------------------code section begin-----------------------------

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 = []
prev_close=-1
for q in quotes:
    t, open, close, high, low = q[:5]
   
    if(prev_close==-1 or close>=prev_close):
        color_edge=colorup
    else:
        color_edge=colordown
    prev_close=close

if close>=open :
color_body=‘w’
lower = open
height = close-open
vline1 = Line2D(
xdata=(t, t), ydata=(close, high),
color=color_edge,
linewidth=0.5,
antialiased=True,
)
vline2 = Line2D(
xdata=(t, t), ydata=(open, low),
color=color_edge,
linewidth=0.5,
antialiased=True,
)

    else           :
        color_body = color_edge
        lower = close
        height = open-close
        vline1 = Line2D(
            xdata=(t, t), ydata=(close, low),
            color=color_edge,
            linewidth=0.5,
            antialiased=True,
            )
        vline2 = Line2D(
            xdata=(t, t), ydata=(open, high),
            color=color_edge,
            linewidth=0.5,
            antialiased=True,
            )

    rect = Rectangle(
        xy    = (t-OFFSET, lower),
        width = width,
        height = height,
        facecolor = color_body,
        edgecolor = color_edge,
        )
    rect.set_alpha(alpha)

    lines.append(vline1)
    lines.append(vline2)
    patches.append(rect)
    ax.add_line(vline1)
    ax.add_line(vline2)
    ax.add_patch(rect)
ax.autoscale_view()

return lines, patches
----------------------------------------code section end-----------------------------