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).
*/
}
//*****************************
//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).
*/
}
//*****************************
Comment