Signal Backtester
Medium ● Live C++ Rust Java PythonThe Problem
Build a backtesting engine for trading signals across 8 assets over 2 years (504 trading days). Handle transaction costs, position sizing, and compute portfolio performance metrics: Sharpe ratio, maximum drawdown, and total P&L.
Test your ability to implement realistic position management, account for slippage and fees, and calculate risk-adjusted returns.
Evaluation Criteria
Correctness (70%)
Position sizing must respect constraints. P&L calculations accurate including transaction costs. Sharpe ratio and drawdown calculations correct to 4 decimal places.
Performance (30%)
Wall-clock time on 504 trading days with 8 assets. Efficient portfolio rebalancing and metric calculation.
Input Format
- First line
N= number of rows.- Each row
timestamp,price,signal- Signal
-1= sell,0= flat,1= buy.
Output Format & Sample
Sample input (first 8 lines, full file linked below):
30 1700000002,99.980984,-1 1700000007,99.956401,-1 1700000012,100.145651,-1 1700000017,100.263009,-1 1700000019,100.078771,-1 1700000021,100.193747,-1 1700000024,100.275232,-1 ... (more)
Expected output for that sample (first 8 lines):
-1.651294 0 0.000000 1.878034 -1.482249 -1.600342 5
↓ download full sample input | ↓ download expected output
Test your solution locally:
./your_solution < input.txt | diff - output.txt
Common Pitfalls
- Trade only on signal transitions. A run of +1 signals doesn't mean keep buying. Position changes when the signal value changes.
- Mark-to-market each row. Daily P&L is current_position * (price_today - price_yesterday).
- Sharpe and Sortino use the standard annualization. If your unit is per-row, scale by sqrt(252) for daily-equivalent.
- Drawdown is the max peak-to-trough on cumulative P&L, not on price.
- Output order matters. Total PnL, num_trades, win_rate, max_drawdown, Sharpe, Sortino, max_consecutive_wins, max_consecutive_losses (in that order, each on its own line, full precision).
Performance Tier Thresholds (applied by the grader)
| Language | Gold | Silver | Bronze | Tier 4 |
|---|---|---|---|---|
| C++ | <300ms | <1000ms | <5000ms | else PASS |
| Rust | <300ms | <1000ms | <5000ms | else PASS |
| Java | <600ms | <2000ms | <10000ms | else PASS |
| Python | <3000ms | <10000ms | <40000ms | else PASS |
Submission Rules
- Single-file solutions only. All code must be in one file.
- Your program must read from stdin and write to stdout.
- Java class must be named Solution.
- No network access, no filesystem access beyond stdin/stdout.
- Time limit: 120 seconds. Memory limit: 2 GB.