Skip to main content

backtest

backtest(
    strategy,
    data,
    *,
    interval='1d',
    stamp='open',
    cash=100000000,
    commission=0,
    min_commission=0,
    sell_tax=0,
    slippage_ticks=0,
    scale=4,
    shorts=False,
    fill='next-open',
    periods_per_year=None,
)

Replays data through strategy and returns the result.

Parameters

Name Type Description Default
strategy object An object with on_bar(ctx, bar) or on_bars(ctx, bars) (not both), and optionally on_stop(ctx). A target set at a bar’s close fills at the next bar’s open (see fill). on_start is refused: the library’s replay never calls it, so set up in __init__. required
data mapping of str to frame Symbol to bars. Each value is a pandas or polars DataFrame, or a mapping of arrays, with open (optional), high, low, close, volume, and the time as a time column or a pandas DatetimeIndex. The time must be datetime64 (naive is read as UTC); integers are refused rather than guessed (pd.to_datetime(df.ts, unit="s")). required
interval str The bars’ interval, as the library names it (“1m”, “5m”, “15m”, “30m”, “1h”, “1d”, “1w”, “1mo”, “1y”). '1d'
stamp str “open” if a bar’s time is its period’s start, “close” if its end. 'open'
cash (int, str or Decimal) Starting cash, KRW. 100000000
commission (float, str or Decimal) Commission rate on every fill (0.00015 is 0.015 %). 0
min_commission (int, str or Decimal) Minimum commission per fill, KRW. 0
sell_tax (float, str or Decimal) Securities transaction tax rate on sales. 0
slippage_ticks int Price ticks against the order on each fill. 0
scale int Decimal places kept when prices and volumes are converted to exact decimals. 4
shorts bool Whether a target below zero may sell shares not held. Off by default: such a sale is refused whole as quantity_not_sellable, so a long position targeted below zero is not closed either. On, the sale fills like any other (taxed by sell_tax, its proceeds added to cash) and the position is valued at each bar’s close; no borrow, fee, margin or recall is modelled. Since the proceeds count as cash, ctx.cash() includes them, and leverage through shorts does not show in lowest_cash or overdrawn_bars. False
fill str The price a target’s orders fill at, on the next eligible bar of that symbol, stamped at its period end. “next-open” (the default) is that bar’s open. “decision-close” is the symbol’s last observed close when the target was set, before slippage: the convention of backtests that trade on the close. It is optimistic, since a real order cannot trade at a close already seen. After a gap in the symbol’s data that close can be days old; with no close seen yet, the order fills at the next bar’s open. “next-close” is that bar’s close: the price basis of backtests that fill on the next bar’s close, a price convention, not a simulation of a closing order, and a bar stamped at its close is taken at that instant. It prices bars of a day or less: an order that would fill on a weekly or longer bar halts the replay, with the reason in halted; receiving such bars alone does not. market_price_rule and market_fills_by_rule in the result name the rule and count its fills. 'next-open'
periods_per_year float The periods in a year that annualized_sharpe uses. None (the default) counts them from the equity curve; see BacktestResult.periods_per_year. Must be finite and above 0. None

Returns

Name Type Description
BacktestResult The fills, the equity curve and the summary figures.