QTrade/test_ma_indicator.py
cuizhan a5ac1c846e feat: 添加MA(移动平均线)指标计算器
主要更新:
- 新增 src/indicator_calculator/ma_indicator.py: MA指标计算器,支持自定义周期列表
- 更新 src/core/constants.py: 添加MA相关常量(IndicatorType.MA, IndicatorColumns.MA_*)
- 更新 src/indicator_calculator/indicator_manager.py: 注册MA指标到管理器
- 更新 configs/global.yaml: 添加MA指标配置(periods: [5, 10, 20, 60])

功能特点:
- 使用ta-lib库计算SMA(简单移动平均线)
- 支持多周期同时计算
- 遵循常量命名规范
- 与IndicatorManager和DataService完整集成
- 自动保存到indicators_1d_ma表

测试:
- test_ma_indicator.py: MA指标单元测试
- test_ma_dataservice.py: DataService集成测试
2025-12-23 18:07:18 +08:00

91 lines
2.8 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.

"""测试MA指标计算器"""
import sys
import os
from pathlib import Path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
os.environ["CONFIG_FILE"] = "configs/backtest.yaml"
import pandas as pd
import numpy as np
from src.indicator_calculator.ma_indicator import MAIndicator
from src.core.constants import IndicatorColumns
# 创建测试数据
dates = pd.date_range("2024-01-01", periods=100, freq="D")
np.random.seed(42)
prices = 100 + np.cumsum(np.random.randn(100) * 2)
df = pd.DataFrame(
{
"timestamp": dates,
"open": prices + np.random.randn(100) * 0.5,
"high": prices + np.abs(np.random.randn(100) * 1.5),
"low": prices - np.abs(np.random.randn(100) * 1.5),
"close": prices,
"volume": np.random.randint(1000000, 10000000, 100),
"amount": prices * np.random.randint(1000000, 10000000, 100),
}
)
print("=" * 70)
print("MA指标计算器测试")
print("=" * 70)
# 测试1: 默认配置
print("\n【测试1】默认配置(5, 10, 20, 60日均线)")
print("-" * 70)
ma = MAIndicator()
result = ma.calculate(df)
print(f"输入数据: {len(df)} 条")
print(f"输出列名: {ma.get_column_names()}")
print(f"\n前5条数据:")
print(result[["close", "ma_5", "ma_10", "ma_20", "ma_60"]].head())
print(f"\n后5条数据:")
print(result[["close", "ma_5", "ma_10", "ma_20", "ma_60"]].tail())
# 测试2: 自定义配置
print("\n【测试2】自定义配置(3, 7, 30日均线)")
print("-" * 70)
ma_custom = MAIndicator(config={"periods": [3, 7, 30]})
result_custom = ma_custom.calculate(df)
print(f"输出列名: {ma_custom.get_column_names()}")
print(f"\n后5条数据:")
print(result_custom[["close", "ma_3", "ma_7", "ma_30"]].tail())
# 测试3: 验证常量使用
print("\n【测试3】验证IndicatorColumns常量")
print("-" * 70)
print(f"MA_5 常量: {IndicatorColumns.MA_5}")
print(f"MA_10 常量: {IndicatorColumns.MA_10}")
print(f"MA_20 常量: {IndicatorColumns.MA_20}")
print(f"MA_60 常量: {IndicatorColumns.MA_60}")
print(f"get_ma_column(5): {IndicatorColumns.get_ma_column(5)}")
print(f"get_columns('ma'): {IndicatorColumns.get_columns('ma')}")
# 测试4: 与IndicatorManager集成
print("\n【测试4】与IndicatorManager集成")
print("-" * 70)
from src.indicator_calculator.indicator_manager import IndicatorManager
config = {
"ma": {"periods": [5, 10, 20, 60]},
"macd": {"fast_period": 12, "slow_period": 26, "signal_period": 9},
}
manager = IndicatorManager(config)
result_all = manager.calculate(df)
print(f"计算的指标: {manager.get_enabled_indicators()}")
print(f"输出列数: {len(result_all.columns)}")
print(f"\n包含MA列: {[col for col in result_all.columns if 'ma_' in col]}")
print(f"\n后5条数据 (部分列):")
print(result_all[["close", "ma_5", "ma_10", "ma_20", "macd_dif"]].tail())
print("\n" + "=" * 70)
print("✓ MA指标测试完成")
print("=" * 70)