""" πŸ“ sightray/sightray.py SightRay 전체 톡합 μ‹€ν–‰ 슀크립트 - μ‚¬μš©μžκ°€ μ’…λͺ© μ½”λ“œ, λ‚ μ§œ, 자본금만 μž…λ ₯ν•˜λ©΄ 전체 νŒŒμ΄ν”„λΌμΈ μžλ™ μ‹€ν–‰ - μˆ˜μ§‘ β†’ ν•™μŠ΅ β†’ 뢄석 β†’ 리슀크 평가 β†’ μ „λž΅ μ‹€ν–‰κΉŒμ§€ ν•œ λ²ˆμ— μ™„λ£Œ - μ‹€ν–‰ μ „ ν•„μš”ν•œ 라이브러리 μ„€μΉ˜ μ—¬λΆ€ 확인 """ import os import importlib.util import subprocess import sys import glob from dotenv import load_dotenv, set_key # ν•„μš”ν•œ νŒ¨ν‚€μ§€ λͺ©λ‘ (importλͺ…: μ„€μΉ˜λͺ…) REQUIRED_PACKAGES = { "pandas": "pandas", "requests": "requests", "xgboost": "xgboost", "dotenv": "python-dotenv" } def check_and_install_packages(): missing = [] for import_name, pip_name in REQUIRED_PACKAGES.items(): if importlib.util.find_spec(import_name) is None: missing.append(pip_name) if not missing: return print(f"❗ ν•„μˆ˜ 라이브러리 λˆ„λ½: {', '.join(missing)}") confirm = input("μžλ™μœΌλ‘œ μ„€μΉ˜ν• κΉŒμš”? (y/n): ").strip().lower() if confirm == 'y': subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing) print("βœ… μ„€μΉ˜ μ™„λ£Œ. λ‹€μ‹œ μ‹€ν–‰ν•΄ μ£Όμ„Έμš”.") sys.exit() else: print("❌ μ„€μΉ˜λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. μˆ˜λ™μœΌλ‘œ μ„€μΉ˜ ν›„ λ‹€μ‹œ μ‹€ν–‰ν•˜μ„Έμš”.") sys.exit() # 라이브러리 μ„€μΉ˜ μ—¬λΆ€ 확인 check_and_install_packages() # API ν‚€ 확인 및 μ„€μ • # 🌐 .env 파일 체크 및 API ν‚€ 확인/생성 if not os.path.exists(".env"): with open(".env", "w") as f: f.write("# SightRay ν™˜κ²½μ„€μ • 파일\n") load_dotenv() api_key = os.getenv("POLYGON_API_KEY") if not api_key or '--reset-api' in sys.argv: print("πŸ” Polygon API ν‚€κ°€ μ„€μ •λ˜μ–΄ μžˆμ§€ μ•Šκ±°λ‚˜ μ΄ˆκΈ°ν™” μš”μ²­μ΄ κ°μ§€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") new_key = input("Polygon API ν‚€λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ").strip() if new_key: set_key(".env", "POLYGON_API_KEY", new_key) print("βœ… API ν‚€κ°€ .env νŒŒμΌμ— μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") else: raise ValueError("API ν‚€κ°€ μž…λ ₯λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.") print("πŸ”‘ Polygon API ν‚€κ°€ μ„€μ •λ˜μ–΄ μžˆμ§€ μ•Šκ±°λ‚˜ μ΄ˆκΈ°ν™” μš”μ²­μ΄ κ°μ§€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") new_key = input("Polygon API ν‚€λ₯Ό μž…λ ₯ν•˜μ„Έμš”: ").strip() if new_key: print("βœ… API ν‚€κ°€ .env νŒŒμΌμ— μ €μž₯λ˜μ—ˆμŠ΅λ‹ˆλ‹€.") # μ—”μ§„ import from data_collection_engine.engine import DataCollectionEngine from data_analysis_engine.analyzer import analyze_stocks_pipeline from data_analysis_engine.train_model import train_model from data_analysis_engine.dataset_builder import build_dataset from risk_manage_engine.evaluate import evaluate_predictions from strategy_engine.run_strategy import run_strategy # (μ‚¬μš©λ˜μ§€ μ•Šμ§€λ§Œ λ³΄κ΄€λœ build_dataset μ˜ˆμ‹œ) def build_dataset(df): df = df.copy() df['target'] = (df['close'].shift(-1) > df['close']).astype(int) df.dropna(inplace=True) X = df[['open', 'high', 'low', 'close', 'volume']] y = df['target'] return X, y def run_sightray(symbol: str, start: str, end: str, capital: float, strategy: str = "adjusted"): # 파일 경둜 μ„€μ • data_dir = "data" ohlcv_path = os.path.join(data_dir, f"{symbol}_ohlcv.csv") prediction_path = "prediction_result.csv" risk_path = "risk_filtered_result.csv" output_path = "final_strategy_result.csv" # μ΅œμ‹  λͺ¨λΈ μžλ™ 탐색 model_dir = "data_analysis_engine/models" model_list = sorted(glob.glob(f"{model_dir}/model_*.json"), reverse=True) if not model_list: raise FileNotFoundError("λͺ¨λΈ 파일이 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.") model_path = model_list[0] # 1️⃣ 데이터 μˆ˜μ§‘ print("[1단계] 데이터 μˆ˜μ§‘ 쀑...") engine = DataCollectionEngine(data_dir=data_dir) engine.collect(symbol, start, end) # 2️⃣ λͺ¨λΈ ν•™μŠ΅ print("[2단계] 뢄석 λͺ¨λΈ ν•™μŠ΅ 쀑...") train_model(data_dir, output_model_path=model_path) # 3️⃣ 뢄석 예츑 print("[3단계] 예츑 μ‹€ν–‰ 쀑...") analyze_stocks_pipeline( cds_dir=data_dir, model_path=model_path, top_n=10, output_path=prediction_path ) # 4️⃣ 리슀크 평가 print("[4단계] 리슀크 평가 μ‹€ν–‰ 쀑...") evaluate_predictions(prediction_path, data_dir, output_path=risk_path) # 5️⃣ μ „λž΅ μ‹€ν–‰ print("[5단계] μ „λž΅ 생성 및 ν¬μ§€μ…˜ μ„€μ • 쀑...") run_strategy( risk_filtered_path=risk_path, total_capital=capital, strategy=strategy, output_path=output_path ) print("\nβœ… SightRay 전체 νŒŒμ΄ν”„λΌμΈ μ‹€ν–‰ μ™„λ£Œ!") print(f"β†’ μ΅œμ’… μ „λž΅ κ²°κ³Ό: {output_path}") if __name__ == "__main__": print("SightRay 싀행을 μ‹œμž‘ν•©λ‹ˆλ‹€. λ‹€μŒ 정보λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”:") symbol = input("μ’…λͺ© μ½”λ“œ (예: AAPL, TSLA): ").strip().upper() start = input("μ‹œμž‘μΌ (YYYY-MM-DD): ").strip() end = input("μ’…λ£ŒμΌ (YYYY-MM-DD): ").strip() capital = float(input("총 투자 자본 (예: 1000000): ").strip()) strategy = input("μ „λž΅ ν…œν”Œλ¦Ώ (adjusted / basic) [κΈ°λ³Έ: adjusted]: ").strip().lower() if strategy not in ["basic", "adjusted"]: strategy = "adjusted" run_sightray(symbol, start, end, capital, strategy)