#!/usr/bin/env bash
# rolling_train_equities_set_seq.sh

# ─── Configuration ───────────────────────────────────────────────────────────────
cd "$(dirname "$0")"/..                 # move to repo root

N_SEEDS=1 #5                              # how many random seeds per year
MAX_CONCURRENT=5                 # cap on simultaneous Hydra jobs

# Use BASE_PATH from environment or set default if not defined
BASE_PATH=${BASE_PATH:-"./"}
# Remove trailing slash if present
BASE_PATH=${BASE_PATH%/}

# ─── Sliding-window loop ─────────────────────────────────────────────────────────
for YEAR in {1990..2013}; do
  TRAIN_BEG=$(printf "%04d0101" "${YEAR}")
  TRAIN_END=$(printf "%04d1231" $((YEAR + 7)))
  VAL_BEG=$(printf  "%04d0101" $((YEAR + 8)))
  VAL_END=$(printf  "%04d1231" $((YEAR + 8)))
  TEST_BEG=$(printf "%04d0101" $((YEAR + 9)))
  TEST_END=$(printf "%04d1231" $((YEAR + 9)))
  VAL_YEAR=$(printf "%04d"     $((YEAR + 8)))

  echo "==========================================="
  echo " Sliding Window Training for YEAR $YEAR"
  echo "   Train: ${TRAIN_BEG} – ${TRAIN_END}"
  echo "   Val:   ${VAL_BEG} – ${VAL_END}"
  echo "   Test:  ${TEST_BEG} – ${TEST_END}"
  echo "==========================================="

  # ── Inner loop over seeds ──────────────────────────────────────────────────────
  for SEED in $(seq 1 "$N_SEEDS"); do
    # throttle: keep ≤ $MAX_CONCURRENT jobs alive
    while [ "$(jobs -p | wc -l)" -ge "$MAX_CONCURRENT" ]; do
      sleep 2
    done

    python -m train experiment=equities/equities_set_seq.yaml \
      dataset.dataset_config.train_beg_date="${TRAIN_BEG}" \
      dataset.dataset_config.train_end_date="${TRAIN_END}" \
      dataset.dataset_config.val_beg_date="${VAL_BEG}" \
      dataset.dataset_config.val_end_date="${VAL_END}" \
      dataset.dataset_config.test_beg_date="${TEST_BEG}" \
      dataset.dataset_config.test_end_date="${TEST_END}" \
      ++dataset.save_data=True \
      ++dataset.load_data=False \
      ++dataset.data_path="${BASE_PATH}/data/equities/equity_dataset_${VAL_YEAR}.npz" \
      ++train.seed="${SEED}" \
      wandb.name="${VAL_BEG}_setseq_seed${SEED}" &

    sleep 5   # avoid hydra-timestamp collisions
  done
done

echo "All sliding-window training jobs have completed."

# chmod +x rolling_train_equities_set_seq.sh
# ./rolling_train_equities_set_seq.sh