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

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,
    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(
            # 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),
        # Add the Data Feed to Cerebro
        # Set our desired cash start
        # Print out the starting conditions
        print('Starting Portfolio Value: %.2f' %
        # Run over everything
        # Print out the final result
        print('Final Portfolio Value: %.2f' %

    I get the following error from Jupiter:

    Starting Portfolio Value: 100000.00
    ValueError                                Traceback (most recent call last)
    <ipython-input-7-50f233adde82> in <module>()
         38     # Run over everything
    ---> 39
         41     # Print out the final result
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\ 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\ in runstrategies(self, iterstrat, predata)
        855                 data._start()
        856                 if self._dopreload:
    --> 857                     data.preload()
        859         for stratcls, sargs, skwargs in iterstrat:
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\ in preload(self)
        665     def preload(self):
    --> 666         while self.load():
        667             pass
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\ in load(self)
        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\ in _load(self)
        686         line = line.rstrip('\n')
        687         linetokens = line.split(self.separator)
    --> 688         return self._loadline(linetokens)
    C:\Users\Cef\Anaconda3\lib\site-packages\backtrader\feeds\ in _loadline(self, linetokens)
         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))
    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


    • 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:

  • 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.