IbkrApi.Backtester.Engine (ibkr_api v1.0.0)
View SourceBacktesting engine that runs strategies against historical data.
The engine processes historical bars sequentially, calling the strategy for each bar and executing the resulting trades on the portfolio.
Summary
Functions
Calculates key performance metrics for a backtest result.
Runs a backtest with the given bars and strategy.
Runs a backtest and returns detailed results with bar-by-bar portfolio values.
Functions
Calculates key performance metrics for a backtest result.
Parameters
result
: Result map from run/3 or run_detailed/3
Returns
Extended performance metrics including:
- Sharpe ratio (if portfolio_values available)
- Maximum drawdown
- Win rate
- Average trade return
@spec run([IbkrApi.Backtester.Bar.t()], module(), keyword()) :: map()
Runs a backtest with the given bars and strategy.
Parameters
bars
: List of historical bars (chronologically ordered, oldest first)strategy_module
: Module implementing the IbkrApi.Backtester.Strategy behaviouropts
: Optional configuration:starting_cash
- Initial portfolio cash (default: 100,000.0):strategy_opts
- Options passed to strategy.init/1 (default: [])
Returns
A map containing:
:portfolio
- Final portfolio state:performance
- Performance statistics:trade_history
- List of all executed trades:bars_processed
- Number of bars processed
Examples
iex> bars = [%IbkrApi.Backtester.Bar{close: 100.0, open: 99.0, high: 101.0, low: 98.0, volume: 1000, timestamp: DateTime.utc_now()}]
iex> result = IbkrApi.Backtester.Engine.run(bars, IbkrApi.Backtester.Strategies.MA)
iex> is_map(result)
true
@spec run_detailed([IbkrApi.Backtester.Bar.t()], module(), keyword()) :: map()
Runs a backtest and returns detailed results with bar-by-bar portfolio values.
This version tracks portfolio value at each bar for plotting and analysis.