Announcement

Collapse
No announcement yet.

Using eval with .getValue

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

  • Using eval with .getValue

    In order to use an array for several different moving averages I need to eval() a variable containing text with .getValue(0). However I get a null response when I debugPrintln().

    How do I correct the following script to output the correct .getValue(0) and .getValue(-1).

    Thank you.

    var myStudy = null;
    var vInit = false;
    function main(){
    if(vInit == false){
    vCommonVar6 = ema( 34 , close(sym("ibm,5")));
    vSymbol = getSymbol() + ","
    vInit=true
    }

    var vCommonVar7 = vCommonVar6 + ".getValue(0)"
    var vCommonVar8 = vCommonVar6 + ".getValue(-1)"
    debugPrintln(eval(vCommonVar7));
    debugPrintln(eval(vCommonVar8));
    }

  • #2
    Hello Wayne,

    The key is to make vCommonVar6 a string when declaring your vCommonVar7 and 8 variables. The entire expression needs to be a string. Surround the object name with quotes ( i.e. "vCommonVar6" ). Try the following.

    PHP Code:
    var myStudy null;
    var 
    vInit false;
    var 
    vCommonVar6 null;  // add to globals

    function main(){
        if(
    vInit == false){
            
    vCommonVar6 ema34 close(sym("ibm,5"))); 
            
    vSymbol getSymbol() + ","
            
    vInit=true
        
    }

        var 
    vCommonVar7 "vCommonVar6" ".getValue(0)"
        
    var vCommonVar8 "vCommonVar6" ".getValue(-1)"
        
    debugPrintln("7: " + eval(vCommonVar7));
        
    debugPrintln("8: " + eval(vCommonVar8));
        

    Jason K.
    Project Manager
    eSignal - an Interactive Data company

    EFS KnowledgeBase
    JavaScript for EFS Video Series
    EFS Beginner Tutorial Series
    EFS Glossary
    Custom EFS Development Policy

    New User Orientation

    Comment


    • #3
      Thanks Jason,

      It worked.

      I still have a problem though. I would like the attached script to draw a table in a non-price pane but the last array does not seem to work or maybe the moving averages are not really defined. I'm not sure what is wrong.

      If you have time I would appreciate your help.
      The sample code is below, I also attached it as an efs.

      Thank you.

      Wayne

      PHP Code:
      debugClear();
      var 
      vMAArray = new Array();
      var 
      myStudy null;
      var 
      bInit false;
      var 
      vSymbol null;
      var 
      avgTR null;
      var 
      highestC null;
      var 
      lowestC null;
      var 
      avgTR null;
      var 
      iInterval1 1;
      var 
      iInterval2 5;
      var 
      iInterval3 15;
      var 
      iInterval4 60;
      var 
      iInterval5 "D";
      var 
      iInterval6 "W";
      var 
      iInterval7 "M"
      var 
      iAvgType1 null;
      var 
      iLength1 null
      var 
      iAvgType2 null;
      var 
      iLength2 null
      var 
      iAvgType3 null;
      var 
      iLength3 null;
      var 
      iAvgType4 null;
      var 
      iLength4 null;
      for (var 
      i=1;i<=1;i++){
          for (var 
      j=1;j<=4;j++){
              var 
      vCommonVar = ("var xAvgr" "c" +" = null");
              eval(
      vCommonVar);
          }
      }
      function 
      main(){
          if(
      bInit==false){
              
      vSource "close()";
              
      avgTR      wma(21,atr(1));
              
      highestC   upperDonchian(21,close());
              
      lowestC    lowerDonchian(21,close());
              if(
      vSymbol == nullvSymbol getSymbol().toUpperCase();
              
      //defines the symbol,interval to use in the next array to define the moving averages
              
      for (var i=1;i<=7;i++){
                  var 
      vCommonVar1 "xSymbol" " = vSymbol + \",\" + iInterval" i;
                  eval(
      vCommonVar1);
              }
              
      //defines the MOVING AVERAGE VARIABLES
              
      for (var i=1;i<=7;i++){
                  var 
      vCommonVar2 "iInterval" i;
                      for (var 
      j=1;j<=4;j++){
                          var 
      vCommonVar3 = ("xAvgr" "c" +
                                             
      " = offsetSeries(eval(iAvgType" ")(iLength" 
                                             
      ",eval(vSource)(sym(xSymbol" "))),Offset)");
                      
      //debugPrintln(vCommonVar3);
                      
      vMAArray.push(eval(vCommonVar3));
      //I CAN'T TELL IF THE MOVING AVERAGES WERE DEFINED AND STORED
      //dfgh = vMAArray[i][1];                
      //debugPrint(dfgh+"\n");
                      
      }
              }
              
      bInit=true;
          } 
      // bInit 
      //HERE I WOULD LIKE TO USE THE DEFINED MOVING AVERAGE VARIABLES TO RUN THROUGH THE CODE
      //BUT HOW DO I CREATE AN ARRAY TO RUN THROUGH AND RECOGNIZE THE DEFINED MOVING AVERAGES   
      for (var k=1;k<=7;k++){
          var 
      vCommonVar4 "iInterval" k;
          if(eval(
      vCommonVar4) != 0){
              var 
      vCommonVar5 "xAvgr" k;
              if(
      1){
                  
      nX inputXstart;
                  
      nY -= nFontCY; ;
              }
              for (var 
      l=1;l<=5;l++){
                  var 
      vCommonVar6 "vCommonVar5" "c" l;
                  var 
      vCommonVar7 "vCommonVar6" ".getValue(0)"
                  
      var vCommonVar8 "vCommonVar6" ".getValue(-1)"
      //debugPrintln(eval(vCommonVar7));//does not output anything?????
                 
      if(5){
                      var 
      RowCol_Number "Row" "Col" l
                      
      if(eval(vCommonVar7) > eval(vCommonVar8)){
                          
      xFGColor Color.black
                          
      xBGColor Color.green
                          
      sStr     "+" ;  
                      }else if(eval(
      vCommonVar7) < eval(vCommonVar8)){
                          
      xFGColor Color.black
                          
      xBGColor Color.red
                          
      sStr     "-" ;
                      }else{
                          
      xFGColor Color.black
                          
      xBGColor Color.yellow
                          
      sStr     "=" 
                      }
                      if(
      1){
                          
      drawTextPixel(nXnYsStrxFGColorxBGColor,
                          
      Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                          
      nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                      }else if(
      &&  5) {
                          
      drawTextPixel(nX+=nFontCXnYsStrxFGColorxBGColor,
                          
      Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                          
      nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                      }                
                  }else if(
      == 5){
                      
      sStr     ""vCommonVar4 ;
                      
      drawTextPixel(nX+=nFontCXnYsStrColor.whiteColor.black,
                      
      Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                      
      nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                  }
              }
          }
      }
          
      //return vMAArray[1][1];

      Attached Files

      Comment


      • #4
        I had a typo in the first array but once corrected it still locks up in "Loading Data".

        Here is the correction:

        PHP Code:
        debugClear();
        var 
        vMAArray = new Array();
        var 
        myStudy null;
        var 
        bInit false;
        var 
        vSymbol null;
        var 
        avgTR null;
        var 
        highestC null;
        var 
        lowestC null;
        var 
        avgTR null;
        var 
        iInterval1 1;
        var 
        iInterval2 5;
        var 
        iInterval3 15;
        var 
        iInterval4 60;
        var 
        iInterval5 "D";
        var 
        iInterval6 "W";
        var 
        iInterval7 "M"
        var 
        iAvgType1 null;
        var 
        iLength1 null
        var 
        iAvgType2 null;
        var 
        iLength2 null
        var 
        iAvgType3 null;
        var 
        iLength3 null;
        var 
        iAvgType4 null;
        var 
        iLength4 null;
        for (var 
        i=1;i<=7;i++){
            for (var 
        j=1;j<=4;j++){
                var 
        vCommonVar = ("var xAvgr" "c" +" = null");
                eval(
        vCommonVar);
            }
        }
        function 
        preMain(){

        }
        function 
        main(){
            if(
        bInit==false){
                
        vSource "close()";
                
        avgTR      wma(21,atr(1));
                
        highestC   upperDonchian(21,close());
                
        lowestC    lowerDonchian(21,close());
                if(
        vSymbol == nullvSymbol getSymbol().toUpperCase();
                
        //defines the symbol,interval to use in the next array to define the moving averages
                
        for (var i=1;i<=7;i++){
                    var 
        vCommonVar1 "xSymbol" " = vSymbol + \",\" + iInterval" i;
                    eval(
        vCommonVar1);
                }
                
        //defines the MOVING AVERAGE VARIABLES
                
        for (var i=1;i<=7;i++){
                    var 
        vCommonVar2 "iInterval" i;
                        for (var 
        j=1;j<=4;j++){
                            var 
        vCommonVar3 = ("xAvgr" "c" +
                                               
        " = offsetSeries(eval(iAvgType" ")(iLength" 
                                               
        ",eval(vSource)(sym(xSymbol" "))),Offset)");
                        
        //debugPrintln(vCommonVar3);
                        
        vMAArray.push(eval(vCommonVar3));
        //I CAN'T TELL IF THE MOVING AVERAGES WERE DEFINED AND STORED
        //dfgh = vMAArray[i][1];                
        //debugPrint(dfgh+"\n");
                        
        }
                }
                
        bInit=true;
            } 
        // bInit 
        //HERE I WOULD LIKE TO USE THE DEFINED MOVING AVERAGE VARIABLES TO RUN THROUGH THE CODE
        //BUT HOW DO I CREATE AN ARRAY TO RUN THROUGH AND RECOGNIZE THE DEFINED MOVING AVERAGES   
        for (var k=1;k<=7;k++){
            var 
        vCommonVar4 "iInterval" k;
            if(eval(
        vCommonVar4) != 0){
                var 
        vCommonVar5 "xAvgr" k;
                if(
        1){
                    
        nX inputXstart;
                    
        nY -= nFontCY; ;
                }
                for (var 
        l=1;l<=5;l++){
                    var 
        vCommonVar6 "vCommonVar5" "c" l;
                    var 
        vCommonVar7 "vCommonVar6" ".getValue(0)"
                    
        var vCommonVar8 "vCommonVar6" ".getValue(-1)"
        //debugPrintln(eval(vCommonVar7));//does not output anything?????
                   
        if(5){
                        var 
        RowCol_Number "Row" "Col" l
                        
        if(eval(vCommonVar7) > eval(vCommonVar8)){
                            
        xFGColor Color.black
                            
        xBGColor Color.green
                            
        sStr     "+" ;  
                        }else if(eval(
        vCommonVar7) < eval(vCommonVar8)){
                            
        xFGColor Color.black
                            
        xBGColor Color.red
                            
        sStr     "-" ;
                        }else{
                            
        xFGColor Color.black
                            
        xBGColor Color.yellow
                            
        sStr     "=" 
                        }
                        if(
        1){
                            
        drawTextPixel(nXnYsStrxFGColorxBGColor,
                            
        Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                            
        nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                        }else if(
        &&  5) {
                            
        drawTextPixel(nX+=nFontCXnYsStrxFGColorxBGColor,
                            
        Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                            
        nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                        }                
                    }else if(
        == 5){
                        
        sStr     ""vCommonVar4 ;
                        
        drawTextPixel(nX+=nFontCXnYsStrColor.whiteColor.black,
                        
        Text.FRAME Text.CENTER Text.VCENTER Text.BOLD,
                        
        nullnFontSizeRowCol_NumbernColWidthnRowThickness);
                    }
                }
            }
        }
            
        return 
        xAvgr1c1;

        Attached Files

        Comment


        • #5
          Hello Wayne,

          The central problem is the string that you are building with this line of code.

          PHP Code:
          var vCommonVar3 = ("xAvgr" "c" +
                          
          " = offsetSeries(eval(iAvgType" ")(iLength" 
                          
          ",eval(vSource)(sym(xSymbol" "))),Offset)"); 
          The resulting string has embedded eval() calls in it. That in itself is not illegal, but I think it is adding a layer of complexity for you. The syntax of the resulting string is invalid for two reasons. You can see the values of your strings by adding a debugPrintln(vCommonVar3) on the following line.



          First, you have references to three variable that are not yet defined, iAvgType#, iLength# and Offset. Secondly, the vSource variable is defined as "close()," which causes the syntax problem. The string, "close()," is getting concatenated to the front of your "sym(.." string, which results in "close()sym(..." Remove the () function call parameters from your variable declaration (i.e. var vSource = "close" ; ) and embed them in their appropriate places when building your string.

          What I would do instead of including the eval() statements as text in this string is to execute these evals while building this string. That way, the values of those variables get embedded properly into the string, which will be evaluated by your eval() statement in the Array.push(). Doing it this way helps when debugging because you can pass the string to debugPrintln() and visually check the syntax to make sure everything is good to go from that point.

          Here's an example of what it should look like. Please note that this will generate an error in your formula because of the undefined variable references. However, once you get those defined, it should work.

          PHP Code:
                          var vCommonVar3 = ("xAvgr" "c" +
                                             
          " = offsetSeries(" + eval("iAvgType" j) + "(" + eval("iLength" j) + 
                                             
          "," vSource "((sym(" + eval("xSymbol" i) + "))))," Offset ");"); 
          Jason K.
          Project Manager
          eSignal - an Interactive Data company

          EFS KnowledgeBase
          JavaScript for EFS Video Series
          EFS Beginner Tutorial Series
          EFS Glossary
          Custom EFS Development Policy

          New User Orientation

          Comment


          • #6
            Everything works fine now.

            Thanks again.

            Comment


            • #7
              Wayne, you're most welcome. Thanks for the update.
              Jason K.
              Project Manager
              eSignal - an Interactive Data company

              EFS KnowledgeBase
              JavaScript for EFS Video Series
              EFS Beginner Tutorial Series
              EFS Glossary
              Custom EFS Development Policy

              New User Orientation

              Comment

              Working...
              X