1

似乎是 matplotlib 的导入错误。一旦我修复了源自 Yahoo Finance 的数据馈送问题,此错误就开始出现。我不得不对源代码进行小修改,特别是 Yahoo.py 文件,但我不认为小改动会破坏与 matplotlib 的任何关系。如果有人有任何建议或修复,请告诉我。

代码:

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

错误堆栈:

---------------------------------------------------------------------------
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)
4

0 回答 0