✍
Oct 2024·12 min read
Building a Backtesting Engine in TypeScript
How I built a performant backtesting engine in TypeScript — from data loading and order simulation to performance metrics.
Backtesting is the foundation of any systematic trading strategy. I built one in TypeScript that runs entirely in the browser.
Data loading
OHLCV data from CSV. Use TypedArrays for memory efficiency — 1M candles at ~40MB. Pre-compute indicators on load to avoid recalculation during strategy runs.
Order simulation
Support market, limit, and stop orders with fill simulation based on high/low of the candle. Handle partial fills and slippage with configurable parameters.
Metrics
Sharpe ratio, max drawdown, win rate, profit factor. Include Monte Carlo simulation to estimate strategy robustness beyond single-run results.