#!/bin/bash
# [2026-10-02] tonight's tests. Stops by 9:10 (Monday's open is far away; weekend is free).
cd /var/www/screener/trade
L=/var/www/screener/trade/reports/tests_1002.log
T=/var/www/screener/trade/reports/open_window/tick20
S=/var/www/screener/trade/data/simulations
END=$(date -d "next monday 09:10" +%s)
left() { echo $(( END - $(date +%s) )); }
# 1. Star Follow, latest version, on 6 untested months
cd reports/playbook
for mm in 80 10; do
  timeout $(left) nice -n 5 python3 sf3_batch.py 2026-03-02..2026-08-26 $mm --v2 > sf3_6mo_m$mm.log 2>&1
  echo "SF3 6 months, warning $mm: $(grep '^[0-9]* days' sf3_6mo_m$mm.log)" >> $L
done
cd /var/www/screener/trade
# 2. exit variants on v2's fixed entries (24 days), live exits = smooth + X1 (with the re-check fix)
BASE="--max-positions 99 --entry-v2 --exit-v2 --xv2-set trail=false --xv2-set climax_exit=true"
for v in base arm07 vwap both; do
  case $v in base) X="";; arm07) X="--xv2-set smooth_arm=0.7";; vwap) X="--xv2-set smooth_need_below_vwap=true";;
             both) X="--xv2-set smooth_arm=0.7 --xv2-set smooth_need_below_vwap=true";; esac
  for d in $(ls $T/cands/ | sed 's/.json//' | awk '$1>="2026-08-27" && $1<="2026-09-30"' | sort); do
    if [ $d = 2026-09-30 ]; then F=$S/live_2026-09-30_fixed.json; C=/tmp/claude-0/-var-www-screener-trade/5844eef7-c648-46de-a03c-fe0b1434b640/scratchpad/cands_0930_live.json
    else F=$S/${d}_sim_maxpos99_gr_r25_es0p_rs45_reabove_entry_v2_cf.json; C=$T/cands/$d.json; fi
    G=""; [ $d = 2026-09-22 ] && G="--synthetic-grid"
    [ $(left) -lt 60 ] && exit 0
    timeout $(left) nice -n 5 python3 simulate.py --date $d $BASE $X $G --fixed-entries $F --candidates-file $C > $T/out/exit1002_${v}_$d.out 2>&1
    echo "exit $v $d $(grep 'total:' $T/out/exit1002_${v}_$d.out)" >> $L
  done
done
# 3. entries: v2 with the live config (3% rule, live exits) vs + B1, every signal, 24 days
BASE="--max-positions 99 --entry-v2 --exit-v2 --xv2-set trail=false --xv2-set climax_exit=true --reentry-after-loss above_entry"
for v in live b1; do
  X=""; [ $v = b1 ] && X="--v2-set block_climax_volx=10.0"
  for d in $(ls $T/cands/ | sed 's/.json//' | awk '$1>="2026-08-27" && $1<="2026-09-30"' | sort); do
    G=""; [ $d = 2026-09-22 ] && G="--synthetic-grid"
    [ $(left) -lt 60 ] && exit 0
    timeout $(left) nice -n 5 python3 simulate.py --date $d $BASE $X $G --candidates-file $T/cands/$d.json > $T/out/entry1002_${v}_$d.out 2>&1
    echo "entry $v $d $(grep 'total:' $T/out/entry1002_${v}_$d.out)" >> $L
  done
done
echo TESTS_1002_DONE >> $L
