QTrade/src/indicator_calculator/rsi_indicator.py
cuizhan 0a1bbefffb refactor: 重组src目录结构,职责更清晰
**目录结构调整:**
- data_provider/data_downloader/  data_downloader/ (下载器独立目录)
- indicators/  indicator_calculator/ (更明确的命名)
- 删除空的data_provider/目录

**更新导入路径:**
- 所有相对导入改为绝对导入(from src.xxx)
- 修复循环导入问题
- 更新所有受影响的模块

**新的src目录结构:**
src/
 3rdparts/              # 第三方集成
 cache/                 # 缓存管理
 core/                  # 核心配置和工具
 data/                  # 数据服务层
 database_manager/      # 数据库管理
 data_downloader/       # 数据下载器
 gui/                   # GUI界面
 indicator_calculator/  # 指标计算器
 main.py

**测试验证:**
- quick_test.py 全部通过
- 所有导入路径正确
- 职责划分更清晰明确
2025-12-19 07:33:08 +08:00

90 lines
2.6 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指标计算器"""
from typing import Dict, List
import numpy as np
import pandas as pd
import talib
from .base_indicator import BaseIndicator
class RSIIndicator(BaseIndicator):
"""RSI(Relative Strength Index,相对强弱指标)计算器
RSI指标是一种动量振荡指标,用于衡量价格变动的速度和变化:
- RSI值范围:0-100
- 超买区域:RSI > 70
- 超卖区域:RSI < 30
- 中性区域:30-70
通常同时计算多个周期的RSI(如6、12、24),形成RSI指标组合
"""
def __init__(self, config: Dict | None = None):
"""初始化RSI指标计算器
Args:
config: 配置参数,包含:
- fast: 快速周期,默认6
- mid: 中速周期,默认12
- slow: 慢速周期,默认24
"""
super().__init__(config)
def _validate_config(self):
"""验证配置参数"""
# 设置默认值
self.config.setdefault("fast", 6)
self.config.setdefault("mid", 12)
self.config.setdefault("slow", 24)
# 验证参数
if self.config["fast"] <= 0:
raise ValueError("fast必须大于0")
if self.config["mid"] <= 0:
raise ValueError("mid必须大于0")
if self.config["slow"] <= 0:
raise ValueError("slow必须大于0")
def calculate(self, df: pd.DataFrame) -> pd.DataFrame:
"""计算RSI指标
Args:
df: K线数据DataFrame
Returns:
添加了RSI指标列的DataFrame
"""
self._validate_dataframe(df)
# 复制DataFrame避免修改原始数据
result = df.copy()
fast_period = self.config["fast"]
mid_period = self.config["mid"]
slow_period = self.config["slow"]
# 获取收盘价数组
close_array = np.asarray(result["close"].values, dtype=np.float64)
# 使用ta-lib计算不同周期的RSI
# 列名使用rsi_fast、rsi_mid、rsi_slow,更直观易懂
result["rsi_fast"] = talib.RSI(close_array, timeperiod=fast_period)
result["rsi_mid"] = talib.RSI(close_array, timeperiod=mid_period)
result["rsi_slow"] = talib.RSI(close_array, timeperiod=slow_period)
return result
def get_column_names(self) -> List[str]:
"""获取RSI指标的列名"""
return ["rsi_fast", "rsi_mid", "rsi_slow"]
@property
def name(self) -> str:
"""指标名称"""
return "rsi"
@property
def description(self) -> str:
"""指标描述"""
return "RSI指标(相对强弱指标)"