Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TradingView STC vs any python STC

I am trying to use in a trading strategy the STC indicator, but I can not find out why its not working properly.

The chart that I am using is BTC/USDT on UTC as a timeframe.

Chart time: 01 Feb 22 - 16:20 UTC

------------------- TradingView: ------------------------

STC value: 97.66

STC settings:

enter image description here

---------------- Python: ----------------

I've tried the following libraries:

Pands ta(link):

dataframe.ta.stc(tclength=12, fast=26, slow=50, factor=0.5, append=True)

Technical indicators(link)

dataframe['stc_2'] = technical.indicators.stc(dataframe, fast=26, slow=50, length=12)

Financial Technical Analysis(link)

dataframe['stc'] = fta.STC(dataframe, period_fast=26, period_slow=50, k_period=12, d_period=3, adjust=True)

And I've also tried to recreate the indicator by converting the pine script from here to python

def stoch(source, high, low, lenght):
    return Series(100 * (source - low[-lenght:].min()) / (high[-lenght:].max() - low[-lenght:].min()))


def fixnan(s: Series):
    mask = np.isnan(s)
    s[mask] = np.interp(np.flatnonzero(mask), np.flatnonzero(~mask), s[~mask])
    return s


def nz(s: Series):
    return s.fillna(0)


def stc(ohlc: DataFrame, fast: int, slow: int, length: int, d1: int, d2: int):
    macd = ta.EMA(ohlc['close'], timeperiod=fast) - ta.EMA(ohlc['close'], timeperiod=slow)
    k = nz(fixnan(stoch(macd, macd, macd, length)))
    d = ta.EMA(k, d1)
    kd = nz(fixnan(stoch(d, d, d, length)))
    stc = ta.EMA(kd, d2)
    r1 = np.where(stc >= 100, 100, stc)
    r2 = np.where(r1 <= 0, 0, r1)
    return r2


dataframe['stc_MINE'] = stc(dataframe, 26, 50, 10, 3, 3)

Here is the output from all of them:

enter image description here

As can be seen, none of them is 97.66, could anyone explain to me what I did wrong or what am I missing?

like image 218
Mircea Avatar asked Mar 13 '26 16:03

Mircea


1 Answers

You could use the ta library from bukosabino.

I was able to get the exact same results as TradingView. Here is the usage:

from ta.trend import STCIndicator

stc = STCIndicator(dataframe["close"], window_slow=50, window_fast=26, cycle=12).stc()
dataframe["stc"] = stc
like image 170
quents Avatar answered Mar 17 '26 02:03

quents



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!