#!/usr/bin/env python3
"""OpenClaw Phase 2 功能验证 Demo"""

print('=' * 60)
print('🦞 OpenClaw Trading - Phase 2 功能验证')
print('=' * 60)
print()

# 1. 验证导入
print('📦 模块导入测试...')
from openclaw.core.config import get_config
from openclaw.core.economy import TradingEconomicTracker, SurvivalStatus
from openclaw.core.costs import DecisionCostCalculator
from openclaw.agents.base import BaseAgent, AgentState, ActivityType
from openclaw.agents.trader import TraderAgent
from openclaw.indicators.technical import sma, ema, rsi, macd, bollinger_bands
from openclaw.monitoring.status import StatusMonitor
print('✅ 所有模块导入成功！')
print()

# 2. 配置系统
print('⚙️  配置系统...')
config = get_config()
print(f'   初始资金: {config.initial_capital}')
print(f'   LLM成本: ${config.cost_structure.llm_input_per_1m}/1M tokens')
print(f'   模拟天数: {config.simulation_days}')
print()

# 3. 成本计算器
print('💰 成本计算器...')
calculator = DecisionCostCalculator.from_config(config.cost_structure)
cost = calculator.calculate_decision_cost(
    tokens_input=1000,
    tokens_output=500,
    market_data_calls=10
)
print(f'   决策成本: ${cost:.4f}')
print()

# 4. 经济追踪器
print('📊 经济追踪器...')
tracker = TradingEconomicTracker(
    agent_id='demo-trader',
    initial_capital=10000.0
)
print(f'   Agent: {tracker.agent_id}')
print(f'   初始资金: ${tracker.initial_capital:,.2f}')
print(f'   当前余额: ${tracker.balance:,.2f}')
print(f'   生存状态: {tracker.get_survival_status().value}')
print()

# 5. TraderAgent
print('🤖 TraderAgent...')
agent = TraderAgent(
    agent_id='trader-001',
    initial_capital=10000.0,
    skill_level=0.7
)
print(f'   Agent ID: {agent.agent_id}')
print(f'   技能等级: {agent.skill_level:.1%}')
print(f'   胜率: {agent.win_rate:.1%}')
print(f'   解锁因子: {agent.state.unlocked_factors}')
print()

# 6. 技术指标
print('📈 技术指标...')
import pandas as pd
import numpy as np

# 生成示例数据
np.random.seed(42)
prices = pd.Series(100 + np.cumsum(np.random.randn(100) * 0.5))

sma20 = sma(prices, 20)
ema12 = ema(prices, 12)
rsi_val = rsi(prices, 14)
macd_result = macd(prices)
bb_result = bollinger_bands(prices)

print(f'   价格数据: {len(prices)} 天')
print(f'   SMA(20): {sma20.iloc[-1]:.2f}')
print(f'   EMA(12): {ema12.iloc[-1]:.2f}')
print(f'   RSI(14): {rsi_val.iloc[-1]:.2f}')
print(f'   MACD: {macd_result["macd"].iloc[-1]:.2f}')
print(f'   布林带: {bb_result["lower"].iloc[-1]:.2f} ~ {bb_result["upper"].iloc[-1]:.2f}')
print()

# 7. 状态监控
print('📡 状态监控...')
monitor = StatusMonitor()
monitor.register_agent('trader-001', tracker)
print(f'   监控Agent数: {monitor.agent_count}')
print(f'   繁荣Agent数: {monitor.thriving_count}')
print(f'   破产Agent数: {monitor.bankrupt_count}')
print()

# 8. 模拟交易流程
print('🎮 模拟交易流程...')
print('   1. Agent分析市场...')
print('   2. 生成交易信号...')
print('   3. 执行交易并扣除成本...')
print('   4. 更新状态...')

# 模拟一次交易
result = tracker.calculate_trade_cost(
    trade_value=1000.0,
    is_win=True,
    win_amount=50.0
)
agent.record_trade(is_win=True, pnl=50.0)

print(f'   交易后余额: ${agent.balance:,.2f}')
print(f'   交易次数: {agent.state.total_trades}')
print(f'   当前胜率: {agent.win_rate:.1%}')
print()

print('=' * 60)
print('✅ Phase 2 所有功能验证通过！')
print('=' * 60)
