mplfinance moving average of specific column

2.1k Views Asked by At

I'm using mplfinance and I want the moving average in this code to represent a specific attribute which is open price.

enter image description here

AAPL1 =pd.read_csv('./HMW1/AAPL.csv',index_col=0,parse_dates=True)

AAPL1.index.name = 'Date'
   
AAPL1 = AAPL1.loc['2020-03-15' : '2020-06-14']

mpf.plot(AAPL1 ,type='candle',mav=(10,20))
1

There are 1 best solutions below

0
On

Presently mplfinance does not support mav for anything other than "Close". (However this would be a relatively easy enhancement if you are interested in contributing to the project; I would be happy to guide you).

In the meantime, if you want a moving average of something other than "Close" you will have to calculate it yourself, and use mpf.make_addplot() to plot it.

AAPL1 =pd.read_csv('./HMW1/AAPL.csv',index_col=0,parse_dates=True)

AAPL1.index.name = 'Date'
   
AAPL1 = AAPL1.loc['2020-03-15' : '2020-06-14']

open_mav10 = AAPL1["Open"].rolling(10).mean().values
open_mav20 = AAPL1["Open"].rolling(20).mean().values
mavdf = pd.DataFrame(dict(OpMav10=open_mav10,OpMav20=open_mav20),index=df.index)

ap = mpf.make_addplot(mavdf,type='line')

mpf.plot(AAPL1 ,type='candle',addplot=ap)

That should do it.

See also this link for more information about using mplfinance's make_addplot() api.
(Or if that link doesn't render, then try this one).