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
110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
"""
|
||
策略模块测试
|
||
"""
|
||
|
||
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
|