 # Candlestick Issues

John,

Thank you for clarifying things for me! As far as the
replacement function goes, well -- yours is right on
the money. It's a simple moving average calculation
and it gets the job done. I just kept getting a
different result when comparing to an actual financial
chart since the calculation was based on open prices.
I just added a calculation for the Exponential Moving
Average (based on
http://www.stockcharts.com/education/IndicatorAnalysis/indic_movingAvg.html):

def ema(s, n):
"""
returns an n period exponential moving average for
the time series s

s is a list ordered from oldest (index 0) to most
recent (index -1)
n is an integer

returns a numeric array of the exponential
moving average
"""
s = array(s)
ema = []
j = 1
#get n sma first and calculate the next n period
ema
sma = sum(s[:n]) / n
multiplier = 2 / float(1 + n)
ema.append(sma)
#EMA(current) = ( (Price(current) - EMA(prev) ) x
Multiplier) + EMA(prev)
ema.append(( (s[n] - sma) * multiplier) + sma)
#now calculate the rest of the values
for i in s[n+1:]:
tmp = ( (i - ema[j]) * multiplier) + ema[j]
j = j + 1
ema.append(tmp)
return ema

I hope you can find it useful in your examples.

--- John Hunter <jdhunter@...4...> wrote:

···

OK, I see what is going on. The lines are being
plotted over the
rectangles, so even if the rectangles are
transparent, you still see
the lines. There are two candlestick functions in
matplotlib
candlestick and candlestick2. They have slightly
different call
signatures and a different implementation under the
hood. candlestick
creates a bunch of separate lines and rectangles,
candlestick2 uses
collections (see the help for the
matplotlib.collections module).

You can control the z-ordering on the plot by
setting the zorder
property (see examples/zorder_demo.py). For
candlestick (see
examples/candlestick_demo.py) you would do

lines, patches = candlestick(ax, quotes,
width=0.6)
set(lines, zorder=0.9*patches.zorder)

for candlestick2 you would do (untested)

linecol, rectcol = candlestick2(blah)
z = rectcol.get_zorder()
linecol.set_zorder(0.9*z)

Argg, that's embarrassing. Good thing mpl is
distributed with no
warranties.... No telling how many billions this
bug has cost the

In matplotlib/finance.py in the candlestick2
function, find this code

colord = { True : colorup,
False : colordown,
}
colors = [colord[open>=close] for open, close in
zip(opens, closes) if open!=-1 and close !=-1]

colors = [colord[close>=open] for open, close in
zip(opens, closes) if open!=-1 and close !=-1]

right? I believe this is already correct in
candlestick, so this is a
candlestick2 specific bug.

OK, if you submit a replacement function that better
matches actual
plots, I will be happy to include it.

Thanks for the report!
JDH

-------------------------------------------------------

SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT
Products from real users.
Discover which products truly live up to the hype.