You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python计算StochRSI与Kraken图表结果不符的问题排查

问题

我正在为加密货币市场分析计算StochRSI指标,目标是将其集成到交易机器人中用于制定交易决策。我使用ccxt库从Kraken获取市场数据,但计算出的StochRSI数值与Kraken图表显示的数值存在显著差异。尝试直接调用Kraken API获取数据后,结果仍不匹配。

我的核心步骤:

  • 使用ccxt从Kraken获取历史价格数据
  • 按标准公式计算StochRSI:StochRSI = (RSI - Lowest RSI) / (Highest RSI - Lowest RSI)
  • 将计算值与Kraken图表对比,二者不匹配

请问是什么原因导致数值差异?Kraken是否应用了我忽略的特定计算方法或数据调整?希望得到指导或建议。

附计算代码:

import ccxt
import pandas as pd

# Initialize the Kraken client with your API credentials (replace with your own)
kraken = ccxt.kraken({
    'apiKey': "XXX",
    'secret': "XXX",
})

trade_symbol = 'ETH/USDT'
stochrsi_period = 14

def calculate_stochrsi(prices, period=14):
    # Ensure 'prices' is a pandas Series
    prices = pd.Series(prices)

    # Calculate the price differences
    delta = prices.diff(1)
    gain = delta.clip(lower=0)  # Keep only gains
    loss = -delta.clip(upper=0)  # Keep only losses

    # Calculate the average gain and loss
    avg_gain = gain.rolling(window=period).mean()
    avg_loss = loss.rolling(window=period).mean()

    # Calculate the RSI
    rs = avg_gain / avg_loss
    rsi = 100 - (100 / (1 + rs))

    # Calculate the StochRSI
    min_rsi = rsi.rolling(window=period).min()
    max_rsi = rsi.rolling(window=period).max()
    stochrsi = (rsi - min_rsi) / (max_rsi - min_rsi)

    return stochrsi

# Fetch OHLC data from Kraken with a 1-hour interval
print('Fetching OHLC data...')
ohlc_data = kraken.fetch_ohlcv(trade_symbol, timeframe='1h', limit=100)
df = pd.DataFrame(ohlc_data, columns=['timestamp', 'open', 'high', 'low', 'close', 'volume'])
closing_prices = df['close']

# Calculate the StochRSI
print('Calculating StochRSI...')
stochrsi = calculate_stochrsi(closing_prices)

# Print the StochRSI values
print(stochrsi)
分析与解决方案

以下是导致数值差异的常见原因及对应修正方案:

1. RSI平滑方式不匹配

Kraken的RSI默认使用**指数移动平均(EMA)**计算平均涨跌幅度,而你的代码用了rolling(window=period).mean()(简单移动平均SMA),这是核心差异之一。

修正代码:

# 替换原avg_gain和avg_loss的计算逻辑
avg_gain = gain.ewm(span=period, adjust=False).mean()
avg_loss = loss.ewm(span=period, adjust=False).mean()

2. StochRSI缺少平滑处理

多数交易平台的StochRSI会对基础计算结果做额外平滑,比如添加3期SMA作为%D线,而你当前代码仅输出了未平滑的原始StochRSI(对应%K线)。

修正代码:

# 在计算完基础stochrsi后添加平滑步骤
stochrsi_k = stochrsi.rolling(window=3).mean()  # 对应平台显示的%K线
stochrsi_d = stochrsi_k.rolling(window=3).mean()  # 对应平台显示的%D线

3. 未过滤未闭合K线

Kraken图表显示的是已完全闭合的K线数据,而ccxt获取的OHLCV可能包含最新的未闭合K线(收盘价随实时行情变动),导致指标计算偏差。

修正代码:

# 过滤未闭合的1小时K线(Kraken时间戳为毫秒级)
current_timestamp = pd.Timestamp.now().timestamp()
last_closed_hour = int(current_timestamp // 3600) * 3600
df = df[df['timestamp'] < last_closed_hour * 1000]
closing_prices = df['close']

4. 价格数据源选择差异

你的代码用收盘价计算StochRSI,但部分平台可能基于典型价格((high+low+close)/3)或加权收盘价计算,而非单纯收盘价。

修正代码:

# 改用典型价格计算
df['typical_price'] = (df['high'] + df['low'] + df['close']) / 3
closing_prices = df['typical_price']

5. 历史数据量不足

Kraken图表可能用更长的历史数据计算初始指标值,你仅获取100条K线会导致前period*2个周期的指标因数据不足出现偏差。

修正代码:

# 增加获取的K线数量
ohlc_data = kraken.fetch_ohlcv(trade_symbol, timeframe='1h', limit=200)

内容的提问来源于stack exchange,提问作者user11278201

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 06:25:08