Announcement

Collapse
No announcement yet.

passing main output back to funtion

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

  • passing main output back to funtion

    I am having a hard time refrencing the value of vEMA for the last completed bar on the chart. plots fine until the current bar in realtime. then the indicator quickly "pulls in " to the current close(). Have tried everything i can think of any thoughts. (sorry for the ugly code but i'm new to efs)


    function main(Length,Length2) {


    var nHL = efsInternal("myCTR"); //this calls the function "range" and creates a series
    vEMA = ema(Length,ema(Length2,nHL)); //the series is used as an input to the builtin study

    return new Array( vEMA);
    }
    //this is the separate function that calculates the center
    function myCTR() {

    if(bInit == false){

    vCtr = close(); // initialize vCtr

    // bInit = true;
    // }

    oCtr = vCtr; // this is where i get hung up need to get oCtr equal to vEMA from last completed bar
    vCtr = (oCtr *8 + close())/9;

    return (vCtr);

    }

  • #2
    Re: passing main output back to funtion

    Stacy
    Pass the vEMA series to the separate function using the efsInternal() call eg
    PHP Code:
    ...
    vEMA ema(Length,ema(Length2,nHL));
    nHL efsInternal("myCTR"vEMA);
    ... 
    Add the parameter in the arguments of the function you are calling and then you can retrieve the value of the series using the getValue() method
    PHP Code:
    function myCTR(source) {
        var 
    PrevEMA source.getValue(-1);
        ... 
    PrevEMA will return the value of the vEMA series at the prior bar
    Note that this example is only intended to illustrate how to retrieve the previous value of vEMA in the myCTR() function and does not take into consideration possible logic errors in the rest of the code
    Alex



    Originally posted by Stacy
    I am having a hard time refrencing the value of vEMA for the last completed bar on the chart. plots fine until the current bar in realtime. then the indicator quickly "pulls in " to the current close(). Have tried everything i can think of any thoughts. (sorry for the ugly code but i'm new to efs)


    function main(Length,Length2) {


    var nHL = efsInternal("myCTR"); //this calls the function "range" and creates a series
    vEMA = ema(Length,ema(Length2,nHL)); //the series is used as an input to the builtin study

    return new Array( vEMA);
    }
    //this is the separate function that calculates the center
    function myCTR() {

    if(bInit == false){

    vCtr = close(); // initialize vCtr

    // bInit = true;
    // }

    oCtr = vCtr; // this is where i get hung up need to get oCtr equal to vEMA from last completed bar
    vCtr = (oCtr *8 + close())/9;

    return (vCtr);

    }

    Comment


    • #3
      I think I have screwed myself up farther here. I'm sure this realtively trivial but I can not seem to get my head around this. Used be okay at programing C, easy language , ensign, but this is driving me nuts. Made a couple of changes based on prior suggestions but now I get a "formula file not found" error. Thanks for your patience.

      my current complete code is :

      function preMain() {

      setPriceStudy(true);
      setStudyTitle("CTR");
      // setCursorLabelName("CTR", 0);
      setCursorLabelName("EMA", 1);

      // setDefaultBarFgColor(Color.blue, 0);
      setDefaultBarFgColor(Color.red, 1);

      var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
      fp1.setLowerLimit(1);
      fp1.setDefault(2);

      var fp2 = new FunctionParameter("Length2", FunctionParameter.NUMBER);
      fp2.setLowerLimit(1);
      fp2.setDefault(2);
      }

      var bInit = false;
      var vCtr ;
      var oCtr ;
      var nHL;

      function main(Length,Length2) {
      if(bInit == false){

      var nHL = close(); // initialize nHL

      bInit = true;
      }


      // var nHL = efsInternal("myCTR"); //this calls the function "range" and creates a series
      vEMA = ema(Length,ema(Length2,nHL)); //the series is used as an input to the builtin study
      nHL = efsInternal("myCTR", vEMA);
      return new Array( vEMA);
      }
      //this is the separate function that calculates the center
      function myCTR(source) {

      var PrevEMA = source.getValue(-1);



      oCtr = PrevEMA;
      vCtr = (oCtr *8 +close())/9;

      return (vCtr);

      }

      Comment


      • #4
        Stacy
        FWIW I am not getting any errors when running the script you posted
        Alex


        Originally posted by Stacy
        I think I have screwed myself up farther here. I'm sure this realtively trivial but I can not seem to get my head around this. Used be okay at programing C, easy language , ensign, but this is driving me nuts. Made a couple of changes based on prior suggestions but now I get a "formula file not found" error. Thanks for your patience.

        my current complete code is :

        function preMain() {

        setPriceStudy(true);
        setStudyTitle("CTR");
        // setCursorLabelName("CTR", 0);
        setCursorLabelName("EMA", 1);

        // setDefaultBarFgColor(Color.blue, 0);
        setDefaultBarFgColor(Color.red, 1);

        var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
        fp1.setLowerLimit(1);
        fp1.setDefault(2);

        var fp2 = new FunctionParameter("Length2", FunctionParameter.NUMBER);
        fp2.setLowerLimit(1);
        fp2.setDefault(2);
        }

        var bInit = false;
        var vCtr ;
        var oCtr ;
        var nHL;

        function main(Length,Length2) {
        if(bInit == false){

        var nHL = close(); // initialize nHL

        bInit = true;
        }


        // var nHL = efsInternal("myCTR"); //this calls the function "range" and creates a series
        vEMA = ema(Length,ema(Length2,nHL)); //the series is used as an input to the builtin study
        nHL = efsInternal("myCTR", vEMA);
        return new Array( vEMA);
        }
        //this is the separate function that calculates the center
        function myCTR(source) {

        var PrevEMA = source.getValue(-1);



        oCtr = PrevEMA;
        vCtr = (oCtr *8 +close())/9;

        return (vCtr);

        }

        Comment


        • #5
          I see my error. I was working on a copy of the code. When saved and added to chart no errors.

          Thanks for your help. You do a fantastic job on thses forums.

          Comment


          • #6
            Stacy
            You are most welcome and thank your for the kind words
            Alex


            Originally posted by Stacy
            I see my error. I was working on a copy of the code. When saved and added to chart no errors.

            Thanks for your help. You do a fantastic job on thses forums.

            Comment


            • #7
              AARRGGGh. Is EFS particularly difficult or am I particularly dense?

              This code works fine until the current bar in realtime. I have tried seting it up with an internal function call, thinking that I needed to refrence vEMA from the prior bar, and everything else I can think of. What am I missing. I went through the tutorial videos but I am obviously missing something. What is the best source to learn a little basic programing in EFS2?
              --------------------------------------------------------
              function preMain() {

              setPriceStudy(true);
              setStudyTitle("CTR");

              setCursorLabelName("CTR", 1);


              setDefaultBarFgColor(Color.red, 1);

              var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
              fp1.setLowerLimit(1);
              fp1.setDefault(2);


              }

              var bInit = false;
              var vCtr =0;
              var oCtr =0;

              function main(Length,Length2) {



              var nHL = efsInternal("myCTR"); //this calls the function "myCTR" and creates a series
              vEMA = ema(2,ema(2,ema(2,nHL))); //the series is used as an input to the builtin study

              return new Array( vEMA);
              }
              //this is the separate function that calculates the range
              function myCTR() {



              oCtr = vCtr;
              vCtr = (oCtr *8 +close())/9;

              return (vCtr);

              }

              Comment


              • #8
                Stacy
                The reason for the behavior you are seeing in real time is because you are assigning the value of vCtr to oCtr on every tick whereas it should occur once only on the first tick of each new bar. Try replacing
                oCtr = vCtr;
                with
                if(getBarState()==BARSTATE_NEWBAR) oCtr = vCtr;
                and it should work fine also in real time
                Alex


                Originally posted by Stacy
                AARRGGGh. Is EFS particularly difficult or am I particularly dense?

                This code works fine until the current bar in realtime. I have tried seting it up with an internal function call, thinking that I needed to refrence vEMA from the prior bar, and everything else I can think of. What am I missing. I went through the tutorial videos but I am obviously missing something. What is the best source to learn a little basic programing in EFS2?
                --------------------------------------------------------
                function preMain() {

                setPriceStudy(true);
                setStudyTitle("CTR");

                setCursorLabelName("CTR", 1);


                setDefaultBarFgColor(Color.red, 1);

                var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
                fp1.setLowerLimit(1);
                fp1.setDefault(2);


                }

                var bInit = false;
                var vCtr =0;
                var oCtr =0;

                function main(Length,Length2) {



                var nHL = efsInternal("myCTR"); //this calls the function "myCTR" and creates a series
                vEMA = ema(2,ema(2,ema(2,nHL))); //the series is used as an input to the builtin study

                return new Array( vEMA);
                }
                //this is the separate function that calculates the range
                function myCTR() {



                oCtr = vCtr;
                vCtr = (oCtr *8 +close())/9;

                return (vCtr);

                }

                Comment


                • #9
                  Can't thank you enough.

                  Stacy

                  Comment


                  • #10
                    Stacy
                    FWIW your myCtr() function ie
                    PHP Code:
                    var vCtr =0;
                    var 
                    oCtr =0;
                    function 
                    myCTR() {
                        if(
                    getBarState()==BARSTATE_NEWBARoCtr vCtr;
                        
                    vCtr = (oCtr *+close())/9
                        return (
                    vCtr);

                    can be condensed to the following
                    PHP Code:
                    ema((2*9)-1); 
                    which means that you don't need a separate function to calculate that average and can accomplish the same result by nesting it into the vEMA calculation in main ie
                    PHP Code:
                    vEMA ema(2,ema(2,ema(2,ema((2*9)-1)))); 
                    Hope this helps
                    Alex

                    Comment


                    • #11
                      I'll try that.

                      Thanks again

                      Comment


                      • #12
                        Stacy
                        My pleasure
                        Alex


                        Originally posted by Stacy
                        I'll try that.

                        Thanks again

                        Comment

                        Working...
                        X