2
$\begingroup$

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?

My code:

In order to gather all the data and use the indicators I've used freqtrade

To download the data I've used:

freqtrade download-data -t 5m --pairs BCH/USDT --erase --timerange 20210101-20220101

And here is my strategy that I've used:

class Mine(IStrategy):
timeframe = '5m'

def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
    dataframe['stc'] = fta.STC(dataframe, period_fast=26, period_slow=50, k_period=12, d_period=3, adjust=True)
    dataframe['stc_2'] = technical.indicators.stc(dataframe, fast=26, slow=50, length=12)
    dataframe.ta.stc(tclength=12, fast=26, slow=50, factor=0.5, append=True)
    dataframe['stc_MINE'] = stc(dataframe, 26, 50, 10, 3, 3)

    return dataframe

def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
    return dataframe

def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
    return dataframe

Both exchanges from there the data are downloaded in TradingView and my strategy is Binance

$\endgroup$
2
  • 1
    $\begingroup$ Are you using the same data as Tradingview or data from an alternative source? Could you provide the code to get your data? This will make it much easier reproducing your results. $\endgroup$
    – Pleb
    Feb 4, 2022 at 6:53
  • $\begingroup$ @Pleb I updated my question with all the new information :) $\endgroup$
    – Mircea
    Feb 4, 2022 at 8:50

1 Answer 1

0
$\begingroup$

Maybe your stoch function is not good

try this:

def stoch(d, l):
    highest= d.rolling(l).max()
    lowest = d.rolling(l).min()
    stoch = 100 * (d - lowest) / (highest - lowest)
    return stoch
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.