NOTE: Please ensure you have installed the latest lumibot version using ``pip install lumibot –upgrade`` before proceeding as there have been some major changes to the backtesting module in the latest version.

Yahoo backtesting is so named because we get data for the backtesting from the Yahoo Finance website. The user is not required to supply data. Any stock information that is available in the Yahoo Finance API should be available for backtesting. The Yahoo backtester is only for stock data (including ETFs). Additionally, you cannot use the Yahoo backtester for intra-day trading, it is for daily trading only. For other securities, use the Polygon or Pandas backtesters.

Using Yahoo backtester, you can also run backtests very easily on your strategies, you do not have to modify anything in your strategies.

To use the Yahoo backtester, you must import the YahooDataBacktesting and BacktestingBroker objects.

from lumibot.backtesting import BacktestingBroker, YahooDataBacktesting

To run a backtest, you must create a YahooDataBacktesting object, then pass it to the BacktestingBroker object. The BacktestingBroker object is then passed to the Strategy object. Finally, the Strategy object is passed to the Trader object and the backtest is run.

There are also several plots that are generated by the backtester. These plots are by defaults saved in the logs folder.

from datetime import datetime

from lumibot.backtesting import BacktestingBroker, YahooDataBacktesting
from lumibot.strategies import Strategy
from lumibot.traders import Trader

# A simple strategy that buys AAPL on the first day
class MyStrategy(Strategy):
    def on_trading_iteration(self):
        if self.first_iteration:
            aapl_price = self.get_last_price("AAPL")
            quantity = self.portfolio_value // aapl_price
            order = self.create_order("AAPL", quantity, "buy")

# Pick the dates that you want to start and end your backtest
# and the allocated budget
backtesting_start = datetime(2020, 11, 1)
backtesting_end = datetime(2020, 12, 31)

# Run the backtest
trader = Trader(backtest=True)
data_source = YahooDataBacktesting(
broker = BacktestingBroker(data_source)
strat = MyStrategy(