question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kaufman's Adaptive Moving Average (KAMA) - calculation issue

See original GitHub issue
import yfinance as yf
from pandas_ta.overlap import kama
import mplfinance as mpf

ticker = yf.Ticker("SPY")
minute_data = ticker.history(period="7d", interval="1m")

kama_results = kama(minute_data["Close"])
addplot = [
    mpf.make_addplot(kama_results, color='blue')
]
mpf.plot(minute_data, volume=True, type='candle', addplot=addplot)

Figure_1

I tried using the Kama indicator (snippet and picture above), seems like there’s a bug with the calculate the Kama. The initial Kama value relies on the previous value which I believe from the snippet below returns zero for the first value.

https://github.com/twopirllc/pandas-ta/blob/bc3b292bf1cc1d5f2aba50bb750a75209d655b37/pandas_ta/overlap/kama.py#L34

This doc was referenced on a comment in the kama.py file and suggests that “the first KAMA is just a simple moving average”.

Is there anyway we can add another arg for starting_kama or generate some sma based on the “slow” length?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
onachassicommented, Oct 22, 2021

Doh! 🤦🏼‍♂️ Sorry. I thought I updated the development branch! Got some stuff to do right now, but I’ll let you know when I actually update it this time.

😅 Thought i was going crazy for a sec.

@twopirllc seems to be working, great job and thank you!

1reaction
twopirllccommented, Oct 20, 2021

@onachassi,

Doh! 🤦🏼‍♂️ Sorry. I thought I updated the development branch! Got some stuff to do right now, but I’ll let you know when I actually update it this time.

If you want to test your local copy, you can add/edit the following:

from pandas_ta.overlap.ma import ma

def kama(close, length=None, fast=None, slow=None, mamode=None, drift=None, offset=None, **kwargs):
    # ...
    valid_ma = [
        "ema", "fwma", "linreg", "midpoint", "pwma", "rma",
        "sinwma", "sma", "swma", "trima", "wma"
    ]
    if isinstance(mamode, str) and mamode.lower() in valid_ma:
        mamode = mamode.lower()
    else:
        mamode = "sma"
    # ...

    ma0 = ma(mamode, close.iloc[:length], length=length, **kwargs).iloc[-1] # 
    result = [npNaN for _ in range(0, length - 1)] + [ma0] # replace the old result with this one

Don’t forget to also update kama in core.py with the new mamode argument.

Apologies, KJ

Read more comments on GitHub >

github_iconTop Results From Across the Web

Kaufman's Adaptive Moving Average (KAMA)
One of the uses of Kaufman's Adaptive Moving Average is to identify the general trend of current market price action. Basically, when the...
Read more >
Kaufman's Adaptive Moving Average (KAMA) - ChartSchool
Developed by Perry Kaufman, Kaufman's Adaptive Moving Average (KAMA) is a moving average designed to account for market noise or volatility.
Read more >
KAMA (Kaufman's Adaptive Moving Average)
KAMA (Kaufman's Adaptive Moving Average) is another popular and widely used moving average indicator. It was developed by Perry J. Kaufman as an...
Read more >
kama - Kaufman Adaptive Moving Average - Tulip Indicators
It adapts to a fast moving average when prices are moving steadily in one direction and a slow moving average when the market...
Read more >
KAMA (Kaufman adaptive moving average) - TraderEvolution
ER = [ABS (Closet - Closet-n]/ [n ∑ (ABS (Closet – Closet-1))],. n - is a selected number of days for the moving...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found