Backtest

class BacktestDataConfig

Bases: object

Represents the data configuration for one specific backtest run.

bar_spec
bar_types
catalog_fs_protocol
catalog_fs_rust_storage_option_keys
catalog_fs_storage_option_keys
catalog_path
client_id
data_type
end_time
filter_expr
instrument_id
instrument_ids
metadata
optimize_file_loading
start_time
class BacktestEngine

Bases: object

PyO3 wrapper around [BacktestEngine].

Exposes the backtest engine to Python as BacktestEngine. Uses unsendable because the inner engine holds Rc<RefCell<…>>.

add_actor(actor)

Adds an actor from a constructed Python instance.

The actor ID and logging flags are sourced from the instance’s config.

add_actor_from_config(config)

Adds an actor from an importable config.

add_actors(actors)

Adds multiple actors from constructed Python instances. Stops at the first error.

add_actors_from_configs(configs)

Adds multiple actors from importable configs. Stops at the first error.

add_builtin_actor(type_name, config)

Adds a built-in example actor from its type name and config.

This method exists only to single-source bundled example actor code across Rust and Python tests/examples. It is not a first-class extension path for adding native actors.

add_builtin_strategy(type_name, config)

Adds a built-in example strategy from its type name and config.

This method exists only to single-source bundled example strategy code across Rust and Python tests/examples. It is not a first-class extension path for adding native strategies.

add_data(data, client_id=None, validate=True, sort=True)

Adds data to the engine.

add_defi_data(data, client_id=None, sort=True)

Adds DeFi data to the engine.

add_exec_algorithm(exec_algorithm)

Adds an execution algorithm from a constructed Python instance.

The execution algorithm ID and logging flags are sourced from the instance’s config.

add_exec_algorithm_from_config(config)

Adds an execution algorithm from an importable config.

add_exec_algorithms(exec_algorithms)

Adds multiple execution algorithms from constructed Python instances. Stops at the first error.

add_exec_algorithms_from_configs(configs)

Adds multiple execution algorithms from importable configs. Stops at the first error.

add_instrument(instrument)

Adds an instrument to the engine.

add_native_exec_algorithm(type_name, config)

Adds a compiled-in native Rust execution algorithm from its type name and config.

The type name determines which built-in execution algorithm is constructed. All execution happens in Rust; Python is the configuration layer.

add_strategies(strategies)

Adds multiple strategies from constructed Python instances. Stops at the first error.

add_strategies_from_configs(configs)

Adds multiple strategies from importable configs. Stops at the first error.

add_strategy(strategy)

Adds a strategy from a constructed Python instance.

The strategy ID, order ID tag, and logging flags are sourced from the instance’s config.

add_strategy_from_config(config)

Adds a strategy from an importable config.

add_venue(venue, oms_type, account_type, starting_balances, base_currency=None, default_leverage=None, leverages=None, margin_model=None, fill_model=None, fee_model=None, latency_model=None, modules=None, book_type=Ellipsis, routing=False, reject_stop_orders=True, support_gtd_orders=True, support_contingent_orders=True, use_position_ids=True, use_random_ids=False, use_reduce_only=True, use_message_queue=True, use_market_order_acks=False, bar_execution=True, bar_adaptive_high_low_ordering=False, trade_execution=True, liquidity_consumption=False, queue_position=False, allow_cash_borrowing=False, frozen_account=False, oto_trigger_mode=Ellipsis, price_protection_points=None, settlement_prices=None, liquidation_enabled=False, liquidation_trigger_ratio=None, liquidation_cancel_open_orders=True)

Adds a simulated exchange with the given parameters to the engine.

# Liquidation parameters

  • liquidation_enabled (bool, default False): if margin liquidation should be triggered when the account’s equity falls to or below the maintenance margin threshold scaled by liquidation_trigger_ratio.

  • liquidation_trigger_ratio (float, optional, default 1.0): the ratio of maintenance margin used as the liquidation threshold. A value of 1.0 liquidates when equity <= maintenance margin; higher values trigger earlier.

  • liquidation_cancel_open_orders (bool, default True): if open resting orders for the venue should be cancelled before synthetic close-out fills are emitted for open positions.

backtest_end

Returns the last backtest range end, in nanoseconds since the UNIX epoch.

backtest_start

Returns the last backtest range start, in nanoseconds since the UNIX epoch.

cache

Returns the cache shared with the kernel and registered components.

change_fill_model(venue, fill_model)

Changes the fill model for a venue.

clear_actors()

Clears all actors from the engine.

clear_data()

Clears all data from the engine.

clear_exec_algorithms()

Clears all execution algorithms from the engine.

clear_strategies()

Clears all strategies from the engine.

dispose()

Disposes of the engine, releasing all resources.

end()

Ends the backtest run, finalizing results.

generate_account_report(venue=None, account_id=None)

Generates an account report as a pandas DataFrame.

At least one of venue or account_id must be provided.

# Errors

Returns an error if neither venue nor account_id is provided, or if the Python ReportProvider import or call fails.

generate_fills_report()

Generates a fills report as a pandas DataFrame.

# Errors

Returns an error if the Python ReportProvider import or call fails.

generate_order_fills_report()

Generates an order fills report as a pandas DataFrame.

# Errors

Returns an error if the Python ReportProvider import or call fails.

generate_orders_report()

Generates an orders report as a pandas DataFrame.

# Errors

Returns an error if the Python ReportProvider import or call fails.

generate_positions_report()

Generates a positions report as a pandas DataFrame.

# Errors

Returns an error if the Python ReportProvider import or call fails.

get_result()

Returns the backtest result from the last run.

instance_id

Returns the unique instance ID for this engine.

iteration

Returns the current iteration count.

list_venues()

Returns the list of registered venue identifiers.

machine_id

Returns the machine ID for this engine.

portfolio

Returns the portfolio shared with the kernel and registered components.

reset()

Resets the engine state for a new run.

run(start=None, end=None, run_config_id=None, streaming=False)

Runs the backtest engine.

run_config_id

Returns the last run config ID, if any.

run_finished

Returns when the last run finished, in nanoseconds since the UNIX epoch.

run_id

Returns the last run ID, if any.

run_started

Returns when the last run started, in nanoseconds since the UNIX epoch.

sort_data()

Sorts the engine’s internal data stream by timestamp.

trader_id

Returns the trader ID for this engine.

class BacktestEngineConfig

Bases: object

Configuration for BacktestEngine instances.

bypass_logging
cache
controller
data_engine
delay_post_stop
exec_engine
instance_id
load_state
logging
msgbus
portfolio
risk_engine
run_analysis
save_state
shutdown_on_error
timeout_connection
timeout_disconnection
timeout_portfolio
timeout_reconciliation
timeout_shutdown
trader_id
class BacktestNode

Bases: object

Orchestrates catalog-driven backtests from run configurations.

BacktestNode connects the [ParquetDataCatalog] with [BacktestEngine] to load historical data and run backtests. Supports both oneshot and streaming modes.

add_actor_from_config(run_config_id, config)
add_builtin_strategy(run_config_id, type_name, config)

Adds a built-in example strategy to the engine for the given run config.

This method exists only to single-source bundled example strategy code across Rust and Python tests/examples. It is not a first-class extension path for adding native strategies.

add_strategy_from_config(run_config_id, config)
build()

Builds backtest engines from the run configurations.

For each config, creates a BacktestEngine, adds venues, and loads instruments from the catalog. If building a config fails with BacktestRunConfig.raise_exception disabled, logs the error and skips that config; successful return does not guarantee an engine for every config.

# Errors

Returns an error if building an engine from a config fails and BacktestRunConfig.raise_exception is enabled for that config.

configs

Returns the run configurations.

dispose()

Disposes all engines and releases resources.

generate_account_report(run_config_id, venue=None, account_id=None)

Generates an account report for the given run config engine.

At least one of venue or account_id must be provided.

# Errors

Returns an error if no engine exists, neither selector is provided, or report generation fails.

generate_fills_report(run_config_id)

Generates a fills report for the given run config engine.

# Errors

Returns an error if no engine exists or report generation fails.

generate_order_fills_report(run_config_id)

Generates an order fills report for the given run config engine.

# Errors

Returns an error if no engine exists or report generation fails.

generate_orders_report(run_config_id)

Generates an orders report for the given run config engine.

# Errors

Returns an error if no engine exists or report generation fails.

generate_positions_report(run_config_id)

Generates a positions report for the given run config engine.

# Errors

Returns an error if no engine exists or report generation fails.

get_engine_cache(run_config_id)

Returns the cache for the given run config engine.

# Errors

Returns an error if no engine exists for the run config ID.

get_engine_portfolio(run_config_id)

Returns the portfolio for the given run config engine.

# Errors

Returns an error if no engine exists for the run config ID.

run()

Runs all configured backtests and returns results.

Automatically calls build() if engines have not been created yet. For each run config, loads data from the catalog and runs the engine. Supports both oneshot (chunk_size = None) and streaming modes. Configs without a built engine are skipped. If a run fails with BacktestRunConfig.raise_exception disabled, logs the error, clears its loaded data, leaves the engine undisposed, and omits its result.

# Errors

Returns an error if building, data loading, or engine execution fails and BacktestRunConfig.raise_exception is enabled for the run config.

class BacktestResult

Bases: object

Results from a completed backtest run.

backtest_end
backtest_start
elapsed_time_secs
instance_id
iterations
machine_id
returns_series
run_config_id
run_finished
run_id
run_started
stats_general
stats_pnls
stats_returns
summary
total_events
total_orders
total_positions
trader_id
class BacktestRunConfig

Bases: object

Represents the configuration for one specific backtest run. This includes a backtest engine with its actors and strategies, with the external inputs of venues and data.

chunk_size
data
dispose_on_completion
end
engine
id
raise_exception
start
venues
class BacktestVenueConfig

Bases: object

Represents a venue configuration for one specific backtest engine.

account_type
allow_cash_borrowing
bar_adaptive_high_low_ordering
bar_execution
base_currency
book_type
default_leverage
fee_model
fill_model
frozen_account
latency_model
leverages
liquidation_cancel_open_orders
liquidation_enabled
liquidation_trigger_ratio
liquidity_consumption
margin_model
modules
name
oms_type
oto_trigger_mode
price_protection_points
queue_position
reject_stop_orders
routing
settlement_prices
starting_balances
support_contingent_orders
support_gtd_orders
trade_execution
use_market_order_acks
use_position_ids
use_random_ids
use_reduce_only
class FXRolloverInterestModule

Bases: object

Simulates FX rollover (swap) interest applied at 5 PM US/Eastern daily.

When holding FX positions overnight, the interest rate differential between the two currencies is credited or debited. Wednesday and Friday rollovers are tripled (Wednesday for T+2 settlement, Friday for the weekend).

class InterestRateRecord

Bases: object

A single interest rate data entry.