Announcement

Collapse
No announcement yet.

Does the Call statement work?

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

  • Does the Call statement work?

    I am trying to call a forumula from another formula, but when I do, the output is erroneous. The two scripts I am using are shown below. When I run the main formula directly in an advanced chart of QQQ, I get a result that coincides with the strategy analyzer result from backtesting it, so I know it works. When I call it from the simple test formula, by loading the test formula into the same chart, the Formula Output Window shows a completely different result. Shouldn't this work, or not?


    //This is the simple EFS used to call the formula:
    //**************
    function main() {

    var v = call("btEMA optimize.efs");

    }
    //*************



    //This is "btEMA optimize.efs":

    /************************************************** **************************************************
    ************************************************** ************************************************** */
    Entry = 0;
    PNL = 0;
    PNLtot = 0;

    function preMain() {
    setPriceStudy(false);
    }

    function main(n, indstart) {

    if(n == null) {
    n = 20;
    }

    if(indstart == null) {
    indstart = -100;
    }

    var study = new MAStudy(n, 0, "Close", MAStudy.EXPONENTIAL);

    var v = study.getValue(MAStudy.MA);

    if(v == null)
    return;

    var indcurr = getCurrentBarIndex();

    if (indcurr >= indstart) {

    Trade = 0;

    if(Entry == 0) {
    Strategy.doLong("Start Long", Strategy.CLOSE, Strategy.THISBAR);
    Entry = close(); //Initial trade entry price
    Trade = 9999;
    }

    if (indcurr >= indstart + 1) {

    if(close() >= v) {
    if(!Strategy.isLong()) {
    Trade = 1;
    Strategy.doLong("Crossing Up", Strategy.CLOSE, Strategy.THISBAR);
    PNL = Entry - close(); //Profit(Loss) from last short trade
    PNLtot += PNL; //Profit(Loss) running total
    Entry = close();
    }

    } else {
    if(!Strategy.isShort()) {
    Trade = 2;
    Strategy.doShort("Crossing Down", Strategy.CLOSE, Strategy.THISBAR);
    PNL = close() - Entry; //Profit(Loss) from last long trade
    PNLtot += PNL; //Profit(Loss) running total
    Entry = close();
    }
    }

    if(Strategy.isLong()) {
    setBarBgColor(Color.green);
    } else if(Strategy.isShort()) {
    setBarBgColor(Color.red);
    }

    }

    if (Trade > 0) {
    debugPrintln(Trade+" "+indcurr+" "+n+" "+Entry+" "+close()+" "+PNLtot);
    return PNLtot;
    }


    }

    // return PNLtot;
    }

    function postMain() {
    /**
    * The postMain() function is called only once, when the EFS is no longer used for
    * the current symbol (ie, symbol change, chart closing, or application shutdown).
    */
    }
    //*****************************
    pa-kay

  • #2
    Try using callFunction...

    callFunction( EFSName, FunctionName, [Parameters...])


    · EFSName: Name of the physical EFS file that contains the function to be called.
    · FunctionName: Name of the function within this EFS file that you want to call.
    · Parameters: Any parameters that need to be passed to this function.

    callFunction() is used to call a specific function inside of a different EFS file.

    Example:

    nValue = callFunction("MyNewEFS.efs", "calcMyValue", open(), close() );
    Brad Matheny
    eSignal Solution Provider since 2000

    Comment


    • #3
      Thanks for the prompt, albeit brief, reply. You suggested a different function, does that mean the one I am referring to does not work? This is the function I am trying to use:

      call()

      call( EFSName, args )

      EFSName: Name of script to call.
      args: Arguments, if any, to pass to the script's main() function.

      Call the "main(args)" function in another file within the formula root.

      Call the moving average formula, passing 50 for the moving average length.

      nValue = call("ma.txt", 50);
      pa-kay

      Comment


      • #4
        More help...

        OK...

        Let's consider what you are trying to accomplish.

        You have one EFS file - the one you load onto a chart.

        and you have another "function" EFS file with your MA function.

        The call() command calls the main() function within another file located in the formularoot directory.

        First, click on TOOLS, then EFS SETTINGS to check/change the location of your formularoot directory. Make sure the "ma.txt" file is located there.

        Once you have completed this, your EFS file should work.

        The second option would be the following...

        First, you stated this MA function is in a main() loop within the second EFS. This should probably be changed to something other than main() - try testMA(). Simply change the function name.

        Next, use callFunction() to call the specific function name in your other EFS. Both EFS files should be located within the same directory.

        If this does not work, let me know..

        Brad
        Brad Matheny
        eSignal Solution Provider since 2000

        Comment


        • #5
          Thanks for your suggestion. The first option didn't work, but the second one did. Now I am on to the next phase, with more questions to follow, no doubt.
          pa-kay

          Comment

          Working...
          X