The following script is suppose to calculate a plot 3 different ATRs and an average of the three. I use a Donchian channel (6 Bar) so I can plot the Daily ATR on a 4 hour chart.
The script will plot a single ATR without a problem however when I use "return new Array" and try to plot all of the ATRs together I get "0.#Q".
Please Help
var AryTTR1 = null;
var AryTTR2 = null;
var AryTTR3 = null;
var iCount1 = 0;
var iCount2 = 0;
var iCount3 = 0;
function preMain() {
setCursorLabelName("ATR");
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.brown, 3);
setCursorLabelName("ATR 5", 0);
setCursorLabelName("ATR 10", 1);
setCursorLabelName("ATR 20", 2);
setCursorLabelName("AVG", 3);
setPriceStudy(false);
var fp1 = new FunctionParameter("ATRPeriod1", FunctionParameter.NUMBER);
fp1.setName("SATR1 Period ");
fp1.setLowerLimit(1);
fp1.setDefault(5);
var fp2 = new FunctionParameter("DCHLength1", FunctionParameter.NUMBER);
fp2.setName("DCH SATR Length");
fp2.setLowerLimit(1);
fp2.setDefault(6);
var fp3 = new FunctionParameter("ATRPeriod2", FunctionParameter.NUMBER);
fp3.setName("SATR2 Period");
fp3.setLowerLimit(1);
fp3.setDefault(10);
var fp4 = new FunctionParameter("ATRPeriod3", FunctionParameter.NUMBER);
fp4.setName("SATR3 Period");
fp4.setLowerLimit(1);
fp4.setDefault(20);
var fp5 = new FunctionParameter("Spread", FunctionParameter.NUMBER);
fp5.setName("Spread");
fp5.setLowerLimit(.0001);
fp5.setDefault(.0004);
}
function main(DCHLength1, ATRPeriod1, ATRPeriod2, ATRPeriod3, Spread) {
var vValue1, vValue2, CATR1, CATR2, CATR3, avg;
if(getBarState() == BARSTATE_ALLBARS){
myDCHstudy1 = new DonchianStudy(DCHLength1, 0);
iCount1 = 0;
iCount2 = 0;
iCount3 = 0;
}
vValue1 = myDCHstudy1.getValue(DonchianStudy.UPPER, 0, -ATRPeriod3);
vValue2 = myDCHstudy1.getValue(DonchianStudy.LOWER, 0, -ATRPeriod3);
if (vValue1 == null || vValue2 == null) return;
if (AryTTR1 == null){
AryTTR1 = new Array(ATRPeriod1);
}
if (AryTTR2 == null){
AryTTR2 = new Array(ATRPeriod2);
}
if (AryTTR3 == null){
AryTTR3 = new Array(ATRPeriod3);
}
var TodayTrueRange1 = vValue1[0] + Spread - vValue2[0];
if (iCount1 < ATRPeriod1){
AryTTR1.shift();
AryTTR1.push(TodayTrueRange1);
iCount1++;
return;
}else{
var PATR1 = ref(-1); // Perious ATR
if (PATR1 == null){
var iSum1=0;
for(i=0;i<AryTTR1.length;i++){
iSum1 = iSum1 + AryTTR1[i];
}
CATR1 = iSum1/ATRPeriod1;
//return CATR1;
}else{
CATR1 = (PATR1*(ATRPeriod1-1)+TodayTrueRange1)/ATRPeriod1;
//return CATR1;
}
}
var TodayTrueRange2 = vValue1[0] + Spread - vValue2[0];
if (iCount2 < ATRPeriod2){
AryTTR2.shift();
AryTTR2.push(TodayTrueRange2);
iCount2++;
return;
}else{
var PATR2 = ref(-1); // Perious ATR
if (PATR2 == null){
var iSum2=0;
for(i=0;i<AryTTR2.length;i++){
iSum2 = iSum2 + AryTTR2[i];
}
CATR2 = iSum2/ATRPeriod2; //return iSum2/ATRPeriod2;
}else{
CATR2 = (PATR2*(ATRPeriod2-1)+TodayTrueRange2)/ATRPeriod2;
//return CATR2;
}
}
var TodayTrueRange3 = vValue1[0] + Spread - vValue2[0];
if (iCount3 < ATRPeriod3){
AryTTR3.shift();
AryTTR3.push(TodayTrueRange3);
iCount3++;
return;
}else{
var PATR3 = ref(-1); // Perious ATR
if (PATR3 == null){
var iSum3=0;
for(i=0;i<AryTTR1.length;i++){
iSum3 = iSum3 + AryTTR3[i];
}
CATR3 = iSum3/ATRPeriod3; //return iSum3/ATRPeriod3;
}else{
CATR3 = (PATR3*(ATRPeriod3-1)+TodayTrueRange3)/ATRPeriod3;
//return CATR3;
}
}
var avg = (CATR1 + CATR2 + CATR3) / 3;
return new Array (CATR1, CATR2, CATR3, avg);
//return CATR1;
//return CATR2;
//return CATR3;
//return avg;
}
The script will plot a single ATR without a problem however when I use "return new Array" and try to plot all of the ATRs together I get "0.#Q".
Please Help
var AryTTR1 = null;
var AryTTR2 = null;
var AryTTR3 = null;
var iCount1 = 0;
var iCount2 = 0;
var iCount3 = 0;
function preMain() {
setCursorLabelName("ATR");
setDefaultBarFgColor(Color.red, 0);
setDefaultBarFgColor(Color.blue, 1);
setDefaultBarFgColor(Color.black, 2);
setDefaultBarFgColor(Color.brown, 3);
setCursorLabelName("ATR 5", 0);
setCursorLabelName("ATR 10", 1);
setCursorLabelName("ATR 20", 2);
setCursorLabelName("AVG", 3);
setPriceStudy(false);
var fp1 = new FunctionParameter("ATRPeriod1", FunctionParameter.NUMBER);
fp1.setName("SATR1 Period ");
fp1.setLowerLimit(1);
fp1.setDefault(5);
var fp2 = new FunctionParameter("DCHLength1", FunctionParameter.NUMBER);
fp2.setName("DCH SATR Length");
fp2.setLowerLimit(1);
fp2.setDefault(6);
var fp3 = new FunctionParameter("ATRPeriod2", FunctionParameter.NUMBER);
fp3.setName("SATR2 Period");
fp3.setLowerLimit(1);
fp3.setDefault(10);
var fp4 = new FunctionParameter("ATRPeriod3", FunctionParameter.NUMBER);
fp4.setName("SATR3 Period");
fp4.setLowerLimit(1);
fp4.setDefault(20);
var fp5 = new FunctionParameter("Spread", FunctionParameter.NUMBER);
fp5.setName("Spread");
fp5.setLowerLimit(.0001);
fp5.setDefault(.0004);
}
function main(DCHLength1, ATRPeriod1, ATRPeriod2, ATRPeriod3, Spread) {
var vValue1, vValue2, CATR1, CATR2, CATR3, avg;
if(getBarState() == BARSTATE_ALLBARS){
myDCHstudy1 = new DonchianStudy(DCHLength1, 0);
iCount1 = 0;
iCount2 = 0;
iCount3 = 0;
}
vValue1 = myDCHstudy1.getValue(DonchianStudy.UPPER, 0, -ATRPeriod3);
vValue2 = myDCHstudy1.getValue(DonchianStudy.LOWER, 0, -ATRPeriod3);
if (vValue1 == null || vValue2 == null) return;
if (AryTTR1 == null){
AryTTR1 = new Array(ATRPeriod1);
}
if (AryTTR2 == null){
AryTTR2 = new Array(ATRPeriod2);
}
if (AryTTR3 == null){
AryTTR3 = new Array(ATRPeriod3);
}
var TodayTrueRange1 = vValue1[0] + Spread - vValue2[0];
if (iCount1 < ATRPeriod1){
AryTTR1.shift();
AryTTR1.push(TodayTrueRange1);
iCount1++;
return;
}else{
var PATR1 = ref(-1); // Perious ATR
if (PATR1 == null){
var iSum1=0;
for(i=0;i<AryTTR1.length;i++){
iSum1 = iSum1 + AryTTR1[i];
}
CATR1 = iSum1/ATRPeriod1;
//return CATR1;
}else{
CATR1 = (PATR1*(ATRPeriod1-1)+TodayTrueRange1)/ATRPeriod1;
//return CATR1;
}
}
var TodayTrueRange2 = vValue1[0] + Spread - vValue2[0];
if (iCount2 < ATRPeriod2){
AryTTR2.shift();
AryTTR2.push(TodayTrueRange2);
iCount2++;
return;
}else{
var PATR2 = ref(-1); // Perious ATR
if (PATR2 == null){
var iSum2=0;
for(i=0;i<AryTTR2.length;i++){
iSum2 = iSum2 + AryTTR2[i];
}
CATR2 = iSum2/ATRPeriod2; //return iSum2/ATRPeriod2;
}else{
CATR2 = (PATR2*(ATRPeriod2-1)+TodayTrueRange2)/ATRPeriod2;
//return CATR2;
}
}
var TodayTrueRange3 = vValue1[0] + Spread - vValue2[0];
if (iCount3 < ATRPeriod3){
AryTTR3.shift();
AryTTR3.push(TodayTrueRange3);
iCount3++;
return;
}else{
var PATR3 = ref(-1); // Perious ATR
if (PATR3 == null){
var iSum3=0;
for(i=0;i<AryTTR1.length;i++){
iSum3 = iSum3 + AryTTR3[i];
}
CATR3 = iSum3/ATRPeriod3; //return iSum3/ATRPeriod3;
}else{
CATR3 = (PATR3*(ATRPeriod3-1)+TodayTrueRange3)/ATRPeriod3;
//return CATR3;
}
}
var avg = (CATR1 + CATR2 + CATR3) / 3;
return new Array (CATR1, CATR2, CATR3, avg);
//return CATR1;
//return CATR2;
//return CATR3;
//return avg;
}
Comment