import pandas as pd
import plotly.graph_objs as go
from plotly import subplots
import plotly.io as pio
from datetime import datetime
pio.renderers.default = 'browser'

from config import *

# ========================================
# 시뮬레이션 설정 - 여기서 코인과 파라미터를 변경하세요
# ========================================

# 코인 선택 (대문자로 입력)
COINS = ['XRP', 'ADA', 'APT', 'AVAX', 'BONK', 'BTC', 'ETC', 'HBAR', 'LINK', 'ONDO', 'PENGU', 'SEI', 'SOL', 'SUI', 'TRX', 'VIRTUAL', 'WLD', 'XLM']
COIN = COINS[0]

# 시뮬레이션 설정
INTERVAL = 3   # 분봉 간격 (3분봉 기준으로 그래프 표시)
BONG_COUNT = 10000      # 분석할 일수
SHOW_GRAPHS = True  # 그래프 표시 여부

# ========================================
# 코인별 전략 클래스들
# ========================================

class CoinStrategy:
    """통합 코인 전략 클래스"""
    
    def __init__(self, coin: str):
        self.coin = coin
        self.name = KR_COINS.get(coin, coin)
        
    def get_buy_amount(self, signal: str, current_price: float, check_5_week_lowest: bool = False) -> float:
        """코인별 매수 금액을 결정합니다."""
        base_amount = 0
        
        # 코인별 매수 금액 설정 (XRP 최적화 전략)
        if self.coin == 'XRP':
            # 3분봉 신호들 (단기 스캘핑 - 소액 진입)
            if signal == 'deviation1440_3m':
                base_amount = 22000  # 강한 이격도 과매도
            elif signal == 'absolute_bottom_3m':
                base_amount = 20000  # 절대 최저점 포착 (9월 26일)
            elif signal == 'rsi_oversold_3m':
                base_amount = 12000  # RSI 과매도 + 반등
            # 1440분봉 신호들 (중장기 추세 - 대량 진입)
            elif signal == 'deviation1440_1d':
                base_amount = 35000  # 강한 이격도 과매도
            elif signal == 'absolute_bottom_1d':
                base_amount = 35000  # 절대 최저점 포착 (9월 26일)
            elif signal == 'macd_golden_1d':
                base_amount = 25000  # MACD 골든크로스 + MA
            # 기존 신호들 (하위 호환성)
            elif signal == 'fall_6p':
                base_amount = 300000 if current_price > 100 else 150000
            elif signal == 'movingaverage':
                base_amount = 10000
            elif signal == 'deviation40':
                base_amount = 30000
            elif signal == 'deviation240':
                base_amount = 7000
            elif signal == 'deviation1440':
                base_amount = 35000
            elif signal == 'Deviation720':
                base_amount = 25000
            else:
                base_amount = 5000
                
        elif self.coin == 'ADA':
            # 3분봉 신호들
            if signal == 'fall_5p_3m':
                base_amount = 70000 if current_price > 1000 else 35000
            elif signal == 'movingaverage_3m':
                base_amount = 4800
            elif signal == 'deviation40_3m':
                base_amount = 15000
            elif signal == 'deviation240_3m':
                base_amount = 3600
            elif signal == 'deviation1440_3m':
                base_amount = 18000
            elif signal == 'Deviation720_3m':
                base_amount = 12000
            # 1440분봉 신호들
            elif signal == 'fall_6p_1d':
                base_amount = 200000 if current_price > 1000 else 100000
            elif signal == 'movingaverage_1d':
                base_amount = 8000
            elif signal == 'deviation40_1d':
                base_amount = 25000
            elif signal == 'deviation240_1d':
                base_amount = 6000
            elif signal == 'deviation1440_1d':
                base_amount = 30000
            elif signal == 'Deviation720_1d':
                base_amount = 20000
            # 기존 신호들
            elif signal == 'fall_6p':
                base_amount = 200000 if current_price > 1000 else 100000
            elif signal == 'movingaverage':
                base_amount = 8000
            elif signal == 'deviation40':
                base_amount = 25000
            elif signal == 'deviation240':
                base_amount = 6000
            elif signal == 'deviation1440':
                base_amount = 30000
            elif signal == 'Deviation720':
                base_amount = 20000
            else:
                base_amount = 4000
                
        elif self.coin == 'APT':
            # 3분봉 신호들
            if signal == 'fall_5p_3m':
                base_amount = 140000 if current_price > 5000 else 70000
            elif signal == 'movingaverage_3m':
                base_amount = 9000
            elif signal == 'deviation40_3m':
                base_amount = 24000
            elif signal == 'deviation240_3m':
                base_amount = 6000
            elif signal == 'deviation1440_3m':
                base_amount = 30000
            elif signal == 'Deviation720_3m':
                base_amount = 18000
            # 1440분봉 신호들
            elif signal == 'fall_6p_1d':
                base_amount = 400000 if current_price > 5000 else 200000
            elif signal == 'movingaverage_1d':
                base_amount = 15000
            elif signal == 'deviation40_1d':
                base_amount = 40000
            elif signal == 'deviation240_1d':
                base_amount = 10000
            elif signal == 'deviation1440_1d':
                base_amount = 50000
            elif signal == 'Deviation720_1d':
                base_amount = 30000
            # 기존 신호들
            elif signal == 'fall_6p':
                base_amount = 400000 if current_price > 5000 else 200000
            elif signal == 'movingaverage':
                base_amount = 15000
            elif signal == 'deviation40':
                base_amount = 40000
            elif signal == 'deviation240':
                base_amount = 10000
            elif signal == 'deviation1440':
                base_amount = 50000
            elif signal == 'Deviation720':
                base_amount = 30000
            else:
                base_amount = 8000
                
        elif self.coin == 'AVAX':
            if signal == 'fall_6p':
                base_amount = 500000 if current_price > 30000 else 300000
            elif signal == 'movingaverage':
                base_amount = 20000
            elif signal == 'deviation40':
                base_amount = 50000
            elif signal == 'deviation240':
                base_amount = 15000
            elif signal == 'deviation1440':
                base_amount = 60000
            elif signal == 'Deviation720':
                base_amount = 40000
            else:
                base_amount = 10000
                
        elif self.coin == 'BONK':
            if signal == 'fall_6p':
                base_amount = 200000 if current_price > 0.03 else 150000
            elif signal == 'movingaverage':
                base_amount = 10000
            elif signal == 'deviation40':
                base_amount = 25000
            elif signal == 'deviation240':
                base_amount = 7000
            elif signal == 'deviation1440':
                base_amount = 25000
            elif signal == 'Deviation720':
                base_amount = 20000
            else:
                base_amount = 5000
                
        elif self.coin == 'BTC':
            if signal == 'fall_6p':
                base_amount = 1000000 if current_price > 150000000 else 800000
            elif signal == 'movingaverage':
                base_amount = 50000
            elif signal == 'deviation40':
                base_amount = 100000
            elif signal == 'deviation240':
                base_amount = 30000
            elif signal == 'deviation1440':
                base_amount = 120000
            elif signal == 'Deviation720':
                base_amount = 80000
            else:
                base_amount = 20000
                
        elif self.coin == 'ETC':
            if signal == 'fall_6p':
                base_amount = 300000 if current_price > 25000 else 200000
            elif signal == 'movingaverage':
                base_amount = 30000
            elif signal == 'deviation40':
                base_amount = 60000
            elif signal == 'deviation240':
                base_amount = 20000
            elif signal == 'deviation1440':
                base_amount = 70000
            elif signal == 'Deviation720':
                base_amount = 50000
            else:
                base_amount = 15000
                
        elif self.coin == 'HBAR':
            if signal == 'fall_6p':
                base_amount = 150000 if current_price > 300 else 100000
            elif signal == 'movingaverage':
                base_amount = 15000
            elif signal == 'deviation40':
                base_amount = 30000
            elif signal == 'deviation240':
                base_amount = 10000
            elif signal == 'deviation1440':
                base_amount = 35000
            elif signal == 'Deviation720':
                base_amount = 25000
            else:
                base_amount = 8000
                
        elif self.coin == 'LINK':
            if signal == 'fall_6p':
                base_amount = 400000 if current_price > 30000 else 300000
            elif signal == 'movingaverage':
                base_amount = 40000
            elif signal == 'deviation40':
                base_amount = 80000
            elif signal == 'deviation240':
                base_amount = 25000
            elif signal == 'deviation1440':
                base_amount = 90000
            elif signal == 'Deviation720':
                base_amount = 60000
            else:
                base_amount = 20000
                
        elif self.coin == 'ONDO':
            if signal == 'fall_6p':
                base_amount = 180000 if current_price > 1300 else 120000
            elif signal == 'movingaverage':
                base_amount = 18000
            elif signal == 'deviation40':
                base_amount = 35000
            elif signal == 'deviation240':
                base_amount = 12000
            elif signal == 'deviation1440':
                base_amount = 40000
            elif signal == 'Deviation720':
                base_amount = 28000
            else:
                base_amount = 10000
                
        elif self.coin == 'PENGU':
            if signal == 'fall_6p':
                base_amount = 120000 if current_price > 40 else 80000
            elif signal == 'movingaverage':
                base_amount = 12000
            elif signal == 'deviation40':
                base_amount = 25000
            elif signal == 'deviation240':
                base_amount = 8000
            elif signal == 'deviation1440':
                base_amount = 28000
            elif signal == 'Deviation720':
                base_amount = 20000
            else:
                base_amount = 6000
                
        elif self.coin == 'SEI':
            if signal == 'fall_6p':
                base_amount = 160000 if current_price > 400 else 120000
            elif signal == 'movingaverage':
                base_amount = 16000
            elif signal == 'deviation40':
                base_amount = 32000
            elif signal == 'deviation240':
                base_amount = 11000
            elif signal == 'deviation1440':
                base_amount = 36000
            elif signal == 'Deviation720':
                base_amount = 25000
            else:
                base_amount = 8000
                
        elif self.coin == 'SOL':
            if signal == 'fall_6p':
                base_amount = 500000 if current_price > 300000 else 400000
            elif signal == 'movingaverage':
                base_amount = 50000
            elif signal == 'deviation40':
                base_amount = 100000
            elif signal == 'deviation240':
                base_amount = 30000
            elif signal == 'deviation1440':
                base_amount = 120000
            elif signal == 'Deviation720':
                base_amount = 80000
            else:
                base_amount = 20000
                
        elif self.coin == 'SUI':
            if signal == 'fall_6p':
                base_amount = 220000 if current_price > 4800 else 180000
            elif signal == 'movingaverage':
                base_amount = 22000
            elif signal == 'deviation40':
                base_amount = 45000
            elif signal == 'deviation240':
                base_amount = 15000
            elif signal == 'deviation1440':
                base_amount = 50000
            elif signal == 'Deviation720':
                base_amount = 35000
            else:
                base_amount = 12000
                
        elif self.coin == 'TRX':
            if signal == 'fall_6p':
                base_amount = 140000 if current_price > 450 else 100000
            elif signal == 'movingaverage':
                base_amount = 14000
            elif signal == 'deviation40':
                base_amount = 28000
            elif signal == 'deviation240':
                base_amount = 10000
            elif signal == 'deviation1440':
                base_amount = 32000
            elif signal == 'Deviation720':
                base_amount = 22000
            else:
                base_amount = 7000
                
        elif self.coin == 'VIRTUAL':
            if signal == 'fall_6p':
                base_amount = 190000 if current_price > 1600 else 130000
            elif signal == 'movingaverage':
                base_amount = 19000
            elif signal == 'deviation40':
                base_amount = 38000
            elif signal == 'deviation240':
                base_amount = 13000
            elif signal == 'deviation1440':
                base_amount = 42000
            elif signal == 'Deviation720':
                base_amount = 30000
            else:
                base_amount = 10000
                
        elif self.coin == 'WLD':
            if signal == 'fall_6p':
                base_amount = 200000 if current_price > 1800 else 150000
            elif signal == 'movingaverage':
                base_amount = 20000
            elif signal == 'deviation40':
                base_amount = 40000
            elif signal == 'deviation240':
                base_amount = 14000
            elif signal == 'deviation1440':
                base_amount = 45000
            elif signal == 'Deviation720':
                base_amount = 32000
            else:
                base_amount = 10000
                
        elif self.coin == 'XLM':
            if signal == 'fall_6p':
                base_amount = 150000 if current_price > 500 else 100000
            elif signal == 'movingaverage':
                base_amount = 15000
            elif signal == 'deviation40':
                base_amount = 30000
            elif signal == 'deviation240':
                base_amount = 10000
            elif signal == 'deviation1440':
                base_amount = 35000
            elif signal == 'Deviation720':
                base_amount = 25000
            else:
                base_amount = 8000
        else:
            # 기본값
            base_amount = 10000
            
        # 5주봉이 가장 낮을 때 매수 금액 2배 (시간봉별 신호 포함)
        if check_5_week_lowest and signal in ['movingaverage_3m', 'movingaverage_1d', 'deviation40_3m', 'deviation40_1d', 
                                             'deviation240_3m', 'deviation240_1d', 'deviation1440_3m', 'deviation1440_1d',
                                             'movingaverage', 'deviation40', 'deviation240', 'deviation1440']:
            base_amount *= 2
            
        return base_amount
    
    def get_sell_signals(self) -> list:
        """매도 신호 목록을 반환합니다."""
        return ['deviation1440_3m', 'absolute_bottom_3m', 'rsi_oversold_3m',
                'deviation1440_1d', 'absolute_bottom_1d', 'macd_golden_1d']
    
    def get_sell_amount_ratio(self, signal: str) -> float:
        """매도 시 판매할 비율을 반환합니다."""
        # 3분봉 신호들 (빠른 회전 - 높은 매도 비율)
        if signal in ['deviation1440_3m', 'absolute_bottom_3m', 'rsi_oversold_3m']:
            return 0.8  # 80% 매도
        # 1440분봉 신호들 (여유 있는 수익 실현 - 낮은 매도 비율)
        elif signal in ['deviation1440_1d', 'absolute_bottom_1d', 'macd_golden_1d']:
            return 0.6  # 60% 매도
        else:
            return 0.5  # 기본 50% 매도
    
    def check_coin_specific_signals(self, data: pd.DataFrame, i: int) -> tuple:
        """코인별 전용 매수 신호를 확인합니다."""
        signal = ''
        point = 0
        
        # Deviation720_strong과 Deviation720_very_strong 신호는 제거됨
        # 기본 신호만 사용: movingaverage, deviation40, Deviation720, deviation1440, fall_6p
        
        return signal, point

# ========================================
# 통합 시뮬레이션 클래스
# ========================================

class CoinSimulation:
    """통합 코인 시뮬레이션 클래스"""

    def __init__(self, coin: str) -> None:
        self.coin = coin
        self.strategy = CoinStrategy(coin)
        # 모니터 클래스 동적 임포트
        self._import_monitor()

    def _import_monitor(self):
        """코인별 모니터 클래스를 동적으로 임포트합니다."""
        try:
            if self.coin == 'XRP':
                from coin_xrp import XRPMonitor
                self.monitor = XRPMonitor()
            elif self.coin == 'ADA':
                from coin_ada import ADAMonitor
                self.monitor = ADAMonitor()
            elif self.coin == 'APT':
                from coin_apt import APTMonitor
                self.monitor = APTMonitor()
            elif self.coin == 'AVAX':
                from coin_avax import AVAXMonitor
                self.monitor = AVAXMonitor()
            elif self.coin == 'BONK':
                from coin_bonk import BONKMonitor
                self.monitor = BONKMonitor()
            elif self.coin == 'BTC':
                from coin_btc import BTCMonitor
                self.monitor = BTCMonitor()
            elif self.coin == 'ETC':
                from coin_etc import ETCMonitor
                self.monitor = ETCMonitor()
            elif self.coin == 'HBAR':
                from coin_hbar import HBARMonitor
                self.monitor = HBARMonitor()
            elif self.coin == 'LINK':
                from coin_link import LINKMonitor
                self.monitor = LINKMonitor()
            elif self.coin == 'ONDO':
                from coin_ondo import ONDOMonitor
                self.monitor = ONDOMonitor()
            elif self.coin == 'PENGU':
                from coin_pengu import PENGUMonitor
                self.monitor = PENGUMonitor()
            elif self.coin == 'SEI':
                from coin_sei import SEIMonitor
                self.monitor = SEIMonitor()
            elif self.coin == 'SOL':
                from coin_sol import SOLMonitor
                self.monitor = SOLMonitor()
            elif self.coin == 'SUI':
                from coin_sui import SUIMonitor
                self.monitor = SUIMonitor()
            elif self.coin == 'TRX':
                from coin_trx import TRXMonitor
                self.monitor = TRXMonitor()
            elif self.coin == 'VIRTUAL':
                from coin_virtual import VIRTUALMonitor
                self.monitor = VIRTUALMonitor()
            elif self.coin == 'WLD':
                from coin_wld import WLDMonitor
                self.monitor = WLDMonitor()
            elif self.coin == 'XLM':
                from coin_xlm import XLMMonitor
                self.monitor = XLMMonitor()
            else:
                raise ValueError(f"지원하지 않는 코인: {self.coin}")
        except ImportError as e:
            raise ImportError(f"코인 모니터 클래스를 찾을 수 없습니다: {e}")

    def render_plotly(self, symbol: str, interval_minutes: int, data: pd.DataFrame, inverseData: pd.DataFrame) -> None:
        """코인별 Plotly 차트 렌더링"""
        fig = subplots.make_subplots(
            rows=3, cols=1,
            subplot_titles=(f"{self.coin} 캔들차트", "이격도/거래량", "장기 이격도"),
            shared_xaxes=True, horizontal_spacing=0.03, vertical_spacing=0.03,
            row_heights=[0.6, 0.2, 0.2]
        )

        # Row 1: 캔들 + 이동평균 + 볼린저
        fig.add_trace(go.Candlestick(x=data.index, open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], name=f'{self.coin} 캔들'), row=1, col=1)
        
        # 이동평균선 표시
        for ma_col, color in [('MA5','red'),('MA20','blue'),('MA40','green'),('MA120','purple'),('MA200','brown'),('MA240','darkred'),('MA720','cyan'),('MA1440','magenta')]:
            if ma_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[ma_col], name=ma_col, mode='lines', line=dict(color=color, width=1)), row=1, col=1)
        
        # 볼린저 밴드
        if 'Lower' in data.columns and 'Upper' in data.columns:
            fig.add_trace(go.Scatter(x=data.index, y=data['Lower'], name='볼린저 하단', mode='lines', line=dict(color='grey', width=1, dash='dot')), row=1, col=1)
            fig.add_trace(go.Scatter(x=data.index, y=data['Upper'], name='볼린저 상단', mode='lines', line=dict(color='grey', width=1, dash='dot')), row=1, col=1)

        # 매수 포인트 (XRP 최적화 신호)
        buy_signals = ['deviation1440_3m', 'absolute_bottom_3m', 'rsi_oversold_3m',
                      'deviation1440_1d', 'absolute_bottom_1d', 'macd_golden_1d',
                      'movingaverage_3m', 'movingaverage_1d', 'deviation40_3m', 'deviation40_1d', 
                      'Deviation720_3m', 'Deviation720_1d', 'fall_5p_3m', 'fall_6p_1d', 
                      'movingaverage', 'deviation40', 'Deviation720', 'deviation1440', 'fall_6p']
        for sig, color in [('deviation1440_3m','purple'),('absolute_bottom_3m','cyan'),('rsi_oversold_3m','magenta'),
                          ('deviation1440_1d','darkviolet'),('absolute_bottom_1d','lime'),('macd_golden_1d','gold'),
                          ('movingaverage_3m','red'),('movingaverage_1d','darkred'),('deviation40_3m','orange'),('deviation40_1d','darkorange'),
                          ('Deviation720_3m','blue'),('Deviation720_1d','darkblue'),('fall_5p_3m','black'),('fall_6p_1d','gray'),
                          ('movingaverage','red'),('deviation40','orange'),('Deviation720','blue'),('deviation1440','purple'),('fall_6p','black')]:
            pts = data[(data['point']==1) & (data['signal']==sig)]
            if len(pts)>0:
                fig.add_trace(go.Scatter(x=pts.index, y=pts['Close'], mode='markers', name=f'{sig} 매수', marker=dict(color=color, size=8, symbol='circle')), row=1, col=1)

        # 매도 포인트
        sell_signals = self.strategy.get_sell_signals()
        inv_sell_pts = inverseData[(inverseData['point']==1) & (inverseData['signal'].isin(sell_signals))]
        if len(inv_sell_pts)>0:
            idx = inv_sell_pts.index.intersection(data.index)
            if len(idx)>0:
                fig.add_trace(
                    go.Scatter(
                        x=idx,
                        y=data.loc[idx, 'Close'],
                        mode='markers',
                        name=f'{self.coin} 매도',
                        marker=dict(color='orange', size=10, symbol='triangle-down')
                    ),
                    row=1, col=1
                )

        # Row 2: 이격도 + 거래량
        for dev_col, color, width in [('Deviation5','red',1),('Deviation20','blue',1),('Deviation40','green',2),('Deviation120','purple',1),('Deviation200','brown',1),('Deviation720','darkred',2),('Deviation1440','magenta',1)]:
            if dev_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[dev_col], name=dev_col, mode='lines', line=dict(color=color, width=width)), row=2, col=1)
        if 'Volume' in data.columns:
            fig.add_trace(go.Bar(x=data.index, y=data['Volume'], name='거래량', marker_color='lightgray', opacity=0.5), row=2, col=1)

        # Row 3: 장기 이격도 및 기준선
        for dev_col, color in [('Deviation720','darkred'),('Deviation1440','magenta')]:
            if dev_col in data.columns:
                fig.add_trace(go.Scatter(x=data.index, y=data[dev_col], name=f'{dev_col}(장기)', mode='lines', line=dict(color=color, width=2)), row=3, col=1)
        
        # 코인별 기준선
        if self.coin == 'XRP':
            for h, color in [(96,'red'),(97,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'ADA':
            for h, color in [(98,'red'),(99,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'APT':
            for h, color in [(110,'red'),(115,'green'),(120,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'AVAX':
            for h, color in [(95,'red'),(97,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'BONK':
            for h, color in [(92,'red'),(95,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'BTC':
            for h, color in [(105,'red'),(108,'green'),(110,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'ETC':
            for h, color in [(94,'red'),(96,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'HBAR':
            for h, color in [(98,'red'),(100,'green'),(102,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'LINK':
            for h, color in [(95,'red'),(97,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'ONDO':
            for h, color in [(96,'red'),(98,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'PENGU':
            for h, color in [(91,'red'),(93,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'SEI':
            for h, color in [(97,'red'),(99,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'SOL':
            for h, color in [(104,'red'),(106,'green'),(110,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'SUI':
            for h, color in [(99,'red'),(101,'green'),(102,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'TRX':
            for h, color in [(96,'red'),(98,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'VIRTUAL':
            for h, color in [(93,'red'),(95,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'WLD':
            for h, color in [(94,'red'),(96,'green'),(100,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)
        elif self.coin == 'XLM':
            for h, color in [(98,'red'),(100,'green'),(102,'black')]:
                fig.add_hline(y=h, line_width=1, line_dash='dash', line_color=color, row=3, col=1)

        fig.update_layout(
            height=1000,
            margin=dict(t=180, l=40, r=240, b=40),
            title=dict(
                text=f"{self.coin} ({symbol}), {interval_minutes} 분봉, ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})",
                x=0.5,
                xanchor='center',
                y=0.995,
                yanchor='top',
                pad=dict(t=10, b=12)
            ),
            xaxis_rangeslider_visible=False,
            xaxis1_rangeslider_visible=False,
            xaxis2_rangeslider_visible=False,
            legend=dict(orientation='v', yref='paper', yanchor='top', y=1.0, xref='paper', xanchor='left', x=1.02),
            dragmode='zoom'
        )
        fig.update_xaxes(title_text='시간', row=3, col=1)
        fig.update_yaxes(title_text='가격 (KRW)', row=1, col=1)
        fig.update_yaxes(title_text='이격도/거래량', row=2, col=1)
        fig.update_yaxes(title_text='장기 이격도', row=3, col=1)

        fig.show(config={'scrollZoom': True, 'displaylogo': False})

    def fetch_coin_price_history(self, symbol: str, interval_minutes: int) -> pd.DataFrame:
        """코인 가격 히스토리를 가져옵니다."""
        return self.monitor.get_coin_more_data(symbol, interval_minutes, bong_count=BONG_COUNT)

    def run_coin_simulation(self, symbol: str, interval_minutes: int):
        """코인 시뮬레이션 실행"""
        data = self.fetch_coin_price_history(symbol, interval_minutes)

        # 인버스 데이터 처리 (시간봉별 신호 포함)
        inverseData = self.monitor.inverse_data(data)
        inverseData = self.monitor.annotate_signals(symbol, interval_minutes, inverseData, simulation=True)

        # 일반 데이터 처리 (시간봉별 신호 포함)
        data = self.monitor.calculate_technical_indicators(data)
        data = self.monitor.annotate_signals(symbol, interval_minutes, data, simulation=True)
        
        # 코인 전용 신호 추가
        for i in range(1, len(data)):
            coin_signal, coin_point = self.strategy.check_coin_specific_signals(data, i)
            if coin_point == 1:
                data.at[data.index[i], 'signal'] = coin_signal
                data.at[data.index[i], 'point'] = coin_point
        
        print(f"{self.coin} 데이터 기간: {data.index[0]} ~ {data.index[-1]}")
        print(f"총 데이터 수: {len(data)}")
        
        # 매수 신호 분석
        alerts = []
        total_buy_amount = 0
        for i in range(len(data)):
            if data['point'].iloc[i] == 1:
                signal = data['signal'].iloc[i]
                price = data['Close'].iloc[i]
                buy_amount = self.strategy.get_buy_amount(signal, price)
                total_buy_amount += buy_amount
                alerts.append((data.index[i], price, signal, buy_amount))
        
        print(f"\n총 매수 신호 수: {len(alerts)}")
        print(f"총 매수 금액: {total_buy_amount:,.0f}원")
        
        # 신호별 분석
        signal_counts = {}
        signal_amounts = {}
        for _, _, signal, amount in alerts:
            signal_counts[signal] = signal_counts.get(signal, 0) + 1
            signal_amounts[signal] = signal_amounts.get(signal, 0) + amount
        
        for signal in signal_counts:
            print(f"  - {signal} 신호: {signal_counts[signal]}회, 총 {signal_amounts[signal]:,.0f}원")
        
        # 매도 신호 분석
        sell_signals = self.strategy.get_sell_signals()
        sell_alerts = []
        for i in range(len(inverseData)):
            if inverseData['point'].iloc[i] == 1 and inverseData['signal'].iloc[i] in sell_signals:
                signal = inverseData['signal'].iloc[i]
                price = inverseData['Close'].iloc[i]
                sell_ratio = self.strategy.get_sell_amount_ratio(signal)
                sell_alerts.append((inverseData.index[i], price, signal, sell_ratio))
        
        print(f"\n총 매도 신호 수: {len(sell_alerts)}")
        for date, price, signal, ratio in sell_alerts:
            print(f"  - {date}: {price:.4f} ({signal}) - 매도비율: {ratio*100:.0f}%")

        # Plotly 기반 시각화
        if SHOW_GRAPHS:
            self.render_plotly(symbol, interval_minutes, data, inverseData)
        return alerts, sell_alerts

# ========================================
# 메인 실행 부분
# ========================================

if __name__ == "__main__":
    print(f"\n=== {COIN} 시뮬레이션 시작 ===")
    print(f"코인: {COIN}")
    print(f"분봉: {INTERVAL}분")
    print(f"봉 개수: {BONG_COUNT}개")
    print(f"그래프 표시: {'예' if SHOW_GRAPHS else '아니오'}")
    print("=" * 50)
    
    try:
        sim = CoinSimulation(COIN)
        buy_alerts, sell_alerts = sim.run_coin_simulation(COIN, INTERVAL)
        
        print(f"\n=== {COIN} 시뮬레이션 완료 ===")
        print(f"매수 신호: {len(buy_alerts)}회")
        print(f"매도 신호: {len(sell_alerts)}회")
        
    except Exception as e:
        print(f"Error analyzing {COIN}: {str(e)}")
        print("\n지원되는 코인 목록:")
        print("XRP, ADA, APT, AVAX, BONK, BTC, ETC, HBAR, LINK, ONDO, PENGU, SEI, SOL, SUI, TRX, VIRTUAL, WLD, XLM")
