# 核心数据模型
import datetime
from typing import Dict, List, Optional, Tuple
import pandas as pd


class MarketData:
    """市场数据模型"""
    
    def __init__(self, symbol: str, kline_data: pd.DataFrame):
        self.symbol = symbol
        self.kline_data = kline_data
        self.timestamp = datetime.datetime.now()
    
    def get_latest_price(self) -> float:
        """获取最新价格"""
        return float(self.kline_data['close'].iloc[-1])
    
    def get_price_range(self, period: int = 20) -> Tuple[float, float]:
        """获取价格范围"""
        prices = self.kline_data['close'].tail(period)
        return float(prices.min()), float(prices.max())


class AnalysisResult:
    """分析结果模型"""
    
    def __init__(self, symbol: str):
        self.symbol = symbol
        self.timestamp = datetime.datetime.now()
        self.trend: Optional[str] = None  # bullish, bearish, neutral
        self.probability: Optional[float] = None  # 胜率
        self.direction: Optional[str] = None  # long, short, wait
        self.cycle: Optional[str] = None  # short, medium, long
        self.atr: Optional[float] = None  # 真实波动幅度
        self.adx: Optional[float] = None  # 平均趋向指标
        self.support: Optional[float] = None  # 支撑位
        self.resistance: Optional[float] = None  # 阻力位
        self.stop_loss: Optional[float] = None  # 止损位
        self.target_price: Optional[float] = None  # 目标价
        self.position_size: Optional[float] = None  # 建议仓位
        self.risk_ratio: Optional[float] = None  # 风险比率
        self.fund_flow: Optional[Dict[str, float]] = None  # 资金流向
        self.signals: Dict[str, str] = {}  # 各维度信号
    
    def to_dict(self) -> Dict:
        """转换为字典"""
        return {
            'symbol': self.symbol,
            'timestamp': self.timestamp.isoformat(),
            'trend': self.trend,
            'probability': self.probability,
            'direction': self.direction,
            'cycle': self.cycle,
            'atr': self.atr,
            'adx': self.adx,
            'support': self.support,
            'resistance': self.resistance,
            'stop_loss': self.stop_loss,
            'target_price': self.target_price,
            'position_size': self.position_size,
            'risk_ratio': self.risk_ratio,
            'fund_flow': self.fund_flow,
            'signals': self.signals
        }


class StrategyConfig:
    """策略配置模型"""
    
    def __init__(self):
        # 技术指标参数
        self.macd_fast = 12
        self.macd_slow = 26
        self.macd_signal = 9
        self.rsi_period = 14
        self.bollinger_period = 20
        self.bollinger_std = 2
        self.kdj_period = 9
        self.kdj_signal = 3
        self.adx_period = 14
        
        # 趋势过滤参数
        self.short_ma = 20
        self.long_ma = 60
        
        # 风险控制参数
        self.atr_multiplier = 2.0
        self.max_risk_percent = 0.02
        self.min_profit_loss_ratio = 1.5
        
        # 资金监控参数
        self.volume_change_threshold = 0.05
        self.open_interest_change_threshold = 0.05


class RiskParams:
    """风险参数模型"""
    
    def __init__(self, account_balance: float):
        self.account_balance = account_balance
        self.max_risk_amount = account_balance * 0.02
        self.max_position_percent = 0.3
        self.max_leverage = 5
