Backtesting Engine Development in JavaScript/TypeScript
A trading team uses Python but wants to integrate testing into a web app. Python scripts are hard to embed in the browser, and rewriting in C# takes too long. A TypeScript engine solves both problems: it runs both on the server (Node.js) and in the browser. Lost millions on paper due to unaccounted slippage is a common story. Our experience — 7+ projects in recent years — guarantees resistance to reentrancy and correct slippage handling. We use Decimal.js for precise calculations. Get a consultation to evaluate your project — we'll respond within a day.
Why Choose TypeScript for Backtesting?
TypeScript provides interfaces for candles and orders — the compiler catches errors before runtime. This is critical: one typo in the close field instead of close can cost millions on paper. We use ethers.js for blockchain interaction, but for historical data, pure Node.js suffices. The engine supports ERC-20 tokens and standard AMM pools. TypeScript also makes it easy to write unit tests and integrate with CI/CD.
What Pitfalls with JavaScript Numbers?
JavaScript uses IEEE 754 double precision. For normal backtests, 15 digits of precision are enough, but for calculations with fees and fractional lots, it's better to include Decimal.js. We include it in the config by default. Example:
import Decimal from 'decimal.js'; const pnl = new Decimal(exitPrice).minus(entryPrice).times(quantity).minus(commission); Parameter Optimization Example
We use grid search with parallel processing. The input is a parameter range, e.g., EMA period from 10 to 50 with step 5. Worker Threads on Node.js or Web Workers in the browser run tests in parallel. Results are sorted by Sharpe ratio. Typical speedup is 4-6x compared to sequential iteration.How We Build the Engine Architecture
The architecture is modular: DataFeed, Broker, Portfolio, Strategy. Each module can be replaced without modifying the others. The strategy inherits from an abstract class and implements the onBar method. For parallel testing we use workers:
- In the browser — Web Workers (MDN)
- On the server — Worker Threads from
node:worker_threads
Comparison with alternatives:
| Parameter | Our JS Engine | Python (backtrader) | C# (Wealth-Lab) |
|---|---|---|---|
| Run in browser | Yes (Web Worker) | No | No |
| Type safety | TypeScript | Optional (mypy) | Strong |
| Speed (1M candles) | ~1.2 sec | ~3.5 sec | ~0.8 sec |
| License | MIT | GPL | Commercial |
Common Backtesting Mistakes
- Look-ahead bias — using future data. Solution: strict timestamp control.
- Ignoring slippage. Solution:
slippagePctparameter (default 0.05%). - Not accounting for fees. Solution:
commissionPctin BrokerOptions. - Numerical overflow. Solution: Decimal.js for large amounts.
- Lack of testing on different timeframes — a strategy may work on 1-hour candles but fail on minute candles. Solution: multi-timeframe analysis.
- Ignoring liquidity — large order volume can increase slippage. We model order book depth via a volume limit parameter.
What's Included in Turnkey Work
- Architecture and interface design
- DataFeed implementation (CSV, REST API, WebSocket)
- Broker with market/limit/stop-limit orders
- Portfolio with equity curve and trades
- Indicator module (EMA, RSI, MACD, ATR)
- Parameter optimizer (grid/random)
- Integration with broker APIs (Binance, Bybit) on request
- Documentation and team training
Work Process
| Stage | Duration |
|---|---|
| Requirements analysis and specification | 2–3 days |
| Core prototype (DataFeed, Broker, Portfolio) | 5–7 days |
| Indicators and strategy | 3–5 days |
| Optimization and testing | 3–5 days |
| Integration and documentation | 2–3 days |
| Total | from 2 to 4 weeks |
Cost is calculated individually — depends on the complexity of broker logic and the need for external API integration. Typical time savings per strategy is up to 80% compared to manual testing, which significantly reduces the development budget. Get a consultation to evaluate your project. Our experience: many years of developing trading systems, we guarantee quality and deadlines. Order engine development for your tasks — guaranteed results.







