Designing a Python Research Backtester with Strategy, Portfolio, and Performance Components
Summary
The document introduces a research backtesting system built around three object-oriented components. A Strategy consumes single-instrument OHLCV bars and emits long, hold, or short signals. A Portfolio turns signals into positions, tracks cash and holdings, and calculates an equity curve and returns. A Performance component is planned to report risk, return, trade, and drawdown measures. Abstract interfaces let each component be extended or replaced as the research system develops.
The example uses pandas and assumes trades occur at bar open or close prices. It illustrates how positions, holdings, cash, total portfolio value, and returns fit together, with a random signal generator producing an example SPY run that loses money. The design prioritizes ease of research and flexibility over market realism. It handles one instrument, assumes unrestricted long and short trading, and omits margin constraints, liquidity limits, detailed execution, and sophisticated transaction costs. The document stresses that backtest results are estimates: data granularity, routing, latency, and fills can all make live performance differ. The example is an introductory framework, not evidence that any strategy is profitable.
Key ideas
- A research backtester can separate signal generation, portfolio accounting, and performance analysis into distinct components.
- A Strategy class maps OHLCV bars to long, hold, or short recommendations.
- A Portfolio class converts signals into positions and tracks holdings, cash, equity, and returns.
- Abstract interfaces support replacing or extending components as the research system evolves.
- Bar-price fills, unrestricted shorting, and omitted trading costs make the example unsuitable as a realistic execution model.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.