enzoampil / fastquant

fastquant — Backtest and optimize your ML trading strategies with only 3 lines of code!
MIT License
1.54k stars 238 forks source link

Matplotlib 'warnings' issue while plotting #369

Closed kdg862 closed 3 years ago

kdg862 commented 3 years ago

I was just trying to follow the tutorial, but cannot get plotting to work. I was trying to run the following:

from fastquant import backtest from fastquant import get_crypto_data crypto = get_crypto_data("BCH/USDT", "2021-01-01", "2021-04-24") backtest("smac", crypto, fast_period=15, slow_period=40)

And received this error: ImportError: cannot import name 'warnings' from 'matplotlib.dates'

Similar issues on stack overflow suggested downgrading Matplotlib to 3.2.2. I tried this and it unfortunately did not help.

kdg862 commented 3 years ago

I was doing this in a Jupyter notebook running Python 3.8.5. Here is the full error message:


ImportError Traceback (most recent call last)

in 1 from fastquant import backtest ----> 2 backtest("smac", crypto, fast_period=15, slow_period=40) 3 # Starting Portfolio Value: 100000.00 4 # Final Portfolio Value: 68742.36 ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/fastquant/backtest/backtest.py in backtest(strategy, data, commission, init_cash, plot, verbose, sort_by, sentiments, strats, return_history, return_plot, channel, symbol, allow_short, short_max, figsize, data_class, data_kwargs, plot_kwargs, fig, **kwargs) 240 ) 241 else: --> 242 fig = plot_results(cerebro, data_format_dict, figsize, **plot_kwargs) 243 244 if return_history and return_plot: ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/fastquant/backtest/post_backtest.py in plot_results(cerebro, data_format_dict, figsize, **plot_kwargs) 226 plt.rcParams["figure.figsize"] = figsize 227 --> 228 fig = cerebro.plot(volume=has_volume, iplot=iplot, **plot_kwargs) 229 230 return fig[0][0] ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/backtrader/cerebro.py in plot(self, plotter, numfigs, iplot, start, end, width, height, dpi, tight, use, **kwargs) 972 973 if not plotter: --> 974 from . import plot 975 if self.p.oldsync: 976 plotter = plot.Plot_OldSync(**kwargs) ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/backtrader/plot/__init__.py in 40 41 ---> 42 from .plot import Plot, Plot_OldSync 43 from .scheme import PlotScheme ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/backtrader/plot/plot.py in 42 from .finance import plot_candlestick, plot_ohlc, plot_volume, plot_lineonclose 43 from .formatters import (MyVolFormatter, MyDateFormatter, getlocator) ---> 44 from . import locator as loc 45 from .multicursor import MultiCursor 46 from .scheme import PlotScheme ~/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/backtrader/plot/locator.py in 33 from matplotlib.dates import AutoDateFormatter as ADFormatter 34 ---> 35 from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN, 36 MONTHS_PER_YEAR, DAYS_PER_WEEK, 37 SEC_PER_HOUR, SEC_PER_DAY, ImportError: cannot import name 'warnings' from 'matplotlib.dates' (/Users/kevin/opt/anaconda3/envs/algoTrade/lib/python3.8/site-packages/matplotlib/dates.py)
CAB0409 commented 3 years ago

You have to change matplotlib backend

if you are using a mac you have to change ~/.matplotlib/matplotlibrc to MacOSX. If you are using linux I believe you change it to another option that I forget but try these:

akhildevelops commented 3 years ago

I've also faced the same issue when calling the backtest method. 💔

If we dig deeper the issue is due to the backtrader library that fastquant depends on. 🤔

There's already a fix for this issue https://github.com/mementum/backtrader/commit/0fa63ef4a35dc53cc7320813f8b15480c8f85517 😃

But it is yet to be released in a new version and I sincerely doubt there will be a new release in short time. The latest release for backtrader was way back in the month of May-2020. 😓

I have a hack around it. Just follow the 2 steps below: 🥳

This installs backtrader with fix and enjoy the plots.

mikeejazmines commented 3 years ago

If this still persists, one can also change the matplotlib version to 3.2.2 as seen here: https://stackoverflow.com/questions/63471764/importerror-cannot-import-name-warnings-from-matplotlib-dates

jdeukel commented 2 years ago

@mikeejazmines THANK YOU! Was going mad. Everything is working perfectly for me now. Cheers!