Announcement

Collapse
No announcement yet.

array passed to function is stuck

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

  • array passed to function is stuck

    I'm passing an array to a function which finds a swing point. It correctly finds the first swingpoint, but on subsequent function calls, it keeps using the same values for it's local array, instead of reading the values from the array that's passed to it.

    Here are the relevent code snippets:

    IN FILE Parent.efs:
    function swinghigh(Instance,aSwPrice,Strength,Length) {

    if(Instance == null) {
    Instance = 1;
    }
    if(aSwPrice == null) {
    return ("no prices");
    }
    if(Strength == null) {
    Strength = 1;
    }
    if(aSwPrice[Length+1] == null) {
    return ("not enough prices");
    }
    aPoint = aSwPrice; // must pick a different var to pass to the SwingPoint function
    debugPrintln("**SwingHigh-RS " +getMonth(0)+"/"+getDay(0)+" "+getHour(0)+":"+getMinute(0)+" [0]: "+aPoint[0]+" [6]: "+aPoint[6]);
    vsw = call("SwingPoint.efs", aPoint,Length,Strength,Strength,Instance,1);

    return vswb[1]; //return bar at swingh high
    }

    function main() {
    ...some other code...
    var nBarState = getBarState();
    if(nBarState == BARSTATE_NEWBAR) {
    aCloses.pop();
    aCloses.unshift(0);
    aCloses[0] = close();
    rs1last=swinghigh(1,aCloses,1,20);
    }

    return rs1last;
    }

    *****************************
    IN FILE SwingPoint.efs:
    // finds a swinghigh or low point


    function main(aswptPrice,Length,LeftStrength,RightStrength, Instance,HiLo) {

    debugPrintln("--BEGIN--SwingPoint " +getMonth(0)+"/"+getDay(0)+" "+getHour(0)+":"+getMinute(0)+" price: "+aswptPrice[0]+" [1]"+aswptPrice[1]);

    ....some more code......

    In examing the debug output, the array "aPoint" in the swinghigh function correctly shows the latest history or closing prices. BUT, the debug statement in SwingPoint.efs ALWAYS shows the FIRST set of closing prices that filled the array, even as the timestamps keep incrementing for each bar on the chart!

    anyone have any ideas?
    thanks!

  • #2
    In your main, it looks like you are inshifting a zero value into an array

    aCloses.unshift(0);.

    Another disparity potentially is that you are assigning the variable aPoint in this line

    "aPoint = aSwPrice; // must pick a different var to pass to the SwingPoint function"

    aPoint is assigned to equal another variable, aSwPrice, however, it is not clear that aSwPrice was ever an array.

    Hopefully that will help some, If not, I would recommend posting the full code, there is just not enough there to figure out anything more than that.

    Comment


    • #3
      Steve, sorry for the late return response--
      I found a work around, which is to included the external funciton as another function in the first file.

      I've narrowed the cause down to the fact the the called external file is executed multiple times per bar. As you can see in the following example, the MAIN and EMBEDDED function logs 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;

      }

      -----------------------------------------------
      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

      Comment

      Working...
      X