[Python] StochRSI Calculation in Python Produces Different Results Than Kraken Chart

Discussão em 'Python' iniciado por Stack, Janeiro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to calculate the StochRSI indicator for cryptocurrency market analysis, with the goal of integrating it into a trading bot to make trade decisions. I'm retrieving market data from Kraken using the ccxt library.

    However, the StochRSI values I calculate differ significantly from what I see on Kraken's charts. I also attempted to fetch the data directly from the Kraken API, but the results still did not match the information displayed on the Kraken chart.

    Here are the key steps in my approach:

    I retrieve the historical price data using ccxt for Kraken. I calculate the StochRSI using the standard formula:

    StochRSI = (RSI - Lowest RSI) / (Highest RSI - Lowest RSI)


    I compare the calculated values to those on the Kraken chart, but they don’t match. What could be causing the discrepancy between my calculated StochRSI values and the values displayed on Kraken? Is there a specific calculation method or data adjustment that Kraken applies which I may be missing? Any guidance or suggestions would be appreciated.

    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)

    Continue reading...

Compartilhe esta Página