"""3구간 가격 지표 — Price(P), Anchor(A), Target(T) 기반 점수(1~99). ⑩ 실시간 페르소나 정확도의 긴장도(Tension) 계측 근거이기도 하다. """ import math from typing import Optional from pydantic import BaseModel from common.enums import IndicatorRangeEnum, IndicatorType class IndicatorResponse(BaseModel): indicator_type: IndicatorType indicator_value: int indicator_range: IndicatorRangeEnum class ThreeSegIndicator: """3-Segment Indicator — 구간별 점수 산출.""" def _case1(self, anchoring_price: int, current_price: int, target_price: int) -> Optional[IndicatorResponse]: """Case 1: Optimal Zone (PZ1) — P ≤ A, 점수 99 고정.""" if anchoring_price >= current_price: return IndicatorResponse( indicator_type=IndicatorType.PERCENT_DETAIL, indicator_value=99, indicator_range=IndicatorRangeEnum.PZ1, ) return None def _case2(self, anchoring_price: int, current_price: int, target_price: int) -> Optional[IndicatorResponse]: """Case 2: Negotiable Zone (PZ2) — A < P < T, 99→80 선형 보간.""" if anchoring_price < current_price < target_price: percent = int(math.floor( 99 - ((current_price - anchoring_price) / (target_price - anchoring_price) * 19))) return IndicatorResponse( indicator_type=IndicatorType.PERCENT_DETAIL, indicator_value=percent, indicator_range=IndicatorRangeEnum.PZ2, ) return None def _case3(self, anchoring_price: int, current_price: int, target_price: int) -> Optional[IndicatorResponse]: """Case 3: Target Exceeded Zone (PZ3) — P ≥ T, 79에서 지수 감쇠.""" if target_price <= current_price: scale_exponent = int(math.floor(math.log10(target_price))) k = math.pow(10, -scale_exponent) exponent = -k * (current_price - target_price) raw_score = 79 * math.exp(exponent) percent = max(1, int(math.floor(raw_score))) return IndicatorResponse( indicator_type=IndicatorType.PERCENT_DETAIL, indicator_value=percent, indicator_range=IndicatorRangeEnum.PZ3, ) return None def calculate(self, anchoring_price: int, current_price: int, target_price: int) -> Optional[IndicatorResponse]: ret = [ self._case1(anchoring_price, current_price, target_price), self._case2(anchoring_price, current_price, target_price), self._case3(anchoring_price, current_price, target_price), ] return next((r for r in ret if r is not None), None)