Announcement

Collapse
No announcement yet.

Why the following does not work?

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

  • Why the following does not work?

    Hi Alex/Jason:

    function main() {
    var vSym = getSymbol();
    var vInt = getInterval();

    var x = calc(length, vSym, vInt);

    return x;
    }

    function calc(length, vS, vI) {
    var vClose = close(0, -length, sym(vS+","+vI));

    }

  • #2
    ..sorry hit the wrong button ... continue...

    so vClose in function calc is null ... why is that?

    in ... do something...

    for (i=0; i<length; i++) {

    debugPrintln(vClose[i]);

    }

    here vClose[i] is null.

    Yet the same statement placed in main would contain the right data.

    Puzzled....

    ziggy

    Comment


    • #3
      z11,

      if you want to hear it from the big guns, you can wait, but from here I would say it is either because you are not returning anything from your function (and you have defined vClose as a local variable to the function) -or- the length you are sending your function is not defined.

      Comment


      • #4
        ziggy
        In addition to what Steve has already said you are mixing EFS1 and EFS2 syntax in
        close(0, -length, sym(vS+","+vI))
        If you want to use the EFS1 syntax then it should be close(0, -length, (vS+","+vI))
        otherwise it should be close(0, sym(vS+","+vI))
        Alex

        Comment


        • #5
          Steve, thank you for your thoughts as always:
          1)you are not returning anything from your function

          I am returning calculatd value.

          2) the length you are sending your function is not defined

          Using debugPrintln, I ascertained that length variable contains the correct value


          Alex, thanks for reply. But using "close(0, sym(vS+","+vI))", and using vClose[i] to access the content of the array vClose still did not yield anything. I think I have key misunderstanding here with EFS2. Please enlighten.

          Thanks as always,

          ziggy

          Comment


          • #6
            ziggy
            1) Actually you are not returning anything from function calc()
            2) That is not apparent from the sample code you posted. Please post a complete working sample when illustrating a problem
            3) vClose[i] will not return anything because vClose is not an array but a specific value.
            If you wanted to do a for loop you would first need to declare the series
            var vClose = close(sym(vS+","+vI));
            then in the loop you would retrieve the specific values using the getValue method
            vClose.getValue(-i)
            Alex

            Comment


            • #7
              Alex, thanks, but still not working.

              First of all, according to efs2 documentation on close():
              close( barIndex [, numBars] [, sym()] [, inv()] )

              So I initially used:
              var vClose = close(0, -length, sym(vS+","+vI));

              and understood vClose as an Object Series, and attempted to access values of vClose via

              var v1 = null;

              for (i=0; i< length; i++) {
              v1 = vClose.getValue(-i);
              debugPrintln(v1);
              }

              return v1;

              ....

              Now I declared: var vClose = close(0, sym(vS+","+vI));

              and v1 = vClose.getValue(-i);

              and still can't get a value.

              Incidently, if move this set of statement into function main, it works.

              Puzzled.

              ziggy

              Comment


              • #8
                ziggy
                [,numbars] is not an available parameter when using the EFS2 syntax.
                With regards to your syntax if you use
                var vClose = close(0, sym("symbol"))
                you are assigning to the variable vClose a specific value ie the close at the current bar. Hence you will not get any values if you then try to access historical values using the getValue() method.
                You need to declare the series ie close(sym("symbol")) at which point you can access/retrieve the historical values using the getValue() method
                As to the function calc not returning anything that is because you do not have any return statement in that function
                Alex

                Comment


                • #9
                  By removing 0 from vClose declaration made it work...I must say I was confused by the documentation.

                  Thanks Alex.

                  happy new year!

                  ziggy

                  Comment


                  • #10
                    ziggy
                    FWIW a more direct method (albeit slightly less efficient) would have been to access the values directly in the for loop without the need of declaring the series. See example enclosed below.
                    Alex

                    PHP Code:
                    for (var i=0i<lengthi++){
                        
                    close(-isym("symbol"));

                    Comment


                    • #11
                      Thanks Alex.

                      Even though this way sym() gets called on each iteration...

                      I however am running into another puzzle:

                      var atrD = null;

                      if (isDaily()) atrD = atr(14);
                      else atrD = atr(14, inv("D"));
                      var atr0 = atrD.getValue(0);

                      now this works on charts of intraday intervals but not on Daily or weekly , monthly charts ... i.e., atr0 gets a null while atr0 gets the correct value for intraday charts.

                      ???

                      ziggy

                      Comment


                      • #12
                        z11
                        Please post a complete working example that illustrates the problem
                        Alex

                        Comment


                        • #13
                          Happy New Year, Alex!

                          function premain() {
                          setPriceStudy(false);
                          }

                          var atrD = null;
                          var nInit = null;
                          var bInit = null;
                          function main() {
                          if (bInit == false) {
                          atrD = atr(14, inv("D");
                          bInit = true;
                          }
                          var atrD0 = atrD.getValue(0);
                          return atrD0
                          }

                          // atrD0 has a null value if applied in a Daily chart


                          Thanks.
                          ziggy

                          Comment


                          • #14
                            sorry missed a closing parantheis in :

                            atrD = atr(14, inv("D"));

                            in the above example.

                            Anyway, this code works in all intraday charts but not on daily chart.

                            Comment


                            • #15
                              z11
                              Actually the code you posted will return an error regardless of the interval being used.
                              Replace var bInit=null with var bInit=false and it will work also on daily charts
                              Also replace premain with preMain. JavScript is a case sensitive language
                              Alex



                              Originally posted by z11
                              Happy New Year, Alex!

                              function premain() {
                              setPriceStudy(false);
                              }

                              var atrD = null;
                              var nInit = null;
                              var bInit = null;
                              function main() {
                              if (bInit == false) {
                              atrD = atr(14, inv("D");
                              bInit = true;
                              }
                              var atrD0 = atrD.getValue(0);
                              return atrD0
                              }

                              // atrD0 has a null value if applied in a Daily chart


                              Thanks.
                              ziggy

                              Comment

                              Working...
                              X