Skip to content

Configuration

Every setting has a default, so a config file only needs what it changes:

# my.yaml
simulation:
  symbols: [SPY, TLT]
  data_source: yfinance
  start: "2015-01-01"
  end: "2025-01-01"
strategy:
  type: momentum_rotation
trading --config my.yaml                  # options on the command line override the file
trading --config my.yaml --strategy rsi

In Python, start from the defaults or a file and override with dotted keys:

from trading import BacktestConfig

config = BacktestConfig.from_yaml("my.yaml").with_overrides({"risk.max_drawdown_pct": 15})

Validation. Every value is checked when the config is built, with the key in the message. Unknown keys are refused with a suggestion (unknown config key 'strategy.fast_perod' (did you mean 'fast_period'?)), so a typo cannot silently fall back to a default. Settings that contradict each other are refused too, such as a pairs strategy without exactly two symbols or without order_manager.allow_short.

Sweeps vary any key below except those in simulation, persistence and visualization, which every run of a sweep shares.

Environment variables. Secrets never go in config files:

Variable Used by
APCA_API_KEY_ID, APCA_API_SECRET_KEY trading paper: Alpaca paper-trading keys
TRADING_ALERT_WEBHOOK Paper trading and trading compare --alert: a Slack-compatible webhook
TRADING_LOG_FORMAT The default of --log-format: text or json

Reference

Generated from config/default.yaml, whose values a test holds equal to the built-in defaults. null turns a setting off.

simulation

Key Default Description
simulation.symbols [AAPL] one or more tickers, traded as one portfolio
simulation.data_source synthetic synthetic | csv | yfinance
simulation.seed 42 synthetic prices and the random strategy

synthetic: geometric Brownian motion, one bar per trading day

Key Default Description
simulation.n_bars 252 bars per symbol
simulation.start_price 150.0 every symbol starts here
simulation.annual_drift 0.08 expected annual return
simulation.annual_volatility 0.25 annualised standard deviation of returns
simulation.correlation 0.0 shared market factor across symbols, in [0, 1)
simulation.calendar nyse nyse (holidays, early closes) | weekdays (every weekday)

yfinance (pip install 'event-trading-system[data]')

Key Default Description
simulation.start "2024-01-01" first day
simulation.end "2024-12-31" exclusive
simulation.interval 1d 1d | 1wk | 1mo | 1h | ...
simulation.cache_dir null keep downloads here, with checksums, and reuse them offline

csv: columns date,open,high,low,close,volume in ascending date order. With several symbols use a template, e.g. data/{symbol}.csv

Key Default Description
simulation.csv_path null a file, or a template for several symbols

strategy

Key Default Description
strategy.type ma_crossover ma_crossover | rsi | random | buy_and_hold | momentum_rotation | pairs
strategy.fast_period 5 ma_crossover
strategy.slow_period 20 ma_crossover
strategy.period 14 rsi (Wilder smoothing)
strategy.overbought 70.0 rsi
strategy.oversold 30.0 rsi
strategy.lookback 60 momentum_rotation, pairs
strategy.top_k 1 momentum_rotation: symbols held
strategy.rebalance_every 21 momentum_rotation: bars between rebalances
strategy.entry_z 2.0 pairs: spread z-score that opens a trade
strategy.exit_z 0.5 pairs: z-score that closes it

order_manager

Key Default Description
order_manager.allow_short false false: SELL exits to flat; true: SELL goes short
order_manager.sizing fixed fixed | percent | atr
order_manager.quantity 100 fixed: shares per position
order_manager.percent_equity 10.0 percent: position value as % of equity
order_manager.risk_pct 1.0 atr: % of equity lost if price moves atr_multiple ATRs
order_manager.atr_period 14 atr
order_manager.atr_multiple 2.0 atr
order_manager.order_type market market | limit (for orders that add exposure)
order_manager.limit_offset_bps 0.0 limit: price this far better than the last close
order_manager.time_in_force day day | gtc (limit orders)

execution

Key Default Description
execution.slippage_bps 1.0 adverse: buys pay more, sells receive less
execution.commission_per_share 0.01 dollars a share
execution.commission_min 0.0 minimum commission per fill
execution.max_volume_pct null cap fills at this % of each bar's volume
execution.spread_bps 0.0 quoted bid-ask spread: each fill pays half of it
execution.impact 0.0 square-root impact: impact * sigma * sqrt(shares / ADV); ~1 is typical
execution.volume_window 20 earlier bars that sigma and ADV are measured over

risk

Note: null disables a limit.

Key Default Description
risk.max_position 500 max absolute shares per symbol
risk.max_position_pct null max position value per symbol, % of equity
risk.max_gross_leverage 2.0 max gross exposure / equity
risk.max_drawdown_pct 10.0 block new exposure beyond this drawdown from peak
risk.max_daily_loss_pct null ...or beyond this loss since the previous day's close

exits

Note: protective orders kept on every open position.

Key Default Description
exits.stop_loss_pct null stop this % beyond the entry price
exits.stop_loss_atr null ...or this many ATRs beyond it
exits.take_profit_pct null take profit this % in the position's favour
exits.atr_period 14 bars for stop_loss_atr

portfolio

Key Default Description
portfolio.starting_cash 100000.0 dollars
portfolio.margin_multiplier 2.0 buying power / equity: 1 cash account, 2 Reg T margin; null: off

Annual rates in percent, accrued on calendar days (actual/360)

Key Default Description
portfolio.interest_rate_pct 0.0 paid on cash
portfolio.margin_rate_pct 0.0 charged on borrowed cash (a negative balance)
portfolio.borrow_rate_pct 0.0 charged on the value of short positions

metrics

Key Default Description
metrics.periods_per_year null null = infer (252 for daily, 52 weekly, ...)
metrics.risk_free_rate 0.0 annual, for Sharpe and Sortino
metrics.bootstrap 0 resamples for Sharpe and return confidence intervals; 0: off
metrics.confidence 0.95 of each interval
metrics.bootstrap_block null mean block length in bars; null: bars ** (1/3)

persistence

Key Default Description
persistence.enabled true store every run in SQLite
persistence.db_path trading.db the SQLite file

visualization

Key Default Description
visualization.enabled false draw the chart after a backtest
visualization.save_path equity_curve.png null opens an interactive window instead

paper

Note: trading paper (backtests ignore this section).

Key Default Description
paper.timeframe 1Day 1Min | 5Min | 15Min | 30Min | 1Hour | 1Day
paper.warmup_bars 100 completed bars replayed to warm up indicators
paper.poll_seconds null null = a quarter of a bar, between 5 and 60 s
paper.settle_seconds 5.0 wait after a bar ends before treating it as complete
paper.feed iex Alpaca market data: iex (free) | sip (subscription)
paper.resume false continue the latest session on the same symbols (--resume)
paper.max_orders_per_day 200 halt once this many orders went to the broker in a day
paper.kill_switch null a file path: while that file exists, the trader halts
paper.heartbeat_seconds 300 log the trader's status this often (null = never)
paper.heartbeat_file null a file path, rewritten with the status at each heartbeat
paper.alert_on [fill, error, session, report] alerts sent to $TRADING_ALERT_WEBHOOK, if set