Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SuperTrend values are not matching with Zerodha #819

Open
prashantrpatil opened this issue Aug 17, 2024 · 9 comments
Open

SuperTrend values are not matching with Zerodha #819

prashantrpatil opened this issue Aug 17, 2024 · 9 comments
Labels
bug Something isn't working

Comments

@prashantrpatil
Copy link

pandas 2.2.2 and pandas_ta 0.3.14b0
Do you have TA Lib also installed in your environment?
No
Have you tried the development version? Did it resolve the issue?
No

Issue:
I am calculating super trend value using ta but my output is not matching with other platforms like Zerodha. And difference is not in decimal points here and there but it is huge.

What i have tried:
instead of rma tried to send sma as mamode while calculating ATR but still values are not matching.

@MLpranav
Copy link

Can you export some sample data from Zerodha after putting supertrend on the chart, so both OHLC and supertrend values are exported, and then try pandas-ta on the same OHLC values with both RMA and SMA mamode. If they still don't match, you can share the exported data here along with your code and I can take a look.

@prashantrpatil
Copy link
Author

prashantrpatil commented Aug 19, 2024

Hi MLpranav,
Thanks for reply.
oneMinDF1_rma.csv
oneMinDF1_sma.csv
attaching two files oneMinDF1_rma and oneMinDF1_sma. Please compare columns 'ST ZERODHA (7,3)' and 'ST-ta'.

At some places values with sma are matching at some places values with rma are matching and some are not matching at all.

i have used following code to generate files.
csvFilePath = os.path.join(dir, "MIDCPNIFTY_19th_AUG_12800_CE.csv")
oneMinDF = pd.read_csv(csvFilePath)
oneMinDF[['into', 'inth','intl', 'intc', 'intv']] = oneMinDF[['into', 'inth','intl', 'intc', 'intv']].apply(pd.to_numeric)
oneMinDF["date"] = pd.to_datetime(oneMinDF["date"], yearfirst=True)
oneMinDF.set_index('date', inplace=True)
oneMinDF.sort_values('date', inplace=True)
oneMinDF['vwap-ta'] = ta.vwap(oneMinDF.inth, oneMinDF.intl, oneMinDF.intc, oneMinDF.intv)
dummyDF = pd.DataFrame(ta.supertrend(high= oneMinDF.inth, low=oneMinDF.intl,
close=oneMinDF.intc,length=7,multiplier=3).round(2))
oneMinDF['ST-ta'] = dummyDF['SUPERT_7_3.0']
oneMinDF['STD-ta'] = dummyDF['SUPERTd_7_3.0']
oneMinDF.to_csv('oneMinDF1.csv')

@MLpranav
Copy link

RMA values are matching 09:53 onwards. Values before that differ because the Kite chart is also using previous days' data for supertrend calculation, but pandas-ta is getting only the current day's data.

Theoretically supertrend(7, 3) requires only 7+1 candles to compute, but I have seen in practice that at least 10-15 times the number of candles should be given to get the most accurate result.

This happens because supertrend formula gives value of both upper and lower band, and the current band depends on where has the price crossed the ST. And that usually falls beyond the 8 candle lookback period.

@twopirllc twopirllc changed the title SuperTrend values are not matching with Zerodha or other leading platforms. SuperTrend values are not matching with Zerodha Aug 20, 2024
@prashantrpatil
Copy link
Author

@MLpranav, Thanks for the reply

@prashantrpatil
Copy link
Author

prashantrpatil commented Aug 27, 2024

@MLpranav @twopirllc
On regular basis i am comparing values with zerodha. Values for first 20 mins are not matching. Ideally values should match after first 7 one minute candles for supertrend(7,3)
Is there any workaround for this to make it work after first 7 one min candles?

@MLpranav
Copy link

MLpranav commented Aug 28, 2024

Theoretically supertrend(7, 3) requires only 7+1 candles to compute, but I have seen in practice that at least 10-15 times the number of candles should be given to get the most accurate result.

As mentioned before, you need to pass previous 2-3 days data as well to get matching values. Try to give around 100 candles.

Zerodha is also using previous few days' data and not just 7 candles.

@prashantrpatil
Copy link
Author

Hi @MLpranav,

BANKNIFTY AUG 51200 CE_ Zerodha.csv
BANKNIFTY28AUG24C51200oneMinDF_ta.csv

Today got horrible difference specially in between period 9.34 to 10.04 AM as attached in above files.

Hi i have tried almost all possible things like -

  1. Passing last 2/3 days data
  2. changing mamode sma/rma (as you suggested in your post Supertrend Value Mismatch - proposal to add both implementations #724)
  3. using SMA instead of ATR (as you suggested in your post Supertrend Value Mismatch - proposal to add both implementations #724)

In your post you mentioned that your ta ST(7,3) values started matching with zerodha after doing certain changes. Can you please try to calculate ST values with your code and attached data if those matches at your end?

@twopirllc
Copy link
Owner

Hello @prashantrpatil @MLpranav

Before you dig too far into the weeds of this, I recommend basing your decuctions on the development code and not version 0.3.14b.

Second, this library is a complementary (or supplementary) to first TA Lib and second (where applicable) TV. However if anyone wants to include variations for blackboxed systems like Zerodha, ToS, Tradestation, et al, then by all means checkout the development branch, make an optional flag (like supertrend(*, zerodha=None, **args), implement flagged option, test till satisfied, and finally submit a PR.

Third. Before I update the indicator, supertrend like other deep conditional based loop indicators, will need a numba version to boost performance. For an example numba implementation, see atrts.

Unfortunately, this is the quickest and surest way to get the changes implemented into the library.

Kind Regards,
KJ

@MLpranav
Copy link

Zerodha offers two charting backends - TradingView and ChartIQ.

https://documentation.chartiq.com/tutorial-Using%20and%20Customizing%20Studies%20-%20Definitions.html#supertrend

I've gone down this rabbit hole a while back and can confirm that the current supertrend implementation is correct and perfectly matches with Zerodha.

Two most common reasons for a discrepancy are - variation in the values of atr_mamode (Zerodha earlier used 'SMA' but now uses 'RMA' if I recall correctly) and more importantly, using a very short lookback window (passing 10-15x as many candles as the supertrend period is good enough in my experience).

@prashantrpatil, do try passing at least past 1 whole day's data in addition to the current day and then check the values, if still not matching then try with past 2 or 3 days. Check with RMA (which is the default) and try SMA also if the values still don't match.

If you are still unable to match the values, please share 3 entire days data and your complete code and I can take a look.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants