Backtesting

Backtesting Broker

class lumibot.backtesting.backtesting_broker.BacktestingBroker(data_source, option_source=None, connect_stream=True, max_workers=20, config=None, **kwargs)

Bases: Broker

IS_BACKTESTING_BROKER = True
calculate_trade_cost(order: Order, strategy, price: float)

Calculate the trade cost of an order for a given strategy

cancel_order(order)

Cancel an order

cash_settle_options_contract(position, strategy)

Cash settle an options contract position. This method will calculate the profit/loss of the position and add it to the cash position of the strategy. This method will not actually sell the contract, it will just add the profit/loss to the cash position and set the position to 0. Note: only for backtesting

property datetime
get_active_tracked_orders(strategy: str) list[Order]

Return active (open/submitted/new) orders for the given strategy.

Backtests can accumulate tens of thousands of filled orders over long windows. Scanning all tracked orders and calling Order.is_active() each bar is a major performance bottleneck, so this method sources active orders directly from the broker’s active-order buckets.

get_historical_account_value()

Get the historical account value of the account. TODO: Fill out the docstring with more information.

get_last_bar(asset)

Returns OHLCV dictionary for last bar of the asset.

get_time_to_close()

Return the remaining time for the market to close in seconds

get_time_to_open()

Return the remaining time for the market to open in seconds

initialize_market_calendars(trading_days_df)

Initialize trading calendar and eagerly build caches for backtesting.

is_market_open()

Return True if market is open else false

limit_order(limit_price, side, open_, high, low)

Limit order logic.

process_expired_option_contracts(strategy)

Checks if options or futures contracts have expried and converts to cash.

Parameters:

strategy (Strategy object.) – Strategy object.

Return type:

List of orders

process_pending_orders(strategy)

Used to evaluate and execute open orders in backtesting.

This method will evaluate the open orders at the beginning of every new bar to determine if any of the open orders should have been filled. This method will execute order events as needed, mostly fill events.

Parameters:

strategy (Strategy object)

should_continue()

In production mode always returns True. Needs to be overloaded for backtesting to check if the limit datetime was reached

stop_order(stop_price, side, open_, high, low)

Stop order logic.

lumibot.backtesting.backtesting_broker.get_futures_margin_requirement(asset: Asset) float

Get the initial margin requirement for a futures contract.

This is used in backtesting to simulate the margin deduction when opening a futures position and margin release when closing.

Parameters:

asset – The futures Asset object

Returns:

Initial margin requirement in dollars

Return type:

float

Note

These are TYPICAL values and may not match current broker requirements. For live trading, brokers handle margin internally.

Data Source Backtesting

class lumibot.data_sources.data_source_backtesting.DataSourceBacktesting(datetime_start: datetime | None = None, datetime_end: datetime | None = None, backtesting_started: datetime | None = None, config: dict | None = None, api_key: str | None = None, show_progress_bar: bool = True, log_backtest_progress_to_file=False, progress_csv_path: str | None = None, delay: int | None = None, pandas_data: dict | list | None = None, **kwargs)

Bases: DataSource, ABC

This class is the base class for all backtesting data sources. It is also an abstract class and should not be instantiated directly because it does not define all necessary methods. Instead, instantiate one of the child classes like PandasData.

IS_BACKTESTING_DATA_SOURCE = True
static estimate_requested_length(length=None, start_date=None, end_date=None, timestep='minute')

Infer the number of rows required to satisfy a backtest data request.

get_datetime(adjust_for_delay=False)

Get the current datetime of the backtest.

Parameters:

adjust_for_delay (bool) – Not used for backtesting data sources. This parameter is only used for live data sources.

Returns:

The current datetime of the backtest.

Return type:

datetime

get_datetime_range(length, timestep='minute', timeshift=None)
log_backtest_progress_to_csv(percent, elapsed, log_eta, portfolio_value, simulation_date=None, cash=None, total_return_pct=None, positions_json=None, orders_json=None)

Log backtest progress to CSV file.

Parameters:
  • percent (float) – Progress percentage (0-100)

  • elapsed (timedelta) – Time elapsed since backtest started

  • log_eta (timedelta) – Estimated time remaining

  • portfolio_value (str or float) – Current portfolio value

  • simulation_date (str, optional) – Current date/time in the backtest simulation (YYYY-MM-DD HH:MM:SS format)

  • cash (float, optional) – Current cash balance

  • total_return_pct (float, optional) – Running total return percentage

  • positions_json (str, optional) – JSON string of minimal position data from Position.to_minimal_dict(): [{“asset”: {“symbol”: “AAPL”, “type”: “stock”}, “qty”: 100, “val”: 15000.0, “pnl”: 500.0}, …]

  • orders_json (str, optional) – JSON string of minimal order data from Order.to_minimal_dict(): [{“asset”: {“symbol”: “AAPL”, “type”: “stock”}, “side”: “buy”, “qty”: 100, “type”: “market”, “status”: “new”}, …]

shutdown()

Cleanup any background resources (thread pools, progress heartbeat).

stop_progress_heartbeat() None