Hello!
Below is the code error that I receive when running the last block of code for the 'Backtesting a strategy based on simple moving average' section of chapter 2 (Signal section) of Python for Finance Cookbook.
The input is:
print(f'Starting Portfolio Value: {cerebro.broker.getvalue():.2f}')
cerebro.run()
print(f'Final Portfolio Value: {cerebro.broker.getvalue():.2f}')
The output is:
Starting Portfolio Value: 1000.00
FileNotFoundError Traceback (most recent call last)
/var/folders/ys/kp32whkx5bn9nwysmmsrhb5w0000gn/T/ipykernel_57259/3974036464.py in
1 print(f'Starting Portfolio Value: {cerebro.broker.getvalue():.2f}')
----> 2 cerebro.run()
3 print(f'Final Portfolio Value: {cerebro.broker.getvalue():.2f}')
/opt/anaconda3/lib/python3.9/site-packages/backtrader/cerebro.py in run(self, **kwargs)
1125 # let's skip process "spawning"
1126 for iterstrat in iterstrats:
-> 1127 runstrat = self.runstrategies(iterstrat)
1128 self.runstrats.append(runstrat)
1129 if self._dooptimize:
/opt/anaconda3/lib/python3.9/site-packages/backtrader/cerebro.py in runstrategies(self, iterstrat, predata)
1208 if self._exactbars < 1: # datas can be full length
1209 data.extend(size=self.params.lookahead)
-> 1210 data._start()
1211 if self._dopreload:
1212 data.preload()
/opt/anaconda3/lib/python3.9/site-packages/backtrader/feed.py in _start(self)
201
202 def _start(self):
--> 203 self.start()
204
205 if not self._started:
/opt/anaconda3/lib/python3.9/site-packages/backtrader/feeds/yahoo.py in start(self)
353
354 # Prepared a "path" file - CSV Parser can take over
--> 355 super(YahooFinanceData, self).start()
356
357
/opt/anaconda3/lib/python3.9/site-packages/backtrader/feeds/yahoo.py in start(self)
92
93 def start(self):
---> 94 super(YahooFinanceCSVData, self).start()
95
96 if not self.params.reverse:
/opt/anaconda3/lib/python3.9/site-packages/backtrader/feed.py in start(self)
672 else:
673 # Let an exception propagate to let the caller know
--> 674 self.f = io.open(self.p.dataname, 'r')
675
676 if self.p.headers:
FileNotFoundError: [Errno 2] No such file or directory: 'BA'
I used a different stock to run the code. It keeps referring to the line 'cerebro.run()' and I've looked to see if there's another way to run the code but I've come up short. Advice?
System
Mac OS Monterey
Python 3.9.7