-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Candlestick shadow is drawn after candlestick body #2546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Looks like this could be a pretty accessible change. Are you interested in giving it a go, and I'lll happily review. Cheers @c0indev3l! |
@pelson that's not as easy as expected... I tried to modify _candlestick function My first idea was to return patches, lines instead of lines, patches so I did a quite dirty fix... There is probably a better way to do this...
|
That is the hard way to do it. Just change the zorder so that the shadow lines are behind the box. |
Something like https://github.com/tacaswell/matplotlib/tree/fix_candlestick_zorder (which I have not tested, and still needs a unit test) |
should, at the same time, make the color shadows modifiable ? change
to
It could be useful to be able to set this when drawing using inverted colors (black background, white foreground) |
@c0indev3l That sounds like a good idea. Could you put together a PR for each of these changes? |
PR ? performance rating ? |
Pull request. |
ok... but I'm quite busy... I also have an other comment about this function (when using a black and white color theme with inverted colors ie black background) it's quite problematic we should also pass coloredgeup and coloredgedown parameter
|
That also looks like a good idea, but these things are much easier to review from a pull request (fork matplotlib on github, create a new branch with your changes, and you can use the website to create the PR against master from your branch) |
Ok I will send a pull request this week. There is also some other problems. |
Changing internal variables is ok, changing the names of kwargs or the default value of anything is an api change and should be done very carefully. |
@c0indev3l Are you still interested in working on this issue? |
pushing to 1.4.x as @c0indev3l has gone silent. |
I see that this issue has been solved on the master branch. The approach to fix the problem is to change the stick color to match the candle color - then it doesn't matter which one is in the front. Given that, I guess this issue can be closed. |
For reference, the color of the sticks was changed in PR #2207 . Closing. |
Sorry to be late to the party when it comes to contributing to this thread, but I've been working independently on this same problem, and I just noticed this thread, which seems to be implementing a color change that while logical, is completely unconventional when it comes to candlestick plotting. Candlestock plotting began in Japan, where it was originally utilized to track price history on the rice exchange. Because the candlestick charts were originally drawn with black ink on white rice paper, the prototype method for drawing candlestick charts has always been to draw them in black ink on white paper, or using a computer, to draw them using black "ink" on a white background. In doing this, the boxes are always drawn with a black outline, and the handles are always drawn in black. If the interval price activity were upward (closing value exceeds opening value), then the candlestick would be drawn as only an outline in black ink on a white background, which produces an empty white candle that is circumscribed by black lines. If the interval price activity were downward (closing price is less than opening price) then the box would be colored-in with black ink. In this fashion, the Japanese rice traders who created this style of plot originally drew their candlesticks either as white boxes with black handles, or as a black box with black handles. The handles were always black (they were using ink on paper, after all), and the candlestick bodies were always either black or white. This is the convention that remains standardized in all of the textbooks on technical analysis and stock charting, such as John J. Murphy's bestselling reference title: "Technical Analysis of the Financial Markets." This is also the convention that is utilized by Yahoo! Finance and many if not all of the online brokerages. In an effort to Westernize this Japanese style of charting, some people have changed the default colors for upward price candlesticks to resemble the Stop/Go signals of a traffic light -- they have changed the colors for upward price candlesticks from white to green, and the colors for downard price candlesticks from black to red, while maintaining a black outline for the boxes and a black handle on the candle. In other words, the convention has aways been to retain a black color for outlining the box and for drawing the handles whenever the candlesticks are drawn on a white background -- even when the Japanese white/black candlesticks were Westernized (perverted) to make them correspond with the idiot-light color scheme of green/red. In recent years, with the advent of the internet, charts with black backgrounds have become popular, and in those cases people will commonly outline the candlesticks, and draw the handles, in white (for the purpose of enhancing contrast against a dark background). I have been using candlestick charts in technical analysis for decades, and I am unfamiliar with candlesticks that draw the handles in the same color as the candlestick body. Doing that would definitely allow people to add "pop!" to their charts, but it would also cause the color of the handles to change colors when the chart transitions from an up day to a down day, and that is definitely NOT the accepted convention. The accepted convention is always for the handle color and for the outline color of the candlestick to remain unchanged, while only the body of the candlestick changes color. As an example of this: traditional candlesticks have always been drawn monochromatically, using black "ink" on a white "rice paper" background. Westernized versions of Candlesticks often do use Green or Red colors, as noted previously, but those have NEVER been conventional or standard. Similarly, coloring the handles to be the same color as the body would never be "conventional" unless the candle's body were black; and this would never be done with a white candle as it would make the candlestick disappear on a white background. In the event that you should decide to implement a change in which the candlestick handles and outlines are given the same color as a candlestick body, I would recommend that you not do this as a default setting, or code the routine so that the user could not change the color pattern to revert to the conventional pattern, where the handle and the candlestick outline are NOT the same color as the candle body. Traditionally, Candlesticks have always been drawn in black and white on a white background, with black body outlines and black handles. More recently people have indeed begun to use green and red, with black outlines and handles, in an effort to make their charting stand out. The idea of color coding the handles to match the color of the candlestick body is a recent idea. It is not at all traditional, and it is definitely NOT an accepted standard. That said, I can see how it would be valuable to allow the user to set each of these color options individually, to facilitate the use of non-conventional colors, especially in the case where the developer wants to implement a dark background, which seems to be all the rage in the internet era. But I think it would be a grave mistake to hard-code the color settings to force the box outlines and the handles to always be the same color as the candlestick body. Doing that would make white candlesticks disappear completely from a white background. It would force people who prefer the traditional color scheme to have to do a lot of unnecessary coding within the library to revert to normal charting behavior. That's more difficult than just coding color variables within a function call. To make color changes easier, I would recommend that the user be granted the option of using the traditional black and white candlesticks with black outlines and black handles, and if the user should decide to over-ride the default colors, to allow the user to individually set four different color parameters: A) the upward price color for the candle body, B) the downward price color for the candle body, C) the candle outline color, and D) the candle handle color. These four color settings would provide maximum flexibility and make everyone happy. Forcing the handles and the box outline to the same value as the box color, without allowing an easy method for the user to change back to a traditional color scheme would be, in my opinion, a very bad choice. Maybe the guys who are new at this sort of thing will think otherwise, but the old-coot in me thinks that black and white on a white background is what candlesticks are all about, and that independent color selection via the 4 parameters above would provide maximum flexibility for customizing the charts while making the most people happy. Thanks for your time. |
@coralnut Thank you for the interesting history lesson, I had not realized that candle stick plots had such a long history. iirc none of the active devs have a background in finance and maintenance on the finance module has faltered. One of the consequences of this gap in expertise is things like the issue you raised here (if you note, I merged the offending commit). Due to this, the plan is to deprecate the finance module in 1.4 and move it to either mpl_toolkits or it's own repository in 1.5. You seem to have a good deal of experience in this area. If you want to take charge of the finance module (either to re-visit the decision to deprecate it, which would need to be done 'soon' before we cut 1.4.0, or to take care of it in |
Yes, it's hard to maintain someone else's code when you're not familiar with what it's designed to do. I'm happy to try to help, but I need to warn you that I'm grossly unqualified to take charge of fixing this sort of thing. Although I've got plenty of experience using these types of charts, I'm a novice coder trying to learn to use python and matplotlib. Though I'd be happy to contribute whatever I can, I'm in no way qualified to spearhead such a project. I've been struggling just to get the candlestick() functions work in my application, and to be honest, I looked at the code in the library and I don't understand the data structures. :( A lesser impediment is that I'm having quite a bit of trouble figuring out how this site works. I'm familiar with buzilla, but this place is like nothing I've ever seen, and it's quite a mystery to me. Is there a tutorial somewhere? |
Not sure what has ultimately been done with this issues, but I created a modification of the candlestick function which addresses all the issues above, and allows for the use of different color themes.
|
@jdgd1 Could you please put that in a PR? |
1. Added ability for the candlestick function to use a "light" and "dark" theme 2. Fill color of candlestick bodies can be user definite and is based upon stock price movement on each given period 3. In the light theme (which uses a white background) candlestick shadows are dark; ie: Follows traditional candlestick format 4. In dark theme (which uses black background) candlestick shadows match box colors
@tacaswell PR created. Let me know what ends up happening with these functions, as I use them heavily. |
thank you very much for your function jdgd1. exactly what i was looking for. it didnt make it into the master branch? how come? |
#3016 needed some revision, and seems to have been closed inadvertently, based on the last comment in it. We still need someone to maintain the finance module. I don't think we want to lose the apparent progress in that PR if we can avoid it. |
Thank you for the link to the code discussion, I ended up altering the finance.py according to jdgd1s input; I dont use the dark/light scheme but a simple replacement of the edgecolor does work wonderfully with =None in constructor so the rest of the code can stay the same. The real issue here was the zorder and the vlines. Now the charts looks just as I wanted. |
@lifetkme42 Glad to hear the code worked for you. Let me know if any issues arise. @efiring I will take a look at the code again today and try to clean it up a little. As I mentioned in the past I'm happy to help, but not as sure I have the time to responsible for maintaining it. |
@jdgd1 If you have any time or expertise it will be an improvement. |
This attempts to give the user the option to choose a color for the edge of a candlestick body and the high-low bars (optional). If no argument is given the behavior is just like before. It also should fix the vertical line (high-low) zorder so its behind the candlestickbody now. Idea mainly by jdgd1. I tried to keep existing code working while adding the feature. matplotlib#3016 matplotlib#2546 Hopefully fixed the whitespaces.
@tacaswell ok, I will help as much as I can. Let me know where to start. |
@jdgd1 For starters, can you review #5160 ? @ssanderson is a dev at Quantopian who has expressed (very very) lose interest in also helping with the finance module, it is probably worth getting in touch with him. A survey of what is in the finance module would be good to do. Are the functions we have actually useful to a 'typical' finance type anymore? Are there obvious functions we are missing? A major struggle with that package right now is that none of the core-devs do finance so we lack the domain expertise to answer these questions. Related, I don't think we have any prose documentation for the finance module, turning some of the examples in to more proper tutorials might be worth doing. There has been some discussion of splitting the finance module out of core mpl (into a I know this is a daunting list and by no means expect you to tackle all of it, but just trying to make the point that this is a domain where there is significant opportunity to have a major impact. |
@tacaswell wow, you weren't kidding - that is quite the list. Will start with checking out #5160 and then trying to get in touch with @ssanderson. I've actually been spending a good bit of time lately working between Quantopian/zipline, so that could work out well. Post those two things will start digging into the finance module and making my own list of what I think can go vs. what's missing. |
@jdgd1, @tacaswell hello! |
(@tacaswell I just realized I've owed you an email reply for like 3 months...sorry about that) |
@ssanderson Don't worry, that still leaves me way in the red in terms of net-emails-owed. |
A couple notes on the discussion here:
|
I only picked up python recently and I just started using matplotlib, but I can tell you these charts are looking quite nice and easy to handle (I used jfreechart before) Things the finance modul could use are eg. to make @jdgd1 list even longer ;-) |
@tacaswell @ssanderson +1 for |
Pinging some people from PyData / Pandas-DataReader and others who might be interested in a Matplotlib / Numpy / Pandas / Finance plotting lib @davidastephens @bashtage @hayd @jorisvandenbossche |
The scope of such a lib could be (or not) to be able to display "realtime" plot. PyQtGraph is nice for realtime plot so pinging also @campagnola See http://www.pyqtgraph.org/ |
so far i use |
@genliu777 There is a quick resolving, use z-order as it had mentioned. It works for me without any side effect. shadows, bodies = candlestick2_ohlc(ax, .... )
shadows.set_zorder(0) # or 9
bodies.set_zorder(1) # or 10 or so |
Hello,
in this example
http://matplotlib.org/examples/pylab_examples/finance_demo.html
there is a little display bug.
Candlestick shadows (vertical segment) should be drawn before candlestick body.
see http://en.wikipedia.org/wiki/Candlestick_chart
Kind regards
The text was updated successfully, but these errors were encountered: