Seems to be an import error with matplotlib. This error started popping up once I fixed a data feed issue stemming from Yahoo Finance. I had to make a small modification to the source code, specifically the Yahoo.py file, but I do not believe that small change could have broken anything to do with matplotlib. If anyone has any suggestions or fixes, please let me know.
Code:
from datetime import datetime
import backtrader as bt
class SmaCross(bt.Strategy):
# list of parameters which are configurable for the strategy
params = dict(
pfast=10, # period for the fast moving average
pslow=30 # period for the slow moving average
)
def __init__(self):
sma1 = bt.ind.SMA(period=self.p.pfast) # fast moving average
sma2 = bt.ind.SMA(period=self.p.pslow) # slow moving average
self.crossover = bt.ind.CrossOver(sma1, sma2) # crossover signal
def next(self):
if not self.position: # not in the market
if self.crossover > 0: # if fast crosses slow to the upside
self.buy() # enter long
elif self.crossover < 0: # in the market & cross to the downside
self.close() # close long position
cerebro = bt.Cerebro() # create a "Cerebro" engine instance
# Create a data feed
data = bt.feeds.YahooFinanceData(dataname='MSFT',
fromdate=datetime(2011, 1, 1),
todate=datetime(2012, 12, 31))
cerebro.adddata(data) # Add the data feed
cerebro.addstrategy(SmaCross) # Add the trading strategy
cerebro.run() # run it all
cerebro.plot() # and plot it with a single command
Error Stack:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-2-86aa5646ae48> in <module>
35 cerebro.addstrategy(SmaCross) # Add the trading strategy
36 cerebro.run() # run it all
---> 37 cerebro.plot() # and plot it with a single command
~/opt/anaconda3/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/lib/python3.8/site-packages/backtrader/plot/__init__.py in <module>
40
41
---> 42 from .plot import Plot, Plot_OldSync
43 from .scheme import PlotScheme
~/opt/anaconda3/lib/python3.8/site-packages/backtrader/plot/plot.py in <module>
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/lib/python3.8/site-packages/backtrader/plot/locator.py in <module>
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/andreilarion/opt/anaconda3/lib/python3.8/site-packages/matplotlib/dates.py)