Kaufman Moving Average Adaptive (KAMA)

ICE Data Services -

KAMA.efs  
EFSLibrary - Discussion Board  

File Name: KAMA.efs

Description:
Kaufman Moving Average Adaptive (KAMA)

Formula Parameters:

  • Length: 21
  • Price: Close

Notes:

Everyone wants a short-term, fast trading trend that works without large losses. That combination does not exist. But it is possible to have fast trading trends in which one must get in or out of the market  quickly, but these have the distinct disadvantage of being whipsawed by market noise when the market is volatile in a sideways trending market. During these periods, the trader is jumping in and out of positions with no profit-making trend in sight. In an attempt to overcome the problem of noise and still be able to get closer to the actual change of the trend, Kaufman developed an indicator that adapts to market movement. This indicator, an adaptive moving average (AMA), moves very slowly when markets are moving sideways but moves swiftly when the markets also move swiftly, change directions or break out of
a trading range.

Download File:
KAMA.efs


EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2008. All rights reserved. This sample eSignal 
    Formula Script (EFS) is for educational purposes only and may be 
    modified and saved under a new file name.  eSignal is not responsible
    for the functionality once modified.  eSignal reserves the right 
    to modify and overwrite this EFS file with each new release.
   

Description:        
    Kaufman Moving Average Adaptive (KAMA)

Version:            1.0  09/23/2008

Notes:
    Everyone wants a short-term, fast trading trend that works without large
    losses. That combination does not exist. But it is possible to have fast
    trading trends in which one must get in or out of the market quickly, but
    these have the distinct disadvantage of being whipsawed by market noise
    when the market is volatile in a sideways trending market. During these
    periods, the trader is jumping in and out of positions with no profit-making
    trend in sight. In an attempt to overcome the problem of noise and still be
    able to get closer to the actual change of the trend, Kaufman developed an
    indicator that adapts to market movement. This indicator, an adaptive moving
    average (AMA), moves very slowly when markets are moving sideways but moves
    swiftly when the markets also move swiftly, change directions or break out of
    a trading range.

Formula Parameters:                     Default:
    Length                                  21
    Price                                 Close
**********************************/

var fpArray = new Array();
var bInit = false;

function preMain()
{
    setStudyTitle("KAMA");
    setCursorLabelName("KAMA", 0);
    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(1,0);
    setPriceStudy(true);
    
    var x=0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);		
        setDefault(21);
    }

    fpArray[x] = new FunctionParameter("Price", FunctionParameter.STRING);
    with(fpArray[x++]){
        addOption("open"); 
        addOption("high");
        addOption("low");
        addOption("close");
        addOption("hl2");
        addOption("hlc3");
        addOption("ohlc4"); 
        setDefault("close"); 
    }
}

var xvnoise = null;
var xMyPrice = null;
var xAMA = null;

function main(Length, Price) {
var nAMA = 0;
	
    if (Length == null) Length = 21;
    if(Price == null) Price= "close";

    if ( bInit == false ) { 
        xMyPrice = eval(Price)();
        xvnoise = efsInternal("Calc_avnoise", xMyPrice);
        xAMA = efsInternal("Calc_AMA",xvnoise, xMyPrice, Length);
        bInit = true; 
    } 

	nAMA = xAMA.getValue(0);
		
    return nAMA;

}

function Calc_AMA(xvnoise, xMyPrice, Length){
	var nefratio = 1;
	var nAMA = 0;
	var nfastend = 0.666;
	var nslowend = 0.0645;
	var nnoise = 0;
	var nsignal = 0;
	var nsmooth = 0;
    var nAMA_Ref = ref(-1);	
    var nBarCount = getCurrentBarCount();


    if (xMyPrice.getValue(-1) == null) return 0;
    if (nAMA_Ref == null) nAMA_Ref = 1;

	if(nBarCount > Length){
		nsignal = Math.abs(xMyPrice.getValue(0) - xMyPrice.getValue(-Length));
		for(var i = 0; i < Length; i++)
			nnoise += xvnoise.getValue(-i);
		if(nnoise != 0)
			nefratio = nsignal / nnoise;
	} 
	
	nsmooth = Math.pow(nefratio * (nfastend - nslowend) + nslowend, 2); 

	if(nBarCount > Length){
        nAMA = nAMA_Ref + nsmooth * (xMyPrice.getValue(0) - nAMA_Ref);
    }

    else if (nBarCount == Length){
        nAMA = xMyPrice.getValue(0); 
    } else {
        return;
    }

	
	return nAMA;
}

function Calc_avnoise(xMyPrice) {
var nRes = 0;
    nRes = Math.abs(xMyPrice.getValue(0) - xMyPrice.getValue(-1));
    if (nRes == null) nRes = 1;
    return nRes;
}