76 lines
2.1 KiB
Python
76 lines
2.1 KiB
Python
|
|
"""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)
|