#!/usr/bin/env python """ 仅运行方案B(LightGBM/XGBoost)训练并保存模型,供实盘脚本 get_live_signal 使用。 需先保证 models/database.db 中有 15m/5m/1h K 线(例如运行 抓取多周期K线.py)。 macOS Apple Silicon 若 LightGBM 报 libomp 错误,可用 --model xgboost 或自动会 fallback 到 XGBoost。 """ import argparse import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) def _lightgbm_available(): try: import lightgbm as lgb # noqa: F401 return True except OSError as e: if 'libomp' in str(e).lower() or 'libomp.dylib' in str(e): return False raise except Exception: return False if __name__ == '__main__': p = argparse.ArgumentParser(description='方案B 训练并保存模型(实盘用)') p.add_argument('--period', type=int, default=15, help='主周期分钟,默认 15') p.add_argument('--start', type=str, default=None, help='回测开始日期,如 2024-01-01') p.add_argument('--end', type=str, default=None, help='回测结束日期,如 2024-12-31') p.add_argument('--model', type=str, default=None, choices=['lightgbm', 'xgboost'], help='模型: lightgbm 或 xgboost;不指定时优先 lightgbm,失败则用 xgboost') args = p.parse_args() model_type = args.model if model_type is None: model_type = 'lightgbm' if not _lightgbm_available(): print('LightGBM 不可用(常见于 macOS 缺 libomp),改用 XGBoost。若需 LightGBM 请安装: brew install libomp') model_type = 'xgboost' from strategy.ai_strategy import run_ai_strategy run_ai_strategy(model_type=model_type, period=args.period, start_date=args.start, end_date=args.end)