matplotlib / mplfinance

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

Feature Request: support animation/live updating of OHLCV data plots. #25

Closed andresberejnoi closed 4 years ago

andresberejnoi commented 4 years ago

Is your feature request related to a problem? Please describe. I am trying to rewrite a live chart plotting function I had with the previous mpl-finance module by using matplotlib animation.FuncAnimation method (documentation here).

The function expects to be passed a figure where to draw. For the func argument, I am passing a simple function animate_plot:

class Plotter(object):
...
    def plot_live_chart(self,ohlc_data):
        def animate_plot(df_generator, *fargs):
            _type = fargs[0]
            mplfinance.plot(df_generator,type=_type)

        fig = plt.figure()
        ax1 = fig.add_subplot(1,1,1)
        #ani = animation.FuncAnimation(fig,animate_plot,interval=self.plot_refresh_rate,blit=True) #blit=True means to only re-draw parts that have changed
        ani = animation.FuncAnimation(fig,animate_plot,frames=ohlc_data.iterrows(),fargs=(self.type,),interval=self.plot_refresh_rate)

When I run my code, I only get a blank canvas at the end. I am thinking that it could be because on each call to mplfinance.plot a new figure is created but I am not really sure. Describe the solution you'd like I would like to have another parameter option for mplfinance.plot to provide a previously created figure. Then it could be checked internally to see if the parameter was provided then that figure is used. If not, then the normal code creates one.

I think it could also be useful to have the function return the figure so it can still be used later. If these are things that are already possible then could you point me in the right direction where to find it?

Describe alternatives you've considered I think it would be great if a live plotter method were implemented with mplfinance so there is no need to build around it. It could be a method that uses the already existing .plot() similar to what I am doing above but better. This method could either animate a full dataframe like historical data and it could also animate continuously updating data.

I think this fits better for another feature request. I may open one soon once I have more time to think of how it could be implemented.

DanielGoldfarb commented 4 years ago

I am planning the ability to pass existing Axes into mpf.plot(), and hope to start work on that feature later this week. I am not very familiar with animating plots (only experimented with it once) so I'm not really sure if passing the axes will do what you want. I am also considering having mplfinance return the figure and axes that it creates.

Ideally, if you could point me to, or send to me, a working version of your existing animated plot code, along with some data to test it, that would help very much for me to better understand "live plotting," as well as your specific use-case; then perhaps we can come up with a way to integrate live plotting into mplfinance

andresberejnoi commented 4 years ago

Thank you for the reply. I will look at my code and try to extract that portion to show. Since it is part of a bigger project and class I will have to make sure nothing breaks in the example. Should I post it right here?

DanielGoldfarb commented 4 years ago

You can post it here, but perhaps it's easier to create a gist or public repo (that can be cloned) under your github account and post a link here to it. Thanks.

andresberejnoi commented 4 years ago

Here is a jupyter notebook that I made. I was trying to use the threading library to simulate a stream of market data coming but it seems it was not successful. The final graph is just one bar and the program seems to stop. https://github.com/andresberejnoi/PublicNotebooks/blob/master/public_gist_live_plotter.ipynb

The parts of interest are the Plotter class which does the live plotting with the plot() method at the end. Datastream class is a container that can be updated with new ticks and read by Plotter. The code was made to originally work with tick data streaming from a broker. It is probably very inefficient since I am turning lists into Dataframes on every refresh of the chart.

in the script I am generating fake data in the form of timestamps (datetime objects) and a list of bid and ask prices based loosely on the price of EUR/USD for forex.

When using with proper data streaming, it used to work and actually plot new candles as they were formed.

On Wed, Feb 5, 2020 at 1:46 PM Daniel Goldfarb notifications@github.com wrote:

You can post it here, but perhaps it's easier to create a gist or public repo (that can be cloned) under your github account and post a link here to it. Thanks.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/matplotlib/mplfinance/issues/25?email_source=notifications&email_token=AED77INQKNHT7DS6RFIVNBDRBL3NXA5CNFSM4KP4SER2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEK4KXTI#issuecomment-582527949, or unsubscribe https://github.com/notifications/unsubscribe-auth/AED77IKPZJJROCF7KCXIWS3RBL3NXANCNFSM4KP4SERQ .

DanielGoldfarb commented 4 years ago

@andresberejnoi -- Thanks. This is very helpful. In the middle of a big project right now. Will get back to reviewing this in a few weeks. I definitely like the idea of mplfinance supporting live candlesticks.

andresberejnoi commented 4 years ago

Thank you. It would be a very useful feature. I will also keep looking into it when I get the time.

DanielGoldfarb commented 4 years ago

I've modified the title because I think the main idea here is to support live updating of the plot, not necessarily via passing in the Figure (although that may be one possible solution). Please let me know if this is correct.

andresberejnoi commented 4 years ago

Yes. I believe it would still be useful to have the ability to pass a figure to use in other situations, but my main intent with this was to get live plotting to work.

On Wed, Feb 12, 2020, 1:47 PM Daniel Goldfarb notifications@github.com wrote:

I've modified the title because I think the main idea here is to support live updating of the plot, not necessarily via passing in the Figure (although that may be one possible solution). Please let me know if this is correct.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/matplotlib/mplfinance/issues/25?email_source=notifications&email_token=AED77IKBJZMCNVYPBOL46QTRCQYZZA5CNFSM4KP4SER2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOELRW5RQ#issuecomment-585330374, or unsubscribe https://github.com/notifications/unsubscribe-auth/AED77ILMMVDSTJ3A2NV3BDLRCQYZZANCNFSM4KP4SERQ .

DanielGoldfarb commented 4 years ago

If desired, can you please describe, in some detail, one or two use-cases for passing in the Figure. The problem is this: I am experimenting with a possible enhancement to allow you to pass in Axes (not Figure), and mplfinance will plot on those Axes, thus allowing the user to configure any number of SubPlot.Axes within a Figure, and plot on them ... however if you pass in Figure, mplfinance doesn't know what to do with it! Does it create the Axes it needs, using the Figure passed in (as it does now for it's own internal Figure)? If so, how much of the Figure should it take up with those Axes? Part of it? All of it? Or does it query the Figure passed in to find out if there are already Axes on it? If so, which of those Axes should it use?

andresberejnoi commented 4 years ago

It seems that passing axes is better. To be honest, I am very inexperienced with matplotlib so I thought figures are usually passed around.

DanielGoldfarb commented 4 years ago

Ok. Noted. Yes, the matplotlib standard is to pass axes. Thanks.

toksis commented 4 years ago

Is this live now? I also want this live plotting. thanks.

LennardGVBM commented 4 years ago

Me, too. Would be a great feature!! Thanks

DanielGoldfarb commented 4 years ago

Support for animation/live-updating will be at least another month or two from now.

There are several other more basic enhancments that I want to release first (including, but not limited to, multiple sublpots, fill_between, tight_layout, and fixing several bugs that have been reported, just to mention a few of things being worked on presently). Rest assured, support for animation/live-updating will come.

LennardGVBM commented 4 years ago

Wow, you're really quick in replying. Thanks a lot and of cause this is fine. I'm grateful for your effort!

ddm-j commented 4 years ago

Excited to have this feature - it will greatly simplify my work!

frozenranger commented 4 years ago

Yes can't wait! thanks for everything you do :)

twopirllc commented 4 years ago

Looking forward to this feature as well. I also have some experimental code somewhere, but not near as thought out as @andresberejnoi has shared.

Thank you

Blaxxun1999 commented 4 years ago

I appreciate all the hard work you've been putting into this project.
Looking forward to new animation and fill capabilities.

m-kypr commented 4 years ago

Also waiting for this feature :) Keep up the amazing work! 👍

DanielGoldfarb commented 4 years ago

Animation is now supported in mplfinance.

frozenranger commented 4 years ago

Yessss Daniel!!!!!! Ty

andresberejnoi commented 4 years ago

Thank you for your work. This feature will be very handy.

Blaxxun1999 commented 4 years ago

Thanks Daniel! Much appreciated.

DanielGoldfarb commented 4 years ago

By the way, I know a lot of people were really looking forward to this feature (along with the feature to be able to pass in external Axes). I would love to see what creative things you do with it, and perhaps include some of them in the examples folder in this repository for others to see and learn what you can do.

Please feel free to attach examples here, and/or submit a PR with one or more examples, and/or email me at dgoldfarb.github@gmail.com --- Very much appreciated! Thank you. --Daniel

manuelwithbmw commented 4 years ago

Animation is now supported in mplfinance.

Sorry, how can I clone the repository again? Never really done it, I'd like to test this feature. Thank you Daniel

DanielGoldfarb commented 4 years ago

@manuelwithbmw Depending what you want to accomplish:

HTH. All the best. --Daniel

manuelwithbmw commented 2 years ago

Great explanation, thank you (I made it after a long time)