ImportError: cannot import name 'warnings' from 'matplotlib.dates' when trying to plot using cerebro.plot
-
Hello, I am trying to run the code below and keep getting the
ImportError: cannot import name 'warnings' from 'matplotlib.dates'
error message. I have read around multiple guthub responses and they all say uninstall matplotlib and perform the following
pip uninstall matplotlib
pip install matplotlib==3.2.2
I do this from the cmd and nothing gets resolved.
Just note I am trying to run this from Jupyter notebook, so not sure id the dates.py in there is getting updated properly.
Thank youimport backtrader as bt
import backtrader.analyzers as btanalyzers
import matplotlib
from datetime import datetimeclass MaCrossStrategy(bt.Strategy):
def __init__(self): ma_fast = bt.ind.SMA(period = 10) ma_slow = bt.ind.SMA(period = 50) self.crossover = bt.ind.CrossOver(ma_fast, ma_slow) def next(self): if not self.position: if self.crossover > 0: self.buy() elif self.crossover < 0: self.close()
cerebro = bt.Cerebro()
data = bt.feeds.YahooFinanceData(dataname = 'AAPL', fromdate = datetime(2010, 1, 1), todate = datetime(2020, 1, 1))
cerebro.adddata(data)cerebro.addstrategy(MaCrossStrategy)
cerebro.broker.setcash(1000000.0)
cerebro.addsizer(bt.sizers.PercentSizer, percents = 10)
cerebro.addanalyzer(btanalyzers.SharpeRatio, _name = "sharpe")
cerebro.addanalyzer(btanalyzers.Transactions, _name = "trans")
cerebro.addanalyzer(btanalyzers.TradeAnalyzer, _name = "trades")back = cerebro.run()
cerebro.broker.getvalue()
back[0].analyzers.sharpe.get_analysis()
back[0].analyzers.trans.get_analysis()
back[0].analyzers.trades.get_analysis()
cerebro.plot()
-
-
@davidavr Thanks but unfortunately I get a different error when putting that into cmd...
ERROR: Error [WinError 2] The system cannot find the file specified while executing command git clone -q https://github.com/mementum/backtrader.git 'C:\Users\brand\AppData\Local\Temp\pip-install-g_gi_wmv\backtrader_61ddfe02b5664377a282d1e46e60fea0'
ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH? -
@bmccoy Sorry, you need
git
installed to do a"pip install"
from a git repository. There might be a way to download that distribution manually and then do a local pip install, but I'm not that familiar with git pull requests, to be honest. -
I believe this error have been fixed in last version of backtrader, and that with this last version you do not need to downgrade matplotlib. How did you installed backtrader, and what version do you have ?
The cause of this error was in locator.py. If you want to see what was changed in the code to fix this issue, you can find it here :
https://github.com/mementum/backtrader/commit/61f0878a4e3616b62d3ad035acafd0ff25da1dec#diff-3fa930b595624ea86609bfa8fc4ab8a78bb62ef2bf029513ce38da8e39fe9b99