Backtesting

Backtesting Broker

class lumibot.backtesting.backtesting_broker.BacktestingBroker(data_source, connect_stream=True, max_workers=20)

Bases: lumibot.brokers.broker.Broker

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

Calculate the trade cost of an order for a given strategy

cancel_order(order)

Cancel an order

property datetime
expired_contracts(strategy)

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

Parameters

strategy (str) – Strategy object name.

Returns

Return type

List of orders

get_chain(chains, exchange='SMART')

Returns option chain for a particular exchange.

get_chains(asset)
get_expiration(chains, exchange='SMART')

Returns expirations and strikes high/low of target price.

get_last_bar(asset)

Returns OHLCV dictionary for last bar of the asset.

get_last_price(asset, quote=None, exchange=None, **kwargs)

Takes an asset asset and returns the last known price

get_last_prices(symbols, quote=None, exchange=None, **kwargs)

Takes a list of symbols and returns the last known prices

get_multiplier(chains, exchange='SMART')

Returns the multiplier

get_strikes(asset)

Returns the strikes for an option asset with right and expiry.

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, close, high, low)

Limit order logic.

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, close, 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=datetime.datetime(2023, 8, 9, 22, 58, 33, 868382))

Bases: lumibot.data_sources.data_source.DataSource

IS_BACKTESTING_DATA_SOURCE = True
get_datetime()
get_datetime_range(length, timestep='minute', timeshift=None)