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/

    cannot import name 'warnings' from 'matplotlib.dates'

    General Code/Help
    alpaca matplotl plot
    3
    3
    2146
    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.
    • G
      gregOh last edited by

      I ran into this import issue and found article below, so I apply the code, but same issue not resolved. any one can help please?

      https://community.backtrader.com/topic/981/importerror-cannot-import-name-min_per_hour-when-trying-to-plot/8

      2020-08-17_18-48-13.png

      The entire source code is posted below(you need to plug in Alpaca keys in order to run)

      from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
      MONTHS_PER_YEAR, DAYS_PER_WEEK,
      SEC_PER_HOUR, SEC_PER_DAY,
      num2date, rrulewrapper, YearLocator,
      MicrosecondLocator)
      import alpaca_backtrader_api
      import backtrader as bt
      from datetime import datetime
      #import matplotlib

      ALPACA_API_KEY = "XXXXX"
      ALPACA_SECRET_KEY = "XXXX"
      ALPACA_PAPER = True

      class SmaCross(bt.SignalStrategy):
      def init(self):
      sma1, sma2 = bt.ind.SMA(period=10), bt.ind.SMA(period=30)
      crossover = bt.ind.CrossOver(sma1, sma2)
      self.signal_add(bt.SIGNAL_LONG, crossover)

      cerebro = bt.Cerebro()
      cerebro.addstrategy(SmaCross)

      store = alpaca_backtrader_api.AlpacaStore(
      key_id=ALPACA_API_KEY,
      secret_key=ALPACA_SECRET_KEY,
      paper=ALPACA_PAPER
      )

      if not ALPACA_PAPER:
      broker = store.getbroker() # or just alpaca_backtrader_api.AlpacaBroker()
      cerebro.setbroker(broker)

      DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData
      data0 = DataFactory(dataname='AAPL', historical=True, fromdate=datetime(
      2015, 1, 1), timeframe=bt.TimeFrame.Days)
      cerebro.adddata(data0)

      print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.run()
      print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
      cerebro.plot()

      E 1 Reply Last reply Reply Quote 0
      • S
        sistemaon last edited by

        I had the same issue... Am using python-3.6.13, conda-4.9.2, backtrader-1.9.76.123.dist-info.

        It seems matplotlib version I was using (matplotlib-3.3.4) were the issue, even importing warnings (import warnings) did not resolve.

        The solution for me was:
        pip uninstall matplotlib
        pip install matplotlib==3.2.2

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

          @gregoh This error was fixed in this file : https://github.com/mementum/backtrader/blob/master/backtrader/plot/locator.py
          with PR #418

          Before fix :

          from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
                                        MONTHS_PER_YEAR, DAYS_PER_WEEK,
                                        SEC_PER_HOUR, SEC_PER_DAY,
                                        num2date, rrulewrapper, YearLocator,
                                        MicrosecondLocator, warnings)
          

          After fix :

          import warnings
          (...)
          from matplotlib.dates import (HOURS_PER_DAY, MIN_PER_HOUR, SEC_PER_MIN,
                                        MONTHS_PER_YEAR, DAYS_PER_WEEK,
                                        SEC_PER_HOUR, SEC_PER_DAY,
                                        num2date, rrulewrapper, YearLocator,
                                        MicrosecondLocator)
          

          You can either install backtrader from its github sources or apply @sistemaon solution

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