Yahoo =================================== **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. .. code-block:: python 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. Option 1: Provide Start and End in Code --------------------------------------- .. code-block:: python 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") self.submit_order(order) # Pick the dates that you want to start and end your backtest in code backtesting_start = datetime(2025, 1, 1) backtesting_end = datetime(2025, 1, 31) # Run the backtest result = MyStrategy.run_backtest( YahooDataBacktesting, backtesting_start, backtesting_end, ) Using Environment Variables (Optional) ------------------------------------- You can set the following environment variables without making any changes to your code. **LumiBot will automatically use them if they are set.** If they are not set, LumiBot will default to normal behavior (i.e., you can specify the start/end in code as above). Below is a table describing these optional environment variables: .. list-table:: :header-rows: 1 :widths: 20 60 20 * - **Variable** - **Description** - **Example** * - IS_BACKTESTING - (Optional) Set to **"True"** to run the strategy in backtesting mode, set to **"False"** to run the strategy live (defaults to False if not set). - False * - BACKTESTING_START - (Optional) The start date for backtesting in the format "YYYY-MM-DD". Only needed if you are backtesting. - 2025-01-01 * - BACKTESTING_END - (Optional) The end date for backtesting in the format "YYYY-MM-DD". Only needed if you are backtesting. - 2025-01-31 Examples of how you might set these variables: - **Render / Other Cloud**: Define Environment Variables in your service’s dashboard. - **Repl.it**: Add them to your project’s “Secrets.” - **Local Development**: Create a `.env` file (or set them at the OS level). Option 2: Rely on Environment Variables (No Start/End in Code) -------------------------------------------------------------- Below is an example strategy that **omits** `backtesting_start` and `backtesting_end`: .. code-block:: python 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") self.submit_order(order) # LumiBot will automatically look for the environment variables # (IS_BACKTESTING, BACKTESTING_START, BACKTESTING_END). # If they are set, no other parameters are needed; if they're not set, # defaults will be used (or the user can supply dates in code instead). result = MyStrategy.run_backtest( YahooDataBacktesting ) In this example, if the variables `IS_BACKTESTING`, `BACKTESTING_START`, and `BACKTESTING_END` are set, LumiBot will pick them up automatically. If they are not set, LumiBot will simply default to other behavior (e.g., a normal run, or you can specify backtesting dates in code as shown in **Option 1**).