''' strategy_engine/templates/risk_adjusted_strategy.py 리스크 점수 기반 전략 템플릿: - 종목별 risk_score에 따라 TP/SL 비율 및 action(Buy 여부)을 다르게 설정 - 리스크가 낮을수록 더 공격적인 전략 적용, 높을수록 보수적으로 필터링 ''' import pandas as pd def apply_risk_adjusted_strategy(df: pd.DataFrame) -> pd.DataFrame: """ risk_score에 따라 전략 조건(TP/SL/Buy 여부)을 결정하는 함수 Parameters: df (pd.DataFrame): selector에서 필터링된 종목 DataFrame (symbol, close, risk_score 포함) Returns: pd.DataFrame: entry_price, target_price, stop_loss, action 포함한 전략 테이블 """ df = df.copy() df['entry_price'] = df['close'] def compute_strategy(row): score = row['risk_score'] price = row['entry_price'] if score >= 80: tp = 0.07 sl = 0.03 action = 'Buy' elif score >= 70: tp = 0.05 sl = 0.03 action = 'Buy' elif score >= 60: tp = 0.03 sl = 0.02 action = 'Buy' else: tp = None sl = None action = 'None' return pd.Series({ 'target_price': price * (1 + tp) if tp is not None else None, 'stop_loss': price * (1 - sl) if sl is not None else None, 'action': action }) strategy_info = df.apply(compute_strategy, axis=1) return pd.concat([df[['symbol', 'entry_price']], strategy_info], axis=1)