How to import/read CSV data?
-
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!
-
Hi Dallascat,
if you post your code, it will be easier to give appropriate help.
Also the error message you get.
Best -
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!
-
@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?
-
'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.
-
@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?
-
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)
-
@vladisld Thanks for your help! Figured it out....feel a little dumb, still a n00b with BT :-)