#!/usr/bin/env bash
set -euo pipefail

SCRIPT_VERSION="watch_and_tune_oleg_weth_usdc_030_monthly_2026_05_05"
echo "[script_version] $0 SCRIPT_VERSION=${SCRIPT_VERSION}"

mkdir -p DEX_REPORTS/oleg_weth_usdc_030_tranche_tune

MONTHS=(feb2026 mar2026 apr2026)

while true; do
  all_done=1
  for TAG in "${MONTHS[@]}"; do
    NPZ="DEX_DATA/fast_npz/mainnet_WETH_USDC_UNIV3_030_${TAG}_fee_replay_v2.npz"
    OUT="DEX_REPORTS/oleg_weth_usdc_030_tranche_tune/${TAG}"
    DONE="${OUT}/.done"
    if [[ -s "${DONE}" ]]; then
      continue
    fi
    all_done=0
    if [[ ! -s "${NPZ}" ]]; then
      echo "[wait] ${NPZ}"
      continue
    fi
    echo "[tune] ${TAG} ${NPZ}"
    python3 dex_platform/backtest/cl_fee_replay_tranche_dca_v1.py \
      --npz "${NPZ}" \
      --out-dir "${OUT}" \
      --fee-rate 0.003 \
      --total-capital-grid 50,100,150,200,300,600 \
      --parts-grid 2,3,4,5,6 \
      --lower-grid 20,25,30,35,40,45,50 \
      --upper-grid 10,15,20,25,30 \
      --add-mode equal,double_once,progressive \
      --max-reentries-grid 1,2,3,4 \
      --min-reentry-hours-grid 6,12,24,72 \
      --anchor-mode current_price,rolling_low \
      --support-lookback-hours 6,12,24 \
      --support-offset-pct 0,2,5 \
      --target-mdd-pct 20 \
      --max-avg-liquidity-share-pct 3 \
      --max-p95-liquidity-share-pct 5 \
      --max-p99-liquidity-share-pct 10 \
      --top-n 100
    date -u > "${DONE}"
  done
  if [[ "${all_done}" == "1" ]]; then
    echo "[done] all monthly Oleg tranche tunes completed"
    break
  fi
  sleep "${WATCH_SLEEP_S:-300}"
done
