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.
Announcement
Collapse
No announcement yet.
efsInternal/efsExternal Problem
Collapse
X
-
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.
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
-
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?
Alex
Comment
Comment