Change support and resistance line color in mplfinance

1.2k Views Asked by At

Currently, I'm using this code. How can I change the hlines to red if it's a resistance and blue if it's a support?

mplfinance.plot(df,  
                type = 'candlestick', 
                style = 'binance',
                hlines=dict(hlines= support_resistance,linestyle='-', linewidths = (1,1)),
                volume = True)

I'm getting results like this:

picture here

2

There are 2 best solutions below

0
On BEST ANSWER
hlines=dict(hlines= support_resistance,linestyle='-',linewidths = (1,1),colors=('b','r')

See for example cell "In [6]" in this tutorial: https://github.com/matplotlib/mplfinance/blob/master/examples/using_lines.ipynb

You may need to include as many colors as you have support_resistance lines. So for example, maybe something like: colors=['b','r','b','r','r','r']


Regarding handling support resistance colors dynamically (per your comment) it is not appropriate for mplfinance to provide algorithms for determining support or resistance, only to provide tools to make it easier for you to visualize them. Also, each user may have their own specific way of determining support or resistance.

Presumably as you are building the support_resistance list, at the point in your code where you are adding a specific price to that list, you probably know whether that price represents support or resistance. At the same point in your code you should add a color ('b' or 'r') to the colors list. That way you dynamic build two lists: support_resistance, and colors which end up being the same length.

0
On

Based on the suggestion from Daniel, I made a list of colors as follows and it worked. support_resistance variable here consists of both support and resistance levels.

colors = []
for lvl in support_resistance:
    if lvl > df['Close'][-1]:
        colors.append('r')
    else:
        colors.append('b')