Master this intelligent adaptive moving average in TradingView's Pinescript for enhanced trend following and noise reduction.
Kaufman's Adaptive Moving Average (KAMA), developed by Perry Kaufman, is a sophisticated moving average that dynamically adjusts its smoothing period based on market volatility. Unlike traditional moving averages (like SMA or EMA) that use a fixed period, KAMA becomes more responsive when the market is trending (high volatility) and less responsive (more smoothed) when the market is ranging or choppy (low volatility). This unique adaptability allows KAMA to filter out noise effectively in sideways markets while remaining sensitive enough to capture real trend changes.
The core of KAMA's adaptability lies in its Efficiency Ratio (ER) and Smoothing Constant (SC).
//@version=5
indicator("My KAMA Indicator", overlay=true)
// Inputs for KAMA parameters
length = input.int(10, title="KAMA Length")
fastestLength = input.int(2, title="Fastest EMA Length")
slowestLength = input.int(30, title="Slowest EMA Length")
// Calculate KAMA using the built-in function
kamaValue = ta.kama(close, length, fastestLength, slowestLength)
// Plot the KAMA line
plot(kamaValue, title="KAMA", color=color.blue, linewidth=2)
Common settings for KAMA are a `length` of 10, `fastestLength` of 2, and `slowestLength` of 30. However, these can be adjusted for different assets or timeframes.
//@version=5
strategy("KAMA Trend Filter Strategy", overlay=true)
// Inputs for KAMA
length = input.int(10, title="KAMA Length")
fastestLength = input.int(2, title="Fastest EMA Length")
slowestLength = input.int(30, title="Slowest EMA Length")
// Calculate KAMA
kamaValue = ta.kama(close, length, fastestLength, slowestLength)
// Plot KAMA
plot(kamaValue, title="KAMA", color=color.blue, linewidth=2)
// Trend conditions
uptrend = close > kamaValue
downtrend = close < kamaValue
// Highlight background based on trend
bgcolor(uptrend ? color.new(color.green, 90) : na)
bgcolor(downtrend ? color.new(color.red, 90) : na)
// Example entry logic: buy on crossover above KAMA, sell on crossunder below KAMA
longCondition = ta.crossover(close, kamaValue)
shortCondition = ta.crossunder(close, kamaValue)
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
//@version=5
strategy("KAMA & SMA Crossover", overlay=true)
// KAMA Inputs
kamaLength = input.int(10, title="KAMA Length")
fastestLength = input.int(2, title="Fastest EMA Length")
slowestLength = input.int(30, title="Slowest EMA Length")
// SMA Input
smaLength = input.int(50, title="SMA Length")
// Calculate KAMA and SMA
kama = ta.kama(close, kamaLength, fastestLength, slowestLength)
sma = ta.sma(close, smaLength)
// Plot lines
plot(kama, title="KAMA", color=color.blue, linewidth=2)
plot(sma, title="SMA", color=color.orange, linewidth=2)
// Crossover conditions
longCondition = ta.crossover(kama, sma)
shortCondition = ta.crossunder(kama, sma)
// Strategy entries
if (longCondition)
strategy.entry("Long", strategy.long)
if (shortCondition)
strategy.entry("Short", strategy.short)
//@version=5
indicator("KAMA Divergence", overlay=true)
length = input.int(10, title="KAMA Length")
fastestLength = input.int(2, title="Fastest EMA Length")
slowestLength = input.int(30, title="Slowest EMA Length")
kamaValue = ta.kama(close, length, fastestLength, slowestLength)
plot(kamaValue, title="KAMA", color=color.blue, linewidth=2)
// Highlight divergence (simplified example, robust divergence detection is complex)
// This example is conceptual and requires robust peak/trough detection for practical use.
bullishDiv = kamaValue > kamaValue[1] and kamaValue[1] > kamaValue[2] and close < close[1] and close[1] < close[2]
bearishDiv = kamaValue < kamaValue[1] and kamaValue[1] < kamaValue[2] and close > close[1] and close[1] > close[2]
plotshape(bullishDiv, title="Bullish Divergence", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(bearishDiv, title="Bearish Divergence", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)
alertcondition(bullishDiv, "KAMA Bullish Divergence", "Potential bullish reversal based on KAMA divergence.")
alertcondition(bearishDiv, "KAMA Bearish Divergence", "Potential bearish reversal based on KAMA divergence.")
To get the most from KAMA in Pinescript:
KAMA adapts to existing volatility; it doesn't predict future volatility. Use it in conjunction with other tools for comprehensive analysis.
Kaufman's Adaptive Moving Average (KAMA) is a powerful and intelligent indicator in Pinescript for TradingView. Its unique ability to adjust its smoothing based on market efficiency helps traders filter out noise and stay in tune with prevailing trends more effectively than traditional moving averages. By understanding its adaptive mechanism and incorporating it thoughtfully into your multi-indicator strategies, KAMA can be an invaluable tool for refining your trend analysis and improving your trading decisions.
Get a high-performance Pinescript analysis tool for actionable market insights, designed for traders on the move.
This strategy runs in live mode on TradingView, helping you identify potential opportunities.
Get Pinescript Strategy