# Backtest Engine Spec

## Architecture

`StrategyTester` in `tester.py` runs strategies on historical data with realistic binary options mechanics.

## Trade model

- **Fixed amount:** $100 per trade
- **Payout:** 80% (WIN = +$80, LOSS = -$100)
- **Break-even win rate:** 55.6%

## Metrics computed

| Metric | Description |
|--------|-------------|
| `win_rate` | Wins / Total signals × 100% |
| `profit_factor` | Gross profit / Gross loss |
| `max_drawdown_pct` | Max peak-to-trough decline % |
| `total_pnl` | Net profit/loss |
| `max_consecutive_wins` | Longest win streak |
| `max_consecutive_losses` | Longest loss streak |
| `avg_confidence_win` | Mean confidence on winning trades |
| `avg_confidence_loss` | Mean confidence on losing trades |
| `equity_curve` | Balance at each trade |

## Win/Loss determination

```python
future_close = df['Close'].iloc[i + 1]       # next candle close
current_close = df['Close'].iloc[i]           # current candle close

if signal == 'CALL':
    win = future_close > current_close        # price went up
else:  # PUT
    win = future_close < current_close        # price went down
```

## Output format (JSON)

```json
{
  "BITCOIN_ema_rsi_trend": {
    "total_signals": 85,
    "wins": 48, "losses": 37,
    "win_rate": 56.5,
    "profit_factor": 1.04,
    "max_drawdown_pct": 12.3,
    "total_pnl": 340.0,
    "max_consecutive_wins": 6,
    "max_consecutive_losses": 4,
    "avg_confidence_win": 72.1,
    "avg_confidence_loss": 68.3,
    "equity_snapshot": [1000.0, 1080.0, ...]
  }
}
```

## Profitability filter

```python
win_rate >= 55% AND profit_factor >= 0.8 AND total_signals >= 50
```
