2

I wanted to draw the candle above lines. But in my plot, all lines are over the candle. So I'm wondering how I draw the candle "on" the line. Any opinions would be helpful.

     apds = [ mpf.make_addplot(a.shift(-26)[howmove:],color='000000',alpha=0.5),
             mpf.make_addplot(b.shift(-26)[howmove:],color='000000',alpha=0.3),
             mpf.make_addplot(c.shift(-52)[howmove:],color='000000',alpha=0.4)]    
     mpf.plot(dff[howmove:], axisoff=True, addplot=apds, figratio=[3,1], figscale=0.5,
             type='candle',update_width_config=dict(candle_linewidth=2), 
             fill_between=dict(y1=hp1.values,y2=hp2.values, alpha=0.15,color='grey'), tight_layout=True,
             style=mpf.make_mpf_style(base_mpf_style='default', facecolor='FFFFFF', gridstyle=''),ylim=[ymin,ymax])

!result(https://i.stack.imgur.com/DlMox.jpg)

kjssag
  • 21
  • 2

2 Answers2

2

There is an enhancement request for this here. Hopefully will get to it sometime in the next 4 to 8 weeks.

Daniel Goldfarb
  • 6,937
  • 5
  • 29
  • 61
1

Here is a little exemple to obtain what you want. There is a lot more you can do. Hope it helps.

import pandas as pd
import mplfinance as mpf
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override()

# Download data from Yahoo Finance
symbol = "AAPL"  # Stock symbol (Apple in this example)
start_date = "2021-01-01"
end_date = "2021-03-31"
df = pdr.get_data_yahoo(symbol, start=start_date, end=end_date)

# Calculate moving averages
df['SMA_10'] = df['Close'].rolling(window=10).mean()
df['SMA_20'] = df['Close'].rolling(window=20).mean()

# Add moving averages as background
ap = mpf.make_addplot(df[['SMA_10', 'SMA_20']], alpha=0.5, width=0.8)

# Plot the chart with candles in the foreground and moving averages in the background
fig, ax = mpf.plot(df, type='candle', volume=False, show_nontrading=True, style='yahoo', returnfig=True, addplot=ap, update_width_config=dict(candle_linewidth=0.4, candle_width=0.5))

# Modify the order of display for collections
ax[0].collections[0].set_zorder(1)  # Candles in foreground
ax[0].collections[1].set_zorder(10)  # Moving averages in background

#mpf.plot(df, type='line', addplot=ap, ax=ax[0])

# Show the chart
mpf.show()
Lumber Jack
  • 602
  • 3
  • 9