Pharos
TAStatistics

TA.LINEARREG_SLOPE() - 线性回归斜率

线性回归斜率 (Linear Regression Slope)

计算线性回归线的斜率,用于判断趋势方向和强度。

返回统计函数目录


语法

python
TA.LINEARREG_SLOPE(close, timeperiod=14)

参数

参数名类型必选默认值说明
closeany-收盘价数组
timeperiodany-回归周期,默认 14

返回值

返回斜率数组:

  • 正值:上升趋势
  • 负值:下降趋势
  • 接近0:横盘震荡

计算方法

线性回归斜率表示回归线的倾斜程度:

plaintext
Slope = b = Σ((x - x̄)(y - ȳ)) / Σ((x - x̄)²)

其中:
x = 时间索引
y = 价格
x̄ = 时间平均值
ȳ = 价格平均值

斜率的绝对值越大,趋势越强。

信号解读

斜率数值

  • slope > 0.1:强势上升趋势
  • 0 < slope < 0.1:弱上升趋势
  • -0.1 < slope < 0:弱下降趋势
  • slope < -0.1:强势下降趋势

斜率变化

  • 斜率由负转正:趋势反转向上
  • 斜率由正转负:趋势反转向下
  • 斜率扩大:趋势加速
  • 斜率缩小:趋势减弱

基础示例

趋势强度判断

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 50:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        
        # 计算线性回归斜率
        slope = TA.LINEARREG_SLOPE(closes, 14)
        current_slope = slope[-1]
        
        Log(f"线性回归斜率: {current_slope:.4f}")
        
        # 判断趋势
        if current_slope > 0.1:
            Log("✅ 强势上升趋势")
        elif current_slope > 0:
            Log("📈 弱上升趋势")
        elif current_slope > -0.1:
            Log("📉 弱下降趋势")
        else:
            Log("❌ 强势下降趋势")
        
        Sleep(60000)

高级应用

趋势反转检测

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 100:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        
        # 计算斜率
        slope = TA.LINEARREG_SLOPE(closes, 20)
        
        # 检测趋势反转
        if slope[-2] < 0 and slope[-1] > 0:
            Log("✅ 斜率由负转正,趋势反转向上")
            # exchange.Buy(...)
        elif slope[-2] > 0 and slope[-1] < 0:
            Log("❌ 斜率由正转负,趋势反转向下")
            # exchange.Sell(...)
        
        # 趋势加速
        if slope[-1] > slope[-2] and slope[-2] > slope[-3]:
            if slope[-1] > 0:
                Log("📈 上升趋势加速")
            else:
                Log("📉 下降趋势加速")
        
        Sleep(60000)

斜率背离策略

python
def main():
    while True:
        records = exchange.GetRecords()
        if len(records) < 100:
            Sleep(1000)
            continue
        
        closes = [r['Close'] for r in records]
        
        # 价格和斜率
        slope = TA.LINEARREG_SLOPE(closes, 20)
        
        # 价格创新高但斜率减弱
        if closes[-1] > max(closes[-20:-1]):
            if slope[-1] < slope[-5]:
                Log("⚠️ 顶背离:价格新高但斜率减弱")
        
        # 价格创新低但斜率减弱  
        if closes[-1] < min(closes[-20:-1]):
            if slope[-1] > slope[-5]:
                Log("💡 底背离:价格新低但斜率减弱")
        
        Sleep(60000)

参数优化建议

周期推荐值适用场景
短期7-10日内交易
中期14-20波段交易
长期30-50趋势跟踪

与其他指标配合

斜率 + 线性回归

python
lr = TA.LINEARREG(closes, 20)
slope = TA.LINEARREG_SLOPE(closes, 20)

# 上升趋势 + 价格回调
if slope[-1] > 0 and closes[-1] < lr[-1]:
    Log("趋势向上,价格回调,买入机会")

斜率 + RSI

python
slope = TA.LINEARREG_SLOPE(closes, 20)
rsi = TA.RSI(closes, 14)

# 强势上升 + RSI未超买
if slope[-1] > 0.1 and rsi[-1] < 70:
    Log("强势上升且未超买")

注意事项

  1. 单位敏感:斜率值受价格单位影响
  2. 阈值调整:不同资产需调整判断阈值
  3. 配合价格:斜率应与价格走势结合分析
  4. 周期影响:长周期更平滑,短周期更敏感
  5. 横盘市场:震荡市斜率接近0,信号不明显

相关指标

实战要点

买入信号

✅ 斜率由负转正(反转) ✅ 斜率 > 0.1(强势上升) ✅ 斜率增大(趋势加速)

卖出信号

❌ 斜率由正转负(反转) ❌ 斜率 < -0.1(强势下降) ❌ 斜率减弱(趋势衰竭)


返回统计函数目录 | 返回 TA 指标总目录