KAMA
Download scriptThis technical indicator is designed to provide a smooth, responsive trend line on your trading chart. It utilizes a specific mathematical approach known as the Kaufman Adaptive Moving Average (KAMA), which aims to solve a common problem in trading: how to balance speed and stability.
How the Logic Works
Unlike standard moving averages that treat all price movements with equal weight, this script analyzes the relationship between "trend" and "noise." It breaks down its logic into three primary steps:
1. Identifying the Signal vs. The Noise The indicator first looks at a specific window of time (the period you set in the settings). Within this window, it calculates two distinct values:
- The Signal: The actual difference between the current price and the price from a set number of bars ago. This tells the script how much direction is being established.
- The Noise: The sum of all the tiny, erratic price fluctuations that occur within that same time window. This represents market "jitter" or sideways movement.
2. Calculating the Efficiency Ratio By comparing the Signal to the Noise, the script determines an Efficiency Ratio.
- If the price is moving strongly in one direction with very little chatter, the ratio is high.
- If the price is moving erratically but not actually going anywhere (high noise, low signal), the ratio is low.
3. Adapting the Movement This is where the "Adaptive" part of the name comes from. The script uses that Efficiency Ratio to change how fast it reacts to new data:
- In a Strong Trend: When the market is moving efficiently in one direction, the indicator speeds up to follow the trend closely so you don't miss out on the move.
- In a Choppy Market: When the market is noisy and sideways, the indicator automatically slows down. This prevents "whipsawing," where a standard moving average might give false signals by jumping around every time the price makes a tiny movement.
Summary for Traders
The end result is a trend line that remains steady during Consolidation phases—preventing you from making impulsive trades based on minor fluctuations—but becomes more reactive when a clear, sustained trend begins to emerge. It effectively filters out market "static" while remaining sensitive to significant price shifts.
//+------------------------------------------------------------------+
//| KAMA.mq4 |
//| Copyright? 2009, Walter Choy. |
//| |
//+------------------------------------------------------------------+
#property copyright "Copyright? 2009, Walter Choy."
#property link ""
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_color2 Red
#property indicator_color3 Red
//---- input parameters
extern int kama_period = 10;
extern double fast_ma_period = 2.0;
extern double slow_ma_period = 30.0;
//---- buffers
double KAMA[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,KAMA);
SetIndexLabel(0, "KAMA");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
int i = Bars - counted_bars;
double fastest = 2 / (fast_ma_period + 1);
double slowest = 2 / (slow_ma_period + 1);
while(i>0){
double er = 1;
double signal = MathAbs(Close[i] - Close[i+kama_period]);
double noise = 0;
for (int j=0; j<kama_period; j++)
noise += MathAbs(Close[i+j] - Close[i+j+1]);
if (noise > 0)
er = signal / noise;
double sc = MathPow((er * (fastest - slowest) + slowest), 2);
KAMA[i] = KAMA[i+1] + sc * (Close[i] - KAMA[i+1]);
i--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments