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.
import backtrader as bt
import backtrader.analyzers as btanalyzers
from datetime import datetime
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.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()
@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
gitinstalled 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 :