2/20 Exponential Moving Average

ICE Data Services -

MovAvgX2_20.efs  
EFSLibrary - Discussion Board  

File Name: MovAvgX2_20.efs

Description:
This Indicator plots 2/20 Exponential Moving Average

Formula Parameters:

  • Moving average source: close
  • Length: 20

Notes:
This indicator plots 2/20 exponential moving average. For the Mov Avg X 2/20 Indicator, the EMA bar will be painted when the Alert criteria is met.

Download File:
MovAvgX2_20.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:        

    This Indicator plots 2/20 Exponential Moving Average

Version:            1.0  09/26/2008

Notes:

    This indicator plots 2/20 exponential moving average. For the Mov 
    Avg X 2/20 Indicator, the EMA bar will be painted when the Alert criteria is met.


Formula Parameters:                     Default:

    Moving average source               close
    Length                              20
 
**********************************/

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

function preMain() {

    setPriceStudy(true);
    setStudyTitle("Mov Avg X 2/20");
    setCursorLabelName("XAverage");

    setColorPriceBars(true);
    setDefaultPriceBarColor(Color.black);
    
    askForInput();
    var x=0;
    
    fpArray[x] = new FunctionParameter("MASource", FunctionParameter.STRING);
	with(fpArray[x++]){
        setName("Source of moving average");
        addOption("open"); 
        addOption("high");
        addOption("low");
        addOption("close");
        addOption("hl2");
        addOption("hlc3");
        addOption("ohlc4"); 
        setDefault("close"); 
    }    
    
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setLowerLimit(1);		
        setDefault(20);
    }
}

var xXA = null;

function main(MASource, Length) {
    
    var nBarState = getBarState();
    
    if(nBarState == BARSTATE_ALLBARS) {
        if (MASource == null) MASource = "close";
        if (Length == null) Length = 20; 
    }  
    
    if (bInit == false) {
        var xPrice = eval(MASource)();
        xXA = ema(Length, xPrice);
        bInit = true;
    }

    if (getCurrentBarCount() < Length) return;
    
    var nHH = Math.max(high(0), high(-1));
    var nLL = Math.min(low(0), low(-1));
    
    var nXA = xXA.getValue(0);
    
    if ((nLL > nXA)||(nHH < nXA)) setPriceBarColor(Color.red);
    
    return nXA;
}