Announcement

Collapse
No announcement yet.

efsInternal/efsExternal Problem

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

  • efsInternal/efsExternal Problem

    I am having trouble understanding why the attached efs program causes the doNothing1 debugPrintln to print twice when I apply it to a historical chart (e.g. MSFT,D.) I first noticed the problem when I was using efsExternal, but was able to reproduce the behavior in just a few lines of code. Hope I'm not missing anything obvious, but an explanation and a workaround to get each efsInternal function to be called only once per bar would be appreciated.
    Attached Files

  • #2
    jphelps
    I believe the reason is explained in this post which was in reply to a similar question
    Alex

    Comment


    • #3
      Hi Alex,

      I read that post before asking my question. The answer in that post gave me insight on what is going on in EFS-land, but I still didn't quite get it. Now, after looking at the referred-to post some more and creating the program below, I think I finally understand.

      A quote from the other post is:
      Every time there is an efsExternal() or efsInternal() call (or actually any time that a call is made to a builtin study on another symbol or interval), the main EFS has to stop execution, and queue a delayed execution. The engine then goes ahead and executes the called efs or efsInternal, or other symbol/interval FIRST, and then re-executes the original main EFS.
      This threw me off because I thought the halting/reexecution was taking place on a per bar basis. In other words, I thought one efsInternal is applied to a bar and then the next efsInternal is applied to the same bar and so on. However, now I realize that the first efsInternal call is actually applied to all available bars and then the next efsInternal gets a crack at all the bars and so on. Please correct me if my new understanding is still wrong.

      Also, the original problem I was having was related to the "scratch" variable in the new example program. Most of the time it kept starting out with its "last" value instead of its globally set initial value. I see now that if I (re)initialize "scratch" in the BARSTATE_ALLBARS section then my problem goes away. I presume this means a good rule of thumb for initializing global variables in efsInternal/efsExternal oriented functions is to put them in a BARSTATE_ALLBARS section?

      Thanks for your help.

      Jeff

      Code:
      var series1 = null;
      var series2 = null;
      var series3 = null;
      var bInit = false;
      
      function main()
      {
          if (bInit == false)
          {
              debugPrintln();
              debugPrintln("main: init");
              if (series1 == null)
                  series1 = efsInternal("doNothing1");
              if (series2 == null)
                  series2 = efsInternal("doNothing2");
              if (series3 == null)
                  series3 = efsInternal("doNothing3");
              bInit = true;
          }
      
          if (getCurrentBarIndex() == -10)
          {
              debugPrintln("main");
          }
      
          return 1;
      }
      
      var scratch = 5;
      function doNothing1()
      {
          if (getBarState() == BARSTATE_ALLBARS)
          {
              debugPrintln("doNothing1: allbars: " + scratch);
          }
          if (getCurrentBarIndex() == -10)
          {
              scratch = 7;
              debugPrintln("doNothing1");
          }
      }
      
      function doNothing2()
      {
          if (getBarState() == BARSTATE_ALLBARS)
          {
              debugPrintln("doNothing2: allbars");
          }
          if (getCurrentBarIndex() == -10)
          {
              debugPrintln("doNothing2");
          }
      }
      
      function doNothing3()
      {
          if (getBarState() == BARSTATE_ALLBARS)
          {
              debugPrintln("doNothing3: allbars");
          }
          if (getCurrentBarIndex() == -10)
          {
              debugPrintln("doNothing3");
          }
      }

      Comment


      • #4
        Jeff

        I presume this means a good rule of thumb for initializing global variables in efsInternal/efsExternal oriented functions is to put them in a BARSTATE_ALLBARS section?
        If you want to reinitialize the global variables on a chart refresh or when scrolling backwards a chart that is set to dynamic mode then enclosing them in a BARSTATE_ALLBARS conditional statement would be the solution to implement. You may also want to see this reply by JasonK on the same topic
        Alex

        Comment

        Working...
        X