The object of this thread is to explain the use of call() and callFunction() without resorting, where possible, to any programming jargon.
I for one know that it took me a while to appreciate the differences between the two functions and to understand when one works (or does not) and why.
Hopefully this will make life easier for others as it did for me.
First of all create a folder in the Formulas folder in the eSignal directory and call it Test
Then in that folder save the attached calledCCI.efs
In this first example I am using CCI as a study because it has only two parameters thereby making these initial examples easier.
Now copy the following formula in a new Formula Editor window and save it as callCCI.efs in the same Test directory
Create a new chart and load callCCI.efs and you should get the plot of the CCI based on the default parameters in the called efs.
At this point copy the next formula in a new Formula Editor window and save it as callfunctionCCI.efs in the Test folder.
Once you load this in a chart (or even run a Syntax check) you will get an error that says that there is an invalid parameter 1 in CCIStudy of calledCCI.efs.
Theoretically the syntax is correct because we are calling "main" in the called efs as suggested and we know that passing parameters is optional as indicated by the Help files and shown with the prior call() function.
The reason for this error is due to the different “use” call() and callFunction() make of the efs they are calling (in this case calledCCI.efs).
In the following image highlighted in yellow is what call() "uses" of the called formula.
As you can see the whole formula is highlighted in yellow which means that callCCI.efs is taking the parameters from preMain() in that formula and applying them to the CCIStudy in main().
In the next image highlighted in yellow is what callFunction() “uses” of the called formula. Unlike what happens with call(), callFunction() ONLY uses the main() section of the efs.
As you can see there are no valid parameters defined for CCIStudy in the main() section of the formula hence the error. It is true that they are defined in preMain() but callFunction() does not use that section of the formula so all it “sees” are the words “Length” and “Source” which are not valid parameters.
There are two solutions to correct this error.
The first solution is to keep the callfunctionCCI.efs as it is but add the parameters that the CCIStudy needs to run correctly.
It is true that parameters are optional, but only if they exist in the main() section of the called efs in the first place.
The correct callfunctionCCI.efs will therefore need to be as follows.
In the above iteration of the formula we are now passing the parameters to the CCIStudy in the order in which it expects to see them and the result is as follows.
The other solution entails modifying calledCCI.efs in such a way that the parameters are defined in the main() section of the formula rather than in preMain() which we know callFunction() does not use.
Here below is calledCCI.efs modified to be used with callFunction() without the need to pass parameters.
Notice that we now have added parameters ie 20 and “Close” in the main() section of the efs which is the section of the formula that callFunction “uses”.
If we now modify callfunctionCCI.efs back to the way we first wrote it ie as follows…
…it will now work even without passing any parameters because those are now defined in the main() section of the calledCCI.efs.
At this point we could pass different parameters if we wanted to plot a CCI with different values hence the notion that parameters to be passed are optional.
Parameters can be passed also when using call() where they are always optional because call() uses all sections of the called formula.
So while call() is somewhat more flexible in as much as it does not force us to pass parameters, it also has a bit more overhead than callFunction().
It is important to remember that with either function you will then have to use Arrays() or ref() to retrieve historical values in the calling efs.
In the next message I intend to cover calling formulas that have multiple returns (ie Arrays) and how to retrieve those values and pass back parameters using both call() and callFunction().
Alex
I for one know that it took me a while to appreciate the differences between the two functions and to understand when one works (or does not) and why.
Hopefully this will make life easier for others as it did for me.
First of all create a folder in the Formulas folder in the eSignal directory and call it Test
Then in that folder save the attached calledCCI.efs
In this first example I am using CCI as a study because it has only two parameters thereby making these initial examples easier.
Now copy the following formula in a new Formula Editor window and save it as callCCI.efs in the same Test directory
PHP Code:
function preMain() {
setPriceStudy(false);
setStudyTitle("callCCI");
setCursorLabelName("CCI");
}
function main() {
var test = null;
test = call("calledCCI.efs");
if (test == null)
return;
return test;
}
At this point copy the next formula in a new Formula Editor window and save it as callfunctionCCI.efs in the Test folder.
PHP Code:
function preMain() {
setPriceStudy(false);
setStudyTitle("callfunctionCCI");
setCursorLabelName("CCI");
}
function main() {
var test = null;
test = callFunction("calledCCI.efs","main");
if (test == null)
return;
return test;
}
Theoretically the syntax is correct because we are calling "main" in the called efs as suggested and we know that passing parameters is optional as indicated by the Help files and shown with the prior call() function.
The reason for this error is due to the different “use” call() and callFunction() make of the efs they are calling (in this case calledCCI.efs).
In the following image highlighted in yellow is what call() "uses" of the called formula.
As you can see the whole formula is highlighted in yellow which means that callCCI.efs is taking the parameters from preMain() in that formula and applying them to the CCIStudy in main().
In the next image highlighted in yellow is what callFunction() “uses” of the called formula. Unlike what happens with call(), callFunction() ONLY uses the main() section of the efs.
As you can see there are no valid parameters defined for CCIStudy in the main() section of the formula hence the error. It is true that they are defined in preMain() but callFunction() does not use that section of the formula so all it “sees” are the words “Length” and “Source” which are not valid parameters.
There are two solutions to correct this error.
The first solution is to keep the callfunctionCCI.efs as it is but add the parameters that the CCIStudy needs to run correctly.
It is true that parameters are optional, but only if they exist in the main() section of the called efs in the first place.
The correct callfunctionCCI.efs will therefore need to be as follows.
PHP Code:
function preMain() {
setPriceStudy(false);
setStudyTitle("callfunctionCCI");
setCursorLabelName("CCI");
}
function main() {
var test = null;
test = callFunction("calledCCI.efs","main",20,”Close”);
if (test == null)
return;
return test;
}
The other solution entails modifying calledCCI.efs in such a way that the parameters are defined in the main() section of the formula rather than in preMain() which we know callFunction() does not use.
Here below is calledCCI.efs modified to be used with callFunction() without the need to pass parameters.
PHP Code:
var vCCI = null;
function preMain() {
setStudyTitle("calledCCI");
setCursorLabelName("CCI", 0);
setDefaultBarFgColor(Color.blue, 0);
var fp1 = new FunctionParameter("Length", FunctionParameter.NUMBER);
fp1.setLowerLimit(1);
fp1.setDefault(20);
var fp2 = new FunctionParameter("Source", FunctionParameter.STRING);
fp2.setName("Source");
fp2.addOption("Close");
fp2.addOption("High");
fp2.addOption("Low");
fp2.addOption("Open");
fp2.setDefault("Close");
}
function main(Length, Source) {
if(Length==null)Length=20;
if(Source==null)Source="Close";
if (vCCI == null) vCCI = new CCIStudy(Length, Source);
return vCCI.getValue(CCIStudy.CCI);
}
If we now modify callfunctionCCI.efs back to the way we first wrote it ie as follows…
PHP Code:
function preMain() {
setPriceStudy(false);
setStudyTitle("callfunctionCCI");
setCursorLabelName("CCI");
}
function main() {
var test = null;
test = callFunction("calledCCI.efs","main");
if (test == null)
return;
return test;
}
At this point we could pass different parameters if we wanted to plot a CCI with different values hence the notion that parameters to be passed are optional.
Parameters can be passed also when using call() where they are always optional because call() uses all sections of the called formula.
So while call() is somewhat more flexible in as much as it does not force us to pass parameters, it also has a bit more overhead than callFunction().
It is important to remember that with either function you will then have to use Arrays() or ref() to retrieve historical values in the calling efs.
In the next message I intend to cover calling formulas that have multiple returns (ie Arrays) and how to retrieve those values and pass back parameters using both call() and callFunction().
Alex
Comment