AttributeError: 'NoneType' object has no attribute 'close'



  • What's reason that f is None?

    "version": "1.9.47.116"
    backtrader\feed.py

    ...

    def preload(self):
        while self.load():
            pass
    
        self._last()
        self.home()
    
        # preloaded - no need to keep the object around - breaks multip in 3.x
        self.f.close()
        self.f = None
    

    ...

    Traceback (most recent call last):
      File "D:/Projects/trading-bot/main/lab/backtrader/playground.py", line 277, in <module>
    	run()
      File "D:/Projects/trading-bot/main/lab/backtrader/playground.py", line 119, in run
    	strategies = run_strategy(back_trader, run_mode == RunMode.DevelopIdea, from_date, to_date)
      File "D:/Projects/trading-bot/main/lab/backtrader/playground.py", line 218, in run_strategy
    	strategies = back_trader.run()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1070, in run
    	runstrat = self.runstrategies(iterstrat)
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 1146, in runstrategies
    	data.preload()
      File "C:\Users\Home\Anaconda3\lib\site-packages\backtrader\feed.py", line 689, in preload
    	self.f.close()
    AttributeError: 'NoneType' object has no attribute 'close'


  • Possible due to this :)

    https://ichart.yahoo.com/table.csv?s=NFLX&a=0&b=1&c=2016&d=4&e=16&f=2017&g=h&ignore=.csv

    alt text



  • @Maxim-Korobov I was going crazy with the same error. Thanks for this post!



  • @backtrader, is there any news/update on this issue? Thanks in advance.


  • administrators

    It's not a platform issue (aside from better handling the error). It's a Yahoo issue or it may simply be that Yahoo has decided to pull the plug and no longer offer unrestricted/unauthenticated csv downloads.



  • Maxim

    I am getting the same type of error running anything that tries to pick up a Yahoo feed.

    Traceback (most recent call last):
      File "btfd.py", line 233, in <module>
        runstrat()
      File "btfd.py", line 177, in runstrat
        cerebro.run(**eval('dict(' + args.cerebro + ')'))
      File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.py", line 1070, in run
        runstrat = self.runstrategies(iterstrat)
      File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/cerebro.py", line 1146, in runstrategies
        data.preload()
      File "/Users/rorymackay/anaconda/envs/backtrader/lib/python2.7/site-packages/backtrader/feed.py", line 689, in preload
        self.f.close()
    AttributeError: 'NoneType' object has no attribute 'close'
    

    I assume it is for the same reason, earlier I had an error message that pointed directly to the url you showed.

    However using pandas and accessing the same data (perhaps not in the same way ) I do get a response.
    I did have to directly install the pandas-Datareader from pip as the anaconda one was not working. (this on a linux box)
    Conda appears to have fixed this as the "latest" version is working on a mac...

    pandas-datareader         0.3.0.post0              py27_0    conda-forge
    
    

    This is the pip version that is now working on the linux box.

    Not sure if that sheds any light on the issue.

    this gets the data to a csv ...

    
    
    import datetime
    import numpy as np
    import pandas as pd
    #import pandas_datareader as pdr
    import pandas_datareader.data as web
    import os 
    import sys  # To find out the script name (in argv[0])
    start = datetime.datetime(2000, 1, 1)
    end = datetime.datetime(2015, 5, 9)
    
    data = web.DataReader('ORCL', 'google',start,end)
    
    os.chdir('/Users/rorymackay/Documents/backtrader_test')
    modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
    datapath = os.path.join(modpath,'backtrader_test/datas/orcl.csv')
    
    data.to_csv('orcl.csv')
    


  • What about implementing Quandl data source internally.
    One cons is that API key is mandatory.


  • administrators

    It would be down to each user to have a key. Something to consider.


  • administrators

    There is a native Quandl data feed (for the WIKI Data) starting with 1.9.48.116


  • administrators

    Yahoo has apparently discontinued the service (without major notice).

    See this note for the updated Yahoo data feed: Docs - Yahoo Data Feed Notes



  • Hi,
    Since this Yahoo service discontinuation, I'm having problems with some symbols, which all happen to be traded in Paris.

    I did update backtrader, and moved to the correct new syntax (unless I'm mistaken).

    This works: data = bt.feeds.YahooFinanceData(dataname='GOOG', fromdate=datetime.datetime(2016, 1, 1), todate=datetime.datetime(2017, 05, 15))

    This doesn't work anymore: data = bt.feeds.YahooFinanceData(dataname='KN.PA', fromdate=datetime.datetime(2016, 1, 1), todate=datetime.datetime(2017, 05, 15))

    I'm getting an error when trying to run the strategy (which didn't change and used to work).

    Here's the trace I get:

    File "C:\Python\27\lib\site-packages\backtrader\cerebro.py", line 1070, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\Python\27\lib\site-packages\backtrader\cerebro.py", line 1146, in runstrategies
        data.preload()
      File "C:\Python\27\lib\site-packages\backtrader\feed.py", line 682, in preload
        while self.load():
      File "C:\Python\27\lib\site-packages\backtrader\feed.py", line 476, in load
        _loadret = self._load()
      File "C:\Python\27\lib\site-packages\backtrader\feed.py", line 704, in _load
        return self._loadline(linetokens)
      File "C:\Python\27\lib\site-packages\backtrader\feeds\yahoo.py", line 110, in _loadline
        o = float(linetokens[next(i)])
    ValueError: could not convert string to float: null
    

    I assume fetching the data fails and the strategy runs on no data.

    Am I missing something?


  • administrators



  • @backtrader My bad, thanks :)


  • administrators


Log in to reply
 

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