Portfolio & SIP Backtesting
Relevant source files
The following files were used as context for generating this wiki page:
The Portfolio and SIP (Systematic Investment Plan) Backtesting modules provide users with robust tools to simulate investment strategies using historical market data. These modules allow for the evaluation of multi-asset portfolios and recurring investment plans against various benchmarks and cost models, helping traders validate ideas before live execution.
The system supports two primary backtesting surfaces: the Portfolio Backtester for analyzing static or rebalanced allocations of multiple symbols, and the SIP Backtester for evaluating the performance of recurring investments over time. Both modules leverage identical statutory cost models (e.g., Indian Equity) and can source data from either a broker API or a local "Historify" data store.
Sources: README.md, frontend/src/pages/PortfolioBacktesterResults.tsx:162-166, frontend/src/pages/SipBacktester.tsx:114-118
Portfolio Backtesting Architecture
The Portfolio Backtester analyzes how a group of assets would have performed together. It computes a wide range of performance statistics, risk metrics, and structural analyses such as co-movement clusters and asset attribution.
Data Flow and Result Generation
A single API call to /portfolio/backtest returns the entire simulation result. This design ensures consistency across different views (tabs) in the frontend, preventing discrepancies that might occur if metrics were fetched individually.
Core Metrics and Analytics
The engine returns detailed metrics categorized into several groups:
| Category | Key Metrics |
|---|---|
| Performance | CAGR, Total Return, Win Rate, Best/Worst Day, Alpha, Beta |
| Risk | Max Drawdown, Sharpe Ratio, Sortino Ratio, Calmar Ratio, Ulcer Index |
| Statistical | Skew, Kurtosis, Value at Risk (VaR), Conditional VaR (CVaR), Tail Ratio |
| Attribution | Selection Effect, Allocation Effect, Cost Effect, Excess Return vs Benchmark |
Sources: frontend/src/api/portfolio.ts:64-87, frontend/src/api/portfolio.ts:251-271, frontend/src/pages/PortfolioBacktesterResults.tsx:210-235
Structural and Robustness Analysis
Beyond simple returns, the system evaluates the internal structure of the portfolio:
- Co-movement Clusters: Groups holdings that are correlated above a specific threshold (e.g., 0.7) to identify "effective bets".
- Monte Carlo Simulation: Uses block-bootstrap resampling to determine how much of the outcome was due to sequence luck.
- Walk-Forward Analysis: Re-runs the same allocation over rolling sub-windows to test consistency.
Sources: frontend/src/api/portfolio.ts:223-231, frontend/src/api/portfolio.ts:208-221, frontend/src/pages/PortfolioBacktesterResults.tsx:758-778
SIP Backtesting System
The SIP Backtester focuses on the power of compounding and dollar-cost averaging. It simulates periodic installments into a single instrument and compares the result against a benchmark or a lumpsum investment.
Installation and Schedule Logic
The SIP engine calculates the timing of each installment based on frequency (weekly, fortnightly, monthly, quarterly). If a scheduled installment falls on a holiday or weekend, the system rolls the execution forward to the next available trading session.
Systematic Investment Metrics
Key performance indicators for SIPs include:
- XIRR: The internal rate of return, adjusted for recurring cash flows.
- Cost Advantage: Percentage gain/loss compared to the average price of the asset over the period.
- Underwater Analysis: Percentage of time the portfolio value was below the total invested capital.
- Crisis Performance: Comparisons of starting a SIP during historical market crashes versus waiting for the recovery.
Sources: frontend/src/pages/SipBacktesterResults.tsx:84-106, frontend/src/pages/SipBacktesterResults.tsx:142-155
Cost Modeling and Statutory Charges
Both backtesters share a common statutory charge schedule for Indian Equity markets. This ensures that the simulated returns account for realistic "cost drag".
Itemized Charges
The cost model itemizes the following deductions:
- Brokerage: Can be configured as a flat fee or percentage rate with a cap.
- STT (Securities Transaction Tax): Calculated as a percentage of the trade value.
- Exchange Transaction Charges: Variable based on NSE or BSE.
- GST: Consumption tax levied on taxable charges.
- SEBI Charges: Calculated per crore of turnover.
- Stamp Duty: State-level tax on transactions.
- Slippage: Estimated execution overhead.
Sources: frontend/src/api/portfolio.ts:133-146, frontend/src/pages/SipBacktester.tsx:102-113, frontend/src/pages/PortfolioBacktesterResults.tsx:173-195
Backtesting Configuration Options
Users can customize the simulation parameters through the frontend interface, which maps to the following API structure:
| Option | Type | Description |
|---|---|---|
source | PriceSource | Either db (Local Historify) or api (Broker API). |
rebalance | RebalanceRule | Options: never, monthly, quarterly, yearly. |
drift_band | number | Threshold for rebalancing triggered by weight deviation. |
initial_capital | number | Starting base currency (default for Portfolio: ₹1,00,00,000). |
step_up_percent | number | Yearly percentage increase in SIP installment amount. |
benchmark | string | Index symbol (e.g., NIFTY) to compare performance against. |
Sources: frontend/src/api/portfolio.ts:12-29, frontend/src/pages/SipBacktester.tsx:90-100, frontend/src/pages/PortfolioBacktesterResults.tsx:321-325
Conclusion
Portfolio & SIP Backtesting in OpenAlgo provides a high-fidelity simulation environment. By integrating realistic cost models, multiple rebalancing rules, and detailed risk analysis (including Monte Carlo and Walk-Forward tests), it offers a comprehensive view of strategy viability. The use of the XIRR metric for SIPs and deep attribution for portfolios ensures that users receive mathematically sound performance data.
Sources: frontend/src/pages/SipBacktesterResults.tsx:13-18, frontend/src/api/portfolio.ts:333-345