For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

[Errno 2] No such file or directory



  • I state that I am a beginner with cerebro.
    I don't understand why this error with this "base" code that worked in the past:
    FileNotFoundError: [Errno 2] No such file or directory: 'BYND'

    import backtrader as bt
    from datetime import datetime
    import datetime
    class firstStrategy(bt.Strategy):
             
        def __init__(self):
            self.dataclose= self.datas[0].close #2
            self.rsi = bt.indicators.RSI_SMA(self.data.Close, period=14)
         def next(self): 
            if not self.position:# se non siamo in posizione
                if self.rsi < 30:
                    self.buy(size=100)  #compro 100 azioni
            else: #cosa succede quando SIAMO già in una posizione
                if self.rsi > 80:
                    self.sell(size=100) #vendo 100 azioni
     
    startcash = 10000
    cerebro = bt.Cerebro()
    cerebro.addstrategy(firstStrategy)
    #data = bt.feeds.BacktraderCSVData(dataname='C:/dati/test/finance/fca.csv')
    # Inseriamo i dati
    start_dt = datetime.datetime(2018, 12, 1)
    end_dt = datetime.datetime(2020, 1, 12)
    data = bt.feeds.YahooFinanceData(dataname='BYND',
                                     fromdate=start_dt,
                                     todate=end_dt)
                                             
    cerebro.adddata(data)
    # Set our desired cash start
    cerebro.broker.setcash(startcash)
    
    # Run over everything
    cerebro.run()
    

    pyt.jpg



  • Usually it means that something wrong happened during data download from Yahoo services. It could be a wrong proxy, encoding error or even Yahoo API version change. Fortunately it is easy to debug - just stop inside the YahooFinanceData.start_v7 (in yahoo.py file) and you will see what's going wrong.


  • administrators

    If the data cannot be downloaded, there will an attempt to open a cached version from a file with the same name as the ticker, which obviously does not exist. This is probably a Yahoo problem.



  • @vladisld said in [Errno 2] No such file or directory:

    Usually it means that something wrong happened during data download from Yahoo services. It could be a wrong proxy, encoding error or even Yahoo API version change. Fortunately it is easy to debug - just stop inside the YahooFinanceData.start_v7 (in yahoo.py file) and you will see what's going wrong.

    Thanks, but unfortunately I can't understand what's wrong. Could you give me some suggestions?



  • It's hard to tell what was wrong just by looking at the provided stack. What is clear is the problem happened during Yahoo data download. No logging information is available as well in this case. But you have a framework source code at your disposal - that's the beauty of an open source project - use it - that's exactly what it is for.



  • @vladisld
    I don't understand how I can act on downloading Yahoo data. But then now it is not possible to use cerebro all over the world? Or is there an alternative system to be able to give cerebro data input to make strategies work?



  • Ok, I tried to debug it for you and it seems that Yahoo response has changed and doesn't return the expected content type anymore ('text/csv' is expected but 'text/plain' is returned):

    Here the snippet of the current code in YahooFinanceData.start_v7 in yahoo.py:

                ctype = resp.headers['Content-Type']
                if 'text/csv' not in ctype:
                    self.error = 'Wrong content type: %s' % ctype
                    continue  # HTML returned? wrong url?
    

    The fix is relatively simple:

    diff --git a/backtrader/feeds/yahoo.py b/backtrader/feeds/yahoo.py
    index abfe97d..bd1f6ea 100644
    --- a/backtrader/feeds/yahoo.py
    +++ b/backtrader/feeds/yahoo.py
    @@ -330,7 +330,7 @@ class YahooFinanceData(YahooFinanceCSVData):
                     continue
    
                 ctype = resp.headers['Content-Type']
    -            if 'text/csv' not in ctype:
    +            if ctype not in ['text/csv', 'text/plain']:
                     self.error = 'Wrong content type: %s' % ctype
                     continue  # HTML returned? wrong url?
    

    Also in your code, the rsi indicator didn't work as expected the way it was defined until it was changed to:

    class firstStrategy(bt.Strategy):
        def __init__(self):
            self.dataclose= self.datas[0].close #2
            self.rsi = bt.indicators.RSI_SMA(self.datas[0], period=14)
    


  • Thanks for the reply. Forgive my perhaps trivial questions but as said I am a neophyte.
    So I have to modify the yahoo.py code that derives from the installation of backtrader?



  • If you don't want to change Backtrader's code, you may just copy the yahoo.py to your source base ( optionally rename appropriate classes), fix the code and use it instead of original code, hoping that someone will provide the official fix in the future.



  • @vladisld I have modified the code as you suggest me, but now i obtain anoyher error:
    (pyt.jpg image url)

    ps
    forgive again what are trivial things for you, for me they are blocks to continue, I am trying to do everything possible



  • @vladisld said in [Errno 2] No such file or directory:

    ['text/csv', 'text/plain']

    Thanks! It worked!



  • @backtrader . Should we have an official fix for this yahoo download issue?



  • @Jiayu-Chen said in [Errno 2] No such file or directory:

    @vladisld said in [Errno 2] No such file or directory:

    ['text/csv', 'text/plain']

    Thanks! It worked!

    It still doesn't work for me.
    Could you attach the yahoo.py to me?



  • @vladisld god bless u, dude!!



  • @Jiayu-Chen said in [Errno 2] No such file or directory:

    @backtrader . Should we have an official fix for this yahoo download issue?

    There's a pull request for this.



  • If I copy the backtrader folder locally and try to make the changes inn yahoo.py, I am getting Traceback (most recent call last): File "simulation.py", line 37, in <module> cerebro.run() # run it all File "/Users/username/PycharmProjects/Nexttrade/local_backtrader/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/Users/username/PycharmProjects/Nexttrade/local_backtrader/backtrader/cerebro.py", line 1217, in runstrategies strat = stratcls(*sargs, **skwargs) File "/Users/username/PycharmProjects/Nexttrade/local_backtrader/backtrader/metabase.py", line 86, in __call__ _obj, args, kwargs = cls.donew(*args, **kwargs) File "/Users/username/PycharmProjects/Nexttrade/local_backtrader/backtrader/strategy.py", line 73, in donew _obj._id = cerebro._next_stid() AttributeError: 'NoneType' object has no attribute '_next_stid'


Log in to reply
 

});