2010 Jun: Fractal Dimension Indicator

ICE Data Services -

FractalDimension.efs  

EFSLibrary - Discussion Board  

 

File Name: FractalDimension.efs

Description:
Fractal Dimension Indicator

 

Formula Parameters:

  • N (must be an even number) : 30
  • Price Source : hl2
  • Band 1 : 1.6
  • Band 2 : 1.4

 

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

 

Download File:
FractalDimension.efs



EFS Code:

/*********************************
Provided By:  
    eSignal (Copyright c eSignal), a division of Interactive Data 
    Corporation. 2010. 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:        
    Fractal Dimension Indicator
    
Version:            1.00  04/07/2010

Formula Parameters:                     Default:
    N (must be an even number)          30
    Price Source                        hl2
    Band 1                              1.6
    Band 2                              1.4
    
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 bInit = false;
var bVersion = null;

function preMain() {
    setPriceStudy(false);
    setShowCursorLabel(true);
    setShowTitleParameters(false);
    setStudyTitle("Fractal Dimension");
    setCursorLabelName("Fractal Dimension", 0);
    setDefaultBarFgColor(Color.red, 0);
    setPlotType(PLOTTYPE_LINE, 0);
    setDefaultBarThickness(2, 0);
    var x=0;
    fpArray[x] = new FunctionParameter("N", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("N (must be an even number)");
        setLowerLimit(1);		
        setDefault(30);
    }    
    fpArray[x] = new FunctionParameter("Band1", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Band 1");
        setDefault(1.6);
    }    
    fpArray[x] = new FunctionParameter("Band2", FunctionParameter.NUMBER);
	with(fpArray[x++]){
        setName("Band 2");
        setDefault(1.4);
    }    
	fpArray[x] = new FunctionParameter("PriceSource", FunctionParameter.STRING);
	with(fpArray[x++]){
        setName("Price Source");
        addOption("open"); 
        addOption("high");
        addOption("low");
        addOption("close");
        addOption("hl2");
        addOption("hlc3");
        addOption("ohlc4"); 
        setDefault("hl2"); 
    }    
}

var xFractalDimension = null;

function main(PriceSource, N, Band1 ,Band2) {
var nBarState = getBarState();
var nFractalDimension = 0;
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;   
    if (nBarState == BARSTATE_ALLBARS) {
        if (N == null) N = 30;
        if (PriceSource == null) PriceSource = "hl2";
    }    
    if (!bInit) { 
        xFractalDimension = efsInternal("CalcFractalDimension", PriceSource, N);
        addBand(Band1, PS_SOLID, 1, Color.blue, "1.6");
        addBand(Band2, PS_SOLID, 1, Color.blue, "1.4");
        bInit = true; 
    }
    nFractalDimension = xFractalDimension.getValue(0);
    if (nFractalDimension == null) return;
    return nFractalDimension;
}

var bSecondInit = false;
var xSmooth = null;
var xSmoothH = null;
var xSmoothL = null;
var xHH_N2 = null;
var xLL_N2 = null;
var aRatio = new Array(20); 

function CalcFractalDimension(PriceSource, N) {
var nBarState =  getBarState();
var N1 = 0;
var N2 = 0;
var N3 = 0;
var SmoothH = 0;
var SmoothL = 0;
var HH_N2 = 0;
var LL_N2 = 0;
var nRef = ref(-1);
var nAverage = 0;
var i = 0;
    if (!bSecondInit) {
        xSmooth = efsInternal("CalcSmooth", PriceSource);
        xSmoothH = upperDonchian(N, xSmooth);
        xSmoothL = lowerDonchian(N, xSmooth);
        xHH_N2 = upperDonchian(N / 2 - 1, xSmooth);
        xLL_N2 = lowerDonchian(N / 2 - 1, xSmooth);
        for (i = 20; i >= 0; i--) {
            aRatio[i] = 0;
        }
        bSecondInit = true;
    }
    if (xHH_N2.getValue(-20) == null) return;
    SmoothH = xSmoothH.getValue(0);
    SmoothL = xSmoothL.getValue(0);
    N3 = (SmoothH - SmoothL) / N;
    HH_N2 = xHH_N2.getValue(0);
    LL_N2 = xLL_N2.getValue(0);
    N1 = (HH_N2 - LL_N2) / (N / 2);
    HH_N2 = xHH_N2.getValue(-(N / 2));
    LL_N2 = xLL_N2.getValue(-(N / 2));
    N2 = (HH_N2 - LL_N2)/(N / 2);
    if (nBarState == BARSTATE_NEWBAR) {
        for (i = 20; i > 0; i--) {
            aRatio[i] = aRatio[i - 1];
        }
    }    
    if (N1 > 0 && N2 > 0 && N3 > 0) {
        aRatio[0] = 0.5 * ((Math.log(N1 + N2) - Math.log(N3)) / Math.log(2) + nRef);
    }    
    for (i = 0; i < 20; i++) {
        nAverage = nAverage + aRatio[i];
    }
    nAverage = nAverage / 20;
    return nAverage;
}

var bCalcSmoothInit = null;
var xPriceSource = null;

function CalcSmooth(PriceSource) {
var nRes = 0;
    if (!bCalcSmoothInit) {
        xPriceSource = eval(PriceSource)();
        bCalcSmoothInit = true;
    }    
    if (xPriceSource.getValue(-3) == null) return;
    nRes = (xPriceSource.getValue(0) + 2 * xPriceSource.getValue(-1) + 
            2 * xPriceSource.getValue(-2) + xPriceSource.getValue(-3)) / 6;
    return nRes;
}

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