Navigation

    Backtrader Community

    • Register
    • 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/

    Why am I receiving an error?

    General Code/Help
    4
    5
    86
    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.
    • Jake West
      Jake West last edited by

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

      Here is the error that I am receiving:

      Traceback (most recent call last):
        File "C:/Users/xboss/PycharmProjects/BackTraderDemo/backtrader_quickstart.py", line 39, in <module>
          cerebro.run()
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1127, in run
          runstrat = self.runstrategies(iterstrat)
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\cerebro.py", line 1212, in runstrategies
          data.preload()
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 688, in preload
          while self.load():
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 479, in load
          _loadret = self._load()
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feed.py", line 710, in _load
          return self._loadline(linetokens)
        File "C:\Users\xboss\PycharmProjects\BackTraderDemo\venv\lib\site-packages\backtrader\feeds\yahoo.py", 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'
      
      1 Reply Last reply Reply Quote 0
      • A
        ab_trader last edited by

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

        1 Reply Last reply Reply Quote 1
        • Jake West
          Jake West last edited by

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

          1 Reply Last reply Reply Quote 0
          • S
            shoaibmalek21 last edited by

            See the link below, I think it's helpful to you.

            https://community.backtrader.com/topic/2637/dt-date-int-dttxt-0-4-int-dttxt-5-7-int-dttxt-8-10

            1 Reply Last reply Reply Quote 0
            • run-out
              run-out last edited by

              Or you could just use this line of code.

              import yfinance as yf
              
              ...
              
                  data = bt.feeds.PandasData(dataname=yf.download("^gspc", start="2015-01-01", stop="2019-12-31"))
              
              
              
              
              1 Reply Last reply Reply Quote 1
              • 1 / 1
              • First post
                Last post
              Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
              $(document).ready(function () { app.coldLoad(); }); }