mplfinance icon indicating copy to clipboard operation
mplfinance copied to clipboard

Bar colors based on indicator value

Open hrpomrx opened this issue 5 years ago • 6 comments

Is there a way to color bars based on TA indicator values (obtained using TA-lib or similar) or based on aggregate values of multiple indicators, e.g. 3 different ohlc bar colors representing “long”, “short” and “no trade”? Thank you for any insight.

hrpomrx avatar Jul 03 '20 02:07 hrpomrx

When you say "ohlc bar" colors, I assume you mean ohlc bars like this: https://raw.githubusercontent.com/matplotlib/mplfinance/master/readme_files/readme_4_1.png

Presently there is no way to color them other than upcolor/downcolor, however possibly, as an relatively easy enhancement, we could allow the caller to pass in a list of colors, where the list is the same length as the data, and each ohlc bar would be colored according the color at that corresponding point in the list. It would then be up to the caller to pass in the appropriate color for each ohlc data point (whether the total number of different colors is two, or three, or more).

DanielGoldfarb avatar Jul 05 '20 05:07 DanielGoldfarb

Yes those bars, although it would be nice for candlesticks also. That sounds like a workable solution. Thanks, Daniel.

hrpomrx avatar Jul 05 '20 15:07 hrpomrx

as an relatively easy enhancement, we could allow the caller to pass in a list of colors, where the list is the same length as the data, and each ohlc bar would be colored according the color at that corresponding point in the list.

Hi Daniel thank you for the awesome functionality and obviously the amount of work you are putting into this.

I have a use case which would be solved by your proposed enhancement as well.

Best regards

geirak avatar Feb 23 '21 16:02 geirak

@geirak thanks for chiming in: it helps prioritize enhancements.

DanielGoldfarb avatar Feb 24 '21 00:02 DanielGoldfarb

Here's what I am thinking:

  • a kwarg, usercolors that applies only to type='ohlc' or type='candle'
  • usercolors is an array or list of the same length as the ohlc data
  • every entry in the list must be either a valid color, or None
    • if a valid color, then the normal candle/ohlc-bar color will be replaced with that user color.
    • if None, then the candle/ohlc-bar will maintain its normal up/down coloring according to the style chosen.

I am also considering the following, if there is need for it:

  • for type='candle' only, usercolors may optionally be a dict with one or more of the following keys:
    • 'candle', 'edge', 'wick'
  • the value for each key is an array or list, with the same specifications as described above (same length as data, and each entry is eitther a valid color or None).
  • This will allow you, for example, to use a signal color for the candle body, while keeping the wicks and/or edges of the candle the normal up/down colors, or while making the wicks and edges all one color.
    • As an example, it may be useful to keep all the wicks and edges black, and coloring only the candle body, effectively outlining the candle.
    • Some styles do this normally, outlining a candle color that otherwise would blend in with the background of the plot (See mplfinance style reference).
  • Please let me know if there is a desire to color wicks and edges separately from candle bodies.

DanielGoldfarb avatar Feb 24 '21 01:02 DanielGoldfarb

Your first proposal would solve my requirement to color the candles according to custom logic (showing momentum/lack of momentum according to a given rule set).

Optionally using a dict instead of an array/list for type='candle' would open up a range of new possibilities, which I see myself utilizing. It is an elegant solution, which requires additional user code only if necessary, but gives flexibility when needed.

If the dict implementation involves a lot more work for you and would significantly impact prioritization, then your first idea sounds more tempting :-)

geirak avatar Feb 24 '21 07:02 geirak

Should this issue be marked as closed since marketcolor_overrides kwargs seem to deliver this feature?

siddiqaa avatar Sep 10 '22 06:09 siddiqaa

@siddiqaa Yes. Thanks.

DanielGoldfarb avatar Sep 11 '22 00:09 DanielGoldfarb