- 新增核心对象模型(Tick, Kline, Order, Trade, Position, Account, Signal, Symbol) - 使用Pydantic BaseModel实现自动验证 - 创建DatabaseManager管理数据库和表结构 - 实现BaseDataDownloader抽象基类 - 实现XtQuantDownloader下载历史K线数据 - 添加Config和Logger工具类 - 清理旧的微服务架构代码 - 更新配置文件为简化版本(去除Redis) - 所有测试通过
366 lines
8.9 KiB
Python
366 lines
8.9 KiB
Python
"""
|
||
测试Pydantic自动验证功能
|
||
展示各种验证场景
|
||
"""
|
||
|
||
from datetime import datetime
|
||
from src.core import (
|
||
OrderSide,
|
||
OrderType,
|
||
PositionDirection,
|
||
ProductType,
|
||
Exchange,
|
||
Tick,
|
||
Bar,
|
||
Order,
|
||
Trade,
|
||
Position,
|
||
Account,
|
||
Signal,
|
||
Symbol,
|
||
)
|
||
|
||
|
||
def test_validation(test_name: str, func):
|
||
"""测试验证辅助函数"""
|
||
try:
|
||
func()
|
||
print(f"✗ {test_name}: 应该抛出异常但没有")
|
||
return False
|
||
except Exception as e:
|
||
print(f"✓ {test_name}: {type(e).__name__}: {str(e)[:80]}")
|
||
return True
|
||
|
||
|
||
def main():
|
||
print("=" * 70)
|
||
print("Pydantic 自动验证测试")
|
||
print("=" * 70)
|
||
print()
|
||
|
||
passed = 0
|
||
total = 0
|
||
|
||
# ==================== Tick 验证 ====================
|
||
print("【Tick 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Tick - 价格必须大于0",
|
||
lambda: Tick(
|
||
symbol="000001.SZ",
|
||
timestamp=datetime.now(),
|
||
price=-10.5, # 负数价格
|
||
volume=1000,
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Tick - 成交量不能为负数",
|
||
lambda: Tick(
|
||
symbol="000001.SZ",
|
||
timestamp=datetime.now(),
|
||
price=10.5,
|
||
volume=-1000, # 负数成交量
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Tick - 标的代码不能为空",
|
||
lambda: Tick(
|
||
symbol="", # 空代码
|
||
timestamp=datetime.now(),
|
||
price=10.5,
|
||
volume=1000,
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Bar 验证 ====================
|
||
print("【Bar 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Bar - 频率格式必须正确",
|
||
lambda: Bar(
|
||
symbol="000001.SZ",
|
||
timestamp=datetime.now(),
|
||
frequency="abc", # 错误格式
|
||
open=10.0,
|
||
high=10.5,
|
||
low=9.8,
|
||
close=10.2,
|
||
volume=10000,
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Bar - 最高价必须>=其他价格",
|
||
lambda: Bar(
|
||
symbol="000001.SZ",
|
||
timestamp=datetime.now(),
|
||
frequency="1m",
|
||
open=10.0,
|
||
high=9.5, # 最高价比最低价还低
|
||
low=9.8,
|
||
close=10.2,
|
||
volume=10000,
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Bar - 最低价必须<=其他价格",
|
||
lambda: Bar(
|
||
symbol="000001.SZ",
|
||
timestamp=datetime.now(),
|
||
frequency="1m",
|
||
open=10.0,
|
||
high=10.5,
|
||
low=10.6, # 最低价比最高价还高
|
||
close=10.2,
|
||
volume=10000,
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Order 验证 ====================
|
||
print("【Order 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Order - 限价单价格必须大于0",
|
||
lambda: Order(
|
||
order_id="ORD_001",
|
||
symbol="000001.SZ",
|
||
side=OrderSide.BUY,
|
||
order_type=OrderType.LIMIT,
|
||
price=0, # 限价单价格为0
|
||
volume=100,
|
||
create_time=datetime.now(),
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Order - 数量必须大于0",
|
||
lambda: Order(
|
||
order_id="ORD_001",
|
||
symbol="000001.SZ",
|
||
side=OrderSide.BUY,
|
||
order_type=OrderType.LIMIT,
|
||
price=10.5,
|
||
volume=0, # 数量为0
|
||
create_time=datetime.now(),
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Order - 已成交量不能大于订单量",
|
||
lambda: Order(
|
||
order_id="ORD_001",
|
||
symbol="000001.SZ",
|
||
side=OrderSide.BUY,
|
||
order_type=OrderType.LIMIT,
|
||
price=10.5,
|
||
volume=100,
|
||
filled_volume=200, # 已成交量大于订单量
|
||
create_time=datetime.now(),
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Trade 验证 ====================
|
||
print("【Trade 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Trade - 成交额应等于价格*数量",
|
||
lambda: Trade(
|
||
trade_id="TRD_001",
|
||
order_id="ORD_001",
|
||
symbol="000001.SZ",
|
||
side=OrderSide.BUY,
|
||
price=10.5,
|
||
volume=100,
|
||
amount=500.0, # 错误的成交额
|
||
trade_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Trade - 价格必须大于0",
|
||
lambda: Trade(
|
||
trade_id="TRD_001",
|
||
order_id="ORD_001",
|
||
symbol="000001.SZ",
|
||
side=OrderSide.BUY,
|
||
price=0, # 价格为0
|
||
volume=100,
|
||
amount=0,
|
||
trade_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Position 验证 ====================
|
||
print("【Position 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Position - 可用量不能大于持仓量",
|
||
lambda: Position(
|
||
symbol="000001.SZ",
|
||
direction=PositionDirection.LONG,
|
||
volume=1000,
|
||
available_volume=1500, # 可用量大于持仓量
|
||
avg_price=10.0,
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Position - 冻结量不能大于持仓量",
|
||
lambda: Position(
|
||
symbol="000001.SZ",
|
||
direction=PositionDirection.LONG,
|
||
volume=1000,
|
||
available_volume=900,
|
||
frozen_volume=200, # 冻结量大于持仓量
|
||
avg_price=10.0,
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Position - 平均成本必须大于0",
|
||
lambda: Position(
|
||
symbol="000001.SZ",
|
||
direction=PositionDirection.LONG,
|
||
volume=1000,
|
||
available_volume=1000,
|
||
avg_price=0, # 平均成本为0
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Account 验证 ====================
|
||
print("【Account 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Account - 总资产应等于各项之和",
|
||
lambda: Account(
|
||
account_id="ACC_001",
|
||
total_asset=1000000.0,
|
||
available_cash=500000.0,
|
||
frozen_cash=10000.0,
|
||
market_value=400000.0, # 总和不等于total_asset
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Account - 可用资金不能为负数",
|
||
lambda: Account(
|
||
account_id="ACC_001",
|
||
total_asset=0.0,
|
||
available_cash=-100.0, # 负数资金
|
||
update_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Signal 验证 ====================
|
||
print("【Signal 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Signal - 信号强度必须在0-1之间",
|
||
lambda: Signal(
|
||
signal_id="SIG_001",
|
||
symbol="000001.SZ",
|
||
strategy_name="Test",
|
||
signal_type="buy",
|
||
price=10.5,
|
||
target_volume=100,
|
||
strength=1.5, # 超过1
|
||
create_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Signal - 目标数量必须大于0",
|
||
lambda: Signal(
|
||
signal_id="SIG_001",
|
||
symbol="000001.SZ",
|
||
strategy_name="Test",
|
||
signal_type="buy",
|
||
price=10.5,
|
||
target_volume=0, # 数量为0
|
||
create_time=datetime.now(),
|
||
),
|
||
)
|
||
|
||
print()
|
||
|
||
# ==================== Symbol 验证 ====================
|
||
print("【Symbol 验证】")
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Symbol - 最小交易单位必须大于0",
|
||
lambda: Symbol(
|
||
code="000001.SZ",
|
||
name="测试",
|
||
exchange=Exchange.SZ,
|
||
product_type=ProductType.STOCK,
|
||
lot_size=0, # 最小单位为0
|
||
),
|
||
)
|
||
|
||
total += 1
|
||
passed += test_validation(
|
||
"Symbol - 期货必须设置保证金比例",
|
||
lambda: Symbol(
|
||
code="IF2401",
|
||
name="期货",
|
||
exchange=Exchange.CFFEX,
|
||
product_type=ProductType.FUTURE,
|
||
# 缺少margin_rate和multiplier
|
||
),
|
||
)
|
||
|
||
print()
|
||
print("=" * 70)
|
||
print(f"验证测试完成: {passed}/{total} 通过")
|
||
print("=" * 70)
|
||
|
||
if passed == total:
|
||
print("✓ 所有验证测试通过!Pydantic自动验证工作正常!")
|
||
else:
|
||
print(f"✗ 有 {total - passed} 个测试未通过")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|