Backtest & Optimize
How partiqon tests strategies on historical data, finds optimal parameters, and validates robustness. Includes backtesting, optimization, walk-forward validation, and WFO.
Backtesting
A backtest simulates a strategy on historical data. For each bar, we check if entry or exit signals fire, and record all trades with their P&L. No real money is used — it's pure simulation to measure past performance.
How It Works
- Load data: Historical bars (OHLCV) from BAR6 files
- Reset state: Strategy parameters, indicators, and positions reset to start
- Iterate bars: For each bar in the period, feed it to the strategy
- Check signals: Does the strategy generate entry or exit signals?
- Execute trades: Entry opens a position; exit closes it. Record entry/exit price, P&L
- Calculate metrics: Win rate, drawdown, profit factor, etc.
- Output results: Trade list, equity curve, performance statistics
Key Metrics
- Total Return: Ending equity − starting capital (in %)
- Win Rate: Winning trades / total trades (higher is better, but not everything)
- Profit Factor: Gross profit / gross loss. >1.5 is respectable; >2.0 is strong
- Max Drawdown: Largest drop from peak equity to trough. Risk metric; lower is better
- Sharpe Ratio: Return per unit of risk. Adjusts for volatility; higher is better
- Trades per year: Frequency. More trades = more data to validate; fewer trades = less slippage/fees
PSO Optimization
PSO (Particle Swarm Optimization) is a search algorithm that finds good parameter values automatically. Instead of trying random combinations, particles "swarm" toward the best regions of the parameter space.
How PSO Works
- Define searchable parameters: Which block parameters can be tuned? (e.g., EMA period 10–100, RSI level 20–50)
- Initialize swarm: Create N particles, each with random parameter values (e.g., 30 particles)
- Evaluate each particle: Run a backtest with those parameters. Record profit factor (or your chosen metric)
- Track best: Each particle remembers its best result ever. The swarm remembers the global best.
- Update velocities: Each particle adjusts its parameters toward its personal best and the global best (with randomness)
- Repeat: Run multiple generations (e.g., 60 iterations). The swarm converges toward good parameters.
- Return champion: The best parameters found across all generations
Why PSO?
- Efficient: Fewer backtests than grid search (which tries every combination)
- Flexible: Handles any combination of parameters, discrete or continuous
- Robust: Avoids getting stuck in local optima better than pure gradient descent
- Transparent: You see which parameters the algorithm favored and why
Walk-Forward Validation
Walk-forward validation prevents overfitting by splitting data into blocks: optimize on the first block (in-sample), then test on the second block (out-of-sample), but never mix them.
The Problem: Overfitting
If you optimize on all your data, you can fit the noise in that specific history. The parameters look great on the past but fail on the future. Walk-forward breaks the data to catch this.
Walk-Forward Process
- Define blocks: Split your 5-year history into 3 blocks (e.g., each ~1.5 years)
- Block 1 (in-sample): Run PSO optimization on this period. Find best parameters.
- Block 2 (out-of-sample): Test those best parameters on the next block. How do they perform on unseen data?
- Block 3 (out-of-sample): Slide the window. Use Block 2 to optimize; test on Block 3.
Interpreting Results
- Good strategy: In-sample profit factor 2.0+, out-of-sample profit factor >1.0. Out-of-sample is lower, but still profitable and consistent.
- Overfitted: In-sample factor 5.0+, out-of-sample <1.0. The parameters don't generalize.
- Underfitted: Both in-sample and out-of-sample are poor. The strategy idea isn't strong enough.
Walk-Forward Optimization (WFO)
WFO combines walk-forward validation with optimization: optimize on each in-sample block, then test the result on the next out-of-sample block. This simulates real trading where you re-optimize periodically.
WFO Process
- Split data: 3 blocks (or more). Blocks 1, 2, 3 for example.
- Cycle 1: Optimize on Block 1 (in-sample) → test optimized params on Block 2 (out-of-sample)
- Cycle 2: Optimize on Block 2 (in-sample) → test optimized params on Block 3 (out-of-sample)
- Aggregate results: Combine all out-of-sample trades from cycles 1, 2, etc.
- Calculate metrics: Profit factor, drawdown, etc. on the full aggregated out-of-sample result
WFO Interpretation
- Strong signal: WFO profit factor >1.5, consistent across all cycles. Parameters generalize.
- Weak signal: WFO profit factor <1.0 or highly variable. Parameters are fragile.
- Quarterly breakdown: If 3 out of 4 out-of-sample periods are profitable, the strategy is robust. If only 1 out of 4, it's brittle.
Strategy Development Workflow
The recommended approach:
- Build: Design your strategy using blocks in the graph editor
- Test: Run a simple backtest with default parameters (2020–2026)
- Optimize: Run PSO on a clean in-sample block (e.g., 2020–2023)
- Validate: Backtest the optimized params on out-of-sample (2023–2026). Profit factor >1.0?
- Walk-Forward: Run WFO with 3 blocks. Profit factor >1.5 and consistent? Ready to trade.
- Paper trade: Demo the strategy on live data for 1–2 weeks before real money
- Redeploy: Every quarter, run WFO again on the latest data to re-optimize parameters
Common Pitfalls
- Using future data: Indicators or signals that "know" tomorrow's close. Always check for lookahead bias.
- Ignoring slippage: Backtests assume instant fills at OHLC prices. Real trading has slippage. Add 0.5–1 ATR as a buffer.
- Too many parameters: Optimizing 20 parameters on 3 years of data = overfitting. Keep parameters minimal.
- Wrong metric: Chasing max profit factor can hide negative skew. Use Sharpe ratio or profit factor with max drawdown together.
- In-sample only: Never optimize and test on the same period. Always validate out-of-sample.
Next Steps
Ready to build a strategy? Start in the graph editor on the /graph page. Or read the block reference to understand what's available.