QTrade/tests/test_services/test_strategies.py

110 lines
3.0 KiB
Python
Raw Normal View History

"""
策略模块测试
"""
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