PercentChange.efs

ICE Data Services -

PercentChange.efs  
EFSLibrary - Discussion Board  

File Name: PercentChange.efs

Description:
Calculates a table of percentage changes in price of two symbols for various time periods.

Formula Parameters:

  • Symbol 1: Defaults to chart symbol.
  • Symbol 2: $INDU
  • Column Width: 73 (pixels)
  • Row Thickness: 1
  • Font Size: 10

Notes:
Study requires version 7.9.1 or later. To ensure that the study has the proper data for calculating the longer time periods it is recommended to use this study with a dynamic time template.

Download File:
PercentChange.efs


EFS Code:

/***************************************
Provided By : eSignal (c) Copyright 2005

Note:  Study requires version 7.9.1 
       (build 732) or later.
***************************************/

var bVersion = null;

var STATE_UNCHG		= 0;
var STATE_UP	    = 1;  // green
var STATE_DN		= -1; // red

var nFontCY = 0;
var nFontCX = 0;
var nSymLoop = 2;

var nCol = new Array(7);

var nColWidth	= 0;
var nRowThickness = 1;
var nFontSize   = 10;

var aHdrLabels = new Array(7);
aHdrLabels[0] = "Symbol";
aHdrLabels[1] = "DAY";
aHdrLabels[2] = "WEEK";
aHdrLabels[3] = "MONTH";
aHdrLabels[4] = "MTH-TO-DATE";
aHdrLabels[5] = "YEAR";
aHdrLabels[6] = "YR-TO-DATE";

var aSym = new Array(nSymLoop);
aSym[0] = getSymbol();
aSym[1] = "$INDU";

var aPrcnt = new Array(nSymLoop);
var xCalc = new Array(nSymLoop);
var aUpdateFlags = new Array(nSymLoop);
var aLast = new Array(nSymLoop);
for (var i = 0; i < 7; i++) {
    nCol[i] = 20;
    if (i < nSymLoop) {
        aLast[i] = 0;
        aPrcnt[i] = new Array(6);
        aUpdateFlags[i] = false;
    }
}


function preMain() {
    setStudyTitle("Percent Change Table ");
    setShowCursorLabel(false);
    setShowTitleParameters(false);

	formatColumns();

	nColWidth	= nFontCX*11;
	nColWidth -= 4;
	
	var fp1 = new FunctionParameter("Sym1", FunctionParameter.STRING);
	fp1.setName("Symbol 1");
	fp1.setDefault("");

	var fp2 = new FunctionParameter("Sym2", FunctionParameter.STRING);
	fp2.setName("Symbol 2");
	fp2.setDefault(aSym[1]);

	var fp3 = new FunctionParameter("inputColWidth", FunctionParameter.NUMBER);
	fp3.setName("Column Width");
	fp3.setLowerLimit(0);
	fp3.setDefault(nColWidth);

	var fp4 = new FunctionParameter("inputRowThickness", FunctionParameter.NUMBER);
	fp4.setName("Row Thickness");
	fp4.setLowerLimit(0);
	fp4.setDefault(1);

	var fp5 = new FunctionParameter("inputFontSize", FunctionParameter.NUMBER);
	fp5.setName("Font Size");
	fp5.setLowerLimit(1);
	fp5.setDefault(10);
}

var bInit = false;
var nY = 20;

function main(Sym1, Sym2, inputColWidth, inputRowThickness, inputFontSize) {    
    if (bVersion == null) bVersion = verify();
    if (bVersion == false) return;    

	var nIndex = getCurrentBarIndex();
	var nState = getBarState();

    if (bInit == false) {
        nColWidth = Math.round(inputColWidth);
        nRowThickness = Math.round(inputRowThickness)*-1;
        nFontSize = Math.round(inputFontSize);
        for (var i = 0; i < nSymLoop; i++) {
            if (i == 0 && (Sym1 == "" || Sym1 == null)) {
                aSym[i] = getSymbol();
            } else {
                aSym[i] = eval("Sym"+(i+1));
            }
            xCalc[i] = efsInternal("calc", sym(aSym[i]+",D"));
            for (var j = 0; j < 6; j++) {
                aPrcnt[i][j] = getSeries(xCalc[i], j);
            }
        }
        formatColumns();
        bInit = true;
    }
    
    if (nState == BARSTATE_NEWBAR && nIndex >= -1) {
        /////////////////////////////////////
        // HEADERS
        /////////////////////////////////////
        
        for (var i = 0; i < 7; i++) {
            drawTextPixel(nCol[i], nY,
                aHdrLabels[i],
                Color.black, Color.lightgrey,
                Text.FRAME | Text.CENTER | Text.VCENTER | Text.BOLD,
                null,
                nFontSize,
                "hdr"+i, 
                nColWidth, nRowThickness);
            if (i < nSymLoop) {
                aUpdateFlags[i] = true;
                drawTextPixel(nCol[0], nY+((i+1)*nFontCY),
                    aSym[i],
                    Color.white, Color.black,
                    Text.CENTER | Text.VCENTER,
                    null,
                    nFontSize,
                    "nColSYM"+i, 
                    nColWidth, nRowThickness);
            }
        }
    }


    // check for price change from last trade and set update flag
    for (var i = 0; i < nSymLoop; i++) {
        if (aSym[i] != null && aSym[i] != " ") {
            var nLast = close(0, sym(aSym[i]));
            if (nLast != null) {
                // price changed from last trade, update table for this symbol.
                if (nLast != aLast[i]) aUpdateFlags[i] = true;                
                aLast[i] = nLast;
            }
        }
        if (aUpdateFlags[i] == true) redraw(i);
    }
    
    

 	return;
}

/********************
    Functions
*********************/

function getStateBG(vCheckState) {
	if(vCheckState == STATE_UNCHG)	return Color.lightgrey;
	if(vCheckState == STATE_UP)		return Color.darkgreen;
	if(vCheckState == STATE_DN)		return Color.maroon;
	return Color.black;
}

function getStateFG(vCheckState) {
	if(vCheckState == STATE_UNCHG)	return Color.black;
	if(vCheckState == STATE_UP)		return Color.white;
	if(vCheckState == STATE_DN)		return Color.white;	
	return Color.black;
}

function getState(vNum) {
	if(vNum > 0) {
		return STATE_UP;
	} else if(vNum < 0) {
		return STATE_DN;
	} else {
        return STATE_UNCHG;
	}

}

function formatColumns() {
	nFontCY		= getTextHeight("A", null, nFontSize)*(-nRowThickness);
	nFontCX		= getTextWidth("A", null, nFontSize);
	
	nCol[0] = 20;                   //SYM
	nCol[1] = nCol[0] + nColWidth;  //DAY
	nCol[2]	= nCol[1] + nColWidth;  //WEEK
	nCol[3]	= nCol[2] + nColWidth;  //MTH
	nCol[4]	= nCol[3] + nColWidth;  //MTHTODATE
	nCol[5]	= nCol[4] + nColWidth;  //YEAR
	nCol[6]	= nCol[5] + nColWidth;  //YEARTODATE

	return;
}


function redraw(num) {
    for (var i = 0; i < nSymLoop; i++) {
        if (i == num) {
            for (var j = 0; j < 6; j++) {
                var nPrcnt = aPrcnt[i][j];
                if (isNaN(nPrcnt)) {
                    drawTextPixel(nCol[j+1], nY+((i+1)*nFontCY),
                        "N/A",
                        Color.black, Color.lightgrey,
                        Text.CENTER | Text.VCENTER,
                        null,
                        nFontSize,
                        "nPrcnt"+i+j, 
                        nColWidth, nRowThickness);
                } else {
                    drawTextPixel(nCol[j+1], nY+((i+1)*nFontCY),
                        nPrcnt.getValue(0).toFixed(2)+"\%",
                        getStateFG(getState(nPrcnt)), getStateBG(getState(nPrcnt)),
                        Text.CENTER | Text.VCENTER,
                        null,
                        nFontSize,
                        "nPrcnt"+i+j, 
                        nColWidth, nRowThickness);
                }
            }
        }
    }
    aUpdateFlags[num] = false;  // reset and wait for price change
    
    return;
}


// clac globals
var bInitCalc = false;
var xcloseDay = null;
var xcloseWTD = null;
var closeWTD = null;
var closeM = null;
var closeMTD = null;
var closeY = null;
var closeYTD = null;

function calc(SymInv){
    if (bInitCalc == false) {
        xcloseDay = close();
        xcloseWTD = close(inv("w"));
        bInitCalc = true;
    }
    var nNew = xcloseDay.getValue(0);
    var nOld = xcloseDay.getValue(-1);
    closeWTD = xcloseWTD.getValue(-1);
    var nDate = new Date();
    var nDateM = (nDate.getMonth())*100 + nDate.getDate();
    var nTestMNum = month(0)*100 + day(0);
    if (nTestMNum <= nDateM) {
        closeM = nNew;
    }
    if(month(0)!=month(-1)){
        closeMTD = nOld;
    }
    var nDateY = (nDate.getMonth()+1)*100 + nDate.getDate();
    var nTestYNum = month(0)*100 + day(0);
    if (year(0) < nDate.getFullYear() && nTestYNum <= nDateY) {
        closeY = nNew;
    }
    if(year(0)!=year(-1)){
        closeYTD = nOld;
    }
    
    var chgDay  = ((nNew/nOld)-1)*100;
    var chgWTD  = ((nNew/closeWTD)-1)*100;
    var chgM    = ((nNew/closeM)-1)*100;
    var chgMTD  = ((nNew/closeMTD)-1)*100;
    var chgY    = ((nNew/closeY)-1)*100;
    var chgYTD  = ((nNew/closeYTD)-1)*100;
    
    return new Array(chgDay, chgWTD, chgM, chgMTD, chgY, chgYTD);
}

function verify() {
    var b = false;
    if (getBuildNumber() < 732) {
        drawTextAbsolute(5, 35, "This study requires version 7.9.1 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;
}