Problem with Backtrader example



  • Hi all,
    I'm new here. Looking forward to use backtrader! I'm kinda new into Python, so perhaps this is really a noob question. When I run this code (the example):

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    
    import datetime  # For datetime objects
    import os.path  # To manage paths
    import sys  # To find out the script name (in argv[0])
    
    # Import the backtrader platform
    import backtrader as bt
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        # Datas are in a subfolder of the samples. Need to find where the script is
        # because it could have been called from anywhere
        modpath = os.path.dirname(os.path.abspath(sys.argv[0]))
        datapath = os.path.join(modpath, 'C:\\Users\\Cef\\orcl-1995-2014.txt')
        
        # Create a Data Feed
        data = bt.feeds.YahooFinanceCSVData(
            dataname=datapath,
            # Do not pass values before this date
            fromdate=datetime.datetime(2000, 1, 1),
            # Do not pass values after this date
            todate=datetime.datetime(2000, 12, 31),
            reverse=False)
    
        # Add the Data Feed to Cerebro
        cerebro.adddata(data)
    
        # Set our desired cash start
        cerebro.broker.setcash(100000.0)
    
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        # Run over everything
        cerebro.run()
    
        # Print out the final result
        print('Final Portfolio Value: %.2f' % cerebro.broker.getvalue())
    

    I get the following error from Jupiter:

    Starting Portfolio Value: 100000.00
    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <ipython-input-7-50f233adde82> in <module>()
         37 
         38     # Run over everything
    ---> 39     cerebro.run()
         40 
         41     # Print out the final result
    
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\cerebro.py in run(self, **kwargs)
        792             # let's skip process "spawning"
        793             for iterstrat in iterstrats:
    --> 794                 runstrat = self.runstrategies(iterstrat)
        795                 self.runstrats.append(runstrat)
        796         else:
    
    C:\Users\Cefe\Anaconda3\lib\site-packages\backtrader\cerebro.py in runstrategies(self, iterstrat, predata)
        855                 data._start()
        856                 if self._dopreload:
    --> 857                     data.preload()
        858 
        859         for stratcls, sargs, skwargs in iterstrat:
    
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\feed.py in preload(self)
        664 
        665     def preload(self):
    --> 666         while self.load():
        667             pass
        668 
    
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\feed.py in load(self)
        459 
        460             if not self._fromstack(stash=True):
    --> 461                 _loadret = self._load()
        462                 if not _loadret:  # no bar use force to make sure in exactbars
        463                     # the pointer is undone this covers especially (but not
    
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\feed.py in _load(self)
        686         line = line.rstrip('\n')
        687         linetokens = line.split(self.separator)
    --> 688         return self._loadline(linetokens)
        689 
        690 
    
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\feeds\yahoo.py in _loadline(self, linetokens)
         90 
         91         dttxt = linetokens[next(i)]
    ---> 92         dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
         93         dtnum = date2num(datetime.combine(dt, self.p.sessionend))
         94 
    
    ValueError: invalid literal for int() with base 10: ''
    

    Problem integer which is zero?! Can someone help me out, thanks!


  • administrators

    The file you are trying to load contains something which is not meant for the YahooFinanceCSVData data feed. Hence the error.

    Note: Please see the top of the page for how to add 3 tick marks to format code postings.


  • administrators

    Options:

    • Post a sample of the file (use the 3 tick marks before and after to make sure the content is posted properly)

    Something that may quickly offer you helpd:

    • You may disable preloading in cerebro and print the incoming data in next until the exception is raised. The offending line in the data will be easier to spot. Use: cerebro.run(preload=False)


  • thank you very much for the prompt answer. that worked.


Log in to reply
 

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