# Runnable examples

Start with the [first backtest](https://docs.openbacktesting.com/quickstart/index.md) for a complete copyable script. The examples below are included in a source checkout or source distribution. An installed wheel includes sample data but not this examples directory.

## Run a complete script

From the repository root, run a direct tutorial:

```sh
uv run --locked python examples/advanced/native_run.py
```

To run a decorated strategy, use the example runner to supply its data and settings:

```sh
uv run --locked python examples/advanced/run_author.py examples/strategies/ema_macd.py
```

[Example index](https://docs.openbacktesting.com/repository/examples/index.md) includes prerequisites and commands.

## Choose by task

| Example | What it demonstrates | How it runs |
| --- | --- | --- |
| [advanced/optimizer_results.py](https://docs.openbacktesting.com/repository/examples/advanced/optimizer_results.py/index.md) | Select inputs; save complete Portfolios and reopen for sorting | Run directly |
| [strategies/local_value_helpers.py](https://docs.openbacktesting.com/repository/examples/strategies/local_value_helpers.py/index.md) | Tick rounding and per-bar gradients | Use `run_author.py` |
| [strategies/local_builtin_indicators.py](https://docs.openbacktesting.com/repository/examples/strategies/local_builtin_indicators.py/index.md) | Conditional VWAP, mode, and five parameter extensions | Use `run_author.py` |
| [strategies/closed_trade_statistics.py](https://docs.openbacktesting.com/repository/examples/strategies/closed_trade_statistics.py/index.md) | Partial exits with fees and frozen statistics | Use `run_author.py` |
| [strategies/strategy_logs.py](https://docs.openbacktesting.com/repository/examples/strategies/strategy_logs.py/index.md) | Three log levels, nonfatal error logs, explicit exceptions, and end records | Use `run_author.py` |
| [advanced/strategy_logs.py](https://docs.openbacktesting.com/repository/examples/advanced/strategy_logs.py/index.md) | Timestamps, bars, source lines, failure retrieval, reruns, and host sessions | Run directly |
| [strategies/bar_states.py](https://docs.openbacktesting.com/repository/examples/strategies/bar_states.py/index.md) | First-bar callbacks, boundary states, positions, labels, and local validity guards | Use `run_author.py` |
| [strategies/custom_indicators.py](https://docs.openbacktesting.com/repository/examples/strategies/custom_indicators.py/index.md) | Custom functions, two parameter sets, named outputs, and reusable composition | Use `run_author.py` |
| [strategies/plotting.py](https://docs.openbacktesting.com/repository/examples/strategies/plotting.py/index.md) | Color Input, Color/strings, transparency, panes, and per-bar coloring | Use `run_author.py` |
| [advanced/portfolio_report.py](https://docs.openbacktesting.com/repository/examples/advanced/portfolio_report.py/index.md) | All report sections, partial-exit identities, MFE/MAE, signals, and frozen JSON | Run directly |
| [advanced/plotting_export.py](https://docs.openbacktesting.com/repository/examples/advanced/plotting_export.py/index.md) | Color overrides, configuration replay, frozen snapshots, strict JSON, signals, and fills | Run directly |
| [strategies/trading_lifecycle.py](https://docs.openbacktesting.com/repository/examples/strategies/trading_lifecycle.py/index.md) | Order events, rejection, partial fees, accounts, and end-of-run policies | Use `run_author.py` |
| [strategies/fractional_sizing.py](https://docs.openbacktesting.com/repository/examples/strategies/fractional_sizing.py/index.md) | Three sizing modes, default overrides, frozen snapshots, named targets, and reduce-only | Use `run_author.py` |
| [strategies/managed_orders.py](https://docs.openbacktesting.com/repository/examples/strategies/managed_orders.py/index.md) | Expiry, mixed-size OrderRequest OCO, protection, and trailing stops | Use `run_author.py` |
| [strategies/first_backtest.py](https://docs.openbacktesting.com/repository/examples/strategies/first_backtest.py/index.md) | First backtest: EMA registration, stepwise decisions, and decorator declarations | Use `run_author.py` |
| [advanced/strategy_settings.py](https://docs.openbacktesting.com/repository/examples/advanced/strategy_settings.py/index.md) | Five enum categories, flat overrides, JSON versions, and interest recalculation | Run directly |
| [advanced/chart_context.py](https://docs.openbacktesting.com/repository/examples/advanced/chart_context.py/index.md) | Instrument/timeframe injection, chart-local hours, statistical day boundaries, and DST repeated hours | Run directly |
| [advanced/run_author.py](https://docs.openbacktesting.com/repository/examples/advanced/run_author.py/index.md) | Host loads author scripts and reads results | Run directly |
| [advanced/indicator_reference.py](https://docs.openbacktesting.com/repository/examples/advanced/indicator_reference.py/index.md) | Explore by indicator ID and check each channel for validity | Run directly |
| [strategies/ema_macd.py](https://docs.openbacktesting.com/repository/examples/strategies/ema_macd.py/index.md) | Preferred template: inputs, EMA, MACD; understand init/next | Use `run_author.py` |
| [strategies/data_sources.py](https://docs.openbacktesting.com/repository/examples/strategies/data_sources.py/index.md) | Eight data sources, saved handles, and derived prices; read the preferred template first | Use `run_author.py` |
| [strategies/cross_signals.py](https://docs.openbacktesting.com/repository/examples/strategies/cross_signals.py/index.md) | Crossover composition and current is_* booleans; understand init/next | Use `run_author.py` |
| [strategies/confirmed_events.py](https://docs.openbacktesting.com/repository/examples/strategies/confirmed_events.py/index.md) | Composition, confirmed events, patterns, and monthly anchors; understand validity/time alignment | Use `run_author.py` |
| [strategies/ema_inputs.py](https://docs.openbacktesting.com/repository/examples/strategies/ema_inputs.py/index.md) | Built-in EMA and inputs; check indicator validity | Use `run_author.py` |
| [strategies/all_inputs.py](https://docs.openbacktesting.com/repository/examples/strategies/all_inputs.py/index.md) | All nine inputs, enum/color runtime values, and time/session boundaries | Use `run_author.py` |
| [advanced/native_run.py](https://docs.openbacktesting.com/repository/examples/advanced/native_run.py/index.md) | Native runs, statistics/equity/trade tables; direct Python script | Run directly |
| [advanced/composable_strategies.py](https://docs.openbacktesting.com/repository/examples/advanced/composable_strategies.py/index.md) | Signal and trailing-stop base classes; understand super call order | Run directly |
| [advanced/multiple_time_frames.py](https://docs.openbacktesting.com/repository/examples/advanced/multiple_time_frames.py/index.md) | Built-in daily/weekly RSI; understand resampling, completed periods, and calendar assumptions | Run directly |
| [advanced/parameter_optimization.py](https://docs.openbacktesting.com/repository/examples/advanced/parameter_optimization.py/index.md) | Four grid combinations and parameter scores; understand native parameters | Run directly |
| [advanced/machine_learning.py](https://docs.openbacktesting.com/repository/examples/advanced/machine_learning.py/index.md) | Feature columns, one-time/rolling training; requires ML and time-series evaluation knowledge | Run directly |

Optimization requires the `optimize` extra; the ML tutorial requires scikit-learn and can take substantially longer. The indicator pages also contain self-contained samples requiring only the installed package.
