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/

    How to import/read CSV data?

    General Code/Help
    4
    8
    1592
    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.
    • Dallascat
      Dallascat last edited by

      I cannot figure out where to put a csv file in a datapath to be read for the datafeed. Even in the 1to100 example of importing ORCL datafeed I get an error it cannot find the path. IDE is Pycharm.

      Thanks!

      1 Reply Last reply Reply Quote 0
      • J
        Jonny8 last edited by

        Hi Dallascat,

        if you post your code, it will be easier to give appropriate help.
        Also the error message you get.
        Best

        1 Reply Last reply Reply Quote 0
        • Dallascat
          Dallascat last edited by

          Here is the data feed:

          
          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, '../../datas/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)
          

          And the error:

          Traceback (most recent call last):
            File "C:/Users/chris/PycharmProjects/BT/main.py", line 40, in <module>
              cerebro.run()
            File "C:\Users\chris\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\cerebro.py", line 1127, in run
              runstrat = self.runstrategies(iterstrat)
            File "C:\Users\chris\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\cerebro.py", line 1210, in runstrategies
              data._start()
            File "C:\Users\chris\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feed.py", line 203, in _start
              self.start()
            File "C:\Users\chris\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feeds\yahoo.py", line 94, in start
              super(YahooFinanceCSVData, self).start()
            File "C:\Users\chris\AppData\Local\Programs\Python\Python38\lib\site-packages\backtrader\feed.py", line 674, in start
              self.f = io.open(self.p.dataname, 'r')
          FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\chris\\PycharmProjects\\BT\\../../datas/orcl-1995-2014.txt'
          
          Process finished with exit code 1
          

          That's from the 1to100 example. I'm mainly trying to figure out how to read any csv data feed. Thanks!

          1 Reply Last reply Reply Quote 0
          • J
            Jonny8 last edited by

            @Dallascat said in How to import/read CSV data?:

            FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\chris\PycharmProjects\BT\../../datas/orcl-1995-2014.txt'

            I think there seems to be an issue with the path? how about using the full path instead?

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

              'C:\Users\chris\PycharmProjects\BT\../../datas/orcl-1995-2014.txt'

              Seems like a windows backslash vs. linux/max frontslash issue. This is python issue (not backtrader) and you should learn about it with google searches.

              RunBacktest.com

              1 Reply Last reply Reply Quote 0
              • Dallascat
                Dallascat @Jonny8 last edited by

                @Jonny8 So if the path is to a csv file on the desktop, what should I use? Or do I need to drop that file into a python project directory and point to that?

                1 Reply Last reply Reply Quote 0
                • vladisld
                  vladisld last edited by

                  I guess you just need to use the absolute path to your file (even if it is on Desktop it has an absolute path - you can see it using right-click->Properties->Location)

                  Dallascat 1 Reply Last reply Reply Quote 1
                  • Dallascat
                    Dallascat @vladisld last edited by

                    @vladisld Thanks for your help! Figured it out....feel a little dumb, still a n00b with BT :-)

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