Announcement

Collapse
No announcement yet.

Variable Question

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

  • Variable Question

    Is it possible to set the value of a variable in premain and capture it in main using something like ... (but this doesn't work) I'm not super experienced with EFS

    function preMain( ){

    var CHFPoint = 7.60;
    var EURPoint = 10.00;
    var JPYPoint = 8.60;

    }

    function main(){

    FirstC = "CHF";
    var CurrentPoint = FirstC+"Point"; //where this picks up the value from premain for CHFPoint

    }
    Last edited by Chris747; 11-05-2008, 06:46 PM.

  • #2
    Hi Chris,

    No, that is not possible the way you described, the variables declared in the preMain function are private to that function.

    Comment


    • #3
      Thanks Steve,
      I was hoping ... chuckle.

      I'm trading 10 currencies and rather than it read 10 IF statements as it iterates through each one, i was hoping it could pull value from a variable.

      Thanks,
      Chris

      Comment


      • #4
        Hi Chris,

        Your welcome. Sorry for the late reply. While you cannot use local variables that are defined within the preMain function, you can use global variables to accomplish what you described.

        By declaring variables outside functions, they are global in scope and they can be accessed from inside all your functions. e.g. - they can be assigned values either in the global scope or inside of one functions scope and accessed inside another functions scope.

        Hope this helps.

        Originally posted by Chris747
        Thanks Steve,
        I was hoping ... chuckle.

        I'm trading 10 currencies and rather than it read 10 IF statements as it iterates through each one, i was hoping it could pull value from a variable.

        Thanks,
        Chris

        Comment


        • #5
          I've been trying to understand some of the subtle nuances of global variables.

          If I decalre a global (ie var nLength; or var myNumber; ) before preMain are those variables truly global?
          Are they available to: preMain(), main() and any function() I create?

          This seems to work for some variables but if I use FunctionParameter in preMain to initialize nLength to a user defined value it is no longer a global.

          On the other hand it appears as though I can change a global value ( myNumber ) in myFunction() and that value is available to main() as well as other functions; not via the return statement but rather as a truly goobal variable.

          I haven't tried it yet but I'm thinking of using askForInput( ) in myFunction() so I can keep them truly global.

          Comment


          • #6
            I think in that post, because i was using 10 charts, Steve was pointing out that i could set a true global variable in main (ie setGlobalValue("nLength", 30) ) and that it would then be available to all 10 charts by calling myVar = getGlobalValue("nLength").
            I believe that you are not declaring globals so much as just declaring a variable and that would only be available to that function in particular.
            Hope that helps...

            Chris

            Comment


            • #7
              Hi pj909,

              As Chris747 states setGlobalValue(...) makes the variable available to functions internal to the efs that generated the setGlobalValue() and to efs studies running in other charts via the getGlobalValue(...) function.

              Global variables defined outside of functions (preMain(), main(), support functions, etc) are available to any function within the same efs. As in:

              PHP Code:
              //nVar1, nVar2, & nVar3 are available to all functions within this efs.
              var nVar1 20;
              function 
              preMain(){
              }
              var 
              nVar2 30;
              function 
              main(){
              }
              var 
              nVar3 40;
              function 
              myFunction(){
                  
              //nVar1, nVar2, & nVar3 are available to every function


              A variable declared within a function is local to that function and is available only within that function as in:
              PHP Code:
              function main(){
                  var 
              nVar 4;
                  if (...) 
              nVar4 50;
                  var 
              yy efsInternal("myFunction"nVarXX);
              }
              function 
              myFunction(vYY){
                  
              //nVar4 is not available to other functions unless passed as a parameter when the function is called 
                  //as was done for nVarXX which is accessed here as vYY.

              FunctionParameter variables are available only within main() but can be made accessible to other function within the same efs if they are assigned to a global variable as in:
              PHP Code:
              var aFPArray = new Array();
              function 
              preMain() {
                  var 
              x=0;
                  
              aFPArray[x] = new FunctionParameter"vSymbol"FunctionParameter.STRING); 
                  
              withaFPArray[x++] ) { 
                      
              setName"Symbol" ); 
                      
              setDefault"INTC" ); 
                  }   
              }
              var 
              xSym null;
              var 
              ySym null;
              var 
              bInit false;
              function 
              main(vSymbol){
                  if(!
              bInit){
                      
              xSym vSymbol;
                      
              vSymbol "IBM";//if you change the value of vSymbol here
                                      //to one different from "INTC", the new
                                      //value "IBM" will only apply within the 
                                      //bInit statement and will revert back to 
                                      //the value "INTC" defined by the user
                                      //input in the preMain() FunctionParameter
                                      //statement
                      
              ySym "RIMM";
                      
              bInit false;
                  }
                  
              //here (outside of "bInit") vSymbol equals "INTC" , xSym equals "INTC",
                  //and ySym = "RIMM"
              }
              function 
              myFunction(){
              //xSym & ySym are available to other functions within the same efs

              askForInput( ) only calls the "Edit Studies" window and does not affect the scope of variables.

              Here is some more info:




              A forum search of "global variable" or "variable scope" will likely produce more info.

              Wayne
              Last edited by waynecd; 10-01-2010, 05:57 AM.

              Comment


              • #8
                Thanks Wayne

                Another note. After some testing of variations I have concluded:

                Declaring a global myVar and then using the same name in preMain() doesn't create a global. If myVar is also created with the new FunctionParameter in preMain it is a local and not the same as the myVar global. Less confusing is to use a different local variable and assign it to the global (2nd example).

                PHP Code:
                var myVar;
                preMain(){
                     new 
                FunctionParameter("myVar"FunctionParameter.NUMBER);
                }
                main(myVar){
                                                   
                // myVar is not a global in this script

                PHP Code:
                var myVar;
                preMain(){
                     new 
                FunctionParameter("mV"FunctionParameter.NUMBER);
                }
                main(myVar){
                     if(
                bInit == false){
                          
                myVar=mV       // mV is assigned to the myVar global
                     
                bInit true;
                     }

                In the 2nd example I can now access and change myVar not only in main() but any user defined function.

                I presume it is the "new" keyword that is making the variable local to preMain(). I suppose buried deep within the javascript info is a note saying that "local" varaibles in premain can be passed to main() because main() is implicitly called by preMain().

                Comment


                • #9
                  That is because the:
                  PHP Code:
                  new FunctionParameter("myVar"FunctionParameter.NUMBER); 
                  changes the scope of the "var myVar" placed outside of preMain().

                  It doesn't retain it's global scope once it is used in the FunctionParameter statement.

                  Less confusing is to use a different local variable and assign it to the global (2nd example).
                  I agree, it is what I explained in the third example of my post below for:
                  PHP Code:
                  aFPArray[x] = new FunctionParameter"vSymbol"FunctionParameter.STRING); 
                  I presume it is the "new" keyword that is making the variable local to preMain(). I suppose buried deep within the javascript info is a note saying that "local" varaibles in premain can be passed to main() because main() is implicitly called by preMain().
                  I don't know but it looks like it's exactly what is going on.

                  Cheers


                  Wayne
                  Last edited by waynecd; 10-01-2010, 12:28 PM.

                  Comment


                  • #10
                    As I understand it, all INPUTS created in PREMAIN are initiated as "constant parameters" when main() is called. Thus, when you load an EFS, these values are set in the scripting engine and then passed to main() with every call to run the script (every tick or new bar).

                    If you want to control these input parameters outside of the premain function, then you need to use GLOBAL VARIABLE as a replacement variable in your script.

                    For example, if I declare an input called nLength in premain() (defaulted to 10), then this value becomes a constant value that is then passed to main() with every tick. If I wanted to alter this constant value, then I would need to EDIT STUDIES and change it.

                    But let's assume I wanted to alter the value "on the fly" within my efs code?? I would simply create a global value and initiate a "sync" feature in my script... like this.

                    PHP Code:

                    var fpArray = new Array();

                    function 
                    preMain() {

                        
                    setPriceStudy(false);
                        
                    setStudyTitle("RSI");
                        
                    setCursorLabelName("RSI"0);
                        
                    setDefaultBarFgColor(Color.blue0);
                        
                    setPlotType(PLOTTYPE_LINE,0);
                        
                    setDefaultBarThickness(1,0);
                        
                    setStudyMin(0);
                        
                    setStudyMax(100);
                        
                    askForInput();
                                
                        var 
                    x=0;
                        
                    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setLowerLimit(1);        
                            
                    setDefault(14);
                        }
                        
                    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    addOption("open"); 
                            
                    addOption("high");
                            
                    addOption("low");
                            
                    addOption("close");
                            
                    addOption("hl2");
                            
                    addOption("hlc3");
                            
                    addOption("ohlc4"); 
                            
                    setDefault("close"); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    setDefault();
                        }
                        
                    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    setDefault();
                        }
                        
                    fpArray[x] = new FunctionParameter("Upper"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setDefault(70); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Lower"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setDefault(30); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
                        
                    with(fpArray[x++]){
                            
                    setName("Show Parameters");
                            
                    setDefault(false);
                        }
                    }

                    var 
                    bInit false;
                    var 
                    xRSI null;

                    var 
                    g_Length 0;  //  New Global Value for Length

                    function main(Length,Source,Symbol,Interval,Upper,Lower,Params) {

                        if(
                    bInit == false){

                            
                    g_Length Length;

                            if(
                    Symbol == nullSymbol getSymbol();
                            if(
                    Interval == nullInterval getInterval();
                            var 
                    vSymbol Symbol+","+Interval;
                            
                    xRSI rsi(g_Length,eval(Source)(sym(vSymbol))); 
                            
                    addBand(Upper,PS_SOLID,1,Color.black,"Upper");
                            
                    addBand(Lower,PS_SOLID,1,Color.black,"Lower");
                            
                    setShowTitleParameters(eval(Params));
                            
                    bInit true;
                        }

                        return 
                    getSeries(xRSI);

                    This example shows you how to set and use the "g_" global variables properly (in this instance). But it also opens up a new world of adaptive control. Lets say I wanted to alter the RSI calc based on market price activity. I would simply re-call the RSI constructor with the new values at any time I like.

                    Like this

                    PHP Code:

                    var fpArray = new Array();

                    function 
                    preMain() {

                        
                    setPriceStudy(false);
                        
                    setStudyTitle("RSI");
                        
                    setCursorLabelName("RSI"0);
                        
                    setDefaultBarFgColor(Color.blue0);
                        
                    setPlotType(PLOTTYPE_LINE,0);
                        
                    setDefaultBarThickness(1,0);
                        
                    setStudyMin(0);
                        
                    setStudyMax(100);
                        
                    askForInput();
                                
                        var 
                    x=0;
                        
                    fpArray[x] = new FunctionParameter("Length"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setLowerLimit(1);        
                            
                    setDefault(14);
                        }
                        
                    fpArray[x] = new FunctionParameter("Source"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    addOption("open"); 
                            
                    addOption("high");
                            
                    addOption("low");
                            
                    addOption("close");
                            
                    addOption("hl2");
                            
                    addOption("hlc3");
                            
                    addOption("ohlc4"); 
                            
                    setDefault("close"); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Symbol"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    setDefault();
                        }
                        
                    fpArray[x] = new FunctionParameter("Interval"FunctionParameter.STRING);
                        
                    with(fpArray[x++]){
                            
                    setDefault();
                        }
                        
                    fpArray[x] = new FunctionParameter("Upper"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setDefault(70); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Lower"FunctionParameter.NUMBER);
                        
                    with(fpArray[x++]){
                            
                    setDefault(30); 
                        }
                        
                    fpArray[x] = new FunctionParameter("Params"FunctionParameter.BOOLEAN);
                        
                    with(fpArray[x++]){
                            
                    setName("Show Parameters");
                            
                    setDefault(false);
                        }
                    }

                    var 
                    bInit false;
                    var 
                    xRSI null;

                    var 
                    g_Length 0;  //  New Global Value for Length
                    var g_ResetRSI false;

                    function 
                    main(Length,Source,Symbol,Interval,Upper,Lower,Params) {


                        if ((
                    getBarState() == BARSTATE_NEWBAR) &&
                            (
                    bInit)
                        ) {
                            
                    //  Only update/adapt when a new bar forms
                            
                    g_Length fMakeAdaptiveRSI(Length);
                        }

                        if ((
                    bInit == false) || (g_ResetRSI == true)){
                          if (!
                    bInit) {
                            
                    g_Length Length;
                            if(
                    Symbol == nullSymbol getSymbol();
                            if(
                    Interval == nullInterval getInterval();
                            var 
                    vSymbol Symbol+","+Interval;
                            
                    xRSI rsi(g_Length,eval(Source)(sym(vSymbol))); 
                            
                    addBand(Upper,PS_SOLID,1,Color.black,"Upper");
                            
                    addBand(Lower,PS_SOLID,1,Color.black,"Lower");
                            
                    setShowTitleParameters(eval(Params));
                            
                    bInit true;
                          }
                          if (
                    g_ResetRSI) {
                            if(
                    Symbol == nullSymbol getSymbol();
                            if(
                    Interval == nullInterval getInterval();
                            var 
                    vSymbol Symbol+","+Interval;
                            
                    xRSI rsi(g_Length,eval(Source)(sym(vSymbol))); 
                          }
                            
                    g_ResetRSI false;
                        }  
                    //  end if bInit


                        
                    return getSeries(xRSI);
                    }

                    function 
                    fMakeAdaptiveRSI(DefLength) {
                       var 
                    FinalLen g_Length;  //  Retain the current global def length

                       
                    if ( (close(-1) > close(-2)) &&
                            (
                    close(-2) > close(-3)) &&
                            (
                    close(-3) > close(-4)) 
                       ) {
                           
                    FinalLen Math.floor(g_Length 1.25);
                       } else 

                       if ( (
                    close(-1) < close(-2)) &&
                            (
                    close(-2) < close(-3)) &&
                            (
                    close(-3) < close(-4)) 
                       ) {
                           
                    FinalLen Math.floor(g_Length 0.85);
                       } else {
                          
                    FinalLen DefLength;
                       }

                      
                    //  Error checking and final logical functions
                      
                    if (FinalLen 1) { FinalLen 1;  }
                      if (
                    FinalLen != g_Length) { g_ResetRSI true; }

                      return 
                    FinalLen;

                    Please understand, this example code may not have any real market requirements because I just created it to show you how this can be done. I have not even tested it with my esignal (yet). But based on my knowledge of EFS and how things work, I believe this should be allowed to operate with moderate additional overhead. In other words, I've done this type of thing before and it generally works without much of an issue.
                    Last edited by Doji3333; 10-04-2010, 10:34 AM.
                    Brad Matheny
                    eSignal Solution Provider since 2000

                    Comment


                    • #11
                      Hi Doji,

                      I appreciate the clarification.

                      The adaptive control concept is especially useful.

                      Wayne

                      Comment

                      Working...
                      X