Announcement

Collapse
No announcement yet.

Why are Symbol & Interval null outside of the bInit block in main?

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

  • Why are Symbol & Interval null outside of the bInit block in main?

    In the code below,
    Symbol and Interval print as null.
    The other ways shown in the code are ok.

    I don't understand why the values of Symbol, Interval and vSymbol are not available outside of the inital if (bInit) block?

    Any explanations?

    Thanks,

    jgr


    var bInit = false;
    var xMA = null;
    var tempv = null;
    var curSym = null;
    var curInt = null;

    function main(Type,Length,Source,Symbol,Interval,Offset,Par ams) {

    if(bInit == false){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();
    var vSymbol = Symbol+","+Interval;
    xMA = offsetSeries(eval(Type)(Length,eval(Source)(sym(vS ymbol))),Offset);
    setShowTitleParameters(eval(Params));
    curSym = Symbol;
    curInt = Interval;
    tempv = vSymbol;
    bInit = true;
    }


    var temps = getCurrentBarIndex();
    var tempsym = getSymbol();
    var tempint = getInterval();
    debugPrintln("Got Here: Count: "+temps);
    debugPrintln("symbol: "+Symbol+" Interval: "+Interval+" vSymbol: "+vSymbol);
    debugPrintln("curSym: "+curSym+" curInt: "+curInt);
    debugPrintln("tempsym: "+tempsym+" tempint: "+tempint+" tempv: "+tempv);
    debugPrintln(" ");
    return getSeries(xMA);

  • #2
    Re: Why are Symbol & Interval null outside of the bInit block in main?

    Hi jgr,

    You need to set these up as global variables.

    Regards,
    Jane

    Originally posted by jgr
    In the code below,
    Symbol and Interval print as null.
    The other ways shown in the code are ok.

    I don't understand why the values of Symbol, Interval and vSymbol are not available outside of the inital if (bInit) block?

    Any explanations?

    Thanks,

    jgr


    var bInit = false;
    var xMA = null;
    var tempv = null;
    var curSym = null;
    var curInt = null;

    function main(Type,Length,Source,Symbol,Interval,Offset,Par ams) {

    if(bInit == false){
    if(Symbol == null) Symbol = getSymbol();
    if(Interval == null) Interval = getInterval();
    var vSymbol = Symbol+","+Interval;
    xMA = offsetSeries(eval(Type)(Length,eval(Source)(sym(vS ymbol))),Offset);
    setShowTitleParameters(eval(Params));
    curSym = Symbol;
    curInt = Interval;
    tempv = vSymbol;
    bInit = true;
    }


    var temps = getCurrentBarIndex();
    var tempsym = getSymbol();
    var tempint = getInterval();
    debugPrintln("Got Here: Count: "+temps);
    debugPrintln("symbol: "+Symbol+" Interval: "+Interval+" vSymbol: "+vSymbol);
    debugPrintln("curSym: "+curSym+" curInt: "+curInt);
    debugPrintln("tempsym: "+tempsym+" tempint: "+tempint+" tempv: "+tempv);
    debugPrintln(" ");
    return getSeries(xMA);

    Comment


    • #3
      Re: Why are Symbol & Interval null outside of the bInit block in main?

      Hi jgr,

      The issue appears to be related to the variable scope in which you have defined the variables. Perform a forum search with this query Variable* AND scope* AND declare* and my user name. There are several threads on variable scope that should explain the behavior you are seeing. If you exclude my user name, there are a number of additional explanatory threads that may help you as well.

      Let me know if this is sufficient.

      Originally posted by jgr
      In the code below,
      Symbol and Interval print as null.
      The other ways shown in the code are ok.

      I don't understand why the values of Symbol, Interval and vSymbol are not available outside of the inital if (bInit) block?

      Any explanations?

      Thanks,

      jgr


      var bInit = false;
      var xMA = null;
      var tempv = null;
      var curSym = null;
      var curInt = null;

      function main(Type,Length,Source,Symbol,Interval,Offset,Par ams) {

      if(bInit == false){
      if(Symbol == null) Symbol = getSymbol();
      if(Interval == null) Interval = getInterval();
      var vSymbol = Symbol+","+Interval;
      xMA = offsetSeries(eval(Type)(Length,eval(Source)(sym(vS ymbol))),Offset);
      setShowTitleParameters(eval(Params));
      curSym = Symbol;
      curInt = Interval;
      tempv = vSymbol;
      bInit = true;
      }


      var temps = getCurrentBarIndex();
      var tempsym = getSymbol();
      var tempint = getInterval();
      debugPrintln("Got Here: Count: "+temps);
      debugPrintln("symbol: "+Symbol+" Interval: "+Interval+" vSymbol: "+vSymbol);
      debugPrintln("curSym: "+curSym+" curInt: "+curInt);
      debugPrintln("tempsym: "+tempsym+" tempint: "+tempint+" tempv: "+tempv);
      debugPrintln(" ");
      return getSeries(xMA);

      Comment


      • #4
        Re: Re: Why are Symbol & Interval null outside of the bInit block in main?

        HI jgr,

        In my haste I forgot to mention some code errors you have. My apologies for the incomplete reply.

        var bInit = false;
        var xMA = null;
        var tempv = null;
        var curSym = null;
        var curInt = null;
        need var vSymbol = null; here if you want it in your code

        function main(Type,Length,Source,Symbol,Interval,Offset,Par
        ams) {

        The line above should read:
        function main(Type,Length,Source,Offset,Par
        ams) {
        Don'y need to pass symbol and interval as parameters since they are global vars.

        if(bInit == false){
        if(Symbol == null) Symbol = getSymbol();

        The line above should read:
        if(curSym == null) curSym = getSymbol();

        if(Interval == null) Interval = getInterval();

        The line above should read:
        if(curInt == null) curInt = getInterval();

        var vSymbol = Symbol+","+Interval;

        The line above should read
        vSymbol = Symbol+","+Interval;

        xMA = offsetSeries(eval(Type)(Length,eval(Source)(sym(vS
        ymbol))),Offset);


        You can think of suimplifying this code a bit, perhaps:
        var bInit = false;
        var xMA = null;
        var tempv = null;
        var curSym = null;

        function main(Type,Length,Source,Offset,Params) {

        if(bInit == false){
        if(Symbol == null) Symbol = getSymbol();
        if(Interval == null) Interval = getInterval();
        var vSymbol = Symbol+","+Interval;
        xMA = offsetSeries(eval(Type)(Length,eval(Source)(sym(vS
        ymbol))),Offset);
        setShowTitleParameters(eval(Params));
        curSym = Symbol;
        curInt = Interval;
        tempv = vSymbol;
        bInit = true;
        }



        Originally posted by jg
        Hi jgr,

        You need to set these up as global variables.

        Regards,
        Jane

        Comment


        • #5
          jg,

          Thanks for all you help. That code I posted was not my working code but a test I was running having started with one of the files in the EFS2 Custom folder.

          I did get my code to work, by declaring global variables as you stated. I went back to see why that was not being done in the esignal efs code supplied by esignal and I found that they define the series inside the if (binit block and don't then reference Symbol, Interval or vSymbol ouside of that block. I was doing that, as the efs I built does not display a series at all but displays bands and generates alerts when the displayed stock value goes outside of those bands.

          What throws me here is that Symbol and Interval were defined by FunctionParameter statements (not shown) and then in the main function parameter list. BUT they are null outside of the that if(bint == false) block.

          To simplify:
          var binit = true;

          *** Symbol comes from a FunctionParameter statement above ***
          function main(Symbol) {
          if (binit) {
          Symbol = getSymbol();
          var test = 7;
          debugPrintln(Symbol+" "+test); // this works.
          }
          debugPrintln(Symbol+" "+test)); // this prints nulls
          }

          So, it seems that variables declared inside an if block, visible outside as defined variables, the values are lost outside.

          I think you are confirming this fact. Is this standard Javascript behavior or something unique to efs?


          PS: I see my code is shows without format. They need to update this forum software, it must go back to DOS days.

          Comment


          • #6
            Hi jgr,

            You are passing Symbol as a parameter to the main() but then you redefine its value inside the bInit. So inside the bInit the value of Symbol will be temporarily redefined and will print the new value in the debug. However, Symbol is still a parameter so once the script runs outside the bInit, Symbol (the parameter) is reset to its default value. If you have not defined a default value for Symbol in the parameter specification in the preMain, I believe it defaults to null which I suspect is what you are seeing when the script moves outside the bInit.

            To confirm this, set Symbol default in the preMain to some string value, say "test". The put a debug statement before the Symbol = getSymbol(); line in the bInit. What should happen is that the first debug will print the default value of the Symbol parameter and the second debug you have will print the new value of Symbol. But, once outside the script, Symbol will revert to its default value (or value you set through the edit studies) and the debug outside the bInit will once agin print the default value "test" - or whatever you set in the edit studies.

            I believe you cannot redefine a parameter passed to the main from within the efs code. To achieve this you need to use global variables or secondary variables inside the main.

            Hope this helps.

            Regards,
            Jane

            Originally posted by jgr
            jg,

            Thanks for all you help. That code I posted was not my working code but a test I was running having started with one of the files in the EFS2 Custom folder.

            I did get my code to work, by declaring global variables as you stated. I went back to see why that was not being done in the esignal efs code supplied by esignal and I found that they define the series inside the if (binit block and don't then reference Symbol, Interval or vSymbol ouside of that block. I was doing that, as the efs I built does not display a series at all but displays bands and generates alerts when the displayed stock value goes outside of those bands.

            What throws me here is that Symbol and Interval were defined by FunctionParameter statements (not shown) and then in the main function parameter list. BUT they are null outside of the that if(bint == false) block.

            To simplify:
            var binit = true;

            *** Symbol comes from a FunctionParameter statement above ***
            function main(Symbol) {
            if (binit) {
            Symbol = getSymbol();
            var test = 7;
            debugPrintln(Symbol+" "+test); // this works.
            }
            debugPrintln(Symbol+" "+test)); // this prints nulls
            }

            So, it seems that variables declared inside an if block, visible outside as defined variables, the values are lost outside.

            I think you are confirming this fact. Is this standard Javascript behavior or something unique to efs?


            PS: I see my code is shows without format. They need to update this forum software, it must go back to DOS days.

            Comment


            • #7
              Jane,

              Yes, I did a test like what you describe. And it does seem that parameters passed to main can not be redefined inside main, except inside the if statement. So, I am now creating a globals, called, curSymbol and curInterval and assigning the values to them inside the if. Those values are then available throughout.

              Thanks for all the help.

              jgr.

              Comment


              • #8
                Jane,

                Followup, I now recall reading that objects including arrays are passed to functions by reference only. Since these FunctionParameters are in an array, they are probably only passed as pointers. The FunctionParameters array must be read only.

                jgr.

                Comment

                Working...
                X