## Re: [Matplotlib-users] Candlestick Issues

 Re: [Matplotlib-users] Candlestick Issues From: Daisy Fuentes - 2005-04-08 03:22:54 ```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 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[0].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 > wall street barons already! > > 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] > > > That should read > > 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. > Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo ```

 Re: [Matplotlib-users] Candlestick Issues From: Daisy Fuentes - 2005-04-04 22:13:03 ```John, Thank you for your reply. A couple of issues though. I looked at the documentation and saw the alpha transperancy parameter, but for some reason I was still getting the same results. Maybe I am missing something very fundamental. If alpha is not provided then the default is for the candlestick box not to be transperant, correct? Even changing the alpha value wouldn't produced desired results, I can increase transperancy, but not eliminate it. I wasn't seeing that since I can still see the lines (wicks) go through the boxes that's why I asked here. Also, this may be another foolish oversight on my part, but how come when I have candlestick2(axMiddle, opens, closes, highs, lows, width=4, colorup='k', colordown='r') instead of getting a bar colored black when it closes above the open price I get the opposite --- red? It seems colorup should be black, colordown -- red as in colorup : the color of the lines where close >= open colordown : the color of the lines where close < open As far as the moving average calculation, I did see the disclaimer and didn't mean to be a stickler, it just seem a bit misleading to me as I was trying to do the Exponential Moving Average calculation and compare the results to the actual financial charts. They didn't match and I thought I would point that out. --- John Hunter wrote: > >>>>> "Daisy" == Daisy Fuentes > writes: > > Daisy> I was looking at the example located @ > Daisy> > http://matplotlib.sourceforge.net/screenshots/finance_work2.py > Daisy> and was wondering if it's possible to > color the candlestick > Daisy> boxes so that the candlestick wicks are > not visible through > Daisy> them. > > Yes, the alpha argument to the candlestick plot > controls the > transparency. alpha is the traditional name for the > transparency > level, so look for it in documentation strings, eg > in > > http://matplotlib.sf.net/matplotlib.finance.html#-candlestick > > Daisy> Additionally, is it possible to have a > candlestick > Daisy> that's not just a plain line when the > open and the close > Daisy> prices are the same. Would it be possible > to add a > Daisy> horizontal tick mark? > Daisy> | > Daisy> | > Daisy> - > Daisy> | > Daisy> | > > You can add these marks fairly easily yourself by > adding an additional > plot command, and using the marker style TICKLEFT or > TICKRIGHT. In > other words, you can plot any kind of line you want > more or less. The > candlestick method may not support it directly, but > it is easy to > overlay. We should probably add a marker style > TICKCENTERX for this > kind of marker, and better add TICKCENTERY for > symmetry. It's only a > few lines of code that need to be added to the > lines.py module. > > Daisy> Also, I noticed that the simple moving > average is > Daisy> calculated based on the price open > instead of price close. > > I'm not attempting to make a financially meaningful > plot, but to > illustrate plotting techniques. You are free to do > moving averages > over open, close, average or random data. As I note > in the > finance_work screenshot text > > Some of the data in the plot, are real financial > data, some are > random traces that I used since the goal was to > illustrate plotting > techniques, not market analysis! > > Hope this helps, > JDH > __________________________________ Do you Yahoo!? Yahoo! Personals - Better first dates. More second dates. http://personals.yahoo.com ```
 Re: [Matplotlib-users] Candlestick Issues From: Daisy Fuentes - 2005-04-04 22:14:28 ```John, Thank you for your reply. A couple of issues though. I looked at the documentation and saw the alpha transperancy parameter, but for some reason I was still getting the same results. Maybe I am missing something very fundamental. If alpha is not provided then the default is for the candlestick box not to be transperant, correct? Even changing the alpha value wouldn't produced desired results, I can increase transperancy, but not eliminate it. I wasn't seeing that since I can still see the lines (wicks) go through the boxes that's why I asked here. Also, this may be another foolish oversight on my part, but how come when I have candlestick2(axMiddle, opens, closes, highs, lows, width=4, colorup='k', colordown='r') instead of getting a bar colored black when it closes above the open price I get the opposite --- red? It seems colorup should be black, colordown -- red as in colorup : the color of the lines where close >= open colordown : the color of the lines where close < open As far as the moving average calculation, I did see the disclaimer and didn't mean to be a stickler, it just seem a bit misleading to me as I was trying to do the Exponential Moving Average calculation and compare the results to the actual financial charts. They didn't match and I thought I would point that out. --- John Hunter wrote: > >>>>> "Daisy" == Daisy Fuentes > writes: > > Daisy> I was looking at the example located @ > Daisy> > http://matplotlib.sourceforge.net/screenshots/finance_work2.py > Daisy> and was wondering if it's possible to > color the candlestick > Daisy> boxes so that the candlestick wicks are > not visible through > Daisy> them. > > Yes, the alpha argument to the candlestick plot > controls the > transparency. alpha is the traditional name for the > transparency > level, so look for it in documentation strings, eg > in > > http://matplotlib.sf.net/matplotlib.finance.html#-candlestick > > Daisy> Additionally, is it possible to have a > candlestick > Daisy> that's not just a plain line when the > open and the close > Daisy> prices are the same. Would it be possible > to add a > Daisy> horizontal tick mark? > Daisy> | > Daisy> | > Daisy> - > Daisy> | > Daisy> | > > You can add these marks fairly easily yourself by > adding an additional > plot command, and using the marker style TICKLEFT or > TICKRIGHT. In > other words, you can plot any kind of line you want > more or less. The > candlestick method may not support it directly, but > it is easy to > overlay. We should probably add a marker style > TICKCENTERX for this > kind of marker, and better add TICKCENTERY for > symmetry. It's only a > few lines of code that need to be added to the > lines.py module. > > Daisy> Also, I noticed that the simple moving > average is > Daisy> calculated based on the price open > instead of price close. > > I'm not attempting to make a financially meaningful > plot, but to > illustrate plotting techniques. You are free to do > moving averages > over open, close, average or random data. As I note > in the > finance_work screenshot text > > Some of the data in the plot, are real financial > data, some are > random traces that I used since the goal was to > illustrate plotting > techniques, not market analysis! > > Hope this helps, > 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. > Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > __________________________________ Do you Yahoo!? Yahoo! Personals - Better first dates. More second dates. http://personals.yahoo.com ```
 Re: [Matplotlib-users] Candlestick Issues From: Daisy Fuentes - 2005-04-08 03:22:54 ```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 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[0].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 > wall street barons already! > > 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] > > > That should read > > 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. > Start reading now. > http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click > _______________________________________________ > Matplotlib-users mailing list > Matplotlib-users@... > https://lists.sourceforge.net/lists/listinfo/matplotlib-users > __________________________________ Do you Yahoo!? Take Yahoo! Mail with you! Get it on your mobile phone. http://mobile.yahoo.com/maildemo ```
 Re: [Matplotlib-users] Candlestick Issues From: John Hunter - 2005-04-06 14:23:38 ```>>>>> "Daisy" == Daisy Fuentes writes: Daisy> John, Thank you for your reply. A couple of issues though. Daisy> I looked at the documentation and saw the alpha Daisy> transperancy parameter, but for some reason I was still Daisy> getting the same results. Maybe I am missing something Daisy> very fundamental. If alpha is not provided then the Daisy> default is for the candlestick box not to be transperant, Daisy> correct? Even changing the alpha value wouldn't produced Daisy> desired results, I can increase transperancy, but not Daisy> eliminate it. I wasn't seeing that since I can still see Daisy> the lines (wicks) go through the boxes that's why I asked Daisy> here. Also, this may be another foolish oversight on my 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[0].zorder) for candlestick2 you would do (untested) linecol, rectcol = candlestick2(blah) z = rectcol.get_zorder() linecol.set_zorder(0.9*z) Daisy> part, but how come when I have candlestick2(axMiddle, Daisy> opens, closes, highs, lows, width=4, colorup='k', Daisy> colordown='r') instead of getting a bar colored black when Daisy> it closes above the open price I get the opposite --- red? Daisy> It seems colorup should be black, colordown -- red as in Daisy> colorup : the color of the lines where close >= open Daisy> colordown : the color of the lines where close < open Argg, that's embarrassing. Good thing mpl is distributed with no warranties.... No telling how many billions this bug has cost the wall street barons already! 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] That should read 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. Daisy> As far as the moving average calculation, I did see the Daisy> disclaimer and didn't mean to be a stickler, it just seem a Daisy> bit misleading to me as I was trying to do the Exponential Daisy> Moving Average calculation and compare the results to the Daisy> actual financial charts. They didn't match and I thought I Daisy> would point that out. OK, if you submit a replacement function that better matches actual plots, I will be happy to include it. Thanks for the report! JDH ```