---
name: mlp-phase2
description: Upgrade MLP Pine strategy from N-feature to M-feature architecture — updates static forward pass dimensions, verifies no arch mismatches, and regenerates presets.
---

Phase 2 upgrades the Pine static forward pass when all TFs have been retrained
with a new feature count. Do NOT run this until all 5 BTC TFs have winner CSVs
pointing to artifacts with the new arch.

## Pre-flight checks (run these first)

```bash
source .venv/bin/activate
# 1. Verify all winner artifacts share the same new arch
python3 tools/mlp_status.py --asset COINBASE_BTCUSD
# Must show consistent arch (e.g. 50→16→8→1) with NO mixed-arch warnings.
# If mixed: retrain remaining TFs first with run_mlp_train.py, re-sweep, promote.

# 2. Run check_pine.py — must exit 0 (no arch mismatch detected)
python3 tools/check_pine.py
```

## Phase 2 steps

When pre-flight passes, update the Pine static forward pass:

1. **Determine new feature count** from the winner artifacts (e.g. 50)
2. **Edit `strategies/strategy_mlp_scores.pine`** — update these 4 items in the static section (outside the sentinel block):
   - `var float[] _x = array.new_float(49, 0.0)` → `array.new_float(NEW_N, 0.0)`
   - `array.set(_x, 49, bb_pct_b_norm)` → add any new feature assignments up to index NEW_N-1
   - `for _j = 0 to 48` → `for _j = 0 to NEW_N-1`
   - `array.get(_w1, _i * 49 + _j)` → `array.get(_w1, _i * NEW_N + _j)`
   - `var float[] _w1 = array.new_float(784, 0.0)` → `array.new_float(NEW_N * H1, 0.0)` where H1 is the first hidden layer size
3. **Regenerate presets** (codegen auto-updates sentinel array sizes):
   ```bash
   python3 tools/generate_pine_mlp_presets.py
   # Must show NO "skipping" warnings
   ```
4. **Run regression table** to confirm no IS regressions:
   ```bash
   python3 tools/mlp_results_table.py
   ```
5. **Paste into TradingView**, confirm it compiles, then mark valid:
   ```bash
   python3 tools/check_pine.py --mark-valid
   ```
6. **Run parity check** with fresh TV export to verify max|Δ| < 0.01:
   ```bash
   python3 tools/check_mlp_parity.py \
     --data "data/mlp/COINBASE_BTCUSD, 240.csv" \
     --weights strategies/params/mlp/mlp_weights_COINBASE_BTCUSD_4H_<winner>.json \
     --params results/winners/optimization_winner_strategy_mlp_scores_COINBASE_BTCUSD_4H.csv
   ```

## Key invariants

- All 5 TFs must use same arch BEFORE Phase 2 — mixed arch = some TFs get zero weights in Pine
- The codegen (`generate_pine_mlp_presets.py`) auto-sizes `_w1` in the sentinel; only the static section needs manual updating
- After Phase 2, re-export ALL data CSVs from TV (the new feature must be in the export) and re-run parity check
