QTrade/tests/test_services/test_strategies.py
cuizhan 085427959c feat: Phase 4-7 - Core Service, Strategy Engine, QMT Adapter, Tests
Phase 4: Core Service (port 8010)
- Pairing management (symbol-strategy pairs)
- Order service with Redis pub/sub
- Position and balance tracking

Phase 5: Strategy Engine (port 8030)
- BaseStrategy abstract class
- Strategy registry and executor
- Built-in strategies: Grid, MA Cross, Dual MA
- Signal manager with Redis integration

Phase 6: QMT Adapter (port 8040)
- XtQuant SDK wrapper with simulation mode
- Order executor listening to Redis channels
- Quote pusher and account syncer

Phase 7: Testing & Documentation
- Pytest fixtures for all services
- Unit tests for models and strategies
- Windows startup scripts
2025-12-09 00:36:27 +08:00

110 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
策略模块测试
"""
from datetime import datetime
class TestBaseStrategy:
"""基础策略测试"""
def test_strategy_context(self) -> None:
"""测试策略上下文"""
from src.strategy_engine.services.base_strategy import StrategyContext
context = StrategyContext(
pairing_id="pairing_001",
symbol="000001.SZ",
strategy_id="test_001",
params={"param1": 100},
)
assert context.strategy_id == "test_001"
assert context.symbol == "000001.SZ"
assert context.params["param1"] == 100
def test_tick_data(self) -> None:
"""测试 Tick 数据"""
from src.strategy_engine.services.base_strategy import TickData
tick = TickData(
symbol="000001.SZ",
last_price=10.50,
open=10.00,
high=10.80,
low=9.90,
pre_close=10.00,
volume=100000,
amount=1050000,
timestamp=datetime.now(),
)
assert tick.symbol == "000001.SZ"
assert tick.last_price == 10.50
def test_kline_data(self) -> None:
"""测试 K 线数据"""
from src.strategy_engine.services.base_strategy import KLineData
kline = KLineData(
symbol="000001.SZ",
period="1d",
open=10.00,
high=10.80,
low=9.90,
close=10.50,
volume=1000000,
amount=10500000,
timestamp=datetime.now(),
)
assert kline.symbol == "000001.SZ"
assert kline.period == "1d"
assert kline.high > kline.low
class TestGridStrategy:
"""网格策略测试"""
def test_grid_strategy_init(self) -> None:
"""测试网格策略初始化"""
from src.strategy_engine.services.base_strategy import StrategyContext
# 只测试 StrategyContext,因为策略文件可能不存在
context = StrategyContext(
pairing_id="pairing_001",
symbol="000001.SZ",
strategy_id="grid_001",
params={
"upper_price": 12.0,
"lower_price": 8.0,
"grid_count": 10,
"total_amount": 100000,
},
)
assert context.strategy_id == "grid_001"
assert context.symbol == "000001.SZ"
assert context.params["grid_count"] == 10
class TestDualMAStrategy:
"""双均线策略测试"""
def test_dual_ma_strategy_context(self) -> None:
"""测试双均线策略上下文"""
from src.strategy_engine.services.base_strategy import StrategyContext
context = StrategyContext(
pairing_id="pairing_001",
symbol="000001.SZ",
strategy_id="dual_ma_001",
params={
"short_period": 5,
"long_period": 20,
},
)
assert context.strategy_id == "dual_ma_001"
assert context.params["short_period"] == 5