import sys from pathlib import Path import click from rich.console import Console from rich.table import Table # Add strategy-engine to sys.path _ROOT = Path(__file__).resolve().parents[5] sys.path.insert(0, str(_ROOT / "services" / "strategy-engine" / "src")) sys.path.insert(0, str(_ROOT / "services" / "strategy-engine")) def _load_all_strategies(): """Load all strategies from the strategies directory.""" try: from strategy_engine.plugin_loader import load_strategies except ImportError as e: click.echo(f"Error: Could not import plugin_loader: {e}", err=True) sys.exit(1) strategies_dir = _ROOT / "services" / "strategy-engine" / "strategies" return load_strategies(strategies_dir) @click.group() def strategy(): """Strategy management commands.""" pass @strategy.command("list") def list_(): """List all available trading strategies.""" strategies = _load_all_strategies() if not strategies: click.echo("No strategies found.") return console = Console() table = Table(title="Available Strategies", show_header=True, header_style="bold cyan") table.add_column("Name", style="bold") table.add_column("Warmup Period", justify="right") for s in strategies: table.add_row(s.name, str(s.warmup_period)) console.print(table) @strategy.command() @click.option("--name", required=True, help="Strategy name") def info(name): """Show detailed information about a strategy.""" strategies = _load_all_strategies() matched = [s for s in strategies if s.name == name] if not matched: available = [s.name for s in strategies] click.echo(f"Error: Strategy '{name}' not found. Available: {available}", err=True) sys.exit(1) strat = matched[0] config_dir = _ROOT / "services" / "strategy-engine" / "strategies" / "config" config_file = config_dir / f"{name}_strategy.yaml" if not config_file.exists(): # Try without _strategy suffix config_file = config_dir / f"{name}.yaml" console = Console() table = Table(title=f"Strategy: {strat.name}", show_header=True, header_style="bold cyan") table.add_column("Property", style="bold") table.add_column("Value") table.add_row("Name", strat.name) table.add_row("Warmup Period", str(strat.warmup_period)) table.add_row("Class", type(strat).__name__) if config_file.exists(): table.add_row("Config File", str(config_file)) else: table.add_row("Config File", "(none)") console.print(table)