Announcement

Collapse
No announcement yet.

Edited Efs Does Not Plot

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Edited Efs Does Not Plot

    THIS IS AN EFS ORIGINALLY BY STEVE HARE. It had bands and an WMA. I have taken those off as it requires huge PC resources.In doing the editing it will not plot just the nReg array. I can't seem to get the variable named correctly. I was getting an error on line 52 - return NewArray. I added the lsf after removing the others. I now get no more error codes, but no plot. Would anybody know how to plot the nReg only or better yet rewrite the entire efs into efs2 format.
    (I have taken out the code for wma and bands)

    *****************************************
    Steve Hare © March 2004
    Use and/or modify this code freely. If you redistribute it
    please include this and/or any other comment blocks and a
    description of any changes you make.
    ************************************************** ********/
    /*================================================= ====================
    Fix History:

    8/25/2005 - modified to refer to previous bar values
    3/4/2004 - added standard deviation calculation plus bands
    1.2.0
    3/4/2004 - Changed default period to 16, modified Regression to provide predicted value two bars in advance(line 69)
    1.1.0
    3/3/2004 - Initial Release
    1.0.0

    ================================================== ===================*/

    //defines global variables
    var nArray;
    var barcount = 0;

    function preMain(){
    setStudyTitle("TEST Regression");
    setPriceStudy(true);//buttons displayed in price pane
    // setPriceStudy(false);//buttons displayed in non price pane
    setCursorLabelName("Reg", 0);
    setDefaultBarFgColor(Color.red, 0);
    setDefaultBarThickness(2,0);

    nEditProperties();//Function Parameter statements
    }

    function main(nReg,Source,Unused){

    if (getBarState() == BARSTATE_ALLBARS) {//true once during initial load and subsequent reload using reloadEFS()
    nArray = Initialize_1X_Array(nReg);//calls array definition function, setting array to length of regression analysis
    }

    if (getCurrentBarIndex() < -1000)return;
    if (getBarState() == BARSTATE_NEWBAR) {
    barcount++;


    var a = Price(Source); //function returns the function Price

    nArray.pop(); nArray.unshift(a); //populating Price Source into an array
    debugPrintln(typeof(nReg));

    if (barcount > nReg){//prevents regression from being called until nArray is long enough
    var lsf = LSF(nReg, nArray);//calls linear regression function (returns an array)
    }

    }
    return new Array(lsf); //returns the one value to the chart

    }


    //--support functions--support functions--support functions--support functions--support functions--

    function LSF(nv1,fnArray){//Linear Least Squares Fit routine
    if (nv1 < 2){nv1 = 2;}//prevents division by zero "ouch!!" for nv1 = 0
    var sXX = 0.0; var sYY = 0;var sXY = 0; var sX = 0.0; var sY = 0;var i;
    for(i = 0; i < nv1; i++) {//y = nInt + nSlope*x
    sX += nv1-i;sY += fnArray[i];
    sYY += Math.pow(fnArray[i],2);sXX += Math.pow((nv1-i),2);
    sXY += (nv1-i)*fnArray[i];
    }
    var xAve = sX /nv1;var yAve = sY / nv1;
    var nSlope = (nv1*sXY - sX*sY) / (nv1*sXX - sX*sX);
    var nInt = (sY - nSlope * sX)/nv1;//if(debug)debugPrintln("a = "+a);
    var RR= (Math.pow((sXY-nv1*yAve*xAve),2))/(((sXX-xAve*xAve*nv1)*(sYY-yAve*yAve*nv1)));
    var pred1 = (nv1+1) + nInt;var pred2 = nSlope*(nv1+2) + nInt;

    return new Array (nSlope.toFixed(2)*1, nInt.toFixed(2)*1, RR.toFixed(2)*1,pred1.toFixed(2)*1,pred2.toFixed(2 )*1);
    //lsf = LSF(nv1,fnArray) where lsf[0] = slope, lsf[1] = Int, lsf[2] = RR, lsf[3] = pred1, lsf[4] = pred2,
    }//End Linear Least Squares Fit routine


    function Initialize_1X_Array(n1){//initialize 1 dim array
    var i; var newArray = new Array();
    if (n1 == null){n1 =2;}
    for (i = 0;i < n1; i++){newArray[i] = 0;}
    return newArray;
    }

    function Price (n){

    var a = close(-1);var b = high(-1);var c = low(-1);var d = open(-1);
    var nn = close(-1);
    if (n == "High")nn = b;
    else if (n == "Low")nn = c;
    else if (n == "Open")nn = d;
    else if (n == "HL/2")nn = (b+c)/2;
    else if (n == "HLC/3")nn = (a+b+c)/3;
    else if (n == "OHLC/4")nn = (a+b+c+d)/4;
    return nn.toFixed(3)*1;
    }

    function nEditProperties() { //contains all the edit studies Function Parameter statements normally in premain, put here for clarity

    var fp1 = new FunctionParameter("nReg", FunctionParameter.NUMBER);
    fp1.setName("Regression Length");
    fp1.setLowerLimit(2);
    fp1.setDefault(50);

    var fp3 = new FunctionParameter("Source", FunctionParameter.STRING);
    fp3.setName("Source");
    fp3.addOption("Close");
    fp3.addOption("High");
    fp3.addOption("Low");
    fp3.addOption("Open");
    fp3.addOption("HL/2");
    fp3.addOption("HLC/3");
    fp3.addOption("OHLC/4");
    fp3.setDefault("Close"); //Edit this value to set a new default

    var fp4 = new FunctionParameter("Unused", FunctionParameter.STRING);//work-around to space out edit sudies
    fp4.setName("Unused");
    fp4.setDefault("[ ]");

    }

  • #2
    ScottD

    I am not sure this is the only isdsue, but if you return just one variable you use

    return lsma

    if you reurn more than one variable you use

    return new Array(var1, var2)

    SO you need to use

    return (lsf);

    Comment

    Working...
    X