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

Why am I receiving an error?

  • I am trying to use the quickstart code from BackTrader. I downloaded S&P500 data from yahoo and saved it to an excel file titled SPY.csv.

    Here is my code so far:

    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\\xboss\\Desktop\\SPY.csv")
        # 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' %

    Here is the error that I am receiving:

    Traceback (most recent call last):
      File "C:/Users/xboss/PycharmProjects/BackTraderDemo/", line 39, in <module>
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\", line 1127, in run
        runstrat = self.runstrategies(iterstrat)
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\", line 1212, in runstrategies
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\", line 688, in preload
        while self.load():
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\", line 479, in load
        _loadret = self._load()
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\", line 710, in _load
        return self._loadline(linetokens)
      File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feeds\", line 129, in _loadline
        dt = date(int(dttxt[0:4]), int(dttxt[5:7]), int(dttxt[8:10]))
    ValueError: invalid literal for int() with base 10: '1/29'

  • Looks like date format in your data file doesn't match to YYYY-MM-DD used by bt.

  • Thank you for your observation! I appreciate your time. I will look into changing the format.

  • Or you could just use this line of code.

    import yfinance as yf
        data = bt.feeds.PandasData("^gspc", start="2015-01-01", stop="2019-12-31"))

Log in to reply