Announcement

Collapse
No announcement yet.

PercentChange.efs

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PercentChange.efs

    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:
    PHP 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 
    07i++) {
        
    nCol[i] = 20;
        if (
    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(Sym1Sym2inputColWidthinputRowThicknessinputFontSize) {    
        if (
    bVersion == nullbVersion 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 
    0nSymLoopi++) {
                if (
    == && (Sym1 == "" || Sym1 == null)) {
                    
    aSym[i] = getSymbol();
                } else {
                    
    aSym[i] = eval("Sym"+(i+1));
                }
                
    xCalc[i] = efsInternal("calc"sym(aSym[i]+",D"));
                for (var 
    06j++) {
                    
    aPrcnt[i][j] = getSeries(xCalc[i], j);
                }
            }
            
    formatColumns();
            
    bInit true;
        }
        
        if (
    nState == BARSTATE_NEWBAR && nIndex >= -1) {
            
    /////////////////////////////////////
            // HEADERS
            /////////////////////////////////////
            
            
    for (var 07i++) {
                
    drawTextPixel(nCol[i], nY,
                    
    aHdrLabels[i],
                    
    Color.blackColor.lightgrey,
                    
    Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                    
    null,
                    
    nFontSize,
                    
    "hdr"+i
                    
    nColWidthnRowThickness);
                if (
    nSymLoop) {
                    
    aUpdateFlags[i] = true;
                    
    drawTextPixel(nCol[0], nY+((i+1)*nFontCY),
                        
    aSym[i],
                        
    Color.whiteColor.black,
                        
    Text.CENTER Text.VCENTER,
                        
    null,
                        
    nFontSize,
                        
    "nColSYM"+i
                        
    nColWidthnRowThickness);
                }
            }
        }


        
    // check for price change from last trade and set update flag
        
    for (var 0nSymLoopi++) {
            if (
    aSym[i] != null && aSym[i] != " ") {
                var 
    nLast close(0sym(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] == trueredraw(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"nullnFontSize)*(-nRowThickness);
        
    nFontCX        getTextWidth("A"nullnFontSize);
        
        
    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 
    0nSymLoopi++) {
            if (
    == num) {
                for (var 
    06j++) {
                    var 
    nPrcnt aPrcnt[i][j];
                    if (
    isNaN(nPrcnt)) {
                        
    drawTextPixel(nCol[j+1], nY+((i+1)*nFontCY),
                            
    "N/A",
                            
    Color.blackColor.lightgrey,
                            
    Text.CENTER Text.VCENTER,
                            
    null,
                            
    nFontSize,
                            
    "nPrcnt"+i+j
                            
    nColWidthnRowThickness);
                    } 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
                            
    nColWidthnRowThickness);
                    }
                }
            }
        }
        
    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(
    chgDaychgWTDchgMchgMTDchgYchgYTD);
    }

    function 
    verify() {
        var 
    false;
        if (
    getBuildNumber() < 732) {
            
    drawTextAbsolute(535"This study requires version 7.9.1 or later."
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"error");
            
    drawTextAbsolute(520"Click HERE to upgrade.@URL=http://www.esignal.com/download/default.asp"
                
    Color.whiteColor.blueText.RELATIVETOBOTTOM|Text.RELATIVETOLEFT|Text.BOLD|Text.LEFT,
                
    null13"upgrade");
            return 
    b;
        } else {
            
    true;
        }
        
        return 
    b;

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation
Working...
X