matplotlib / mplfinance

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

alines on addplots #446

Open waemm opened 3 years ago

waemm commented 3 years ago

Firstly, thank you so much for this tool! It has made my foray into financial algorithms a real joy! I am curious if there is a way to add the alines to other panels or addplots? I have RSI for instance that I would like to plot and mark up with some lines. The only way I can think of doing this at the moment is to create a second plot using the RSI value instead of the close ?

Thanks again! Warren

DanielGoldfarb commented 3 years ago

@waemm Warren, Thanks for expressing your appreciation, and thank you also for contributing your question.

I looked through the code and it appears that the alines kwarg (and other ..lines kwargs) are limited to the main panel.

As a work-around, I tried experimenting with returnfig=True and then calling _construct_aline_collections() directly, and passing the output into Axes.add_collection() for one of the Axes returned from mpf.plot(). In theory that should work; not sure why it did not initially work for me. When I have more time I will try debugging.

It is a reasonable enhancement request to allow alines to work for any panel. If you are interested in making the change I will be happy to guide you through it.

All the best. --Daniel

xt99 commented 2 years ago

Hello everyone!

I experience the same need to have alines on addplots. Is there any news on this as for now?

xt99 commented 2 years ago

Thanks to an idea at linked workaround, I've came to working code:

import mplfinance as mpf
...

aplots = [
  # create additional plot with `extra_value` column values and type `bar`
  mpf.make_addplot(data['extra_value'], panel=1, type='bar'),
]

# generate axes for main and additional plots
figure, axlist = mpf.plot(data, type='candle', addplot=aplots, returnfig=True)

# construct a line with the same values as data's index 
line_points = [('2022-06-11', 20), ('2022-08-12', -20)]
# generate aline collection
alines = mpf._utils._construct_aline_collections(dict(alines=line_points), data.index)
# finally add aline collection to additional plot's primary axe (index `2` is for primary axe of panel 1)
axlist[2].add_collection(alines)

mpf.show()

Still it will be nice feature to have to support lines natively for addplots.

@waemm Warren, Thanks for expressing your appreciation, and thank you also for contributing your question.

I looked through the code and it appears that the alines kwarg (and other ..lines kwargs) are limited to the main panel.

As a work-around, I tried experimenting with returnfig=True and then calling _construct_aline_collections() directly, and passing the output into Axes.add_collection() for one of the Axes returned from mpf.plot(). In theory that should work; not sure why it did not initially work for me. When I have more time I will try debugging.

It is a reasonable enhancement request to allow alines to work for any panel. If you are interested in making the change I will be happy to guide you through it.

All the best. --Daniel

andreasklippinge commented 1 year ago

Hey @waemm @DanielGoldfarb @xt99 ,

I came up with a work-around solution for my charts - you can use the fill_between function for drawing the lines in whatever panel you want. So for RSI I want to (among other things) mark the 30 and 70 level with lines. I wrote these lines to make it happen:

mpf.make_addplot(df["RSI"], type="line",panel=2,ylabel="RSI",color="orange",fill_between=dict(y1=df["RSI"].min(),y2=df["RSI"].values,color="grey",alpha=0.5)),
mpf.make_addplot(df["RSI"], type="line",panel=2, ylabel="RSI",color="orange",fill_between=dict(y1=30,y2=30,color="red")),
mpf.make_addplot(df["RSI"], type="line",panel=2, ylabel="RSI",color="orange",fill_between=dict(y1=70,y2=70,color="red"))

So basically I'm creating a fill_between line with the same values which creates a simple line. This should work for whatever panel you'd like. My chart looks like this: image

alexpvpmindustry commented 1 year ago

i think this issue can be closed due the hlines,vlines,lines,tlines kwargs added. see this notebook at master/examples/using_lines.ipynb

MushiTheMoshi commented 6 months ago

how can lines have different color when having many of them? I have like 200 lines and would like to group them by color?

any help is much appreciated...