Troubleshooting Differences Between Python and TradingView STC
Summary
The document investigates why several Python implementations of the Schaff Trend Cycle (STC) produce values that differ from a TradingView chart for a BTC/USDT example. It lists library calls and a custom implementation built from MACD, stochastic calculations, and exponential moving averages, while noting that settings and data sources are part of the comparison. The example uses five-minute exchange data and reports a chart value that the tested implementations did not reproduce.
A brief answer suggests that the custom stochastic calculation may be responsible and recommends calculating rolling highs and lows over the selected window before normalizing the input. This points to a possible implementation error, but it does not establish that this change alone explains every discrepancy. Differences in parameters, warm-up history, candle data, timestamps, and platform conventions can also affect indicator values; the document provides no systematic comparison to isolate them.
Key ideas
- The Schaff Trend Cycle combines MACD and stochastic-style smoothing steps.
- Different libraries and custom code can return values that disagree with a charting platform.
- The suggested correction is to compute rolling maxima and minima over the lookback window.
- Matching inputs, historical warm-up data, and candle conventions is necessary for a meaningful comparison.
Tags
Full text
# TradingView STC vs any python STC
# 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:
---------------- 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:
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
## Answer by Lamari Alaa (score 0)
https://quant.stackexchange.com/a/70976
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
```Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.