def on_trading_iteration

This lifecycle method contains the main trading logic. When the market opens, it will be executed in a loop. After each iteration, the strategy will sleep for self.sleeptime minutes. If no crash or interuption, the loop will be stopped self.minutes_before_closing minutes before market closes and will restart on the next day when market opens again.

This is also the most common place to run an AI trading agent with self.agents["name"].run(...). The strategy still controls when the agent runs, how often it runs, and what tools the agent is allowed to use. See AI Trading Agents and Agentic Backtesting.

class MyStrategy(Strategy):
    def on_trading_iteration(self):
        # pull data
        # check if should buy an asset based on data
        # if condition, buy/sell asset
        pass

Reference

lumibot.strategies.strategy.Strategy.on_trading_iteration(self)

Use this lifecycle method for your main trading loop. This method is called every self.sleeptime minutes (or seconds/hours/days if self.sleeptime is “30S”, “1H”, “1D”, etc.).

Example

>>> def on_trading_iteration(self):
>>>     self.log_message("Hello")
>>>     order = self.create_order("SPY", 10, "buy")
>>>     self.submit_order(order)