#!/bin/bash
# High-risk options sleeve intraday monitor (added 2026-08-31). Cron: every 5
# min during TSX hours, same window as fetch_prices.py. Cheap, mechanical,
# no LLM cost (sleeve_monitor.py is pure Python + tmx_quotes -- no Claude
# invocation) unless a candidate actually crosses the momentum+acceleration
# thresholds, in which case ONE backgrounded `claude -p` call per newly
# flagged ticker runs sleeve_reconfirm_prompt.md to pull real Questrade
# volume/RSI/MACD data and decide whether to alert Neil with a scored idea.
#
# ALERT-ONLY -- see sleeve_reconfirm_prompt.md's header. Nothing in this
# pipeline places an order. Neil reviews each ntfy alert and executes
# manually in the Questrade app within minutes if he agrees.
#
# Separate from check_positions.sh/check_positions.py on purpose -- this
# monitors a different watchlist (sleeve_watchlist.json's liquid-options
# large caps, not open positions or the daily scan's watchlist) for a
# different purpose (high-risk sleeve idea generation, not stop/target
# checks on existing positions).

REPO=/media/raid/rshare/SwingTrader
cd "$REPO" || exit 1

if ! /usr/bin/python3 "$REPO/is_trading_day.py"; then
    exit 0
fi

ALERTS=$(/usr/bin/python3 "$REPO/sleeve_monitor.py")

if [ -n "$ALERTS" ]; then
    echo "$(date) [sleeve_monitor] $ALERTS"
    echo "$ALERTS" | awk '{print $1}' | sort -u | \
    while read -r TICKER; do
        [ -z "$TICKER" ] && continue
        TRIGGER_LINE=$(echo "$ALERTS" | grep "^$TICKER ")
        /home/neilm/.local/bin/claude -p \
          --dangerously-skip-permissions \
          --allowedTools "Bash,Read,Write,mcp__Questrade__list_accounts,mcp__Questrade__search_symbols,mcp__Questrade__get_quotes,mcp__Questrade__get_historical_data,mcp__Questrade__get_option_expiries,mcp__Questrade__get_option_chain" \
          >>"$REPO/daily_run.log" 2>&1 << REPROMPT &
Read /media/raid/rshare/SwingTrader/sleeve_reconfirm_prompt.md and execute it for
ticker $TICKER. Its mechanical trigger line from sleeve_monitor.py this run: $TRIGGER_LINE
REPROMPT
    done
fi
