Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# 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.

    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

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)