Elders Safe Zone

ICE Data Services -

EldersSafeZone.efs                                                                                   EFSLibrary - Discussion Board

File Name: EldersSafeZone.efs


Description:
Elders Safe Zone

 

Formula Parameters:
Length : 13
Stop Factor : 3
Look Back Bars : 10

 

Notes:

Download File:
EldersSafeZone.efs




EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2009. 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:        
    Elders Safe Zone
Version:            1.0  09/25/2009
 
Formula Parameters:                     Default:
    Length                              13
    Stop Factor                          3
    Look Back Bars                      10
    
Notes:
    
**********************************/
var fpArray = new Array();
var bInit = false;

function preMain(){
    setPriceStudy(true);
    setShowCursorLabel(true);
    setShowTitleParameters(false);
    setStudyTitle("Elders Safe Zone");
    setCursorLabelName("EldersSZ", 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarThickness(2, 0);        
    setDefaultBarFgColor(Color.red, 0);
    var x = 0;
    fpArray[x] = new FunctionParameter("Length", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setLowerLimit(1);
        setDefault(13);
    }    
    fpArray[x] = new FunctionParameter("StopFactor", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setName("Stop Factor");
        setLowerLimit(1);
        setDefault(3);
    }        
    fpArray[x] = new FunctionParameter("LookBack", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setName("Look Back Bars");
        setLowerLimit(1);
        setDefault(10);
    }        
}

var xEldersSafeZone = null;

function main(Length, StopFactor, LookBack) {
var nBarState = getBarState();
var nEldersSafeZone = 0;
    if (nBarState == BARSTATE_ALLBARS) {
        if(Length == null) Length = 13;
        if(StopFactor == null) StopFactor = 3;        
        if(LookBack == null) LookBack = 10;               
	}
	if (bInit == false) {
        xEldersSafeZone = efsInternal("Calc_EldersSafeZone", Length, StopFactor, LookBack);
        bInit = true;
	}
    nEldersSafeZone = xEldersSafeZone.getValue(0);
    if (nEldersSafeZone == null) return;
    return nEldersSafeZone;
}

var xEMA = null;
var xLow = null;
var xHigh = null;
var bSecondInit = false;

function Calc_EldersSafeZone(Length, StopFactor, LookBack) {
var nEMA0 = 0;
var nEMA1 = 0;
var nEMA2 = 0;
var Pen = 0;
var i = 0;
var nPreSafeStop = ref(-1);
var nSafeStop = 0;
var Counter = 0;
	if (bSecondInit == false) {
        xEMA = ema(Length);
        xLow = low();
        xHigh = high();
        bSecondInit = true;
	}
    nEMA0 = xEMA.getValue(0);
    nEMA1 = xEMA.getValue(-1);
    nEMA2 = xEMA.getValue(-2);
    if (nEMA2 == null) return;
    if (nEMA0 > nEMA1) {
        for(i = 0; i <= LookBack; i++) {
            if (xLow.getValue(-i) < xLow.getValue(-(i + 1))) {
                Pen = xLow.getValue(-(i + 1)) - xLow.getValue(-i) + Pen;
                Counter=Counter + 1;
             }
        }
        if (Counter != 0) nSafeStop = close(0) - (StopFactor * (Pen / Counter));
            else nSafeStop = close(0) - (StopFactor * Pen);
        if ((nSafeStop < nPreSafeStop) && (nEMA1 > nEMA2)) nSafeStop = nPreSafeStop;
    }
    if (nEMA0 < nEMA1) {
        for(i = 0; i <= LookBack; i++) {
            if (xHigh.getValue(-i) > xHigh.getValue(-(i + 1))) {
                Pen = xHigh.getValue(-i) - xHigh.getValue(-(i + 1)) + Pen;
                Counter=Counter + 1;
            }
        }
        if (Counter != 0) nSafeStop = close(0) + (StopFactor * (Pen / Counter));
            else nSafeStop = close(0) + (StopFactor * Pen);
        if ((nSafeStop > nPreSafeStop) && (nEMA1 < nEMA2)) nSafeStop = nPreSafeStop;
    }
    return nSafeStop;
}