#!/usr/bin/env python3
"""
CrackingMarkets dip sleeve via IBKR (paper / live).

**Default: recommend-only** — writes tickets sized at ``sleeve / 10``, skips when
≥10 dips are open. Does not place orders unless ``--live``.

Paper (TWS / Gateway port **7497**, client_id **5**)::

    cd /Users/robzingale/trading_bot && PYTHONUNBUFFERED=1 .venv/bin/python \\
      -m RenTech.live.run_stock_book_ibkr --recommend-only --force-entry-now

    # After validating RenTech/data/logs/stock_book_cm_dip_recommendation.json:
    cd /Users/robzingale/trading_bot && PYTHONUNBUFFERED=1 .venv/bin/python \\
      -m RenTech.live.run_stock_book_ibkr --live --force-entry-now

Live Gateway: set ``broker.port`` to **7496** in
``RenTech/live/config/live_stock_book_cm_dip.json``.

Kill switch: ``RenTech/live/config/KILL_SWITCH`` with content ``HALT``.
"""

from __future__ import annotations

import argparse
import sys
from pathlib import Path

_REPO = Path(__file__).resolve().parents[2]
if str(_REPO) not in sys.path:
    sys.path.insert(0, str(_REPO))

from RenTech.live.orchestrator import run_from_config_path

DEFAULT_CONFIG = _REPO / "RenTech" / "live" / "config" / "live_stock_book_cm_dip.json"


def main() -> None:
    ap = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
    ap.add_argument("--config", type=Path, default=DEFAULT_CONFIG)
    ap.add_argument(
        "--recommend-only",
        action="store_true",
        help="Tickets only (default if neither --live nor config override).",
    )
    ap.add_argument(
        "--live",
        action="store_true",
        help="Place orders (paper or live per config port).",
    )
    ap.add_argument(
        "--force-entry-now",
        action="store_true",
        help="Bypass entry/exit time windows (useful for smoke tests).",
    )
    ap.add_argument("--audit", action="store_true", help="List open dips only.")
    args = ap.parse_args()

    if args.live and args.recommend_only:
        raise SystemExit("Pass only one of --live / --recommend-only")
    rec_only = True if args.recommend_only else (False if args.live else True)

    run_from_config_path(
        args.config.expanduser().resolve(),
        recommend_only=rec_only,
        force_entry_now=bool(args.force_entry_now),
        audit_only=bool(args.audit),
        strategy_filter="stock_book_cm_dip",
    )


if __name__ == "__main__":
    main()
