I am trying to program an EFS that calculates the ATR of several symbols and am running into a problem.
Whenever eSignal is FIRST started, and the symbols I'm interested in have NOT yet been charted, then a call to the atr() function for a particular symbol will return null the FIRST time it is called. When called the second and subsequent times (for that symbol), it works fine.
Note that if a symbol has been charted before the formula is loaded, the ATR for that symbol is always calculated correctly for that symbol.
Clearly I'm missing something in regards to how these series are initialized. The specific line of code exhibiting this behavior is:
result = atr(50,sym(aSymbol+",D")).getValue(0);
Please find a complete snippet of code that I created that exhibits the problem, together with the formula output.
Thanks for your help.
----------------------------------
var completed = false;
function preMain() {
setPriceStudy(false);
setStudyTitle("Test");
setCursorLabelName("vatr1", 0);
setCursorLabelName("vatr2", 1);
setCursorLabelName("vatr3", 2);
setComputeOnClose();
}
function calcATR(aSymbol) {
var result = 0;
debugPrintln("Processing: " + aSymbol);
result = atr(50,sym(aSymbol+",D")).getValue(0);
if (result == null) {debugPrintln("ATR is null");}
return result;
}
function main() {
var vatr1;
var vatr2;
var vatr3;
if ( (isDaily(getInterval()) == true) && (isLastBarOnChart() == true) && (!completed)) {
debugPrintln();
vatr1 = calcATR("MATK");
if (vatr1 != null) vatr2 = calcATR("MOGN");
if (vatr2 != null) vatr3 = calcATR("NVDA");
completed = true;
}
return new Array (vatr1,vatr2,vatr3);
}
function postMain() {
}
Formula Output (from bottom up. Note would be nice to be able to reverse output to print top down!)
Note selected chart symbols are not NVDA, MOGN or MATK. If these are charted prior to running the
formula, then there is no problem.
Processing: NVDA
Processing: MOGN // ATR for all ok
Processing: MATK // Selection of new chart symbol
ATR is null
Processing: NVDA
Processing: MOGN // ATR for MATK & MOGN ok, but failed for NVDA
Processing: MATK // Selection of new chart symbol
ATR is null
Processing: MOGN // ATR for MATK ok, but failed for MOGN
Processing: MATK // Selection of new chart symbol
ATR is null // ATR for MATK failed
Processing: MATK // Upon initial load of formula
Whenever eSignal is FIRST started, and the symbols I'm interested in have NOT yet been charted, then a call to the atr() function for a particular symbol will return null the FIRST time it is called. When called the second and subsequent times (for that symbol), it works fine.
Note that if a symbol has been charted before the formula is loaded, the ATR for that symbol is always calculated correctly for that symbol.
Clearly I'm missing something in regards to how these series are initialized. The specific line of code exhibiting this behavior is:
result = atr(50,sym(aSymbol+",D")).getValue(0);
Please find a complete snippet of code that I created that exhibits the problem, together with the formula output.
Thanks for your help.
----------------------------------
var completed = false;
function preMain() {
setPriceStudy(false);
setStudyTitle("Test");
setCursorLabelName("vatr1", 0);
setCursorLabelName("vatr2", 1);
setCursorLabelName("vatr3", 2);
setComputeOnClose();
}
function calcATR(aSymbol) {
var result = 0;
debugPrintln("Processing: " + aSymbol);
result = atr(50,sym(aSymbol+",D")).getValue(0);
if (result == null) {debugPrintln("ATR is null");}
return result;
}
function main() {
var vatr1;
var vatr2;
var vatr3;
if ( (isDaily(getInterval()) == true) && (isLastBarOnChart() == true) && (!completed)) {
debugPrintln();
vatr1 = calcATR("MATK");
if (vatr1 != null) vatr2 = calcATR("MOGN");
if (vatr2 != null) vatr3 = calcATR("NVDA");
completed = true;
}
return new Array (vatr1,vatr2,vatr3);
}
function postMain() {
}
Formula Output (from bottom up. Note would be nice to be able to reverse output to print top down!)
Note selected chart symbols are not NVDA, MOGN or MATK. If these are charted prior to running the
formula, then there is no problem.
Processing: NVDA
Processing: MOGN // ATR for all ok
Processing: MATK // Selection of new chart symbol
ATR is null
Processing: NVDA
Processing: MOGN // ATR for MATK & MOGN ok, but failed for NVDA
Processing: MATK // Selection of new chart symbol
ATR is null
Processing: MOGN // ATR for MATK ok, but failed for MOGN
Processing: MATK // Selection of new chart symbol
ATR is null // ATR for MATK failed
Processing: MATK // Upon initial load of formula
Comment