For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/
YahooFinance
-
Hi,
I haven't used Backtrader for over six months, and wow, none of my programs works any more :(
The problem seems to be with YahooFinance. I saw some people have had similar problems online, but other posts didn't help me, so I thought of opening a new one.
When I run any strategy, I get FileNotFoundError.
In this post (https://community.backtrader.com/topic/3979/help-with-yahoofinance-data/3)
@run-out recommended to add the following line to the file feeds/yahoo.py: sess.headers['User-Agent'] = 'backtrader'However, when I do that, I get TypeError.
I'm attaching below a basic code I tested this with, and more information about the two exceptions:
import backtrader as bt import datetime enddate=datetime.date.today() startdate = enddate - datetime.timedelta(days=(365*2)) cerebro = bt.Cerebro() cerebro.adddata(bt.feeds.YahooFinanceData( dataname="AAPL", timeframe=bt.TimeFrame.Days, fromdate=startdate, todate=enddate, reverse=False, )) class basicStrategy(bt.Strategy): print("basicStrategy has been added") def __init__(self): print("Strategy has started. This is init") def prenext(self): print("Prenext") def next(self): firstdataname = self.datas[0]._name stockpricetoday = self.datas[0].close[0] today = bt.num2date(self.datas[0].datetime[0]).date() print(today,firstdataname,"is trading for",stockpricetoday) cerebro.broker.setcash(100000.0) cerebro.addstrategy(basicStrategy) print("Launching the strategy") cerebro.run() print("Strategy is over")
File "/Users/simoneromeo/Investment Files/Learning Backtrader/67 debugging_yahoof_errors.py", line 38, in <module> cerebro.run() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1210, in runstrategies data._start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feed.py", line 203, in _start self.start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 355, in start super(YahooFinanceData, self).start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 94, in start super(YahooFinanceCSVData, self).start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/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: 'AAPL'
runfile('/Users/simoneromeo/Investment Files/Learning Backtrader/67 debugging_yahoof_errors.py', wdir='/Users/simoneromeo/Investment Files/Learning Backtrader') basicStrategy has been added Launching the strategy Traceback (most recent call last): File "/Users/simoneromeo/Investment Files/Learning Backtrader/67 debugging_yahoof_errors.py", line 38, in <module> cerebro.run() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1127, in run runstrat = self.runstrategies(iterstrat) File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/cerebro.py", line 1210, in runstrategies data._start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feed.py", line 203, in _start self.start() File "/Users/simoneromeo/opt/anaconda3/lib/python3.8/site-packages/backtrader/feeds/yahoo.py", line 355, in start # Prepared a "path" file - CSV Parser can take over TypeError: super(type, obj): obj must be an instance or subtype of type