ta-lib-python icon indicating copy to clipboard operation
ta-lib-python copied to clipboard

Clarification on your documentation

Open couzhei opened this issue 9 months ago • 1 comments

In your documentation you say:

If you wonder why STOCHRSI gives you different results than you expect, probably you want STOCH applied to RSI, which is a little different than the STOCHRSI which is STOCHF applied to RSI:

>>> import talib
>>> import numpy as np
>>> c = np.random.randn(100)

# this is the library function
>>> k, d = talib.STOCHRSI(c)

# this produces the same result, calling STOCHF
>>> rsi = talib.RSI(c)
>>> k, d = talib.STOCHF(rsi, rsi, rsi)

# you might want this instead, calling STOCH
>>> rsi = talib.RSI(c)
>>> k, d = talib.STOCH(rsi, rsi, rsi)

I happened to be that guy in the description, unfortunately. This is a little confusing to me, what are k and d!? Why do you give rsi 3 times to STOCH? You can safely ignore the comments, but I mentioned them, if you're curious what my intentions are for calculation of this indicator.

Here's how I used your library in my app:

import talib as ta

def stoch_rsi(series, stoch_length=14, rsi_length=14, smooth_k=3, smooth_d=3):
    # rsi_serie = rsi(series, rsi_length)

    # stochrsi = (rsi_serie - rsi_serie.rolling(stoch_length).min()) / (
    #     rsi_serie.rolling(stoch_length).max() - rsi_serie.rolling(stoch_length).min()
    # )
    # stochrsi_K = stochrsi.rolling(smooth_k).mean()
    # stochrsi_D = stochrsi_K.rolling(smooth_d).mean()

    # return stochrsi * 100, stochrsi_K * 100, stochrsi_D * 100
    if isinstance(series, pd.Series):
        series = series.to_numpy()

    return ta.STOCHRSI(
        series,
        timeperiod=stoch_length,
        fastk_period=smooth_k,
        fastd_period=smooth_d,
    )

couzhei avatar Feb 19 '25 03:02 couzhei

What are k and d!?

The fast (%K) and slow (%d) stochastic oscillator.

Why do you give rsi 3 times to STOCH?

The STOCH takes a "high", "low", and "close" price series arguments.

The STOCHF is described as the "fast stochastic", but basically it's not smoothed like the STOCH version. That's sometimes what people want.

/*
 * TA_STOCH - Stochastic
 * 
 * Input  = High, Low, Close
 * Output = double, double
 * 
 * Optional Parameters
 * -------------------
 * optInFastK_Period:(From 1 to 100000)
 *    Time period for building the Fast-K line
 * 
 * optInSlowK_Period:(From 1 to 100000)
 *    Smoothing for making the Slow-K line. Usually set to 3
 * 
 * optInSlowK_MAType:
 *    Type of Moving Average for Slow-K
 * 
 * optInSlowD_Period:(From 1 to 100000)
 *    Smoothing for making the Slow-D line
 * 
 * optInSlowD_MAType:
 *    Type of Moving Average for Slow-D
 * 
 * 
 */
STOCH(high, low, close, fastk_period=-2147483648, slowk_period=-2147483648, slowk_matype=0, slowd_period=-2147483648, slowd_matype=0)
    STOCH(high, low, close[, fastk_period=?, slowk_period=?, slowk_matype=?, slowd_period=?, slowd_matype=?])

    Stochastic (Momentum Indicators)

    Inputs:
        prices: ['high', 'low', 'close']
    Parameters:
        fastk_period: 5
        slowk_period: 3
        slowk_matype: 0
        slowd_period: 3
        slowd_matype: 0
    Outputs:
        slowk
        slowd



/*
 * TA_STOCHF - Stochastic Fast
 * 
 * Input  = High, Low, Close
 * Output = double, double
 * 
 * Optional Parameters
 * -------------------
 * optInFastK_Period:(From 1 to 100000)
 *    Time period for building the Fast-K line
 * 
 * optInFastD_Period:(From 1 to 100000)
 *    Smoothing for making the Fast-D line. Usually set to 3
 * 
 * optInFastD_MAType:
 *    Type of Moving Average for Fast-D
 * 
 * 
 */
STOCHF(high, low, close, fastk_period=-2147483648, fastd_period=-2147483648, fastd_matype=0)
    STOCHF(high, low, close[, fastk_period=?, fastd_period=?, fastd_matype=?])

    Stochastic Fast (Momentum Indicators)

    Inputs:
        prices: ['high', 'low', 'close']
    Parameters:
        fastk_period: 5
        fastd_period: 3
        fastd_matype: 0
    Outputs:
        fastk
        fastd

mrjbq7 avatar Feb 23 '25 00:02 mrjbq7

If you think we should add anything to the documentation based on the above, let me know!

mrjbq7 avatar Aug 08 '25 18:08 mrjbq7