- 新增核心对象模型(Tick, Kline, Order, Trade, Position, Account, Signal, Symbol) - 使用Pydantic BaseModel实现自动验证 - 创建DatabaseManager管理数据库和表结构 - 实现BaseDataDownloader抽象基类 - 实现XtQuantDownloader下载历史K线数据 - 添加Config和Logger工具类 - 清理旧的微服务架构代码 - 更新配置文件为简化版本(去除Redis) - 所有测试通过
326 lines
8.4 KiB
Python
326 lines
8.4 KiB
Python
"""
|
|
核心对象测试脚本
|
|
验证所有核心对象的创建和功能
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from src.core import (
|
|
OrderSide,
|
|
OrderType,
|
|
OrderStatus,
|
|
PositionDirection,
|
|
SignalType,
|
|
ProductType,
|
|
Exchange,
|
|
Tick,
|
|
Kline,
|
|
Order,
|
|
Trade,
|
|
Position,
|
|
Account,
|
|
Signal,
|
|
Symbol,
|
|
)
|
|
|
|
|
|
def test_tick():
|
|
"""测试 Tick 对象"""
|
|
print("=" * 50)
|
|
print("测试 Tick 对象")
|
|
print("=" * 50)
|
|
|
|
tick = Tick(
|
|
symbol="000001.SZ",
|
|
timestamp=datetime.now(),
|
|
price=10.5,
|
|
volume=1000,
|
|
bid_prices=[10.49, 10.48, 10.47],
|
|
bid_volumes=[500, 800, 1000],
|
|
ask_prices=[10.50, 10.51, 10.52],
|
|
ask_volumes=[600, 900, 1200],
|
|
)
|
|
|
|
print(f"✓ Tick 创建成功: {tick.symbol} @ {tick.price}")
|
|
print(f" 时间: {tick.timestamp}")
|
|
print(f" 买一: {tick.bid_prices[0]} x {tick.bid_volumes[0]}")
|
|
print(f" 卖一: {tick.ask_prices[0]} x {tick.ask_volumes[0]}")
|
|
|
|
# 测试不可变性
|
|
try:
|
|
tick.price = 11.0
|
|
print("✗ Tick 应该是不可变的!")
|
|
except Exception:
|
|
print("✓ Tick 不可变性验证成功")
|
|
|
|
# 测试序列化
|
|
tick_dict = tick.model_dump()
|
|
print(f"✓ 序列化: {len(tick_dict)} 个字段")
|
|
|
|
# 测试反序列化
|
|
tick2 = Tick.model_validate(tick_dict)
|
|
print(f"✓ 反序列化成功: {tick2.symbol}")
|
|
print()
|
|
|
|
|
|
def test_kline():
|
|
"""测试 Kline 对象"""
|
|
print("=" * 50)
|
|
print("测试 Kline 对象")
|
|
print("=" * 50)
|
|
|
|
kline = Kline(
|
|
symbol="000001.SZ",
|
|
timestamp=datetime.now(),
|
|
frequency="1m",
|
|
open=10.0,
|
|
high=10.5,
|
|
low=9.8,
|
|
close=10.2,
|
|
volume=10000,
|
|
amount=102000.0,
|
|
)
|
|
|
|
print(f"✓ Kline 创建成功: {kline.symbol} [{kline.frequency}]")
|
|
print(f" O: {kline.open}, H: {kline.high}, L: {kline.low}, C: {kline.close}")
|
|
print(f" 成交量: {kline.volume}, 成交额: {kline.amount}")
|
|
print(f"✓ Kline 不可变性: frozen={kline.model_config.get('frozen')}")
|
|
print()
|
|
|
|
|
|
def test_order():
|
|
"""测试 Order 对象"""
|
|
print("=" * 50)
|
|
print("测试 Order 对象")
|
|
print("=" * 50)
|
|
|
|
order = Order(
|
|
order_id="ORD_20240101_001",
|
|
symbol="000001.SZ",
|
|
side=OrderSide.BUY,
|
|
order_type=OrderType.LIMIT,
|
|
price=10.5,
|
|
volume=100,
|
|
create_time=datetime.now(),
|
|
update_time=datetime.now(),
|
|
strategy_name="TestStrategy",
|
|
)
|
|
|
|
print(f"✓ Order 创建成功: {order.order_id}")
|
|
print(f" {order.side.value} {order.volume} @ {order.price}")
|
|
print(f" 状态: {order.status.value}")
|
|
print(f" 策略: {order.strategy_name}")
|
|
|
|
# 测试状态更新(可变)
|
|
order.status = OrderStatus.FILLED
|
|
order.filled_volume = 100
|
|
print(f"✓ 状态更新成功: {order.status.value}, 成交量: {order.filled_volume}")
|
|
print()
|
|
|
|
|
|
def test_trade():
|
|
"""测试 Trade 对象"""
|
|
print("=" * 50)
|
|
print("测试 Trade 对象")
|
|
print("=" * 50)
|
|
|
|
trade = Trade(
|
|
trade_id="TRD_20240101_001",
|
|
order_id="ORD_20240101_001",
|
|
symbol="000001.SZ",
|
|
side=OrderSide.BUY,
|
|
price=10.5,
|
|
volume=100,
|
|
amount=1050.0,
|
|
commission=1.5,
|
|
trade_time=datetime.now(),
|
|
)
|
|
|
|
print(f"✓ Trade 创建成功: {trade.trade_id}")
|
|
print(f" {trade.side.value} {trade.volume} @ {trade.price}")
|
|
print(f" 成交额: {trade.amount}, 手续费: {trade.commission}")
|
|
print(f"✓ Trade 不可变性: frozen={trade.model_config.get('frozen')}")
|
|
print()
|
|
|
|
|
|
def test_position():
|
|
"""测试 Position 对象"""
|
|
print("=" * 50)
|
|
print("测试 Position 对象")
|
|
print("=" * 50)
|
|
|
|
position = Position(
|
|
symbol="000001.SZ",
|
|
direction=PositionDirection.LONG,
|
|
volume=1000,
|
|
available_volume=900,
|
|
frozen_volume=100,
|
|
avg_price=10.0,
|
|
update_time=datetime.now(),
|
|
)
|
|
|
|
print(f"✓ Position 创建成功: {position.symbol}")
|
|
print(f" 方向: {position.direction.value}")
|
|
print(f" 持仓量: {position.volume}, 可用: {position.available_volume}")
|
|
print(f" 平均成本: {position.avg_price}")
|
|
|
|
# 测试更新市值
|
|
position.update_market_value(10.5)
|
|
print(f"✓ 市值更新: {position.market_value}")
|
|
print(f" 浮动盈亏: {position.unrealized_pnl}")
|
|
|
|
# 测试 T+1
|
|
position.today_volume = 100
|
|
print(f"✓ T+1 支持: 今日买入 {position.today_volume}")
|
|
print()
|
|
|
|
|
|
def test_account():
|
|
"""测试 Account 对象"""
|
|
print("=" * 50)
|
|
print("测试 Account 对象")
|
|
print("=" * 50)
|
|
|
|
account = Account(
|
|
account_id="ACC_001",
|
|
total_asset=1000000.0,
|
|
available_cash=500000.0,
|
|
frozen_cash=10000.0,
|
|
market_value=490000.0,
|
|
update_time=datetime.now(),
|
|
)
|
|
|
|
print(f"✓ Account 创建成功: {account.account_id}")
|
|
print(f" 总资产: {account.total_asset}")
|
|
print(f" 可用资金: {account.available_cash}")
|
|
print(f" 冻结资金: {account.frozen_cash}")
|
|
print(f" 持仓市值: {account.market_value}")
|
|
|
|
# 验证总资产计算
|
|
calculated_total = (
|
|
account.available_cash + account.frozen_cash + account.market_value
|
|
)
|
|
print(f"✓ 总资产验证: {calculated_total} (应等于 {account.total_asset})")
|
|
print()
|
|
|
|
|
|
def test_signal():
|
|
"""测试 Signal 对象"""
|
|
print("=" * 50)
|
|
print("测试 Signal 对象")
|
|
print("=" * 50)
|
|
|
|
signal = Signal(
|
|
signal_id="SIG_20240101_001",
|
|
symbol="000001.SZ",
|
|
strategy_name="MA_Strategy",
|
|
signal_type=SignalType.BUY,
|
|
price=10.5,
|
|
target_volume=100,
|
|
strength=0.8,
|
|
reason="MA金叉",
|
|
create_time=datetime.now(),
|
|
metadata={"ma_fast": 10.3, "ma_slow": 10.1},
|
|
)
|
|
|
|
print(f"✓ Signal 创建成功: {signal.signal_id}")
|
|
print(f" 策略: {signal.strategy_name}")
|
|
print(f" 类型: {signal.signal_type.value}")
|
|
print(f" 目标: {signal.target_volume} @ {signal.price}")
|
|
print(f" 强度: {signal.strength}")
|
|
print(f" 原因: {signal.reason}")
|
|
print(f" 元数据: {signal.metadata}")
|
|
|
|
# 测试执行状态更新
|
|
signal.executed = True
|
|
print(f"✓ 执行状态更新: {signal.executed}")
|
|
print()
|
|
|
|
|
|
def test_symbol():
|
|
"""测试 Symbol 对象"""
|
|
print("=" * 50)
|
|
print("测试 Symbol 对象")
|
|
print("=" * 50)
|
|
|
|
# 测试股票
|
|
stock = Symbol(
|
|
code="000001.SZ",
|
|
name="平安银行",
|
|
exchange=Exchange.SZ,
|
|
product_type=ProductType.STOCK,
|
|
lot_size=100,
|
|
tick_size=0.01,
|
|
)
|
|
|
|
print(f"✓ 股票 Symbol 创建成功: {stock.code}")
|
|
print(f" 名称: {stock.name}")
|
|
print(f" 交易所: {stock.exchange.value}")
|
|
print(f" 类型: {stock.product_type.value}")
|
|
print(f" 最小单位: {stock.lot_size}股")
|
|
print(f" 最小变动: {stock.tick_size}元")
|
|
|
|
# 测试期货
|
|
future = Symbol(
|
|
code="IF2401",
|
|
name="沪深300股指期货",
|
|
exchange=Exchange.CFFEX,
|
|
product_type=ProductType.FUTURE,
|
|
lot_size=1,
|
|
tick_size=0.2,
|
|
margin_rate=0.12,
|
|
multiplier=300,
|
|
)
|
|
|
|
print(f"\n✓ 期货 Symbol 创建成功: {future.code}")
|
|
print(f" 保证金比例: {future.margin_rate}")
|
|
print(f" 合约乘数: {future.multiplier}")
|
|
print()
|
|
|
|
|
|
def test_enum_values():
|
|
"""测试枚举值"""
|
|
print("=" * 50)
|
|
print("测试枚举类型")
|
|
print("=" * 50)
|
|
|
|
print(f"OrderSide: {[e.value for e in OrderSide]}")
|
|
print(f"OrderType: {[e.value for e in OrderType]}")
|
|
print(f"OrderStatus: {[e.value for e in OrderStatus]}")
|
|
print(f"PositionDirection: {[e.value for e in PositionDirection]}")
|
|
print(f"SignalType: {[e.value for e in SignalType]}")
|
|
print(f"ProductType: {[e.value for e in ProductType]}")
|
|
print(f"Exchange: {[e.value for e in Exchange]}")
|
|
print()
|
|
|
|
|
|
def main():
|
|
"""运行所有测试"""
|
|
print("\n" + "=" * 50)
|
|
print("QTrade 核心对象测试")
|
|
print("=" * 50 + "\n")
|
|
|
|
try:
|
|
test_enum_values()
|
|
test_tick()
|
|
test_kline()
|
|
test_order()
|
|
test_trade()
|
|
test_position()
|
|
test_account()
|
|
test_signal()
|
|
test_symbol()
|
|
|
|
print("=" * 50)
|
|
print("✓ 所有测试通过!")
|
|
print("=" * 50)
|
|
|
|
except Exception as e:
|
|
print(f"\n✗ 测试失败: {e}")
|
|
import traceback
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|