#!/bin/bash
# [2026-10-01] Nightly (cron 16:50 ET, Mon-Fri): add the finished trading day to every test
# data set and to the playbook library. Log: logs/nightly_<date>.log
D=$(TZ=America/New_York date +%F)
LOG=/var/www/screener/trade/logs/nightly_$D.log
{
  echo "== $D nightly start $(date)"
  cd /var/www/screener/trade/reports/open_window || exit 1
  nice -n 10 python3 add_day.py "$D"                     # bars, tick cache, candidate lists
  nice -n 10 python3 build_flows.py | tail -1            # buy/sell per minute
  cd /var/www/screener/trade/reports/playbook || exit 1
  nice -n 10 python3 analyze_trades.py "$D" --no-charts                  # trade bot cases
  [ -f "/var/www/screener/premarket/data/trades/${D}_trades.jsonl" ] && \
    nice -n 10 python3 analyze_trades.py "$D" --source sip --no-charts   # sip_bot cases (read only)
  nice -n 10 python3 label_days.py                       # best-trade marks, all days
  rm -f /var/www/screener/trade/data/playbook/library_paths.npz   # reference library rebuilds on next use
  echo "== done $(date)"
} >> "$LOG" 2>&1
