2008 May: The Quest For Reliable Cross-overs

ICE Data Services -

TEMA.efs,ZeroLag_TEMA.efs, HA_ZeroLag_TEMA.efs, ZeroLag_HA_Tema_Cross.efs  
EFSLibrary - Discussion Board  

File Name:

  • TEMA.efs
  • ZeroLag_TEMA.efs
  • HA_ZeroLag_TEMA.efs
  • ZeroLag_HA_Tema_Cross.efs

Description:
These studies are based on the May 2008 article, The Quest For Reliable Cross-overs, by Sylvain Vervoort.

Formula Parameters:

TEMA.efs:

  • Periods: 10
  • Price Source: Close [Close, HL/2, HLC/3, OHLC/4]

ZeroLag_TEMA.efs:

  • Periods: 10
  • Price Source: Close [Close, HL/2, HLC/3, OHLC/4]

HA_ZeroLag_TEMA.efs:

  • Periods: 10

ZeroLag_HA_Tema_Cross.efs:

  • Periods: 55
  • Zero Lag TEMA Price Source: HLC/3 [Close, HL/2, HLC/3, OHLC/4]
  • Long Only: false

Notes:
The Zero Lag EMA study mentioned in the article can be found in the Feb. 2008 EFS KnowledgeBase article published here. The original Heiken-Ashi study mentioned in the article can be found in the Feb 2004 EFS KnowledgeBase article here. Also, the ZeroLag_HA_Tema_Cross.efs contains a formula parameter (Long Only) to force the strategy to allow only long trades, which is also configured for back testing with the Strategy Analyzer. The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download Files:
TEMA.efs
ZeroLag_TEMA.efs
HA_ZeroLag_TEMA.efs
ZeroLag_HA_Tema_Cross.efs




EFS Code:
TEMA.efs

/*********************************
Provided By:  
    eSignal (Copyright © 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:        The Quest For Reliable Crossovers
                    by Sylvain Vervoort

Version:            1.0  3/11/2008

Notes:
* May 2008 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.

Formula Parameters:                 Defaults:
Periods                             10
Price Source                        Close [Close, HL/2, HLC/3, OHLC/4]
**********************************/


function preMain() {
    setPriceStudy(true);
    setStudyTitle("TEMA ");
    setCursorLabelName("TEMA", 0);
    setDefaultBarFgColor(Color.blue, 0);
    setDefaultBarThickness(2, 0);

    var fp1 = new FunctionParameter("nPeriods", FunctionParameter.NUMBER);
        fp1.setName("Periods");
        fp1.setLowerLimit(0);
        fp1.setDefault(10);
    var fp2 = new FunctionParameter("sSource", FunctionParameter.STRING);
        fp2.setName("Price Source");
        fp2.addOption("Close");
        fp2.addOption("HA Close");
        fp2.setDefault("Close");
}


// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var xTema = null;
var xHaOpen     = null;
var xHaClose    = null;

function main(nPeriods, sSource) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    
    if (bInit == false) {
        xHaOpen     = efsInternal("calcHaOpen");
        xHaClose    = efsInternal("calcHaClose", xHaOpen);
        if (sSource == "Close") {
            xSource = close();
        } else {
            xSource = xHaClose;
        }
        xTema = efsInternal("calcTEMA", nPeriods, xSource);
        bInit = true;
    }
    
    var nTema = xTema.getValue(0);
    if (nTema == null) return;
    
    return nTema;    
}

// HaOpen globals
var nHaOpen   = null;
var nHaOpen_1 = null;

function calcHaOpen() {
    if (nHaOpen_1 == null && open(-1) != null) {
        nHaOpen_1 = open(-1);
    } else if (nHaOpen != null) {
        nHaOpen_1 = nHaOpen;
    }
    if (nHaOpen_1 == null) return;
    
    var nC_1 = sma(1, ohlc4(), -1);
    if (nC_1 == null) return;
    
    nHaOpen = (nC_1 + nHaOpen_1) / 2;
    
    return nHaOpen;
}


function calcHaClose(xHaO) {
    var n4 = sma(1, ohlc4(), 0);
    var nHaO = xHaO.getValue(0);
    if (n4 == null || nHaO == null) return;

    var nHaCl = (n4 + nHaO +
        Math.max(n4, high(0), nHaO) +
        Math.min(n4, low(0), nHaO )  ) / 4;
    
    return nHaCl;
}

// TEMA globals
var xAvg1 = null;
var xAvg2 = null;
var xAvg3 = null;
var bInit2 = false;

function calcTEMA(nLength, xSrc){

    if(bInit2 == false){
        xAvg1 = ema(nLength, xSrc);
        xAvg2 = ema(nLength,xAvg1);
        xAvg3 = ema(nLength,xAvg2);
        bInit2 = true;
    }
    
    var nAvg1 = xAvg1.getValue(0);
    var nAvg2 = xAvg2.getValue(0);
    var nAvg3 = xAvg3.getValue(0);
    if (nAvg1 == null || nAvg2 == null || nAvg3 == null) return;

    var nTEMA = (3*nAvg1)-(3*nAvg2)+nAvg3;
    
    return nTEMA;
}




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;
}

ZeroLag_TEMA.efs

/*********************************
Provided By:  
    eSignal (Copyright © 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:        The Quest For Reliable Crossovers
                    by Sylvain Vervoort

Version:            1.0  3/11/2008

Notes:
* May 2008 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.

Formula Parameters:                 Defaults:
Periods                             10
Price Source                        Close [Close, HL/2, HLC/3, OHLC/4]
**********************************/


function preMain() {
    setPriceStudy(true);
    setStudyTitle("Zero Lag TEMA ");
    setCursorLabelName("ZTEMA", 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);

    var fp1 = new FunctionParameter("nPeriods", FunctionParameter.NUMBER);
        fp1.setName("Periods");
        fp1.setLowerLimit(1);
        fp1.setDefault(10);
    var fp2 = new FunctionParameter("sSource", FunctionParameter.STRING);
        fp2.setName("Price Source");
        fp2.addOption("Close");
        fp2.addOption("HL/2");
        fp2.addOption("HLC/3");
        fp2.addOption("OHLC/4");
        fp2.setDefault("Close");
}

// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var xTema1    = null;
var xTema2    = null;
var xSource   = null;

function main(nPeriods, sSource) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    
    if (bInit == false) {
        if (sSource == "Close") {
            xSource = close();
        } else if (sSource == "HL/2") {
            xSource = hl2();
        } else if (sSource == "HLC/3") {
            xSource = hlc3();
        } else if (sSource == "OHLC/4") {
            xSource = ohlc4();
        }
        xTema1 = efsInternal("calcTEMA", nPeriods, xSource);
        xTema2 = efsInternal("calcTEMA", nPeriods, xTema1);
        bInit = true;
    }
    
    var nZEma = null;
    var nTema1 = xTema1.getValue(0);
    var nTema2 = xTema2.getValue(0);
    if (nTema1 == null || nTema2 == null) return;
    
    nZTEma = nTema1 + (nTema1 - nTema2);
    
    return nZTEma;    
}



// TEMA globals
var xAvg1 = null;
var xAvg2 = null;
var xAvg3 = null;
var bInit2 = false;

function calcTEMA(nLength,xSource){

    if(bInit2 == false){
        xAvg1 = ema(nLength,xSource);
        xAvg2 = ema(nLength,xAvg1);
        xAvg3 = ema(nLength,xAvg2);
        bInit2 = true;
    }
    
    var nAvg1 = xAvg1.getValue(0);
    var nAvg2 = xAvg2.getValue(0);
    var nAvg3 = xAvg3.getValue(0);
    if (nAvg1 == null || nAvg2 == null || nAvg3 == null) return;

    var nTEMA = (3*nAvg1)-(3*nAvg2)+nAvg3;
    
    return nTEMA;
}

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;
}

HA_ZeroLag_TEMA.efs

/*********************************
Provided By:  
    eSignal (Copyright © 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:        The Quest For Reliable Crossovers
                    by Sylvain Vervoort

Version:            1.0  3/11/2008

Notes:
* May 2008 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.

Formula Parameters:                 Defaults:
Periods                             10
**********************************/


function preMain() {
    setPriceStudy(true);
    setStudyTitle("HA Zero Lag TEMA ");
    setCursorLabelName("HA ZTEMA", 0);
    setDefaultBarFgColor(Color.green, 0);
    setDefaultBarThickness(2, 0);

    var fp1 = new FunctionParameter("nPeriods", FunctionParameter.NUMBER);
        fp1.setName("Periods");
        fp1.setLowerLimit(1);
        fp1.setDefault(10);
}



// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var xTema1    = null;
var xTema2    = null;
var xHaOpen     = null;
var xHaClose    = null;

function main(nPeriods) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    
    if (bInit == false) {
        xHaOpen     = efsInternal("calcHaOpen");
        xHaClose    = efsInternal("calcHaClose", xHaOpen);
        xTema1      = efsInternal("calcTEMA", nPeriods, xHaClose);
        xTema2      = efsInternal("calcTEMA", nPeriods, xTema1);
        bInit = true;
    }
    
    var nHAZEma = null;
    var nTema1 = xTema1.getValue(0);
    var nTema2 = xTema2.getValue(0);
    if (nTema1 == null || nTema2 == null) return;
    
    nHAZTEma = nTema1 + (nTema1 - nTema2);
    
    return nHAZTEma;    
}

// HaOpen globals
var nHaOpen   = null;
var nHaOpen_1 = null;

function calcHaOpen() {
    if (nHaOpen_1 == null && open(-1) != null) {
        nHaOpen_1 = open(-1);
    } else if (nHaOpen != null) {
        nHaOpen_1 = nHaOpen;
    }
    if (nHaOpen_1 == null) return;
    
    var nC_1 = sma(1, ohlc4(), -1);
    if (nC_1 == null) return;
    
    nHaOpen = (nC_1 + nHaOpen_1) / 2;
    
    return nHaOpen;
}


function calcHaClose(xHaO) {
    var n4 = sma(1, ohlc4(), 0);
    var nHaO = xHaO.getValue(0);
    if (n4 == null || nHaO == null) return;

    var nHaCl = (n4 + nHaO +
        Math.max(n4, high(0), nHaO) +
        Math.min(n4, low(0), nHaO )  ) / 4;
    
    return nHaCl;
}

// TEMA globals
var xAvg1 = null;
var xAvg2 = null;
var xAvg3 = null;
var bInit2 = false;

function calcTEMA(nLength,xSource){

    if(bInit2 == false){
        xAvg1 = ema(nLength,xSource);
        xAvg2 = ema(nLength,xAvg1);
        xAvg3 = ema(nLength,xAvg2);
        bInit2 = true;
    }
    
    var nAvg1 = xAvg1.getValue(0);
    var nAvg2 = xAvg2.getValue(0);
    var nAvg3 = xAvg3.getValue(0);
    if (nAvg1 == null || nAvg2 == null || nAvg3 == null) return;

    var nTEMA = (3*nAvg1)-(3*nAvg2)+nAvg3;
    
    return nTEMA;
}

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;
}

ZeroLag_HA_Tema_Cross.efs

/*********************************
Provided By:  
    eSignal (Copyright © 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:        The Quest For Reliable Crossovers
                    by Sylvain Vervoort

Version:            1.0  3/11/2008

Notes:
* May 2008 Issue of Stocks and Commodities Magazine
* Study requires version 8.0 or later.
* Study requires ZeroLag_TEMA.efs and HA_ZeroLag_TEMA.efs
    to be located in the same folder as this formula.

Formula Parameters:                 Defaults:
Periods                             55
Zero Lag TEMA Price Source          HLC/3 [Close, HL/2, HLC/3, OHLC/4]
Long Only                           false
**********************************/


function preMain() {
    setPriceStudy(true);
    setStudyTitle("Zero Lag HA TEMA Crossover ");
    setCursorLabelName("ZTEMA", 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2, 0);
    setCursorLabelName("HA ZTEMA", 1);
    setDefaultBarFgColor(Color.green, 1);
    setDefaultBarThickness(2, 1);

    var fp1 = new FunctionParameter("nPeriods", FunctionParameter.NUMBER);
        fp1.setName("Periods");
        fp1.setLowerLimit(0);
        fp1.setDefault(55);
    var fp2 = new FunctionParameter("sSource", FunctionParameter.STRING);
        fp2.setName("Zero Lag TEMA Price Source");
        fp2.addOption("Close");
        fp2.addOption("HL/2");
        fp2.addOption("HLC/3");
        fp2.addOption("OHLC/4");
        fp2.setDefault("HLC/3");
    var fp3 = new FunctionParameter("bLong", FunctionParameter.BOOLEAN);
        fp3.setName("Long Only");
        fp3.setDefault(false);
}


// Global Variables
var bVersion  = null;    // Version flag
var bInit     = false;   // Initialization flag

var xZTema       = null;
var xHaZTema     = null;
var vPosition    = 0;  // 0=Flat, 1=long, -1=Short

function main(nPeriods, sSource, bLong) {
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    
    if (bInit == false) {
        xZTema      = efsExternal("ZeroLag_TEMA.efs", nPeriods, sSource);
        xHaZTema    = efsExternal("HA_ZeroLag_TEMA.efs", nPeriods);
        bInit = true;
    }
    
    var nState     = getBarState();
    var nZTema_0   = xZTema.getValue(0);
    var nZTema_1   = xZTema.getValue(-1);
    var nZTema_2   = xZTema.getValue(-2);
    var nHaZTema_0 = xHaZTema.getValue(0);
    var nHaZTema_1 = xHaZTema.getValue(-1);
    var nHaZTema_2 = xHaZTema.getValue(-2);

    // Check for proper amount of bar data.
    if ((nZTema_0 == null || nHaZTema_0 == null) && getCurrentBarIndex() >= -1) {
        drawTextPixel(20, 40, "Formula requires more bar data.", Color.red, Color.lightgrey, Text.FRAME|Text.BOLD|Text.RELATIVETOBOTTOM, null, 14, "dataError");
    } else {
        removeText("dataError");
    }
    
    if (nZTema_2 == null || nHaZTema_2 == null) return;
    
    // Cross-over
    var bBT = true;
    if (getCurrentBarIndex() == 0) bBT = false;
    
    if (nState == BARSTATE_NEWBAR) { 
        if (vPosition != 1 ) {
            if (nZTema_1 >= nHaZTema_1 && nZTema_2 < nHaZTema_2) {
                // Long Cross
                vPosition = 1;
                Strategy.doLong("Long Cross", Strategy.MARKET, Strategy.THISBAR);
                drawShape(Shape.UPARROW, BelowBar1, Color.blue, rawtime(0));
            }
        } else if (vPosition != -1) {
            if (nZTema_1 <= nHaZTema_1 && nZTema_2 > nHaZTema_2) {
                // Short Cross
                if (bLong == true) {
                    vPosition = 0;
                    Strategy.doSell("Long Exit", Strategy.MARKET, Strategy.THISBAR);
                } else {
                    vPosition = -1;
                    Strategy.doShort("Long Cross", Strategy.MARKET, Strategy.THISBAR);
                }
                drawShape(Shape.DOWNARROW, AboveBar1, Color.blue, rawtime(0));
            }
        }
    }
    
    
    
    return new Array(nZTema_0, nHaZTema_0);    
}


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;
}