Announcement

Collapse
No announcement yet.

function executes more times than it's called

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

  • function executes more times than it's called

    I'm confused--- I'm calling a function (in a separate file) once per bar, but that function is executing several times per bar.

    In the following example, the MAIN and EMBEDDED function prints (correctly) once/bar, but the EXTERNAL function that is called by the embedded function is run multiple times per bar.

    Why isn't the external function only run once per bar (I'm only calling it once per bar)

    Thanks,


    // **************************************************
    ******************
    // test where a Main calls a function, which calls an external function
    function embedded(aPrice1) {

    debugPrintln("embedded: "+getMonth(0)+"/"+getDay(0)+" "+getHour(0)+":"+getMinute(0)+" close from embedded function:"+aPrice1[0]);

    var externalclose = call("/Library/externalfunction.efs", aPrice1);

    return ;
    }


    function preMain() {
    setComputeOnClose();
    setStudyTitle("array test");

    }

    var aMain = new Array(5);

    function main() {

    aMain.pop(); //remove right most element, and put the latest closing price at element [0]
    aMain.unshift(close());

    debugPrintln("MAIN: "+getMonth(0)+"/"+getDay(0)+" "+getHour(0)+":"+getMinute(0)+" close from main:"+aMain[0]);

    var Cembedded = embedded(aMain); //ask the embedded function to pass closing price to called function

    return;

    }

    ------------------------------------------------
    FILE externalfunction.efs:
    function main(aPrice2) {

    debugPrintln("extfunction: "+getMonth(0)+"/"+getDay(0)+" "+getHour(0)+":"+getMinute(0)+" close from extfunction:"+aPrice2[0]);


    return;

    }

    -----------------------------------------------
    Formula Output:
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 10:57 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    MAIN: 7/2 10:57 close from main:1486.5
    embedded: 7/2 10:57 close from embedded function:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:0 close from extfunction:1486.5
    extfunction: 7/2 11:3 close from extfunction:1486.5
    MAIN: 7/2 11:0 close from main:1487.5
    embedded: 7/2 11:0 close from embedded function:1487.5

  • #2
    tecesig,

    There is a basic misunderstanding here on what call() does I think. When you use call, it spawns another complete data stream - (ie: It acts just as if the called function had been loaded onto the chart). So it iterates all the bars for the called function separately from the iteration of bars from the calling program.

    To get the behavior you are after, use callFunction() instead.

    Garth
    Garth

    Comment


    • #3
      tecesig,

      Also - for more information about call() and callFunction you might look at thisthread by Alex Montenegro, which does a good job of describing these functions in layman's terms. It also includes a link to a document written by Matt Gunderson which describes these functions on a more techies level.

      Garth
      Garth

      Comment


      • #4
        Garth,
        thanks for the clarification. I knew that call invoked premain in addition to the actual main of the function, but I didn't realize that it spawned a separate free-running process.

        Comment

        Working...
        X