Announcement

Collapse
No announcement yet.

internal function problem

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

  • internal function problem

    I think this is the same problem as my custom MACD in the other post. It never seems to get to the calcLR function.


    /************************************************** *******
    By PJ Sep 2008
    Based on the Chopiness indicator
    the caclLR function is from the eSignal downloads: LinearRegression2.efs
    ************************************************** ********/
    var myStudy1 = null;
    var myStudy2 = null;
    var myStudy3 = null;
    var aFPArray = new Array();
    var nLR = null;
    var sd;
    var lenBB;
    var bColor = true;
    debugClear();

    function preMain() {
    var x;
    setPriceStudy(false);
    setStudyTitle("Fractal Dimension 201");
    setShowTitleParameters( false );


    setCursorLabelName("FDI raw", 0);
    setDefaultBarFgColor(Color.RGB(212,212,255), 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(1,0);

    setCursorLabelName("Fractal Dimension", 1);
    setDefaultBarFgColor(Color.blue, 1);
    setPlotType(PLOTTYPE_LINE,1);
    setDefaultBarThickness(1,1);

    setCursorLabelName("mid", 2);
    setDefaultBarFgColor(Color.lightgrey, 2);
    setPlotType(PLOTTYPE_LINE,2);
    setDefaultBarThickness(1,2);

    setCursorLabelName("BBu", 3);
    setDefaultBarFgColor(Color.brown, 3);
    setPlotType(PLOTTYPE_LINE,3);
    setDefaultBarThickness(1,3);

    setCursorLabelName("BBl", 4);
    setDefaultBarFgColor(Color.brown, 4);
    setPlotType(PLOTTYPE_LINE,4);
    setDefaultBarThickness(1,4);

    setCursorLabelName("slope", 5);
    setDefaultBarFgColor(Color.red, 5);
    setPlotType(PLOTTYPE_LINE,5);
    setDefaultBarThickness(2,5);

    // addBand(61.8,PS_SOLID,1,Color.maroon,"Upper");
    // addBand(38.2,PS_SOLID,1,Color.maroon,"Lower");
    // addBand(55,PS_SOLID,1,Color.teal,"Mid");

    x=0;

    aFPArray[x] = new FunctionParameter("C_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("Chop length");
    setLowerLimit(1);
    setDefault(13); // this is the period size for the Chop indicator
    }
    x++;
    aFPArray[x] = new FunctionParameter("MA_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("MA Length");
    setLowerLimit(1);
    setDefault(3); // MA used to smooth Chop
    }
    x++
    aFPArray[x] = new FunctionParameter("sd", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("BB Std Dev");
    setDefault(1.5);
    }
    x++
    aFPArray[x] = new FunctionParameter("lenBB", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("BB Length");
    setDefault(130);
    }
    x++
    aFPArray[x] = new FunctionParameter("LR_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("Length for Linear Regression");
    setDefault(10);
    }
    x++
    aFPArray[x] = new FunctionParameter("bColor", FunctionParameter.BOOLEAN);
    with( aFPArray[x] ) {
    setName("Color Trending");
    setDefault(true); // This will color the background during Trending phases
    }
    }
    var bInit = false;
    var FDI_study = null; // Fractal Dimension Indicator is the EMA of Chop
    var nMA = null;
    var LR_study = null; // used to get Slope of the the Linear Regression of FDI
    var nLR = null;
    var nLR_1 = null;

    function main(C_len, MA_len, sd, lenBB, LR_len, bColor) {

    if (bInit == false) {
    vChop = chop(C_len);
    FDI_study = efsInternal("getFDI", MA_len, vChop);
    LR_study = efsInternal("calcLR", LR_len, FDI_study);
    bInit = true;
    }

    nMA = FDI_study.getValue(0);

    // looking for the slope of the FDI line.
    nLR = LR_study.getValue(0);
    if (nLR < 0 ){
    if (bColor) setBarBgColor( Color.lime, 0 );
    }

    var vBBu;
    var vBBm;
    var vBBl;

    myStudy1 = upperBB( lenBB, sd, vChop );
    myStudy2 = middleBB( lenBB, sd, vChop );
    myStudy3 = lowerBB( lenBB, sd, vChop );


    //retrieve the current BB values
    vBBu = myStudy1.getValue(0);
    vBBm = myStudy2.getValue(0);
    vBBl = myStudy3.getValue(0);

    return new Array( vChop.getValue(0), nMA, vBBm, vBBu, vBBl, nLR );


    }
    function getFDI(l, c){
    myStudy = ema( l, c );
    return myStudy
    }

    function calcLR(nL, myStudy) {
    debugPrintln( getCurrentBarIndex() + " " + nL + "FDI(-10)= " + myStudy(-10) );

    // y = Ax + B;
    // A = SUM( (x-xAVG)*(y-yAVG) ) / SUM( (x-xAVG)^2 )
    // A = slope
    // B = yAVG - (A*xAVG);

    if (myStudy(-(nL-1)) != null) {

    var xSum = 0;
    var ySum = 0;
    var i = 0;
    for (i = 0; i < nL; i++) {
    xSum += i;
    ySum += myStudy(-i);

    }
    var xAvg = xSum/nL;
    var yAvg = ySum/nL;
    var aSum1 = 0;
    var aSum2 = 0;
    i = 0;
    for (i = 0; i < nL; i++) {
    aSum1 += (i-xAvg) * (myStudy(-i)-yAvg);
    aSum2 += (i-xAvg)*(i-xAvg);
    }
    var A = (aSum1 / aSum2);
    var B = yAvg - (A*xAvg);
    }

    return A;
    }



    (BTW how do you get this to post the efs with all the formatting)

  • #2
    Re: internal function problem

    pj909
    In the calcLR function you need to replace each instance of myStudy(...) which is not a valid syntax with myStudy.getValue(...) to retrieve the values from the myStudy series that you are passing to that function
    To format the code you need to use either the [ PHP ] or [ CODE ] tags (see the vB Help for examples on how to use these tags)
    Alex


    Originally posted by pj909
    I think this is the same problem as my custom MACD in the other post. It never seems to get to the calcLR function.


    /************************************************** *******
    By PJ Sep 2008
    Based on the Chopiness indicator
    the caclLR function is from the eSignal downloads: LinearRegression2.efs
    ************************************************** ********/
    var myStudy1 = null;
    var myStudy2 = null;
    var myStudy3 = null;
    var aFPArray = new Array();
    var nLR = null;
    var sd;
    var lenBB;
    var bColor = true;
    debugClear();

    function preMain() {
    var x;
    setPriceStudy(false);
    setStudyTitle("Fractal Dimension 201");
    setShowTitleParameters( false );


    setCursorLabelName("FDI raw", 0);
    setDefaultBarFgColor(Color.RGB(212,212,255), 0);
    setPlotType(PLOTTYPE_LINE,0);
    setDefaultBarThickness(1,0);

    setCursorLabelName("Fractal Dimension", 1);
    setDefaultBarFgColor(Color.blue, 1);
    setPlotType(PLOTTYPE_LINE,1);
    setDefaultBarThickness(1,1);

    setCursorLabelName("mid", 2);
    setDefaultBarFgColor(Color.lightgrey, 2);
    setPlotType(PLOTTYPE_LINE,2);
    setDefaultBarThickness(1,2);

    setCursorLabelName("BBu", 3);
    setDefaultBarFgColor(Color.brown, 3);
    setPlotType(PLOTTYPE_LINE,3);
    setDefaultBarThickness(1,3);

    setCursorLabelName("BBl", 4);
    setDefaultBarFgColor(Color.brown, 4);
    setPlotType(PLOTTYPE_LINE,4);
    setDefaultBarThickness(1,4);

    setCursorLabelName("slope", 5);
    setDefaultBarFgColor(Color.red, 5);
    setPlotType(PLOTTYPE_LINE,5);
    setDefaultBarThickness(2,5);

    // addBand(61.8,PS_SOLID,1,Color.maroon,"Upper");
    // addBand(38.2,PS_SOLID,1,Color.maroon,"Lower");
    // addBand(55,PS_SOLID,1,Color.teal,"Mid");

    x=0;

    aFPArray[x] = new FunctionParameter("C_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("Chop length");
    setLowerLimit(1);
    setDefault(13); // this is the period size for the Chop indicator
    }
    x++;
    aFPArray[x] = new FunctionParameter("MA_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("MA Length");
    setLowerLimit(1);
    setDefault(3); // MA used to smooth Chop
    }
    x++
    aFPArray[x] = new FunctionParameter("sd", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("BB Std Dev");
    setDefault(1.5);
    }
    x++
    aFPArray[x] = new FunctionParameter("lenBB", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("BB Length");
    setDefault(130);
    }
    x++
    aFPArray[x] = new FunctionParameter("LR_len", FunctionParameter.NUMBER);
    with( aFPArray[x] ) {
    setName("Length for Linear Regression");
    setDefault(10);
    }
    x++
    aFPArray[x] = new FunctionParameter("bColor", FunctionParameter.BOOLEAN);
    with( aFPArray[x] ) {
    setName("Color Trending");
    setDefault(true); // This will color the background during Trending phases
    }
    }
    var bInit = false;
    var FDI_study = null; // Fractal Dimension Indicator is the EMA of Chop
    var nMA = null;
    var LR_study = null; // used to get Slope of the the Linear Regression of FDI
    var nLR = null;
    var nLR_1 = null;

    function main(C_len, MA_len, sd, lenBB, LR_len, bColor) {

    if (bInit == false) {
    vChop = chop(C_len);
    FDI_study = efsInternal("getFDI", MA_len, vChop);
    LR_study = efsInternal("calcLR", LR_len, FDI_study);
    bInit = true;
    }

    nMA = FDI_study.getValue(0);

    // looking for the slope of the FDI line.
    nLR = LR_study.getValue(0);
    if (nLR < 0 ){
    if (bColor) setBarBgColor( Color.lime, 0 );
    }

    var vBBu;
    var vBBm;
    var vBBl;

    myStudy1 = upperBB( lenBB, sd, vChop );
    myStudy2 = middleBB( lenBB, sd, vChop );
    myStudy3 = lowerBB( lenBB, sd, vChop );


    //retrieve the current BB values
    vBBu = myStudy1.getValue(0);
    vBBm = myStudy2.getValue(0);
    vBBl = myStudy3.getValue(0);

    return new Array( vChop.getValue(0), nMA, vBBm, vBBu, vBBl, nLR );


    }
    function getFDI(l, c){
    myStudy = ema( l, c );
    return myStudy
    }

    function calcLR(nL, myStudy) {
    debugPrintln( getCurrentBarIndex() + " " + nL + "FDI(-10)= " + myStudy(-10) );

    // y = Ax + B;
    // A = SUM( (x-xAVG)*(y-yAVG) ) / SUM( (x-xAVG)^2 )
    // A = slope
    // B = yAVG - (A*xAVG);

    if (myStudy(-(nL-1)) != null) {

    var xSum = 0;
    var ySum = 0;
    var i = 0;
    for (i = 0; i < nL; i++) {
    xSum += i;
    ySum += myStudy(-i);

    }
    var xAvg = xSum/nL;
    var yAvg = ySum/nL;
    var aSum1 = 0;
    var aSum2 = 0;
    i = 0;
    for (i = 0; i < nL; i++) {
    aSum1 += (i-xAvg) * (myStudy(-i)-yAvg);
    aSum2 += (i-xAvg)*(i-xAvg);
    }
    var A = (aSum1 / aSum2);
    var B = yAvg - (A*xAvg);
    }

    return A;
    }



    (BTW how do you get this to post the efs with all the formatting)

    Comment


    • #3
      thank you
      How can I return both the Slope and the Linear Regression or do I cave to call the function twice? If the later how do I tell it which variable to return?

      Comment


      • #4
        pj909
        You don't need to call the function twice. Return both in the function in which you calculate them and then retrieve them individually from the efsInternal() call using the getSeries() function. For the syntax of this function and an example of its use see the related article in the EFS KnowledgeBase. Also if you search the forums for getSeries* you will find several other examples on how to use the function for this purpose
        Alex


        Originally posted by pj909
        thank you
        How can I return both the Slope and the Linear Regression or do I cave to call the function twice? If the later how do I tell it which variable to return?

        Comment

        Working...
        X