matplotlib / mplfinance

Financial Markets Data Visualization using Matplotlib
https://pypi.org/project/mplfinance/
Other
3.58k stars 620 forks source link

Feature Request: Moving Average on Volume panel #216

Open strikar21 opened 4 years ago

strikar21 commented 4 years ago

How to add moving average on Volume Panel (panel = 1) if volume is set to True. I have plotted mav(9,26) in Panel 0 using 'mav' function If I edit and enter Volume = False and add Volume in Panel 1 using addplot funtion. Color of the volume candles becomes solid blue irrespective of the color of the candle in Panel '0'

EDIT: here is a part of the code

#chart style parameters and plotting function
mc = mpf.make_marketcolors(up='g',down='r',
                           edge='inherit',
                           wick='black',
                           volume='in',
                           ohlc='i')
s    = mpf.make_mpf_style(marketcolors=mc)
apc = [mpf.make_addplot(stck['rsi'].iloc[170:],panel=2,color='g',type = 'line',ylabel='RSI'),
       mpf.make_addplot(stck['rsi_overbought'].iloc[170:], panel=2,color='blue',type = 'line',linestyle='-.',secondary_y=False),
       mpf.make_addplot(stck['rsi_oversold'].iloc[170:], panel=2,color='red',type = 'line',linestyle='-.', secondary_y=False),
       mpf.make_addplot(stck['Volume'].iloc[170:],panel =1,type = 'bar', mav = 10,color = 'in')]

mpf.plot(stck.iloc[170:], type='candlestick', volume=False,title = ticker, tight_layout=True, hlines=ml_results,
 mav=(9,26), style = s, returnfig=True,addplot = apc)
strikar21 commented 4 years ago

EDIT: mpf.make_addplot(stck['Volume'].iloc[170:],type = 'line',linestyle=' ',panel =1, mav = 10)

mpf.plot(stck.iloc[170:], type='candlestick', volume=True,title = ticker, tight_layout=True, hlines=ml_results, mav=(9,26), style = s, returnfig=True,addplot = apc)

Volume Set to True, and volume as line with line type: ' ' space works for now.

image

DanielGoldfarb commented 4 years ago

What you have done here is the simplest way to do this with the present version of mplfinance.

Would you be interested in contributing an enhancement for mpf.plot() to accept a volmav= kwarg?
I will be happy to guide you though the process of contributing code if you are interested. Let me know.

Thanks. --Daniel

strikar21 commented 4 years ago

What you have done here is the simplest way to do this with the present version of mplfinance.

Would you be interested in contributing an enhancement for mpf.plot() to accept a volmav= kwarg? I will be happy to guide you though the process of contributing code if you are interested. Let me know.

Thanks. --Daniel

If you can guide me through the steps. Happy to help you :) i'm not a coder but will try to help you out :)

DanielGoldfarb commented 4 years ago

In this comment, I will guide you though the code, to make the volmav enhancment. Not sure what you mean by "i'm not a coder", since you presumably wrote the code above to do the rsi addplots and you came up with a creative way to do the volume moving average; if you can do that you can certainly make this enhancment.

So I will focus here on the relevant parts of the code, and suggest an approach to make the changes needed to support volmav. I will assume that you know how to

If you need help with any of the above procedures, no worries, just let me know and I will walk you through it.


All of the changes you will make are in file plotting.py. The function that calculates and plots the moving average lines is here: https://github.com/matplotlib/mplfinance/blob/master/src/mplfinance/plotting.py#L732-L758

The function _plot_mav() gets called in a several places in that file, for things such as (a) the main candlestick/ohlc plot, (b) addplots, and (c) renko plots. Each of these places passes in its version of "prices" from that place.

The mav settings (number of points to use in mav calculations) also get passed in. Read through that function. You will see that there are two sources for the mav settings: (1) config['mav'] for the main plot area, and (2) apmav for addplot moving averages.

This made sense when there were only two sources for "mav" settings, but now we want to add a third (volmav) and maybe someday there will be more. Therefore, I suggest that the parameters of _plot_mav() be changed such that all of the data that is now being taken from config will instead get passed in explicitly:

def _plot_mav(ax,config,xdates,prices,apmav=None,apwidth=None):

becomes

def _plot_mav(ax,mav,xdates,prices,mavwidths=None,mavcolors=None):

This way, there will be no need for the "if apmav ..." inside _plot_mav() to decide which mav to use. Instead, the function always uses whatever is passed in as mav, and each place that calls _plot_mav() will have to decide the correct thing to pass in there.

Now might be a good time to read through _plot_mav() and read each of the four places (in plotting.py) where it gets called, and think about how it will be called differently in each place based on the above suggested parameter change. Note that of the four places where _plot_mav() is called: two are for the main plot, and two are for addplot.

Once the above change is done, you have changed code but effectively you have not changed any functionality, so everything should continue to run and work as it did previously with no errors (you may want to test it).

Next you will want to call _plot_mav() for the volume, somewhere in this section here: https://github.com/matplotlib/mplfinance/blob/master/src/mplfinance/plotting.py#L423-L437 and you will want to pass the volume values in as the "prices" argument to _plot_mav() so that it calculates the moving average of the volume. And of course you will pass in the volmav settings for the mav argument to _plot_mav().

Before doing that you will have to add the volmav kwarg.
Valid kwargs are all defined here: https://github.com/matplotlib/mplfinance/blob/master/src/mplfinance/plotting.py#L81-L248

The mav kwarg itself is defined here: https://github.com/matplotlib/mplfinance/blob/master/src/mplfinance/plotting.py#L108-L110

You will want to pretty much copy/paste the couple of lines for the mav kwarg to created a similar volmav kwarg. Then, when calling _plot_mav() where volume is plotted you can access the value of the volmav kwarg as config['volmav']

That's it in a nutshell. Then test and make sure its working. And of course, feel free to post any questions here, or email me at dgoldfarb.github@gmail.com

All the best. --Daniel