Announcement

Collapse
No announcement yet.

callFunction

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

  • callFunction

    Alexis,

    Reference: Low Lag MACD.

    Thank you for your instruction on using the BuiltIn MACD function rather than build the code to compute it separately.

    I located your paper on how to use the callFunction. I am calling my LoLag Moving average with parameter passing. THANKS!

    Question:
    When I call it a second time with different LENGTH (30 & 50) parameters, it continues to use the original values (10 & 20).

    The two external calls:

    var vMAofMA1 = callFunction("/EDs Formulas/LoLagCallFn.efs","main", 10, 20);
    debugPrint(" MA1= ", vMAofMA1, "\n");

    var vMAofMA2 = callFunction("/EDs Formulas/LoLagCallFn.efs","main", 30, 50);
    debugPrint(" MA2= ", vMAofMA2, "\n", "\n");

    The results from the debugPrint statements show that the Length inputs don't change. Also the chart only shows one trace, rather than two.

    Thank You,

    #549584 - Ed Hoopes - [email protected]

  • #2
    Ed
    I don't know if more than one set of parameters can be passed using callFunction(), but I will try to find out.
    In the mean time you could use call() which I know works. As an example download the attached basicMAx2called.efs and then use the following script as the calling efs

    PHP Code:
    function preMain() {
        
    setPriceStudy(true);
        
    setStudyTitle("Call-CallFunction test");
        
    setDefaultBarFgColor(Color.blue,0);
        
    setDefaultBarFgColor(Color.red,1);
        
    setDefaultBarFgColor(Color.lime,2);
        
    setDefaultBarFgColor(Color.magenta,3);
        
    setDefaultPriceBarColor(Color.black);
    }

    function 
    main() {
        
        var 
    test null;
        var 
    testx null;
            
        
    test call("basicMAx2called.efs",5,0,"Close",15,0,"Close");
        
    testx call("basicMAx2called.efs",30,0,"Close",60,0,"Close");
        
        if (
    test == null
        return;
        if (
    testx==null)
        return;
        
        var 
    test1 test[0];
        var 
    test2 test[1];
        
        var 
    test3 testx[0];
        var 
    test4 testx[1]
            
            
        return new Array(
    test1,test2,test3,test4);

    The result should be as in the chart below
    Alex

    Attached Files

    Comment


    • #3
      Hello Ed and Alex,

      In this scenario, call() is the way to go. We can make it work with callFunction() 2 ways. The first method is not recommend because it makes the routine very inefficient. The second method requires some coding changes to both formulas. The following info may sound counter intuitive Alex, based on our previous exercises with call() and callFunction(). Keep in mind this is specific to this type of example where we actually need two separate instances of the called EFS. I'll try to explain.

      First, let's look at what we would need to do to make this work with callFunction().

      callFunction()
      Method 1) In main of the called EFS, we are checking to see if vMA1 and vMA2 are null before we set up the study with the passed parameters. Global variables established outside of main() such as vMA1 and vMA2 (in the formula's global scope) will maintain their state between calls from our calling EFS. So, the first time callFunction() is called, vMA1 and vMA2 are null and theses study objects gets established with the first set of parameters. Immediately after our first callFunction() call, we're doing another callFunction() call with new parameters. However, vMA1 and vMA2 are no longer null. That's why the second set never gets established and we end up with a duplicated set of MA lines. Now, to make this work with callFunction() we have to initialize vMA1 and vMA2 on every call in order to reset each study with each set of parameters every time we execute the routine.

      PHP Code:
      function main(MA1Length,MA1Offset,MA1Source,MA2Length,
          
      MA2Offset,MA2Source) {

          
      vMA1 = new MAStudy(MA1LengthMA1OffsetMA1SourceMAStudy.SIMPLE);
          
      vMA2 = new MAStudy(MA2LengthMA2OffsetMA2SourceMAStudy.SIMPLE); 
      That's the inefficient part, which is a no-no of course.

      Method 2) The recommended way to use this routine with callFunction(), is to only have 1 callFunction() statement from the main formula and pass 4 sets of parameters to the called EFS which will use 4 study names, vMA1, vMA2, vMA3, and vMA4.

      call()
      When we use call() for this routine, we get a unique instance of the called formula, which allows us to have 4 studies established with the 4 sets of parameters. Think of it this way. The end result is the same as if we made a copy of the called EFS with a new name and changed our calling formula to look like the following. Notice the 2 added to the name in the testx call.

      PHP Code:
      test call("basicMAx2called.efs",5,0,"Close",15,0,"Close");
      testx call("basicMAx2called2.efs",30,0,"Close",60,0,"Close"); 
      The function, call(), saves us the trouble of having to make a second copy with a new name. call() does this for us in the background and keeps track of these unique instances with the variables "test" and testx." That's why we can use call() to call the same formula multiple times within main(). Each subsequent time call() calls the same formula name, EFS creates a unique instance of the formula and then just references it from that point on. So the first time main() executes from our calling EFS, vMA1 and vMA2 are null so when we do, test = call..., they get initialized with the first set of parameters. Then the second call, testx = call..., EFS sees that we're using the same formula name and establishes a new instance of the formula as if it were being executed for the first time. Therefore, vMA1 and vMA2 are null and get established with the second set of parameters. Now every time our main calling EFS executes it just makes a reference to the unique instances of basicMAx2called(test).efs and basicMAx2called(testx).efs. Does this all make sense?
      Jason K.
      Project Manager
      eSignal - an Interactive Data company

      EFS KnowledgeBase
      JavaScript for EFS Video Series
      EFS Beginner Tutorial Series
      EFS Glossary
      Custom EFS Development Policy

      New User Orientation

      Comment

      Working...
      X