data completed but next error: IndexError Traceback (most recent call last)
Cell In[11], line 186
184 # Run backtest
185 print("Starting backtest...")
--> 186 portfolio, individual_results, trade_logs = backtest_strategy()
188 # Plot results
189 plt.figure(figsize=(14, 7))
Cell In[11], line 175, in backtest_strategy()
173 for ticker in tickers:
174 print(f"Running strategy for {ticker}...")
--> 175 portfolio_value, trade_log = implement_strategy(data, ticker)
176 results[ticker] = portfolio_value
177 trades[ticker] = trade_log
Cell In[11], line 52, in implement_strategy(data, index_name)
50 current_price = prices.iloc[i]
51 if i > 1: # Need at least 2 days of data for returns
---> 52 previous_return = returns.iloc[i-1]
54 # Rule 4: Skip entry signals when already in position
55 if position == 0:
56 # Rule 1: Buy when yesterday was down, sell when yesterday was up
File ~\anaconda3\Lib\site-packages\pandas\core\indexing.py:1191, in _LocationIndexer.getitem(self, key)
1189 maybe_callable = com.apply_if_callable(key, self.obj)
1190 maybe_callable = self._check_deprecated_callable_usage(key, maybe_callable)
-> 1191 return self._getitem_axis(maybe_callable, axis=axis)
File ~\anaconda3\Lib\site-packages\pandas\core\indexing.py:1752, in _iLocIndexer._getitem_axis(self, key, axis)
1749 raise TypeError("Cannot index by location index with a non-integer key")
1751 # validate the location
-> 1752 self._validate_integer(key, axis)
1754 return self.obj._ixs(key, axis=axis)
File ~\anaconda3\Lib\site-packages\pandas\core\indexing.py:1685, in _iLocIndexer._validate_integer(self, key, axis)
1683 len_axis = len(self.obj._get_axis(axis))
1684 if key >= len_axis or key < -len_axis:
-> 1685 raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds