Ulcer Index

ICE Data Services -

UlcerIndex.efs  
EFSLibrary - Discussion Board  

File Name: UlcerIndex.efs

Description:
Ulcer Index

Formula Parameters:

  • nLength : 20
  • nSafeLevel : 5
  • Source of Price : Close

Notes:
The Ulcer Index is a measure of the stress level related to market condition.

Download File:
UlcerIndex.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:        
    Ulcer Index 
    
Version:            1.0  05/05/2009
    
Formula Parameters:                     Default:
    nLength                             20
    nSafeLevel                          5
    Source of Price                     Close

Notes:
    The Ulcer Index is a measure of the stress level related to market condition. 
**********************************/
var fpArray = new Array();
var bInit = false;

function preMain() {
    setStudyTitle("Ulcer Index");
    setCursorLabelName("Ulcer Index", 0);
    setShowTitleParameters(false);    
    setPlotType(PLOTTYPE_HISTOGRAM);
    var x = 0;
    fpArray[x] = new FunctionParameter("nLength", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setLowerLimit(1);
        setDefault(20);
    }
    fpArray[x] = new FunctionParameter("nSafeLevel", FunctionParameter.NUMBER);
    with(fpArray[x++]) {
        setLowerLimit(0);
        setDefault(5);
    }
    fpArray[x] = new FunctionParameter("sPrice", FunctionParameter.STRING);
	with(fpArray[x++]){
        setName("Source of Price");
        addOption("open"); 
        addOption("high");
        addOption("low");
        addOption("close");
        addOption("hl2");
        addOption("hlc3");
        addOption("ohlc4"); 
        setDefault("close"); 
    }    
}

var xUlcerIndex = null;

function main(sPrice, nLength, nSafeLevel) {
var nBarState = getBarState();
var nUlcerIndex = 0;
    if (nBarState == BARSTATE_ALLBARS) {
        if (sPrice == null) sPrice = "close";
        if (nLength == null) nLength = 20;
        if (nSafeLevel == null) nSafeLeel = 5;
    }    
    if (bInit == false) {
        addBand(nSafeLevel, PS_SOLID, 2, Color.blue, "SafeLevel");
        xUlcerIndex = efsInternal("Calc_UI", sPrice, nLength);
        bInit = true;
    }
    nUlcerIndex = xUlcerIndex.getValue(0);
    if (nUlcerIndex == null) return;
    if(nUlcerIndex > nSafeLevel) setBarBgColor(Color.red);
    return nUlcerIndex;
}

var bSecondInit = false;
var xPrice = null;
var xHHPrice = null;

function Calc_UI(sPrice, nLength) {
var nRes = 0;
var HiHi = 0;
var i = 0;
var nTmp = 0;
    if (bSecondInit == false) {
        xPrice = eval(sPrice)();
        xHHPrice = upperDonchian(nLength, xPrice);
        bSecondInit = true;
    }    
    HiHi = xHHPrice.getValue(0);
    if (xPrice.getValue(-nLength) == null) return;
    for (i = 0; i < nLength; i++) {
        nTmp = ((HiHi - xPrice.getValue(-i)) / HiHi) * 100;
        nRes += nTmp * nTmp;
    }
    nRes = Math.sqrt(nRes / nLength);
    return nRes;
}