matplotlib / mplfinance

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

`vlines` (vertical lines, sharing x axis) on multiple panels #513

Open Tirbo06 opened 2 years ago

Tirbo06 commented 2 years ago

Hello Daniel, πŸ‘‹

Thank you for your dedication in improving the MPF library ! πŸ“ˆ I am looking for different options for the X_AXIS/GRID adjustments:

If i well understood what others did is, returning the axes and then calling the add_subplot method from MPL wich will force me to start again from scratch using only MPL.

I guess I missed something, here is a part of my code:


df = 'pandas dataframe with datetime index and ohlcv and RSI values'
vls = pd.date_range(df.index.min(), df.index.max(), freq='D').tolist()

ap1 = [
    mpf.make_addplot(df['RSI'], panel=1, color='royalblue')
    ] 

kwargs = dict(type='candle', vlines=dict(vlines=vls, linewidths=0.5, colors=('r')))
mpf.plot(df, addplot=ap1, **kwargs)

(If you prefer 2 different posts i can separate each request..)

DanielGoldfarb commented 2 years ago

@Tirbo06 can you provide an image (maybe draw on it showing what you want, and/or indicating what it's doing that you don't want) so that I can be more certain i understand what you're asking?

DanielGoldfarb commented 2 years ago

There is a request to make vlines available for all panels. It should be a relatively easy enhancement. Would you be willing to make the enhancement if I guide you through the process?

Tirbo06 commented 2 years ago

Okey, so I was able to plot the grid and adjust the 'minor' and 'major' by returning 'axes'. Here is the code for the GRID that helped me :

myfig, myaxs = mpf.plot(here, style=s, addplot=ap1, returnfig=True, **kwargs)

myaxs[0].minorticks_on()
myaxs[0].grid(which='both', linestyle=':', linewidth='1', color='gray')

# for the added plot (e.g indicators)
myaxs[2].minorticks_on()
myaxs[2].grid(which='both', linestyle=':', linewidth='0.5', color='gray')

How it was: image

How it is now : image

And i certainly missed a point on how to customize the frequency of the grid x axis , I saw how to format (datetime to str etc) but how if we need the intraday infos like every 15 minutes ?

Tirbo06 commented 2 years ago

And here for the VLINES (image is cropped just for ease of display), so the goal is to have those vertical lines all the way down. image

I never contributed before and i have no idea of what it implies and i have not a lot of experience with MPL. What do you think ?

Tirbo06 commented 2 years ago

https://github.com/matplotlib/mplfinance/issues/428#issuecomment-893826162

I see that you have a workaround for intraday but is there an update about this functionality?

DanielGoldfarb commented 2 years ago

Regarding the project you mentioned (https://github.com/matplotlib/mplfinance/issues/428#issuecomment-893826162) it seems that various small (and faster to implement) enhancements are repeatedly distracting from it. (Something comes up that would be a nice enhancement, and turns out easy to implement (1 to 3 days work) so I stop what I'm doing and do the nice quick enhancement. However, there is slow, gradual progress on the xticks enhancement; I just can't say at this point if it will be another 6 weeks, or another 6 months; most likely something in between.

In the meantime, as a workaround, you may try this: set returnfig=True then do the following:

fig, axlist = mpf.plot(df,...,returnfig=True)
for ax in axlist:
    for vline in my_vlines:
        ax.axvline(x=vline, linestyle='--')
mpf.show()

where my_vlines is a sequence of x values where you want the vertical lines to appear, however these x values need to be the row number in your DataFrame (corresponding to the datetime where you want the vertical line).

Please try that and let me know how it works for you.

DanielGoldfarb commented 2 years ago

Regarding

I never contributed before and i have no idea of what it implies and i have not a lot of experience with MPL. What do you think ?

If you know the basics of using git, then I can easily walk you through the process. It's a very good feeling to contribute to a project. I have honestly only been doing it a couple of years now, and was largely clueless my first time, but someone walked me through it and it's a piece of cake really.

Tirbo06 commented 2 years ago

Regarding the project you mentioned (#428 (comment)) it seems that various small (and faster to implement) enhancements are repeatedly distracting from it. (Something comes up that would be a nice enhancement, and turns out easy to implement (1 to 3 days work) so I stop what I'm doing and do the nice quick enhancement. However, there is slow, gradual progress on the xticks enhancement; I just can't say at this point if it will be another 6 weeks, or another 6 months; most likely something in between.

In the meantime, as a workaround, you may try this: set returnfig=True then do the following:

fig, axlist = mpf.plot(df,...,returnfig=True)
for ax in axlist:
    for vline in my_vlines:
        ax.axvline(x=vline, linestyle='--')
mpf.show()

where my_vlines is a sequence of x values where you want the vertical lines to appear, however these x values need to be the row number in your DataFrame (corresponding to the datetime where you want the vertical line).

Please try that and let me know how it works for you.

Hello Daniel, Thank you for your return, that worked perfectly ! 🦾

image

Tirbo06 commented 2 years ago

Regarding the contribution for MPF vlines now that I more deeply understand the logic, i am wondering if it not should stay as it is in order to let us control which panel we want the vlines to appear on.

However, if you still plan to implement the functionality I will be honored to give it a try but I will not be able to work on it until April.

Let me know if it is ok for you ?

DanielGoldfarb commented 2 years ago

@Tirbo06 April is fine. I have a few other things I am working on as well. Regarding

... let us control which panel we want the vlines to appear on.

I was thinking something like this maybe:

Tirbo06 commented 2 years ago

Hello Daniel ! Sorry for the delay, but i really can not free some time right now to dive into this. I will definitely come back to you when I have more time if it still not fix. Thanks again for your work and the good communication !

ateeq-code commented 2 years ago

I would like to work on this project

DanielGoldfarb commented 2 years ago

@ateeq-code Ateeq,
I'm glad to hear it. Do you need any information or guidance to get started? If not, then go ahead and describe your plan on how to implement this, or just write the code so we can take a look. Or if you need information or guidance, please let me know and we will guide you on how best to work on this project.

All the best. --Daniel

facealtha commented 2 years ago

Hello, we are students from CMU-Q looking to contribute to open-source projects as part of our final homework assignment for foundations of software engineering.

We would like to ask for permission to work on this as well.

DanielGoldfarb commented 2 years ago

@facealtha Permission is granted. You should read through the code and then propose a solution to an Issue that is described in this repository.

Of course you need to first have some idea how to use this package. You can learn the package by going through the tutorials listed here.

alexpvpmindustry commented 1 year ago

seems like no one is working on this at the moment? I may have a go at this.

I was thinking something like this maybe:

  • vlines as a kwarg to mpf.plot(), by default are drawn on the main panel (as it is now).
  • Since vlines can also be a dict, then the dict could also allow the keyword panels which could be a list of panel numbers through which the vlines should be drawn, or could also be the sting 'all', to indicate all panels.

having this API work as a kwarg for mpf.plot() makes the most sense (to me). users can select specific panels the vline is displayed and "all" if desired.

  • mpf.make_addplot() could also accept a vlines kwarg, but in this case the dict version would not accept a panels keyword because mpf.make_addplot() already accepts a panel kwarg. As a kwarg to mpf.make_addplot() one can create different vlines on different panels.

this could be added later, if someone else desires this api. i don't foresee a use for me.

alexpvpmindustry commented 10 months ago

xD this came back to bite me... 6 months later... and I once again bump into this issue.