Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    ImportError: cannot import name 'warnings' from 'matplotlib.dates' when trying to plot using cerebro.plot

    General Discussion
    3
    5
    677
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B
      BMccoy last edited by

      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 you

      import backtrader as bt
      import backtrader.analyzers as btanalyzers
      import matplotlib
      from datetime import datetime

      class 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()

      D 1 Reply Last reply Reply Quote 0
      • D
        davidavr @BMccoy last edited by

        @bmccoy I believe there is a pull request for Backtrader to fix this, discussed here. Try:

        pip install git+https://github.com/mementum/backtrader.git@0fa63ef4a35dc53cc7320813f8b15480c8f85517#egg=backtrader
        
        B 1 Reply Last reply Reply Quote 0
        • B
          BMccoy @davidavr last edited by

          @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?

          D 1 Reply Last reply Reply Quote 0
          • D
            davidavr @BMccoy last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • E
              EMR last edited by

              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

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors