Supertend - different values comparing with Tradingview
sivo1981 opened this issue · comments
BTCUSDT Perpetual - 1 day - ST (period = 10, multiplier = 3)
https://www.tradingview.com/chart/?symbol=BINANCE%3ABTCUSDTPERP
2022-07-01 H: 20926.4 L: 18948.8 C: 19272 Supertrend-Debut: undefined Supertend-TW: 23675.0
2022-07-02 H: 19456 L: 18959 C: 19240.9 Supertrend-Debut: undefined Supertend-TW: 23382.5
2022-07-03 H: 19645.4 L: 18780.1 C: 19305.9 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-04 H: 20350 L: 19046.3 C: 20226.6 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-05 H: 20749.2 L: 19291.6 C: 20170 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-06 H: 20667.6 L: 19753 C: 20554.1 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-07 H: 21830 L: 20244.9 C: 21613.4 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-08 H: 22579.9 L: 21175.2 C: 21587.9 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-09 H: 22000 L: 21309.6 C: 21580 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-10 H: 21594.2 L: 20600 C: 20853.8 Supertrend-Debut: undefined Supertend-TW: 23229.8
2022-07-11 H: 20853.9 L: 19870 C: 19954.3 Supertrend-Debut: 23570.9 Supertend-TW: 23229.8
2022-07-12 H: 20048.9 L: 19228.3 C: 19322.9 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-13 H: 20401.6 L: 18901 C: 20220.1 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-14 H: 20900 L: 19600 C: 20578.6 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-15 H: 21200 L: 20364.3 C: 20823.1 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-16 H: 21580 L: 20471 C: 21183.5 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-17 H: 21674.1 L: 20742 C: 20794.4 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-18 H: 22800 L: 20764.4 C: 22423.9 Supertrend-Debut: 22772.835 Supertend-TW: 23179.9
2022-07-19 H: 23817.9 L: 21575 C: 23385.9 Supertrend-Debut: 18871.8084752485 Supertend-TW: 18702.9
2022-07-20 H: 24277 L: 22912.4 C: 23221.5 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-21 H: 23441.1 L: 22340.2 C: 23144.5 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-22 H: 23750 L: 22500 C: 22675.1 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-23 H: 22990 L: 21933.5 C: 22440.2 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-24 H: 22999 L: 22256.4 C: 22570.6 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-25 H: 22656.8 L: 21232 C: 21300 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-26 H: 21334.4 L: 20689.6 C: 21241 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-27 H: 23100 L: 21031 C: 22941.1 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-28 H: 24194.6 L: 22583.1 C: 23836.2 Supertrend-Debut: 19743.14262772365 Supertend-TW: 19567.9
2022-07-29 H: 24500 L: 23426.3 C: 23771.1 Supertrend-Debut: 20168.013285324443 Supertend-TW: 20100.1
2022-07-30 H: 24700 L: 23505 C: 23629.9 Supertrend-Debut: 20328.376956791995 Supertend-TW: 20267.3
2022-07-31 H: 24187.3 L: 23222 C: 23290.1 Supertrend-Debut: 20328.376956791995 Supertend-TW: 20267.3
Supertrend values are quite different from what is published on Tradingview. Also direction is completely wrong based on this data.
Please advise if i'm doing something wrong beacuse i don't get the same values.
in tradingview default smoothing value for ATR indicator (used for sub calculations in SuperTrend) looks like this
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
i current indicators SDK default smooth value for ATR is 'WEMA'.
I published patch in debut/indicators@1.3.10
, now you can change default smoothing type for SuperTrend indicator, please try to do like this:
const st = new SuperTrend(10, 3, 'RMA');
The third argument added in patch and allow configure smoothing MA type for ATR indicator, please check all of it and compare with TradingView, let me know about results, thanks for reporting
I have tested this fix and the values are still not the same as on TW. I have used new RMA smoothing but looks like there is a problem when starting with OHLC from start of the month. I guess there is problem with historic data.
What do you think?
After further debuging i have found out that the problem is in ATR indicator with RMA smoothing.
- Issue
ATR indicator uses getTrueRange function which doesn't work correctly.
TW description of getTrueRange:
handle_na (simple bool) How NaN values are handled. if true, and previous day's close is NaN then tr would be calculated as current day high-low. Otherwise (if false) tr would return NaN in such cases. Also note, that ta.atr uses ta.tr(true).
Currently getTrueRange returns undefined if prevClose is undefined. It should return high - low
.
- Issue
After manually patching 1. issue i have test ATR with RMA and the values are still not the same. Then i have tried ATR with SMA and values are OK. Looks like there is something also wrong with the RMA calculcation.
Thanks for clarify! I Add units for SuperTrend and True Range and bug was founded.
Check it with 'SMA' type smoothing.
- @debut/indicators@1.3.11
Tested Supertrend with SMA smoothing and it works OK. If i test with RMA it doesn't work correctly. This is beacuse the way RMA is calculated which is used in ATR. If prevValue
in RMA is undefined then SMA for whole period should be used as first value but this is not the case here.
Can you check please.
I have attached excel comparison for ATR (period = 10) with SMA | RMA with Tradingview. See attached file.
ATR comparison.xlsx
RMA is not correct in TradingView, because its actually not RMA. Let's check it!
Debut RMA - it is "Relative Moving Average".
TradingView RMA (as we can see it from description)
it is Exponentially Weighted Moving Average . Correct name for TV RMA - is EWMA
. Looks like they have wrong name for function. It is most popular problem with Moving Average. Nobody know how to do correct naming. For example RMA
what is that means : Rounded Moving Average or Rolling Moving Average or Relative Moving Average - all this MA's have a different formulas to calc. This is big problem, but i do in my package with hard standards of naming and formulas validated many times.
The formula for RMA in TV is the same and correct. Source code:
//@version=5
indicator("ta.rma")
plot(ta.rma(close, 15))
//the same on pine
pine_rma(src, length) =>
alpha = 1/length
sum = 0.0
sum := na(sum[1]) ? ta.sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1])
plot(pine_rma(close, 15))
The formula for RMA in TV is the same and correct. Source code:
//@version=5 indicator("ta.rma") plot(ta.rma(close, 15)) //the same on pine pine_rma(src, length) => alpha = 1/length sum = 0.0 sum := na(sum[1]) ? ta.sma(src, length) : alpha * src + (1 - alpha) * nz(sum[1]) plot(pine_rma(close, 15))
Strange, description are different with implementation :) EWMA but.... RMA actually :)
RMA comparison (period = 10). See attached file.
RMA comparison.xlsx
RMA comparison (period = 10). See attached file. RMA comparison.xlsx
I have checked RMA, and nothing it there... formula are same. I have one idea. TradingView used pre-calculated values, that mean the calculations can have "machine precision error", when we have more price value then more error, can you try validate with some new tokens (less than 1 month history should they have) which price also not so big less than 1 - its ideal for testing. And send to me results please :)
1000SHIBUSDTPERP - Binance - 1 day
https://www.tradingview.com/chart/?symbol=BINANCE%3A1000SHIBUSDTPERP
RMA comparison (period = 10). See attached file.
I think the problem is beacuse RMA function uses also SMA for first value. After N periods SMA is initialized but RMA start calculating after N periods. TV only uses SMA for whole period at start.
Can you validate RMA at long period i think the result are sync later.... may be. Another idea - TV calculate SMA for first value on history for ticker, all next values uses previous results from history start that affect the results and make it different. You need to check results from history begin.
check it here https://www.tradingview.com/chart/UqxDzKHm/?symbol=BINANCE%3AFLOWUSDT
You are correct. Calculating from start gives the correct result. I guess nothing we can do about it, if i start using indicator in between or at some specific date.
Thanks for your help.