IbkrApi.Backtester.Example (ibkr_api v1.0.0)

View Source

Example usage of the IBKR backtesting framework.

This module demonstrates how to fetch historical data from IBKR and run backtests with different strategies.

Summary

Functions

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

compare_strategies(symbol, period, bar_size, strategies \\ [Strategies.MA], opts \\ [])

@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.

run_backtest(symbol, period, bar_size, strategy_module \\ Strategies.MA, opts \\ [])

@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, %{...}}

run_sample_backtest(opts \\ [])

@spec run_sample_backtest(keyword()) :: map()

Quick backtest example with sample data (for testing without API calls).

Generates synthetic price data and runs a backtest.