mplfinance Change Color Depending on Value Is Positive or Negative

November 2, 2020
import yfinance as yf
import mplfinance as mpf
import talib as ta

ticker_name = 'MSFT'
yticker = yf.Ticker("MSFT")
data = yticker.history(period="1y") # max, 1y, 3mo

# trim volume to avoid exponential form
data['Volume'] = data['Volume'] / 1000

# macd
data["macd"], data["macd_signal"], data["macd_hist"] = ta.MACD(data['Close'])

# macd panel
colors = ['g' if v >= 0 else 'r' for v in data["macd_hist"]]
macd_hist_plot = mpf.make_addplot(data["macd_hist"], type='bar', panel=1, color=colors) # color='dimgray'

# plot
mpf.plot(data, type='candle', style='yahoo', addplot=macd_hist_plot, title=f"\n{ticker_name}", ylabel='')

mplfinance change color

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.