twopirllc / pandas-ta

Technical Analysis Indicators - Pandas TA is an easy to use Python 3 Pandas Extension with 150+ Indicators
https://twopirllc.github.io/pandas-ta/
MIT License
5.27k stars 1.03k forks source link

kama: invalid value encountered in double_scalars #197

Closed melikesofta closed 3 years ago

melikesofta commented 3 years ago

Which version are you running? The lastest version is on Github. Pip is for major releases.

0.2.23b0

Describe the bug

project-path/env/lib/python3.7/site-packages/pandas_ta/overlap/kama.py:36: RuntimeWarning: invalid value encountered in double_scalars
  result.append(sc[i] * close[i] + (1 - sc[i]) * result[i - 1])

For some tickers and some resample options (e.g. this one is for 15 mins of intervals), sc[i] hits infinite, breaking the calculation of all following kama values.

Screenshot 2021-01-25 at 13 27 51

To Reproduce Following data is ohlc and calculated kama for GUBRF from XIST; so happens in Turkey that when a ticker hits 10% profit on a day, it's stabilized at that value for the rest of the day (sometimes several days) so the close value won't change. Basically, for any ticker that this condition occurs, no kama values are calculated after that point thus we can't use that indicator in those tickers.

Screenshot 2021-01-25 at 13 27 17

My usage is:

        df.ta.kama(length=21, append=True)

over the ohlc resample

Expected behavior

inf values should be handled / kama should still be calculated in such cases.

Screenshot 2021-01-25 at 13 32 56

Extra Not sure if this is a direct bug or if it's something I can work around by manipulating something in my data. Let me know if you have any suggestions please, or if you need any help replicating the issue! Thanks

twopirllc commented 3 years ago

Hello @melikesofta,

project-path/env/lib/python3.7/site-packages/pandas_ta/overlap/kama.py:36: RuntimeWarning: invalid value encountered in double_scalars
  result.append(sc[i] * close[i] + (1 - sc[i]) * result[i - 1])

For some tickers and some resample options (e.g. this one is for 15 mins of intervals), sc[i] hits infinite, breaking the calculation of all following kama values.

That's certainly not good.

To Reproduce Following data is ohlc and calculated kama for GUBRF from XIST; so happens in Turkey that when a ticker hits 10% profit on a day, it's stabilized at that value for the rest of the day (sometimes several days) so the close value won't change. Basically, for any ticker that this condition occurs, no kama values are calculated after that point thus we can't use that indicator in those tickers.

Ohhh... that is interesting! I can see how that can be problematic.

Expected behavior inf values should be handled / kama should still be calculated in such cases.

Of course, these cases should be handled appropriately.

Extra Not sure if this is a direct bug or if it's something I can work around by manipulating something in my data. Let me know if you have any suggestions please, or if you need any help replicating the issue!

I think this may be a direct bug because of the 10% profit temporary lock. Do you have other dates or tickers so I can download and test different cases?

Also who is the author of the KAMA indicator on TradingView you showed? Are you able to show the source of the TradingView KAMA indicator?

If the author is DWSKTrader, they appear to be using Heiken Ashi for their calculation of KAMA. Have you tried converting to Heiken Ashi and then running KAMA? For instance, something like this:

import pandas as pd
import pandas_ta as ta

df = # your ohlcv data
heikenashi = df.ta.ha()
heikenashi.columns = ["open", "high", "low", "close"]
heikenashi.ta.kama(append=True)
heikenashi

Please let me know what happens and if you have additional info that you can provide. Unfortunately, I am currently back logged on Issues and enhancements so I have no resolution on when this will be resolved. If you want to take a shot at fixing this, I and the community would greatly appreciate it.

Kind Regards, KJ

twopirllc commented 3 years ago

Hello @melikesofta,

I assume by no response that the solution provided was sufficient. Thus I will be closing this issue in a few days.

Kind Regards, KJ

melikesofta commented 3 years ago

Hello @twopirllc, Sorry I missed the notification on your first reply. The issue is still there for me unfortunately.

I think this may be a direct bug because of the 10% profit temporary lock. Do you have other dates or tickers so I can download and test different cases?

Sure; GUBRF hit the lock again 28 Jan, 21 SEKUR on 29 Jan, 21 KAPLM 27 Jan, 21

Another way to replicate the issue would be e.g. to fill a single day with the same close values for a 15 mins resampled dataframe. I find that the issue occurs when we have large-ish amounts of repetitive values.

Also who is the author of the KAMA indicator on TradingView you showed? Are you able to show the source of the TradingView KAMA indicator? If the author is DWSKTrader, they appear to be using Heiken Ashi for their calculation of KAMA. Have you tried converting to Heiken Ashi and then running KAMA? For instance, something like this:

Indeed I was looking at the indicator by DWSKTrader. I will certainly look at the Hekien Ashi aspect.

Please let me know what happens and if you have additional info that you can provide. Unfortunately, I am currently back logged on Issues and enhancements so I have no resolution on when this will be resolved. If you want to take a shot at fixing this, I and the community would greatly appreciate it.

I have some higher-prio issues in the project so I might not get to this right away, but either this week or the next I'll try to give it a shot and see if I can submit a fix. Unfort I am not super familiar with the math underneath the indicators, but I'll try some stuff out looking at the source codes in tradingview, and the heiken ashi thing you mentioned.

Thanks for following up!

twopirllc commented 3 years ago

Hello @melikesofta,

Sorry I missed the notification on your first reply. The issue is still there for me unfortunately.

No worries. Also I recommend being on the latest version of the library. Though this Issue is not solved by the latest version, but it is recommended to being up to date.

$ pip install -U git+https://github.com/twopirllc/pandas-ta

Sure; GUBRF hit the lock again 28 Jan, 21 SEKUR on 29 Jan, 21 KAPLM 27 Jan, 21

Another way to replicate the issue would be e.g. to fill a single day with the same close values for a 15 mins resampled dataframe. I find that the issue occurs when we have large-ish amounts of repetitive values.

Great! Thank you. Saves me some time.

Indeed I was looking at the indicator by DWSKTrader. I will certainly look at the Hekien Ashi aspect.

Ok. Let me know if it helps. Does DWSKTrader's indicator work as you want? Perhaps I can incorporate a Heiken Ashi option into kama.

I have some higher-prio issues in the project so I might not get to this right away, but either this week or the next I'll try to give it a shot and see if I can submit a fix. Unfort I am not super familiar with the math underneath the indicators, but I'll try some stuff out looking at the source codes in tradingview, and the heiken ashi thing you mentioned.

Understandable, no worries. I am trying to avoid dead and abandoned Issues piling up so I can prioritize Issues appropriately. Please keep me posted when you can.

Kind Regards, KJ

twopirllc commented 3 years ago

Hello @melikesofta,

Reopen this Issue when you get a chance to work on it again.

Thanks, KJ