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_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, **kwargs)

Submit orders

Data Source Backtesting

class lumibot.data_sources.data_source_backtesting.DataSourceBacktesting(datetime_start, datetime_end, backtesting_started=None, config=None, api_key=None, pandas_data=None)

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