Announcement

Collapse
No announcement yet.

Rerun historical data multiple times from button function

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

  • Rerun historical data multiple times from button function

    I have an efs study that has various function parameters defined in preMain. These include the start day, month, year and end day, month, year. I also have an 'optimisation' button function defined that allows me to run multiple iterations of the study for incremental changes in some of the parameters such as stop loss and DMI length to determine which values give the best result.

    I want to be able to use some of the defined function parameters in this optimisation function. For example I always want to run it against the same dates of data to have a meaningful comparison.

    I can see that it is not possible to do this by passing parameters as part of the drawTextAbsolute command and I have seen various comments about setting the function parameters as global variables. However when I try to do this I get an undefined variable error message.

    How am I supposed to define the global variables?
    Attached Files

  • #2
    Rerun historical data multiple times from button function

    I have a button function defined within an EFS study that when pressed will carry out an optimisation back test.

    I want to press the button once and have it run multiple iterations against the same historical data for slight variations in the parameters.

    When I start to process the code within the button function I am initially at bar index 0. So I included the following code within the button function....

    if (getCurrentBarIndex() == 0) {
    debugPrintln("run refresh EFS");
    reloadEFS();
    }

    However, this does not appear to work as I am always processing for bar index 0.

    What am I doing wrong?

    Comment


    • #3
      Hi richthomas,

      There are a couple of things that you need to familiarize yourself with, but in my opinion, the first is variable scope.

      Originally posted by richthomas

      How am I supposed to define the global variables?

      Variable scope has to do with where a variable can legally be used, and is determined by where it was originally declared or initialized.

      A variable declared or initialized outside a function body has a global scope, making it accessible to all other statements within the same efs.

      A variable declared or initialized within a function body has a local scope, making it accessible only to statements within the same function body.



      How to initialize a Local variable?

      PHP Code:
      //~ Any variable that is initialized inside a function using the var keyword will 
      //~ have a local scope. If a variable is initialized inside a function without var, it will have 
      //~ a global scope. A local variable can have the same name as a global variable.

      var 10;

      disp_a();

      function 
      disp_a()
         {
         var 
      20;
         
      debugPrintln("Value of 'a' inside the function " a);
         }

      debugPrintln("Value of 'a' outside the function " a);


      //~ We first declare a global variable a and assign it a value of 10. Then we call a function in which 
      //~ we again initialize a variable named a. Since we have used the var keyword inside the function, this 
      //~ variable will have a local scope. Once we come out of the function, the local variable 
      //~ no longer exists and the global variable takes over. 
      Now, let us see how we can change the value of a global variable in a function.

      PHP Code:
      var 10;

      disp_a();

      function 
      disp_a()
         {
         
      20;
         
      debugPrintln("Value of 'a' inside the function " a);
         }

      debugPrintln("Value of 'a' outside the function " a); 

      On to FunctionParameter()...

      I have an efs study that has various function parameters defined in preMain. These include the start day, month, year and end day, month, year.

      ...

      I want to be able to use some of the defined function parameters in this optimization function. For example I always want to run it against the same dates of data to have a meaningful comparison.
      The FunctionParameter() method is used to create data objects which are used by the efs engine. The main function is called from the efs engine every tick and is passed the parameters declared using the FunctionParameter() method.

      These variables are passed by value and cannot be intercepted or modified in any way. Since they are passed every time the main function is called, the variables in the main function parenthesis re-declared in the local scope of the main function to the values you declared using the FunctionParameter() method.

      Let me know if this makes any sense and if you want, we can continue the discussion to where you can code a working efs that will meet your requirements.

      Comment


      • #4
        Steve,
        Thanks for your responses they were very helpful and educational - clearly I did not have a good enough grasp of variable scope. I will let you know how I get on.

        Comment


        • #5
          Hi richthomas,

          Your most welcome. Variable and function scope is a difficult concept to grasp, good to hear the info was of assistance.

          Comment

          Working...
          X