Strategy API Overview¶
Most LumiBot strategies need one lifecycle method and a small group of data, account, and order methods. Start with this complete daily-stock example:
from datetime import datetime
from lumibot.backtesting import YahooDataBacktesting
from lumibot.strategies import Strategy
class BuyAndHold(Strategy):
def initialize(self):
self.sleeptime = "1D"
def on_trading_iteration(self):
if self.first_iteration:
price = self.get_last_price("SPY")
quantity = int(self.get_cash() // price)
order = self.create_order("SPY", quantity, "buy")
self.submit_order(order)
if __name__ == "__main__":
BuyAndHold.run_backtest(
YahooDataBacktesting,
datetime(2025, 1, 1),
datetime(2025, 2, 1),
)
Core methods¶
Task |
Method |
Detailed reference |
|---|---|---|
Read the latest price |
|
|
Read historical bars |
|
|
Read cash and positions |
|
|
Create and submit an order |
|
|
Run logic on a schedule |
|
|
Backtest the strategy |
|
Use Strategy Properties for fields such as first_iteration and
portfolio_value. Continue to Strategy Methods for the complete
categorized method reference or Run your first AI backtest to add an AI agent to
the same lifecycle.
Choose how to use LumiBot¶
Use a Strategy subclass for trading and backtesting, including AI teams.
Use Use LumiBot in another Python project for selected data/research helpers in another
program. A configured strategy can run continuously with run_live() or
execute one scheduled lifecycle with run_live(run_once=True). The latter is
not an arbitrary step-through-backtest API.
There is no requirement for a function named main. An
if __name__ == "__main__": guard prevents your runner executing on import.
See LumiBot for coding agents for exact entry points for coding agents.