Skip to content
All library documents

LumiBot Strategy Lifecycle and Core Trading API Methods

Article Lumibot

Summary

This overview explains how a LumiBot trading strategy uses a lifecycle method alongside data, account, and order methods. Its example describes a daily stock strategy that checks the latest price, calculates a whole-share quantity from available cash, and submits a buy order on the first iteration. The accompanying reference groups common tasks: reading current or historical prices, checking cash and positions, creating and submitting orders, scheduling iterations, and running a backtest.

The document distinguishes strategy subclasses, which support trading and backtesting, from standalone components for selected research and data tasks. It notes that strategies may run continuously or execute a single scheduled lifecycle, and clarifies that the latter is not a step-through backtest interface. The example is a usage illustration rather than evidence of profitability: it gives no performance evaluation, risk controls, or trading-cost treatment. Readers need the detailed API references for method behavior and should not infer execution or broker specifics from this brief overview.

Key ideas

  • A LumiBot strategy typically defines initialization and recurring trading-iteration behavior.
  • The example buys a stock once using available cash and the latest price.
  • The API overview organizes methods for market data, account state, orders, scheduling, and backtesting.
  • Standalone components support selected data and research tasks outside a strategy subclass.
  • A one-shot live run executes a scheduled lifecycle and is not an arbitrary backtest stepping mechanism.

Tags

Full text
# strategy api overview


Strategy API Overview
=====================

.. meta::
   :description: Learn the core LumiBot Strategy API for prices, positions, orders, portfolio state, schedules, backtests, and trading lifecycle methods.

Most LumiBot strategies need one lifecycle method and a small group of data,
account, and order methods. Start with this complete daily-stock example:

.. code-block:: python

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

.. list-table:: Common Strategy methods
   :header-rows: 1
   :widths: 24 42 34

   * - Task
     - Method
     - Detailed reference
   * - Read the latest price
     - ``self.get_last_price(asset)``
     - :doc:`strategy_methods.data`
   * - Read historical bars
     - ``self.get_historical_prices(asset, length, timestep)``
     - :doc:`strategy_methods.data`
   * - Read cash and positions
     - ``self.get_cash()`` and ``self.get_positions()``
     - :doc:`strategy_methods.account`
   * - Create and submit an order
     - ``self.create_order(...)`` and ``self.submit_order(order)``
     - :doc:`strategy_methods.orders`
   * - Run logic on a schedule
     - ``initialize()`` and ``on_trading_iteration()``
     - :doc:`lifecycle_methods`
   * - Backtest the strategy
     - ``Strategy.run_backtest(...)``
     - :doc:`backtesting.backtesting_function`

Use :doc:`strategy_properties` for fields such as ``first_iteration`` and
``portfolio_value``. Continue to :doc:`strategy_methods` for the complete
categorized method reference or :doc:`agents_quickstart` 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 :doc:`standalone_components` 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 :doc:`agent_start_here` for exact entry points for coding agents.

Shown in full with attribution under the source's licence. Licence: GPL-3.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.