2008 Feb: Trading Divergences

ICE Data Services -

ZeroLag_EMA.efs, TrailingStop_Reversal.efs  
EFSLibrary - Discussion Board  

File Name: ZeroLag_EMA.efs

Description:
These studies are based on the February 2008 article, Trading Divergences, by Sylvain Vervoort.

Formula Parameters:

ZeroLag_EMA.efs

  • Periods: 20

TrailingStop_Reversal.efs

  • Trailing Stop: 7

Notes:
The SVAPO study mentioned in the article can be found in our EFS Library, which was completed for the November 2007 issue. The related article is copyrighted material. If you are not a subscriber of Stocks & Commodities, please visit www.traders.com.

Download File:
ZeroLag_EMA.efs
TrailingStop_Reversal.efs


EFS Code:

ZeroLag_EMA.efs

/*********************************
Provided By:  
    eSignal (Copyright © eSignal), a division of Interactive Data 
    Corporation. 2007. 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:        Trading Divergences
                    by Sylvain Vervoort

Version:            1.0  12/7/2007

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

Formula Parameters:                 Defaults:
Periods                             20
**********************************/


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

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

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

var xEma1    = null;
var xEma2    = null;

function main(nPeriods) {
    var nState = getBarState();
    var nIndex = getCurrentBarIndex();
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    
    
    
    if (bInit == false) {
        xEma1 = ema(nPeriods);
        xEma2 = ema(nPeriods, xEma1);
        bInit = true;
    }
    
    var nZEma = null;
    var nEma1 = xEma1.getValue(0);
    var nEma2 = xEma2.getValue(0);
    if (nEma1 == null || nEma2 == null) return;
    
    nZEma = nEma1 + (nEma1 - nEma2);
    
    return nZEma;    
}


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:

TrailingStop_Reversal.efs

/*********************************
Provided By:  
    eSignal (Copyright © eSignal), a division of Interactive Data 
    Corporation. 2007. 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:        Trading Divergences
                    by Sylvain Vervoort

Version:            1.0  12/7/2007

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

Formula Parameters:                 Defaults:
Trailing Stop                       7
**********************************/


function preMain() {
    setPriceStudy(true);
    setStudyTitle("Trailing Stop Reversal ");
    setCursorLabelName("Stop Rev", 0);
    setDefaultBarFgColor(Color.brown, 0);
    setDefaultBarThickness(2, 0);

    var fp1 = new FunctionParameter("nStop", FunctionParameter.NUMBER);
        fp1.setName("Trailing Stop");
        fp1.setLowerLimit(0);
        fp1.setDefault(7);
}

// Global Variables
var bVersion  = null;    // Version flag

var nTrail    = null;
var nTrail_1  = 0;

function main(nStop) {
    var nState = getBarState();
    var nIndex = getCurrentBarIndex();
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    


    if (nState == BARSTATE_NEWBAR && nTrail != null) {
        nTrail_1 = nTrail;
    }
    
    var nClose    = close(0);
    var nClose_1  = close(-1);
    if (nClose_1 == null) return;
    
    if (nClose == nTrail_1) {
        nTrail = nTrail_1;
    } else if (nClose_1 < nTrail_1 && nClose < nTrail_1) {
        nTrail = Math.min(nTrail_1, (nClose*(1+nStop/100)));
    } else if (nClose_1 > nTrail_1 && nClose > nTrail_1) {
        nTrail = Math.max(nTrail_1, (nClose*(1-nStop/100)));
    } else if (nClose > nTrail_1) {
        nTrail = nClose*(1-nStop/100);
    } else {
        nTrail = nClose*(1+nStop/100)
    }
    
    return nTrail;    
}


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