IbkrApi.Backtester.Example (ibkr_api v1.0.0)
View SourceExample usage of the IBKR backtesting framework.
This module demonstrates how to fetch historical data from IBKR and run backtests with different strategies.
Summary
Functions
Demonstrates multiple strategy comparison.
Print a formatted backtest report.
Complete example: Search for a contract, fetch historical data, and run backtest.
Quick backtest example with sample data (for testing without API calls).
Functions
@spec compare_strategies(String.t(), String.t(), String.t(), [module()], keyword()) :: {:ok, map()} | {:error, any()}
Demonstrates multiple strategy comparison.
Runs the same historical data through different strategies and compares results.
@spec print_report(map()) :: :ok
Print a formatted backtest report.
@spec run_backtest(String.t(), String.t(), String.t(), module(), keyword()) :: {:ok, map()} | {:error, any()}
Complete example: Search for a contract, fetch historical data, and run backtest.
Parameters
symbol
: Stock symbol to backtest (e.g., "AAPL")period
: Historical data period (e.g., "1mo", "3mo", "1y")bar_size
: Bar size (e.g., "1day", "1hour", "30min")strategy_module
: Strategy module to use (default: MA strategy)opts
: Additional options
Examples
# Run a simple moving average backtest on Apple stock
iex> IbkrApi.Backtester.Example.run_backtest("AAPL", "3mo", "1day")
{:ok, %{performance: %{return_percent: 12.5, ...}, ...}}
# Run with custom strategy options
iex> IbkrApi.Backtester.Example.run_backtest("MSFT", "6mo", "1hour",
...> IbkrApi.Backtester.Strategies.MA, strategy_opts: [window: 20])
{:ok, %{...}}
Quick backtest example with sample data (for testing without API calls).
Generates synthetic price data and runs a backtest.