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

# Backtesting

## Backtesting Broker

class lumibot.backtesting.backtesting_broker.BacktestingBroker(data_source, 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_expiration(chains, exchange='SMART')

    Returns expirations and strikes high/low of target price.

  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

  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.

  submit_order(order)

    Submit an order for an asset

  submit_orders(orders)

    Submit orders

## Data Source Backtesting

class lumibot.backtesting.data_source_backtesting.DataSourceBacktesting(datetime_start, datetime_end, backtesting_started=None)

  Bases: `DataSource`

  IS_BACKTESTING_DATA_SOURCE = True
  get_datetime()

    Returns the current datetime in the default timezone

    Return type:

      datetime

  get_datetime_range(length, timestep='minute', timeshift=None)