Announcement

Collapse
No announcement yet.

Script Execution

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

  • Script Execution

    I'm a little confused about when PreMain and Main execute, and how often each executes as new bars are loaded. Several EFS2 scripts have a variable set to false outside of main, and then that variable is checked inside main. If it's false, some things run and then the variable is set to true inside main.

    When does this variable get set back to false? It seems this is doing something similar to a combination of getBarState() and setComputeOnClose(true or false...).

    I know the answer is in here somewhere, but I can't seem to find it.

    Thanks.

  • #2
    rauthur
    preMain() is executed once only when a formula is loaded for the first time or is reloaded.
    main() executes once on every historical bar as the formula loads and when it reaches the current bar it executes on every tick unless you have a setComputeOnClose() statement in preMain or you have checked the Make all formulas compute on close (rather than tick by tick) setting in the EFS Preferences.
    Variables declared outside of main() also referred to as global variables maintain their value between iterations of an efs and are reset when a formula is reloaded. As an example run the enclosed script with the Formula Output Window open and you will see that when you first load the formula line4 will be false once and then it will show true on every subsequent iteration of the efs. When you reload the EFS it will again show false one time only.
    PHP Code:
    var myVar false;
    function 
    main(){
        
    debugPrintln("line4 "+myVar)
        if(
    myVar==false) {
            
    myVar true;
        }
        return;

    For an in-depth explanation of all the above you may want to read through the Tutorials which are available under Help Guides in the EFS KnowledgeBase
    Alex

    Comment


    • #3
      Thanks, but I knew that. If you look at customMACD.efs in the EFS2 formula folder, it would seem that the lines of code for calculating MACD are only executed once because once bInit is set to true at the end of the first execution, it stays true.

      If the lines of code between PreMain and Main also run each time a new bar appears, I don't see why you would even need to set bInit to anything.

      I see these variables in a lot of code, and I'm not sure what they are supposed to be doing. Thanks for the help.

      Comment


      • #4
        rauthur
        I am well aware of how the scripts in the EFS2 Custom folder are written having programmed them myself.
        The bInit boolean variable is used to intialize the studies only once. The fomula engine would actually initialize them only once even without the bInit routine however in that case it would have to check on every tick if that series already existed. Using the bInit routine makes the study more efficient.
        FWIW another method of accomplishing the same result is shown in the example below.
        PHP Code:
        var xMACD null;//declare global variable
        function main(){
            if(
        xMACD==nullxMACD macd(12,26,9);//initialize study
        //etc 
        In this case the variable xMACD will be null only the first time that the efs engine iterates through the code. At each subsequent iteration the variable will no longer be null and that condition will no longer be evaluated.
        Alex

        Comment


        • #5
          OK. I think I understand now. Once a routine is initialized, it continues to update with the settings in fpArray as new data comes in with whatever timing sequence is selected.

          Resetting fpArray values through edit studies will cause the entire script to rerun -- and reinitialize the study.

          Thanks.

          Comment

          Working...
          X