[YahooFinanceData] FileNotFoundError

  • Hello, thanks for your work!

    I'm having an issue with YahooFinanceData feed – it raises an exception after calling cerebro.run. I'm using sample code from GH repo:

    from datetime import datetime
    import backtrader as bt
    class SmaCross(bt.SignalStrategy):
        def __init__(self):
            sma1 = bt.ind.SMA(period=10)
            sma2 = bt.ind.SMA(period=30)
            crossover = bt.ind.CrossOver(sma1, sma2)
            self.signal_add(bt.SIGNAL_LONG, crossover)
    cerebro = bt.Cerebro()
    data0 = bt.feeds.YahooFinanceData(dataname='YHOO',
                                      fromdate=datetime(2011, 1, 1),
                                      todate=datetime(2012, 12, 31))

    How does actual exception look like:

    Traceback (most recent call last):
      File "backtrade/strategy.py", line 25, in <module>
      File "/Users/env/lib/python3.6/site-packages/backtrader/cerebro.py", line 1117, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Users/env/lib/python3.6/site-packages/backtrader/cerebro.py", line 1194, in runstrategies
      File "/Users/env/lib/python3.6/site-packages/backtrader/feed.py", line 203, in _start
      File "/Users/env/lib/python3.6/site-packages/backtrader/feeds/yahoo.py", line 456, in start
        super(YahooFinanceData, self).start()
      File "/Users/env/lib/python3.6/site-packages/backtrader/feeds/yahoo.py", line 90, in start
        super(YahooFinanceCSVData, self).start()
      File "/Users/env/lib/python3.6/site-packages/backtrader/feed.py", line 671, in start
        self.f = io.open(self.p.dataname, 'r')
    FileNotFoundError: [Errno 2] No such file or directory: 'YHOO'

    Info: CPython v3.6.1

    I have investigated the problen so resolving chain looks like this:

    (/feeds/yahoo.py): YahooFinanceData.start --> YahooFinanceData.super().start --> YahooFinanceCSVData.super().start -->
    (/feed.py): CSVDataBase.start --> FileNotFoundError

    It actually tries to check if a string (dataname) has the readline attribute:

    # NOTE: Returns true
    if self.f is None:
        # NOTE: Returns false for the string
        if hasattr(self.p.dataname, 'readline'):
            self.f = self.p.dataname
            # Let an exception propagate to let the caller know
            self.f = io.open(self.p.dataname, 'r')  # <- raises FileNotFoundError

  • administrators

    This is a hidden feature and a problem following the Yahoo API fiasco. First:

    • YHOO seems to no longer be available as ticker. Even if you aren't religious the proper exclamation here would be: "God knows why!"

      Try YHOO.MX for example


    • In case the on-line data feed cannot open the network connection and by means of using the base class it will try to open a file of the same name located in the current directory

    This was used several times for the samples when no network connection was available but the file had been pre-downloaded.

Log in to reply

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.