Skip to main content

Defining the replay sequence

backtest() replays history as it happened. To test a strategy on other sequences (windows of the history, or paths resampled from it), feed a Replay yourself. 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. Parallel runs belong in separate processes.

import numpy as np
import pandas as pd
import pyatl

rng = np.random.default_rng(11)
days = pd.bdate_range("2023-01-02", periods=500)
symbols = ["005930", "000660", "035420"]
returns = rng.normal(0, 0.012, (len(days), len(symbols)))
closes = np.round(30_000 * np.exp(np.cumsum(returns, axis=0)), -1)
data = {}
for i, s in enumerate(symbols):
    c = closes[:, i]
    data[s] = pd.DataFrame(
        {"open": c, "high": c, "low": c, "close": c, "volume": 1e6}, index=days
    )


class Reversal:
    """Each day, holds 1,000,000 KRW of the symbol that fell most."""

    def __init__(self):
        self.prev, self.held = {}, None

    def on_bars(self, ctx, bars):
        moves = []
        for bar in bars:
            if bar.symbol in self.prev:
                change = bar.close / self.prev[bar.symbol] - 1
                moves.append((change, bar.symbol, bar.close))
            self.prev[bar.symbol] = bar.close
        if not moves:
            return
        _, pick, price = min(moves)
        if self.held and self.held != pick:
            ctx.target(self.held, 0)
        ctx.target(pick, int(1_000_000 // price))
        self.held = pick

Rolling windows

Each window is its own run: a new strategy and a new Replay (one replay is one run and has no reset).

rows = []
for start in range(0, 500 - 120 + 1, 60):
    window = {s: f.iloc[start:start + 120] for s, f in data.items()}
    result = pyatl.backtest(Reversal(), window)
    rows.append((window[symbols[0]].index[0].date(), result.total_return, result.sharpe))
pd.DataFrame(rows, columns=["start", "total_return", "sharpe"])
start total_return sharpe
0 2023-01-02 0.000687 0.056063
1 2023-03-27 0.000475 0.033717
2 2023-06-19 -0.000925 -0.067102
3 2023-09-11 -0.000768 -0.054269
4 2023-12-04 -0.000190 -0.013216
5 2024-02-26 -0.000977 -0.067560
6 2024-05-20 -0.000479 -0.031506

A block bootstrap of the history

Resampling draws blocks of days and joins them into a new path. Two rules keep the path valid:

  • Draw days, not symbols, so every symbol’s bars in a block come from the same days and the cross-section keeps its correlation. The helper below draws positions, so every frame must share the same dates in the same order (align real data to a common date index first); it checks this.
  • Give the path its own timeline. Drawn blocks come out of order and may repeat, while a replay needs time to move forward; the path’s bars are placed on new consecutive business days, and gaps and calendars then follow that timeline.

Prices are rebuilt from the drawn daily returns, so blocks join without jumps.

def bootstrap_path(data, block, rng):
    days = next(iter(data.values())).index
    assert all(f.index.equals(days) for f in data.values()), "align every frame to the same dates"
    n = len(days)
    ratio = {s: (f["close"] / f["close"].shift(1)).to_numpy()[1:] for s, f in data.items()}
    # n prices give n - 1 daily ratios; a block may start at any ratio from 0 to n - 1 - block.
    starts = rng.integers(0, n - block, size=(n - 1) // block)
    picked = np.concatenate([np.arange(a, a + block) for a in starts])
    timeline = pd.bdate_range("2030-01-01", periods=len(picked) + 1)
    path = {}
    for s, f in data.items():
        close = f["close"].iloc[0] * np.concatenate([[1.0], np.cumprod(ratio[s][picked])])
        close = np.round(close, -1)
        path[s] = pd.DataFrame(
            {"open": close, "high": close, "low": close, "close": close, "volume": 1e6},
            index=timeline,
        )
    return path


rng = np.random.default_rng(3)
sharpes = [
    pyatl.backtest(Reversal(), bootstrap_path(data, 20, rng)).sharpe for _ in range(50)
]
low, mid, high = np.quantile(sharpes, [0.05, 0.5, 0.95])
print(f"Sharpe over 50 paths: median {mid:.3f}, 5% {low:.3f}, 95% {high:.3f}")
Sharpe over 50 paths: median -0.004, 5% -0.090, 95% 0.073

Feeding a generated sequence in blocks

When a path is generated as the run goes, feed_bars takes each block as columns, in the order you want them replayed.

replay = pyatl.Replay(Reversal(), symbols)
path = bootstrap_path(data, 20, np.random.default_rng(5))
table = pd.concat(path, names=["symbol", "time"]).reset_index()
table["order"] = table["symbol"].map(symbols.index)  # ties follow the replay's symbols
table = table.sort_values(["time", "order"])
for _, block in table.groupby(table["time"].dt.to_period("M")):
    replay.feed_bars(
        block["symbol"].to_numpy(), block["time"].to_numpy(),
        open=block["open"], high=block["high"], low=block["low"],
        close=block["close"], volume=block["volume"],
    )
print(replay.finish().closing_equity)
99760150