Kaufman2_v1
Download scriptThis script is designed as a technical analysis tool for the MetaTrader platform to help traders identify trends by filtering out "market noise." It utilizes a specialized smoothing logic known as the Kaufman Adaptive Moving Average (KAMA).
Core Concept
The primary objective of this tool is to provide a moving average that adapts to market conditions. Unlike standard moving averages, which move at a constant speed regardless of how volatile the market is, this script adjusts its sensitivity based on the "efficiency" of the price movement.
How the Logic Works
The script processes data through several distinct stages to determine the final line drawn on your chart:
1. Measuring Market Efficiency The tool first looks at the relationship between the current price change and the cumulative price movement over a specific period.
- Signal: It calculates the total distance traveled from a starting point to an ending point.
- Noise: It calculates every individual price fluctuation (up and down) during that same timeframe.
- Efficiency Ratio (ER): By dividing the Signal by the Noise, the script determines how "clean" the trend is. If the price moves in a straight line, the ratio is high; if the price is vibrating wildly without making much progress, the ratio is low.
2. Adjusting the Speed The script uses two different speeds—a "fast" and a "slow" setting. It applies the Efficiency Ratio to these speeds to determine how quickly the moving average should react:
- In a strong, clear trend (high efficiency), the script allows the moving average to move faster to stay close to the current price.
- In a choppy, sideways market (low efficiency/high noise), the script slows down the moving average significantly to prevent it from reacting to "fake" price movements.
3. Calculating the Final Average Finally, the tool takes the previous value of the average and combines it with the current market price, weighted by the adjusted speed and an exponential power factor. This results in a smooth line that ignores minor fluctuations but remains responsive during significant trend shifts.
Summary for the Trader
When you view this on a chart, the resulting line acts as a dynamic filter. During trending markets, it hugs the price closely to help you stay in a trade. During periods of consolidation or "noise," it flattens out to help prevent premature entries into trades based on insignificant price swings.
//+------------------------------------------------------------------+
//| Kaufman.mq4 |
//| Copyright © 2004, by konKop & wellx |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by konKop, GOODMAN, Mstera, af + wellx"
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Sienna
//---- input parameters
extern int periodAMA=9;
extern int nfast=2;
extern int nslow=30;
extern int G=2;
//---- buffers
double kAMAbuffer[];
//+------------------------------------------------------------------+
int cbars=0;
double slowSC,fastSC;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
//SetIndexDrawBegin(0,nslow+nfast);
SetIndexBuffer(0,kAMAbuffer);
IndicatorDigits(6);
//slowSC=0.064516;
//fastSC=0.2;
cbars=IndicatorCounted();
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,pos=0;
double noise,noise0,AMA,AMA0,signal,ER;
double dSC,ERSC,wlxSSC;
//---- TODO: add your code here
slowSC=(2.0 /(nslow+1));
fastSC=(2.0 /(nfast+1));
if (Bars<=(periodAMA+2)) return(0);
//---- check for possible errors
if (cbars<0) return(-1);
//---- last counted bar will be recounted
if (cbars>0) cbars--;
pos=Bars-periodAMA-2;
//pos=100;
//Print("cbars1: ", cbars);
AMA0=Close[pos+1];
while (pos>=0)
{
if(pos==Bars-periodAMA-2) AMA0=Close[pos+1];
signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
noise=0;
for(i=0;i<periodAMA;i++)
{
noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
};
ER =signal/noise;
dSC=(fastSC-slowSC);
ERSC=ER*dSC;
wlxSSC=ERSC+slowSC;
AMA=AMA0+(MathPow(wlxSSC,G)*(Close[pos]-AMA0));
kAMAbuffer[pos]=AMA;
/*
Print("dsC: ", dSC);
Print("ERSC: ", ERSC);
Print("slowSc: ", slowSC);
Print("fastSc: ", fastSC);
Print("signal: ", signal);
Print("noise: ", noise);
Print("SSC: ", wlxSSC);
Print("AMA0: ", AMA0);
Print("AMA: ", AMA);
Print("MathPow: ", MathPow(wlxSSC,G));
Print("pos: ", pos);
Print("close[pos]: ", Close[pos]);
Print("------------------------ ", 0);
*/
AMA0=AMA;
pos--;
};
//Print("cbars2: ", cbars);
//----
return(0);
}
Comments