Backtrader Community

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

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

    General Code/Help
    5
    14
    10301
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Maxim Korobov
      Maxim Korobov last edited by Maxim Korobov

      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'
      
      1 Reply Last reply Reply Quote 1
      • Maxim Korobov
        Maxim Korobov last edited by

        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

        Z 1 Reply Last reply Reply Quote 1
        • Z
          Zubin Bharucha @Maxim Korobov last edited by

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

          Z 1 Reply Last reply Reply Quote 0
          • Z
            Zubin Bharucha @Zubin Bharucha last edited by

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

            1 Reply Last reply Reply Quote 0
            • B
              backtrader administrators last edited by

              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.

              1 Reply Last reply Reply Quote 0
              • R
                rorymack last edited by rorymack

                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')
                
                1 Reply Last reply Reply Quote 0
                • Maxim Korobov
                  Maxim Korobov last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • B
                    backtrader administrators last edited by

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

                    1 Reply Last reply Reply Quote 0
                    • B
                      backtrader administrators last edited by

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

                      1 Reply Last reply Reply Quote 0
                      • B
                        backtrader administrators last edited by

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

                        • Yahoo Forums: https://forums.yahoo.net/t5/Yahoo-Finance-help/Is-Yahoo-Finance-API-broken/td-p/250503/page/3
                        • Stack Overflow: http://stackoverflow.com/a/44046477/5642219 (points to yahoo, not likely to be deleted, like the above may be)

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

                        1 Reply Last reply Reply Quote 1
                        • T
                          Trisspah last edited by

                          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?

                          1 Reply Last reply Reply Quote 0
                          • B
                            backtrader administrators last edited by

                            See here: Community - Error in cerebro.run() when getting data with YahooFinanceData (new API)

                            T 1 Reply Last reply Reply Quote 0
                            • T
                              Trisspah @backtrader last edited by

                              @backtrader My bad, thanks :)

                              1 Reply Last reply Reply Quote 0
                              • B
                                backtrader administrators last edited by

                                See the news release, Community - Release 1.9.50.117 and this note Community - YahooFinace Data Feeds

                                1 Reply Last reply Reply Quote 0
                                • 1 / 1
                                • First post
                                  Last post
                                Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors