2010 Nov: Zero Lag (Well, Almost) Indicator and Strategy

ICE Data Services -

ZeroLag_EC.efs ZeroLag_EC_Strategy.efs  
EFSLibrary - Discussion Board  

File Name:

  • ZeroLag_EC.efs
  • ZeroLag_EC_Strategy.efs

Description:
Zero Lag (Well, Almost) Indicator and Strategy by John Ehlers and Ric Way

Formula Parameters:

ZeroLag_EC.efs

  • Length: 20
  • Gain Limit: 50

ZeroLag_EC.efs

  • Length: 20
  • Gain Limit: 50
  • Thresh: 1

Notes:
The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
ZeroLag_EC.efs
ZeroLag_EC_Strategy.efs

ZeroLag_EC.efs

ZeroLag_EC.efs

EFS Code:

ZeroLag_EC.efs

/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2010) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release. 
	
Description:        
    Zero Lag (Well, Almost) Indicator by John Ehlers and Ric Way
	
Version:            1.0  13/09/2010
 
Formula Parameters:                     Default:
    Length                              20
    Gain Limit                          50
    
Notes:
    The related article is copyrighted material. If you are not
	a subscriber of Stocks & Commodities, please visit www.traders.com.
	
**********************************/

var fpArray = new Array();
var bVersion = null;

function preMain()
{
    setPriceStudy(true);
    setStudyTitle("zeroLag_EC");
    setCursorLabelName("zeroLag_EMA", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setCursorLabelName("zeroLag_EC", 1);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.blue, 1);
    setDefaultBarThickness(2, 1);
    setPlotType(PLOTTYPE_LINE, 1);
    var x=0;
    fpArray[x] = new FunctionParameter("gLength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length");
        setLowerLimit(1);		
        setDefault(20);
    }
    fpArray[x] = new FunctionParameter("gGainLimit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
	    setName("Gain Limit");
        setLowerLimit(1);		
        setDefault(50);
    }
}

var bMainInit = false; 
var xEMA = null;
var xEC = null;
var nAlpha = 0;

function main(gLength, gGainLimit)
{
    var nGain = 0;
    var nBestGain = 0;
    var nError = 0;
    var nLeastError = 1000000;
 
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    var nBarState = getBarState();
    if (nBarState == BARSTATE_ALLBARS) {
        if (gLength == null) gLength = 20;
        if (gGainLimit == null) gGainLimit = 50;
        nAlpha = 2/(gLength+1);
    }    
    if (!bMainInit)
    {
        xEMA = efsInternal("calcEMA",nAlpha);
        xEC = efsInternal("calcEC",nAlpha,gGainLimit,xEMA);
    }

    var nEMA = xEMA.getValue(0);
    var nEC = xEC.getValue(0);
    if( nEMA == null || nEC == null) return ;

    return new Array( nEMA, nEC) ;
}

function calcEMA(nAlpha)
{
    if (getCurrentBarCount()==1) var nRefEMA = close(0) 
    else  var nRefEMA = ref(-1);
    var vEMA = nAlpha*close(0)+(1-nAlpha)*nRefEMA;
    return vEMA;
}

var nBestGain = 0;

function calcEC(nAlpha,nGaintLimit, xEMA)
{
    var nLeastError = 1000000;
    if (getCurrentBarCount()==1) var nRefEC = close(0) 
    else var nRefEC = ref(-1);

    var nClose = close(0);
    var vEMA = xEMA.getValue(0);
    var nCount = 0;
    var nGain = 0;
    var nError = 0;
    var vEC = 0 ;
    for (nCount=-nGaintLimit; nCount<=nGaintLimit; nCount++)
    {
        nGain = nCount/10;
        vEC = nAlpha*(vEMA+nGain*(nClose-nRefEC))+(1-nAlpha)*nRefEC;
        nError = nClose - vEC;
        if (Math.abs(nError)<nLeastError)
        {
            nLeastError = Math.abs(nError);
            nBestGain = nGain;
        }
    }
    vEC = nAlpha * (vEMA + nBestGain*(nClose - nRefEC))+(1-nAlpha)*nRefEC;
  
    return vEC;
}

function verify() {
    var b = false;
    if (getBuildNumber() < 779) {
        drawTextAbsolute(5, 35, "This study requires version 8.0 or later.", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "error");
        drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "upgrade");
        return b;
    } else {
        b = true;
    }
    return b;
}

EFS Code:

/*********************************
Provided By:  
    Interactive Data Corporation (Copyright © 2010) 
    All rights reserved. This sample eSignal Formula Script (EFS)
    is for educational purposes only. Interactive Data Corporation
    reserves the right to modify and overwrite this EFS file with 
    each new release.                                        

Description:        
    Zero Lag (Well, Almost) Strategy  by John Ehlers and Ric Way

Version:            1.0  13/09/2010
 
Formula Parameters:                     Default:
    Length                              20
    Gain Limit                          50
    Thresh                               1
    
Notes:
    The related article is copyrighted material. If you are not
	a subscriber of Stocks & Commodities, please visit www.traders.com.
	
**********************************/

var fpArray = new Array();
var bVersion = null;

function preMain()
{
    setPriceStudy(true);
    setStudyTitle("zeroLag_EC_Strategy");
    setColorPriceBars(false);
    setCursorLabelName("zeroLag_EMA", 0);
    setDefaultBarStyle(PS_SOLID, 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setCursorLabelName("zeroLag_EC", 1);
    setDefaultBarStyle(PS_SOLID, 1);
    setDefaultBarFgColor(Color.blue, 1);
    setDefaultBarThickness(2, 1);
    setPlotType(PLOTTYPE_LINE, 1);
    var x=0;
    fpArray[x] = new FunctionParameter("gLength", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Length");
        setLowerLimit(1);		
        setDefault(20);
    }
    fpArray[x] = new FunctionParameter("gGainLimit", FunctionParameter.NUMBER);
	with(fpArray[x++]){
	    setName("Gain Limit");
        setLowerLimit(1);		
        setDefault(50);
    }
    fpArray[x] = new FunctionParameter("gThresh", FunctionParameter.NUMBER);
	with(fpArray[x++]){
	    setName("Thresh");
        setLowerLimit(1);		
        setDefault(1);
    }
}

var bMainInit = false; 
var xEMA = null;
var xEC = null;
var nAlpha = 0;
var bBuy = false;
var bSell = false;

function main(gLength, gGainLimit, gThresh)
{
    var nGain = 0;
    var nBestGain = 0;
    var nError = 0;
    var nLeastError = 1000000;
 
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    var nBarState = getBarState();
    var nBarCount = getCurrentBarCount();
    if (nBarState == BARSTATE_ALLBARS) {
        if (gLength == null) gLength = 20;
        if (gGainLimit == null) gGainLimit = 50;
        if (gThresh == null) gThresh = 1;
        nAlpha = 2/(gLength+1);
    }    
    if (!bMainInit)
    {
        xEMA = efsInternal("calcEMA",nAlpha);
        xEC = efsInternal("calcEC",nAlpha,gGainLimit,xEMA);
    }

    var nEMA = xEMA.getValue(0);
    var nEC = xEC.getValue(0);
    if( nEMA == null || nEC == null) return ;
    if (getCurrentBarIndex() == 0) return;
    
    var vEMA1 = xEMA.getValue(-1);
    var vEMA2 = xEMA.getValue(-2);
    var vEC1 = xEC.getValue(-1);
    var vEC2 = xEC.getValue(-2);    
    var nClose = close(-1);
    if (vEC2 == null || vEMA2 == null) return;
   
    if (vEC2 > vEMA2 && vEC1 < vEMA1 && 100*nLeastError/nClose > gThresh) 
    {
        drawTextRelative(0, TopRow1, " LONG ", Color.white, Color.green, Text.PRESET|Text.CENTER|Text.FRAME, "Arial Black", 10, "b"+(nBarCount));
        if(!Strategy.isLong()) Strategy.doLong("Entry Long", Strategy.MARKET, Strategy.THISBAR);
    }
    if (vEC2 < vEMA2 && vEC1 > vEMA1 && 100*nLeastError/nClose > gThresh)
    {
        drawTextRelative(0, TopRow1+1,  " SHORT ", Color.white, Color.red, Text.PRESET|Text.CENTER|Text.FRAME, "Arial Black", 10, "b"+(getCurrentBarCount())); 
        if(!Strategy.isShort()) Strategy.doShort("Entry Short", Strategy.MARKET, Strategy.THISBAR);
    }
	if(Strategy.isLong()) setBarBgColor(Color.lime);
	if(Strategy.isShort()) setBarBgColor(Color.yellow);
        
    return new Array( nEMA, nEC) ;
}

function calcEMA(nAlpha)
{
    if (getCurrentBarCount()==1) var nRefEMA = close(0) 
    else  var nRefEMA = ref(-1);
    var vEMA = nAlpha*close(0)+(1-nAlpha)*nRefEMA;
    return vEMA;
}

var nBestGain = 0;
function calcEC(nAlpha,nGaintLimit, xEMA)
{
    var nLeastError = 1000000;
    if (getCurrentBarCount()==1) var nRefEC = close(0) 
    else var nRefEC = ref(-1);

    var nClose = close(0);
    var vEMA = xEMA.getValue(0);
    var nCount = 0;
    var nGain = 0;
    var nError = 0;
    var vEC = 0 ;
    for (nCount=-nGaintLimit; nCount<=nGaintLimit; nCount++)
    {
        nGain = nCount/10;
        vEC = nAlpha*(vEMA+nGain*(nClose-nRefEC))+(1-nAlpha)*nRefEC;
        nError = nClose - vEC;
        if (Math.abs(nError)<nLeastError)
        {
            nLeastError = Math.abs(nError);
            nBestGain = nGain;
        }
    }
    vEC = nAlpha * (vEMA + nBestGain*(nClose - nRefEC))+(1-nAlpha)*nRefEC;
  
    return vEC;
}

function verify() {
    var b = false;
    if (getBuildNumber() < 779) {
        drawTextAbsolute(5, 35, "This study requires version 8.0 or later.", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "error");
        drawTextAbsolute(5, 20, "Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp", 
            Color.white, Color.blue, Text.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
            null, 13, "upgrade");
        return b;
    } else {
        b = true;
    }
    return b;
}