Replay
Replay(
strategy,
symbols,
*,
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,
)One backtest that you feed yourself, in as many steps as you like, then finish once.
backtest() is a Replay fed once. Use a Replay when the sequence of bars is yours to define: a resampled path, a rolling window, bars generated as the run goes. The bars still run through the C++ engine one at a time; in our measurements, feeding a sequence in blocks took no longer than one backtest() call over the same bars.
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 |
| symbols | sequence of str | Every symbol the replay may receive. Their order breaks ties between bars of the same period. | 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 |
Attributes
| Name | Type | Description |
|---|---|---|
| symbols | tuple of str | The symbols the replay may receive, in the order that breaks ties. |
Notes
Every feed is checked whole before its first bar reaches the engine: each symbol’s bars must come later than its previous one, and no bar may end before the bar fed before it. A refused feed raises ValueError and changes nothing. An exception raised by the strategy ends the replay: later calls raise RuntimeError. A strategy handler may not feed or finish its own replay.
Methods
| Name | Description |
|---|---|
| feed | Feeds whole series, one per symbol, merged by time. |
| feed_bars | Feeds bars in the order given: the sequence you define. |
| finish | Ends the replay: calls on_stop, fills nothing further, and returns the result. |
feed
Replay.feed(data)Feeds whole series, one per symbol, merged by time.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| data | mapping of str to frame | Symbol to bars, as backtest() takes them: 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. A symbol’s rows may come in any order; two rows for the same period are refused. |
required |
feed_bars
Replay.feed_bars(symbol, time, *, high, low, close, volume, open=None)Feeds bars in the order given: the sequence you define.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| symbol | array of int or str | Each bar’s symbol: an index into symbols, or the symbol itself. |
required |
| time | array of datetime64 | Each bar’s time; naive is read as UTC, integers are refused. | required |
| high | array of float | Each bar’s prices and volume. | required |
| low | array of float | Each bar’s prices and volume. | required |
| close | array of float | Each bar’s prices and volume. | required |
| volume | array of float | Each bar’s prices and volume. | required |
| open | array of float | Each bar’s open; omitted or NaN where the source has none. | None |
finish
Replay.finish()Ends the replay: calls on_stop, fills nothing further, and returns the result.