QTrade/tests/test_rsi.py
cuizhan 43ab91b44d 重构数据系统:ta-lib指标、RSI多周期、数据库精度优化、表结构统一
核心改进:
- 指标计算:全部切换为ta-lib实现(MACD/KDJ/BOLL)
- RSI指标:新增多周期RSI(6/12/24),列名rsi_fast/mid/slow
- 数据精度:数据库DECIMAL(10,3)支持ETF 3位小数,下载器round(x,3)
- 表结构:所有表使用(symbol,timestamp)复合主键,移除id自增
- 表命名:统一为indicators_{period}_{indicator}格式,删除42个旧表
- 自动计算:DataProvider下载后自动计算并保存指标
- 列名规范:macd_dif/dea/histogram, kdj_k/d/j, boll_upper/mid/lower
- KlineReader:保留timestamp和symbol列供指标计算使用

已验证:1m周期2400+条数据,所有指标正常计算保存
2025-12-17 22:58:22 +08:00

76 lines
2.1 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.

"""RSI指标测试脚本"""
import sys
from pathlib import Path
# 添加项目根目录到路径
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
from src.data_provider.indicator_calculator import RSIIndicator, IndicatorManager
# 创建测试数据
df = pd.DataFrame(
{
"timestamp": [datetime(2024, 1, 1) + timedelta(days=i) for i in range(100)],
"open": np.linspace(10, 20, 100),
"high": np.linspace(10.5, 20.5, 100),
"low": np.linspace(9.5, 19.5, 100),
"close": np.linspace(10, 20, 100),
"volume": [1000000] * 100,
"amount": [10000000] * 100,
}
)
print("=" * 60)
print("测试单个RSI指标")
print("=" * 60)
# 测试RSI指标
rsi = RSIIndicator()
result = rsi.calculate(df)
print(f"RSI指标列名: {rsi.get_column_names()}")
print(f"\n最后5行数据:")
print(result[["close", "rsi_fast", "rsi_mid", "rsi_slow"]].tail())
print("\n" + "=" * 60)
print("测试指标管理器(包含RSI)")
print("=" * 60)
# 测试指标管理器
indicator_configs = {
"macd": {"fast_period": 12, "slow_period": 26, "signal_period": 9},
"kdj": {"period": 9, "k_period": 3, "d_period": 3},
"boll": {"period": 20, "std_multiplier": 2.0},
"rsi": {"fast": 6, "mid": 12, "slow": 24},
}
manager = IndicatorManager(indicator_configs)
print(f"已启用的指标: {manager.get_enabled_indicators()}")
kline_data = {"TEST_1d": df.copy()}
result = manager.calculate_all(kline_data)
print(f"\n计算结果:")
for key, df_result in result.items():
print(f"\n{key}:")
print(f" 总列数: {len(df_result.columns)}")
indicator_cols = [
col
for col in df_result.columns
if any(ind in col for ind in ["macd", "kdj", "boll", "rsi"])
]
print(f" 指标列数: {len(indicator_cols)}")
print(f" 指标列: {', '.join(indicator_cols)}")
print(f"\n 最后3行数据:")
print(
df_result[["close"] + [col for col in indicator_cols if "rsi" in col]].tail(3)
)
print("\n" + "=" * 60)
print("测试完成!")
print("=" * 60)