# MLP Strategy — M4 Mac Studio Handoff

Pick up here after `scp`-ing the projects folder. All implementation is done and
committed; only compute-heavy steps remain.

## First: set up the Python environment

```bash
cd ~/projects/TradingBot25
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

Verify GPU is available (MPS on M4):
```bash
python3 -c "import torch; print(torch.backends.mps.is_available())"
```

Run fast tests to confirm the environment is healthy:
```bash
python -m pytest tests/ -q --ignore=tests/test_acceptance_mlp.py
# Expected: 30 passed
```

---

## Step 1 — Paste Pine into TradingView (compile test)

Before training anything, settle the compiled-size question.

1. Open `strategies/strategy_mlp_scores.pine` in an editor
2. Select All → Copy
3. In TradingView: Pine Editor → New script → Paste → Save & Add to chart
4. If it **compiles**: proceed with the default 49→16→8→1 arch (941 params)
5. If it **fails with a token/size error**: all training runs below need `--hidden 8 4`
   (49→8→4→1, 441 params — regenerates faster than retraining from scratch)

---

## Step 2 — Train 6H (first real run)

```bash
python3 tools/train_mlp.py --data data/COINBASE_BTCUSD-6H.csv
# Default arch: 49→16→8→1  (~30-60 min on M4)
# If TV compile failed above: add --hidden 8 4
```

Artifact lands at: `strategies/params/mlp/mlp_weights_COINBASE_BTCUSD_6H.json`

Watch for ES collapsing to no-trade fitness (all zeros). If that happens:
- Check artifact `training.phase2.best_fitness` — should be a negative number (good Calmar)
- If fitness is 0.0 for all gens, raise `--l2 1e-2` or lower `--es-sigma 0.02`

---

## Step 3 — Inject 6H weights into Pine and re-compile

```bash
python3 tools/generate_pine_mlp_presets.py
# Rewrites the sentinel block in strategies/strategy_mlp_scores.pine
```

Paste the updated Pine into TradingView again and verify it compiles with real weights.

---

## Step 4 — Threshold sweep for 6H

```bash
python strategies/optimize_strategy.py \
  --data data/COINBASE_BTCUSD-6H.csv \
  --strategy_file strategy_mlp_scores.py \
  --params_file strategies/params/params_strategy_mlp_scores_COINBASE_BTCUSD_6H.json \
  --random_search 5000 --metric "Calmar Ratio"
```

Winner lands at: `results/winners/optimization_winner_strategy_mlp_scores_COINBASE_BTCUSD_6H.csv`

Quick sanity check:
```bash
python strategies/validate_strategy.py \
  --data data/COINBASE_BTCUSD-6H.csv \
  --strategy_file strategy_mlp_scores.py \
  --params_file results/winners/optimization_winner_strategy_mlp_scores_COINBASE_BTCUSD_6H.csv \
  --show-trades
```

---

## Step 5 — Train remaining TFs

```bash
python3 tools/train_mlp.py --data data/COINBASE_BTCUSD-4H.csv
python3 tools/train_mlp.py --data data/COINBASE_BTCUSD-8H.csv
python3 tools/train_mlp.py --data data/COINBASE_BTCUSD-12H.csv
python3 tools/train_mlp.py --data data/COINBASE_BTCUSD-1D.csv
# These can be run sequentially or in separate terminals
```

After all 5 artifacts exist:
```bash
python3 tools/generate_pine_mlp_presets.py
# Rewrites Pine with all 5 presets
```

Paste into TradingView one final time.

---

## Step 6 — TV/Python parity check

After re-exporting from TradingView with the MLP strategy active (mlp_score must
be plotted to appear in the CSV export):

```bash
python3 tools/check_mlp_parity.py \
  --data data/COINBASE_BTCUSD-6H.csv \
  --weights strategies/params/mlp/mlp_weights_COINBASE_BTCUSD_6H.json \
  --params results/winners/optimization_winner_strategy_mlp_scores_COINBASE_BTCUSD_6H.csv
```

Pass criteria: `max|Δ| < 0.01` AND `0 threshold-side disagreements`.
If it fails, see `docs/tv_parity_workflow.md` for the full diagnostic runbook.

After score parity passes, export the TradingView Strategy Tester trade list and
compare it against the Python MLP strategy:

```bash
python3 tools/compare_tv_trades.py \
  --strategy-file strategy_mlp_scores.py \
  --tv-trades data/MLPScores_COINBASE_BTCUSD_2026-06-17.csv \
  --data data/COINBASE_BTCUSD-6H.csv \
  --params results/winners/optimization_winner_strategy_mlp_scores_COINBASE_BTCUSD_6H.csv \
  --verbose --exit-detail
```

---

## Progress tracker

See `docs/mlp_strategy_plan.md` for the full checklist. Steps 1–11 are tracked
there; steps 1–8/10–11 are complete as of the M1 handoff.
