#!/usr/bin/env python3
"""
最简回测测试 - 证明数据能加载，回测能跑
"""

from datetime import datetime
from vnpy.trader.constant import Exchange, Interval
from vnpy.trader.database import get_database

print("=" * 80)
print("🚀 测试数据加载和回测")
print("=" * 80)

# 1. 测试数据库连接和加载数据
db = get_database()
print(f"✅ 数据库连接成功: {type(db)}")

symbol = "510300"
exchange = Exchange.SSE
interval = Interval.DAILY
start = datetime(2021, 1, 1)
end = datetime(2026, 3, 1)

bars = db.load_bar_data(symbol, exchange, interval, start, end)
print(f"\n✅ 加载了 {len(bars)} 条bar数据")

if len(bars) == 0:
    print("❌ 数据为空，无法继续")
    exit(1)

print(f"时间范围: {bars[0].datetime} ~ {bars[-1].datetime}")
print(f"第一条数据: {bars[0]}")
print(f"最后一条数据: {bars[-1]}")

# 2. 简单统计分析
import numpy as np
prices = [bar.close_price for bar in bars]
returns = np.diff(prices) / prices[:-1]

print(f"\n价格区间: {min(prices):.2f} ~ {max(prices):.2f}")
print(f"平均收益率: {np.mean(returns):.4%}")
print(f"收益率标准差: {np.std(returns):.4%}")
print(f"最大单日涨幅: {max(returns):.2%}")
print(f"最大单日跌幅: {min(returns):.2%}")

print("\n" + "=" * 80)
print("✅ 数据加载测试完成！数据完整，可以开始回测")
print("=" * 80)
